From ef1374097f781975dc5634217301d972eaa8cce3 Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Tue, 16 Feb 2016 22:24:03 -0700 Subject: [PATCH] Marko v3: Allow --- taglibs/core/else-tag.js | 25 +++++++++++++++++-- .../autotest/if-else-if-concise/expected.html | 1 + .../if-else-if-concise/template.marko | 4 +++ .../autotest/if-else-if-concise/test.js | 1 + 4 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/render/autotest/if-else-if-concise/expected.html create mode 100644 test/fixtures/render/autotest/if-else-if-concise/template.marko create mode 100644 test/fixtures/render/autotest/if-else-if-concise/test.js diff --git a/taglibs/core/else-tag.js b/taglibs/core/else-tag.js index 19c8989e4..425285081 100644 --- a/taglibs/core/else-tag.js +++ b/taglibs/core/else-tag.js @@ -1,5 +1,7 @@ +'use strict'; + module.exports = function nodeFactory(el, context) { - var attributes = el.attributes; + var elseStatement = context.builder.elseStatement(); var argument = el.argument; @@ -7,8 +9,27 @@ module.exports = function nodeFactory(el, context) { context.addError(elseStatement, 'Invalid tag. Argument is not allowed'); } - if (attributes.length) { + if (el.hasAttribute('if')) { + let ifAttr = el.getAttribute('if'); + el.removeAttribute('if'); + + if (el.attributes.length) { + context.addError(elseStatement, 'Invalid tag. Only the "if" attribute is allowed.'); + return el; + } + + var testExpression = ifAttr.argument; + if (!testExpression) { + context.addError(elseStatement, 'Invalid tag. Invalid "if" attribute. Expected: )>'); + return el; + } + var elseIfStatement = context.builder.elseIfStatement(testExpression); + return elseIfStatement; + } + + if (el.attributes.length) { context.addError(elseStatement, 'Invalid tag. Attributes not allowed.'); + return el; } return elseStatement; diff --git a/test/fixtures/render/autotest/if-else-if-concise/expected.html b/test/fixtures/render/autotest/if-else-if-concise/expected.html new file mode 100644 index 000000000..7371f47a6 --- /dev/null +++ b/test/fixtures/render/autotest/if-else-if-concise/expected.html @@ -0,0 +1 @@ +B \ No newline at end of file diff --git a/test/fixtures/render/autotest/if-else-if-concise/template.marko b/test/fixtures/render/autotest/if-else-if-concise/template.marko new file mode 100644 index 000000000..7406859d7 --- /dev/null +++ b/test/fixtures/render/autotest/if-else-if-concise/template.marko @@ -0,0 +1,4 @@ +if(false) + - A +else if(true) + - B \ No newline at end of file diff --git a/test/fixtures/render/autotest/if-else-if-concise/test.js b/test/fixtures/render/autotest/if-else-if-concise/test.js new file mode 100644 index 000000000..c4013b344 --- /dev/null +++ b/test/fixtures/render/autotest/if-else-if-concise/test.js @@ -0,0 +1 @@ +exports.templateData = {};