From 8f9d1094af9b473561129a7df2a48c02f79c7764 Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Thu, 11 Feb 2016 10:53:22 -0700 Subject: [PATCH] Marko v3: Fixes #222 - Allow open only tags to be defined in tag definition --- compiler/HtmlJsParser.js | 6 +++++- compiler/Parser.js | 5 +++++ compiler/taglib-loader/Taglib/Tag.js | 1 + compiler/taglib-loader/loader-tag.js | 4 ++++ .../render/autotest/custom-tag-open-tag-only/expected.html | 2 ++ .../autotest/custom-tag-open-tag-only/marko-taglib.json | 6 ++++++ .../autotest/custom-tag-open-tag-only/open-tag-only-tag.js | 3 +++ .../render/autotest/custom-tag-open-tag-only/template.marko | 2 ++ .../render/autotest/custom-tag-open-tag-only/test.js | 1 + 9 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/render/autotest/custom-tag-open-tag-only/expected.html create mode 100644 test/fixtures/render/autotest/custom-tag-open-tag-only/marko-taglib.json create mode 100644 test/fixtures/render/autotest/custom-tag-open-tag-only/open-tag-only-tag.js create mode 100644 test/fixtures/render/autotest/custom-tag-open-tag-only/template.marko create mode 100644 test/fixtures/render/autotest/custom-tag-open-tag-only/test.js diff --git a/compiler/HtmlJsParser.js b/compiler/HtmlJsParser.js index 3d333b07d..c963b66a9 100644 --- a/compiler/HtmlJsParser.js +++ b/compiler/HtmlJsParser.js @@ -77,7 +77,11 @@ class HtmlJsParser { } }; - var parser = this.parser = htmljs.createParser(listeners); + var parser = this.parser = htmljs.createParser(listeners, { + isOpenTagOnly: function(tagName) { + return handlers.isOpenTagOnly(tagName); + } + }); parser.parse(src); } } diff --git a/compiler/Parser.js b/compiler/Parser.js index 9ce7502c3..9ee6ec0b4 100644 --- a/compiler/Parser.js +++ b/compiler/Parser.js @@ -268,6 +268,11 @@ class Parser { return null; // Default parse state } + + isOpenTagOnly(tagName) { + var tagDef = this.context.getTagDef(tagName); + return tagDef && tagDef.openTagOnly; + } } module.exports = Parser; \ No newline at end of file diff --git a/compiler/taglib-loader/Taglib/Tag.js b/compiler/taglib-loader/Taglib/Tag.js index c7a23a302..1bae32bd2 100644 --- a/compiler/taglib-loader/Taglib/Tag.js +++ b/compiler/taglib-loader/Taglib/Tag.js @@ -50,6 +50,7 @@ class Tag{ this.isRepeated = null; this.isNestedTag = false; this.parentTagName = null; + this.openTagOnly = null; this.body = null; this.type = null; // Only applicable for nested tags this._nodeFactory = undefined; diff --git a/compiler/taglib-loader/loader-tag.js b/compiler/taglib-loader/loader-tag.js index 7c0974bac..d9e6adfb6 100644 --- a/compiler/taglib-loader/loader-tag.js +++ b/compiler/taglib-loader/loader-tag.js @@ -426,6 +426,10 @@ TagHandlers.prototype = { } else { throw new Error('Invalid value for "body". Allowed: "static-text", "parsed-text" or "html"'); } + }, + + openTagOnly: function(value) { + this.tag.openTagOnly = value; } }; diff --git a/test/fixtures/render/autotest/custom-tag-open-tag-only/expected.html b/test/fixtures/render/autotest/custom-tag-open-tag-only/expected.html new file mode 100644 index 000000000..55167dbd2 --- /dev/null +++ b/test/fixtures/render/autotest/custom-tag-open-tag-only/expected.html @@ -0,0 +1,2 @@ +Hello Frank! +Hello John! diff --git a/test/fixtures/render/autotest/custom-tag-open-tag-only/marko-taglib.json b/test/fixtures/render/autotest/custom-tag-open-tag-only/marko-taglib.json new file mode 100644 index 000000000..8cd75662c --- /dev/null +++ b/test/fixtures/render/autotest/custom-tag-open-tag-only/marko-taglib.json @@ -0,0 +1,6 @@ +{ + "": { + "renderer": "./open-tag-only-tag.js", + "open-tag-only": true + } +} \ No newline at end of file diff --git a/test/fixtures/render/autotest/custom-tag-open-tag-only/open-tag-only-tag.js b/test/fixtures/render/autotest/custom-tag-open-tag-only/open-tag-only-tag.js new file mode 100644 index 000000000..96b7643b0 --- /dev/null +++ b/test/fixtures/render/autotest/custom-tag-open-tag-only/open-tag-only-tag.js @@ -0,0 +1,3 @@ +module.exports = function (input, out) { + out.write('Hello ' + input.name + '!\n'); +}; \ No newline at end of file diff --git a/test/fixtures/render/autotest/custom-tag-open-tag-only/template.marko b/test/fixtures/render/autotest/custom-tag-open-tag-only/template.marko new file mode 100644 index 000000000..b12e9c948 --- /dev/null +++ b/test/fixtures/render/autotest/custom-tag-open-tag-only/template.marko @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/test/fixtures/render/autotest/custom-tag-open-tag-only/test.js b/test/fixtures/render/autotest/custom-tag-open-tag-only/test.js new file mode 100644 index 000000000..c4013b344 --- /dev/null +++ b/test/fixtures/render/autotest/custom-tag-open-tag-only/test.js @@ -0,0 +1 @@ +exports.templateData = {};