Fixes #695 - Default to ignoring unrecognized tags for XML files.

This commit is contained in:
Austin Kelleher 2017-05-18 20:46:15 -04:00
parent 3d757a20f3
commit 4cf05973a5
12 changed files with 48 additions and 10 deletions

1
.gitignore vendored
View File

@ -12,6 +12,7 @@ coverage
~* ~*
/.cache /.cache
*.marko.js *.marko.js
*.marko.xml.js
/test/generated/ /test/generated/
.nyc_output .nyc_output
yarn.lock yarn.lock

View File

@ -46,6 +46,10 @@ function relPath(path) {
} }
} }
function shouldIgnoreUnrecognizedTags(path) {
return path.endsWith('.xml') || path.endsWith('.xml.marko');
}
var args = require('argly').createParser({ var args = require('argly').createParser({
'--help': { '--help': {
type: 'boolean', type: 'boolean',
@ -293,8 +297,6 @@ if (args.clean) {
console.log('Deleted: ' + file); console.log('Deleted: ' + file);
context.endAsync(); context.endAsync();
}); });
} }
} }
}, },
@ -318,11 +320,18 @@ if (args.clean) {
return; return;
} }
found[path] = true; if (shouldIgnoreUnrecognizedTags(path)) {
compileOptions = compileOptions || {};
compileOptions.ignoreUnrecognizedTags = true;
}
found[path] = true;
var outPath = path + '.js'; var outPath = path + '.js';
console.log('Compiling:\n Input: ' + relPath(path) + '\n Output: ' + relPath(outPath) + '\n'); console.log('Compiling:\n Input: ' + relPath(path) + '\n Output: ' + relPath(outPath) + '\n');
context.beginAsync(); context.beginAsync();
markoCompiler.compileFile(path, compileOptions, function(err, src) { markoCompiler.compileFile(path, compileOptions, function(err, src) {
if (err) { if (err) {
failed.push('Failed to compile "' + relPath(path) + '". Error: ' + (err.stack || err)); failed.push('Failed to compile "' + relPath(path) + '". Error: ' + (err.stack || err));

View File

@ -110,6 +110,7 @@ class CompileContext extends EventEmitter {
this.compilerType = this.options.compilerType || 'marko'; this.compilerType = this.options.compilerType || 'marko';
this.compilerVersion = this.options.compilerVersion || markoPkgVersion; this.compilerVersion = this.options.compilerVersion || markoPkgVersion;
this.writeVersionComment = writeVersionComment !== 'undefined' ? writeVersionComment : true; this.writeVersionComment = writeVersionComment !== 'undefined' ? writeVersionComment : true;
this.ignoreUnrecognizedTags = this.options.ignoreUnrecognizedTags || false;
this._vars = {}; this._vars = {};
this._uniqueVars = new UniqueVars(); this._uniqueVars = new UniqueVars();
@ -421,9 +422,10 @@ class CompileContext extends EventEmitter {
if (typeof tagName === 'string') { if (typeof tagName === 'string') {
tagDef = taglibLookup.getTag(tagName); tagDef = taglibLookup.getTag(tagName);
if (!tagDef && if (!tagDef &&
!this.isMacro(tagName) && !this.isMacro(tagName) &&
tagName.indexOf(':') === -1 && tagName.indexOf(':') === -1 &&
!htmlElements.isRegisteredElement(tagName, this.dirname)) { !htmlElements.isRegisteredElement(tagName, this.dirname) &&
!this.ignoreUnrecognizedTags) {
if (this._parsingFinished) { if (this._parsingFinished) {
this.addErrorUnrecognizedTag(tagName, elNode); this.addErrorUnrecognizedTag(tagName, elNode);

View File

@ -138,7 +138,7 @@ class Compiler {
var ast = this.parser.parse(src, context); var ast = this.parser.parse(src, context);
context._parsingFinished = true; context._parsingFinished = true;
if (context.unrecognizedTags) { if (!context.ignoreUnrecognizedTags && context.unrecognizedTags) {
for(let i=0; i<context.unrecognizedTags.length; i++) { for(let i=0; i<context.unrecognizedTags.length; i++) {
let unrecognizedTag = context.unrecognizedTags[i]; let unrecognizedTag = context.unrecognizedTags[i];
// See if the tag is a macro // See if the tag is a macro
@ -167,4 +167,4 @@ class Compiler {
} }
} }
module.exports = Compiler; module.exports = Compiler;

View File

@ -43,7 +43,13 @@ if (g.__MARKO_CONFIG) {
* Whether the version should be written to the template as a comment e.g. * Whether the version should be written to the template as a comment e.g.
* // Compiled using marko@4.0.0 - DO NOT EDIT * // Compiled using marko@4.0.0 - DO NOT EDIT
*/ */
writeVersionComment: true writeVersionComment: true,
/**
* Whether unrecognized tags should be ignored or not. This flag will
* be enabled by default when compiling XML.
*/
ignoreUnrecognizedTags: false
}; };
} }

View File

@ -4,6 +4,7 @@ exports.check = function(marko, markoCompiler, expect, helpers, done) {
expect(compiler.config.writeToDisk).to.equal(true); expect(compiler.config.writeToDisk).to.equal(true);
expect(compiler.config.preserveWhitespace).to.equal(false); expect(compiler.config.preserveWhitespace).to.equal(false);
expect(compiler.config.writeVersionComment).to.equal(true); expect(compiler.config.writeVersionComment).to.equal(true);
expect(compiler.config.ignoreUnrecognizedTags).to.equal(false);
compiler.configure({ compiler.configure({
preserveWhitespace: true preserveWhitespace: true

View File

@ -0,0 +1 @@
<urlset></urlset>

View File

@ -0,0 +1,8 @@
var expect = require('chai').expect;
exports.test = function(helpers) {
expect(helpers.existsSync('template.marko.xml.js')).to.equal(false);
var result = helpers.spawnSync(['template.marko.xml']);
expect(helpers.existsSync('template.marko.xml.js')).to.equal(true);
expect(result.status).to.equal(0);
};

View File

@ -0,0 +1 @@
<unrecognized></unrecognized>

View File

@ -0,0 +1 @@
<unrecognized></unrecognized>

View File

@ -0,0 +1,4 @@
var expect = require('chai').expect;
exports.templateData = {};
exports.ignoreUnrecognizedTags = true;

View File

@ -101,6 +101,10 @@ module.exports = function runRenderTest(dir, helpers, done, options) {
require('marko/compiler').defaultOptions.preserveWhitespace = true; require('marko/compiler').defaultOptions.preserveWhitespace = true;
} }
if (main.ignoreUnrecognizedTags) {
require('marko/compiler').defaultOptions.ignoreUnrecognizedTags = true;
}
var oldDone = done; var oldDone = done;
done = function(err) { done = function(err) {
require('marko/compiler').configure(); require('marko/compiler').configure();
@ -225,4 +229,4 @@ module.exports = function runRenderTest(dir, helpers, done, options) {
delete require('marko/compiler').defaultOptions.preserveWhitespace; delete require('marko/compiler').defaultOptions.preserveWhitespace;
} }
} }
}; };