From 1967f85d069cb7b1f3d814635da0c03a3a2a4f89 Mon Sep 17 00:00:00 2001 From: Austin Kelleher Date: Mon, 12 Jun 2017 15:24:11 -0400 Subject: [PATCH] Fixes #695 - Always ignore unrecognized tags for XML files when using the compiler. --- bin/markoc.js | 9 -- src/compiler/index.js | 10 ++- .../require-hook-compiler-options/e.xml.marko | 1 + .../api/require-hook-compiler-options/test.js | 3 +- test/autotests/compiler/xml-file/expected.js | 13 +++ .../compiler/xml-file/template.xml.marko | 1 + test/compiler-test.js | 86 ++++++++++++------- 7 files changed, 79 insertions(+), 44 deletions(-) create mode 100644 test/autotests/api/require-hook-compiler-options/e.xml.marko create mode 100644 test/autotests/compiler/xml-file/expected.js create mode 100644 test/autotests/compiler/xml-file/template.xml.marko diff --git a/bin/markoc.js b/bin/markoc.js index cdeec0ea8..ac3e060c2 100644 --- a/bin/markoc.js +++ b/bin/markoc.js @@ -46,10 +46,6 @@ function relPath(path) { } } -function shouldIgnoreUnrecognizedTags(path) { - return path.endsWith('.xml') || path.endsWith('.xml.marko'); -} - var args = require('argly').createParser({ '--help': { type: 'boolean', @@ -325,11 +321,6 @@ if (args.clean) { return; } - if (shouldIgnoreUnrecognizedTags(path)) { - compileOptions = compileOptions || {}; - compileOptions.ignoreUnrecognizedTags = true; - } - found[path] = true; var outPath = path + '.js'; diff --git a/src/compiler/index.js b/src/compiler/index.js index d1375ede0..c05e3b5b8 100644 --- a/src/compiler/index.js +++ b/src/compiler/index.js @@ -59,9 +59,13 @@ function createWalker(options) { return new Walker(options); } +function shouldIgnoreUnrecognizedTags(path) { + return path.endsWith('.xml') || path.endsWith('.xml.marko'); +} + function _compile(src, filename, userOptions, callback) { registerCoreTaglibs(); - + ok(filename, '"filename" argument is required'); ok(typeof filename === 'string', '"filename" argument should be a string'); @@ -75,6 +79,10 @@ function _compile(src, filename, userOptions, callback) { var compiler = defaultCompiler; + if (shouldIgnoreUnrecognizedTags(filename)) { + options.ignoreUnrecognizedTags = true; + } + var context = new CompileContext(src, filename, compiler.builder, options); if (callback) { diff --git a/test/autotests/api/require-hook-compiler-options/e.xml.marko b/test/autotests/api/require-hook-compiler-options/e.xml.marko new file mode 100644 index 000000000..07fec1f07 --- /dev/null +++ b/test/autotests/api/require-hook-compiler-options/e.xml.marko @@ -0,0 +1 @@ + diff --git a/test/autotests/api/require-hook-compiler-options/test.js b/test/autotests/api/require-hook-compiler-options/test.js index 3e30342ed..a7966c319 100644 --- a/test/autotests/api/require-hook-compiler-options/test.js +++ b/test/autotests/api/require-hook-compiler-options/test.js @@ -24,6 +24,7 @@ exports.check = function(marko, markoCompiler, expect, done) { expect(markoCompiler.config.preserveWhitespace).to.equal(true); compileAndCheck('./a.marko', true /* should write to disk */); + compileAndCheck('./e.xml.marko', true /* should write to disk */); require('marko/node-require').install({ compilerOptions: { @@ -72,4 +73,4 @@ exports.check = function(marko, markoCompiler, expect, done) { compileAndCheck('./d.marko', false /* should write to disk */); done(); -}; \ No newline at end of file +}; diff --git a/test/autotests/compiler/xml-file/expected.js b/test/autotests/compiler/xml-file/expected.js new file mode 100644 index 000000000..60d8a1ba3 --- /dev/null +++ b/test/autotests/compiler/xml-file/expected.js @@ -0,0 +1,13 @@ +"use strict"; + +var marko_template = module.exports = require("marko/src/html").t(__filename); + +function render(input, out) { + var data = input; + + out.w(""); +} + +marko_template._ = render; + +marko_template.meta = {}; diff --git a/test/autotests/compiler/xml-file/template.xml.marko b/test/autotests/compiler/xml-file/template.xml.marko new file mode 100644 index 000000000..07fec1f07 --- /dev/null +++ b/test/autotests/compiler/xml-file/template.xml.marko @@ -0,0 +1 @@ + diff --git a/test/compiler-test.js b/test/compiler-test.js index b8c325f80..fc8d5aa8e 100644 --- a/test/compiler-test.js +++ b/test/compiler-test.js @@ -10,45 +10,65 @@ var fs = require('fs'); require('marko/node-require').install(); +const EXTENSIONS = ['.marko', '.xml.marko']; + +function runTestForExtension (dir, helpers, extension, done) { + var templatePath = path.join(dir, `template${extension}`); + + if (!fs.existsSync(templatePath)) { + return false; + } + + var mainPath = path.join(dir, 'test.js'); + var main; + + if (fs.existsSync(mainPath)) { + main = require(mainPath); + } + + var compilerOptions = { writeVersionComment: false }; + + if (main && main.checkError) { + var e; + + try { + compiler.compileFile(templatePath, compilerOptions); + } catch(_e) { + e = _e; + } + + if (!e) { + throw new Error('Error expected'); + } + + main.checkError(e); + done(); + + } else if(main && main.checkTemplate) { + var template = require('marko').load(templatePath, Object.assign(compilerOptions, main.compilerOptions)); + main.checkTemplate(template); + done(); + } else { + var compiledSrc = compiler.compileFile(templatePath, Object.assign(compilerOptions, main && main.compilerOptions)); + compiledSrc = compiledSrc.replace(/marko\/dist\//g, 'marko/src/'); + helpers.compare(compiledSrc, '.js'); + done(); + } + + return true; +} + describe('compiler', function() { var autoTestDir = path.join(__dirname, 'autotests/compiler'); autotest.scanDir(autoTestDir, function run(dir, helpers, done) { - var templatePath = path.join(dir, 'template.marko'); - var mainPath = path.join(dir, 'test.js'); - var main; + for (let i = 0; i < EXTENSIONS.length; i++) { + const extension = EXTENSIONS[i]; + let complete = runTestForExtension(dir, helpers, extension, done); - if (fs.existsSync(mainPath)) { - main = require(mainPath); - } - - var compilerOptions = { writeVersionComment: false }; - - if (main && main.checkError) { - var e; - - try { - compiler.compileFile(templatePath, compilerOptions); - } catch(_e) { - e = _e; + if (complete) { + return; } - - if (!e) { - throw new Error('Error expected'); - } - - main.checkError(e); - done(); - - } else if(main && main.checkTemplate) { - var template = require('marko').load(templatePath, Object.assign(compilerOptions, main.compilerOptions)); - main.checkTemplate(template); - done(); - } else { - var compiledSrc = compiler.compileFile(templatePath, Object.assign(compilerOptions, main && main.compilerOptions)); - compiledSrc = compiledSrc.replace(/marko\/dist\//g, 'marko/src/'); - helpers.compare(compiledSrc, '.js'); - done(); } });