enketo-transformer
Advanced tools
Comparing version 1.0.7 to 1.0.8
{ | ||
"name": "enketo-transformer", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "Library that transforms ODK-compliant XForms in a format that enketo-core consumes", | ||
@@ -5,0 +5,0 @@ "main": "src/transformer.js", |
@@ -17,4 +17,5 @@ /* global setTimeout */ | ||
* Performs XSLT transformation on XForm asynchronously. | ||
* @param {string} xform XForm string | ||
* @return {Function} promise | ||
* | ||
* @param {{xform: string, theme: string}} survey Survey object with at least an xform property | ||
* @return {Promise} promise | ||
*/ | ||
@@ -24,3 +25,2 @@ function transform( survey ) { | ||
deferred = Q.defer(), | ||
result = {}, | ||
startTime = new Date().getTime(); | ||
@@ -34,6 +34,6 @@ | ||
formStylesheet = transformer.readXsltString( sheets.xslForm ); | ||
result.form = _stripRoot( transformer.transform( formStylesheet, doc, [ 'wtf', 'why' ] ) ); | ||
survey.form = _stripRoot( transformer.transform( formStylesheet, doc, [ 'wtf', 'why' ] ) ); | ||
instanceStylesheet = transformer.readXsltString( sheets.xslModel ); | ||
result.model = _stripRoot( transformer.transform( instanceStylesheet, doc, [ 'wtf', 'why' ] ) ); | ||
survey.model = _stripRoot( transformer.transform( instanceStylesheet, doc, [ 'wtf', 'why' ] ) ); | ||
@@ -43,4 +43,6 @@ xsltEndTime = new Date().getTime(); | ||
survey.form = _replaceMediaSources( result.form, survey.manifest ); | ||
survey.model = _replaceMediaSources( result.model, survey.manifest ); | ||
survey.form = _replaceTheme( survey.form, survey.theme ); | ||
survey.form = _replaceMediaSources( survey.form, survey.manifest ); | ||
survey.model = _replaceMediaSources( survey.model, survey.manifest ); | ||
debug( 'post-processing transformation result took ' + ( new Date().getTime() - xsltEndTime ) / 1000 + ' seconds' ); | ||
@@ -66,2 +68,24 @@ | ||
function _replaceTheme( xml, theme ) { | ||
var doc, formClassAttr, formClassValue, | ||
HAS_THEME = /(theme-)[^"'\s]+/; | ||
if ( !theme ) { | ||
return xml; | ||
} | ||
doc = libxmljs.parseXml( xml ); | ||
formClassAttr = doc.root().get( '/form' ).attr( 'class' ); | ||
formClassValue = formClassAttr.value(); | ||
if ( HAS_THEME.test( formClassValue ) ) { | ||
formClassAttr.value( formClassValue.replace( HAS_THEME, '$1' + theme ) ); | ||
} else { | ||
formClassAttr.value( formClassValue + ' ' + 'theme-' + theme ); | ||
} | ||
// TODO: probably result in selfclosing tags for empty elements where not allowed in HTML. Check this. | ||
return doc.toString(); | ||
} | ||
function _replaceMediaSources( xmlStr, manifest ) { | ||
@@ -99,3 +123,3 @@ var doc; | ||
//TODO: probably result in selfclosing tags for empty elements where not allowed in HTML. Check this. | ||
// TODO: probably result in selfclosing tags for empty elements where not allowed in HTML. Check this. | ||
return doc.toString(); | ||
@@ -102,0 +126,0 @@ } |
@@ -16,3 +16,3 @@ /* global describe, require, it*/ | ||
describe( 'transforms valid XForms', function() { | ||
var xform = fs.readFileSync( './test/forms/widgets.xml' ); | ||
var xform = fs.readFileSync( './test/forms/widgets.xml', 'utf8' ); | ||
var result = transformer.transform( { | ||
@@ -32,3 +32,3 @@ xform: xform | ||
return expect( result ).to.eventually.not.have.property( 'xform' ); | ||
} ) | ||
} ); | ||
@@ -49,2 +49,56 @@ } ); | ||
} ); | ||
describe( 'manipulates themes', function() { | ||
var xform = fs.readFileSync( './test/forms/widgets.xml', 'utf8' ); | ||
it( 'adds a provided theme if none is defined in the XForm', function() { | ||
var result = transformer.transform( { | ||
xform: xform, | ||
theme: 'mytheme' | ||
} ); | ||
return expect( result ).to.eventually.have.property( 'form' ).and.to.contain( 'theme-mytheme' ); | ||
} ); | ||
it( 'leaves the XForm-defined theme unchanged if the theme value provided is falsy', function() { | ||
var newXform = xform.replace( '<h:body>', '<h:body class="theme-one">' ), | ||
result1 = transformer.transform( { | ||
xform: newXform | ||
} ), | ||
result2 = transformer.transform( { | ||
xform: newXform, | ||
theme: '' | ||
} ), | ||
result3 = transformer.transform( { | ||
xform: newXform, | ||
theme: null | ||
} ), | ||
result4 = transformer.transform( { | ||
xform: newXform, | ||
theme: false | ||
} ); | ||
return Q.all( [ | ||
expect( result1 ).to.eventually.have.property( 'form' ).and.to.contain( 'theme-one' ), | ||
expect( result2 ).to.eventually.have.property( 'form' ).and.to.contain( 'theme-one' ), | ||
expect( result3 ).to.eventually.have.property( 'form' ).and.to.contain( 'theme-one' ), | ||
expect( result4 ).to.eventually.have.property( 'form' ).and.to.contain( 'theme-one' ) | ||
] ); | ||
} ); | ||
it( 'replaces a theme defined in the XForm with a provided one', function() { | ||
var newXform = xform.replace( '<h:body>', '<h:body class="theme-one">' ), | ||
result = transformer.transform( { | ||
xform: newXform, | ||
theme: 'mytheme' | ||
} ); | ||
return Q.all( [ | ||
expect( result ).to.eventually.have.property( 'form' ).and.to.not.contain( 'theme-one' ), | ||
expect( result ).to.eventually.have.property( 'form' ).and.to.contain( 'theme-mytheme' ) | ||
] ); | ||
} ); | ||
} ); | ||
} ); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
71553
290