From f68fe75ded906610be4aefe9cf921e8e8032f7a0 Mon Sep 17 00:00:00 2001 From: Matthew Arbesfeld Date: Tue, 15 Apr 2014 19:58:10 -0400 Subject: [PATCH] Add support for block comments in Spacebars --- packages/spacebars-compiler/spacebars_tests.js | 18 +++++++++++++++++- packages/spacebars-compiler/templatetag.js | 12 +++++++++++- packages/spacebars-tests/template_tests.html | 6 ++++++ packages/spacebars-tests/template_tests.js | 8 ++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/packages/spacebars-compiler/spacebars_tests.js b/packages/spacebars-compiler/spacebars_tests.js index 5e809eb3cf..2ef521d014 100644 --- a/packages/spacebars-compiler/spacebars_tests.js +++ b/packages/spacebars-compiler/spacebars_tests.js @@ -32,6 +32,12 @@ Tinytest.add("spacebars - stache tags", function (test) { run('{{! asdf }}', {type: 'COMMENT', value: ' asdf '}); run('{{ ! asdf }}', {type: 'COMMENT', value: ' asdf '}); run('{{ ! asdf }asdf', "Unclosed"); + run('{{!-- asdf --}}', {type: 'BLOCKCOMMENT', value: ' asdf '}); + run('{{ !-- asdf -- }}', {type: 'BLOCKCOMMENT', value: ' asdf '}); + run('{{ !-- {{asdf}} -- }}', {type: 'BLOCKCOMMENT', value: ' {{asdf}} '}); + run('{{ !-- {{as--df}} --}}', {type: 'BLOCKCOMMENT', value: ' {{as--df}} '}); + run('{{ !-- asdf }asdf', "Unclosed"); + run('{{ !-- asdf --}asdf', "Unclosed"); run('{{else}}', {type: 'ELSE'}); run('{{ else }}', {type: 'ELSE'}); run('{{else x}}', "Expected"); @@ -225,6 +231,9 @@ Tinytest.add("spacebars - parse", function (test) { test.equal(HTML.toJS(Spacebars.parse('{{!foo}}')), 'null'); test.equal(HTML.toJS(Spacebars.parse('x{{!foo}}y')), '"xy"'); + test.equal(HTML.toJS(Spacebars.parse('{{!--foo--}}')), 'null'); + test.equal(HTML.toJS(Spacebars.parse('x{{!--foo--}}y')), '"xy"'); + test.equal(HTML.toJS(Spacebars.parse('{{#foo}}x{{/foo}}')), 'HTMLTools.Special({type: "BLOCKOPEN", path: ["foo"], content: "x"})'); @@ -254,7 +263,10 @@ Tinytest.add("spacebars - parse", function (test) { Spacebars.parse(' x}}>'); }); - test.equal(HTML.toJS(Spacebars.parse('')), + test.equal(HTML.toJS(Spacebars.parse('')), + 'HTML.A({b: "c"})'); + + test.equal(HTML.toJS(Spacebars.parse('')), 'HTML.A({b: "c"})'); // currently, if there are only comments, the attribute is truthy. This is @@ -264,5 +276,9 @@ Tinytest.add("spacebars - parse", function (test) { 'HTML.INPUT({selected: ""})'); test.equal(HTML.toJS(Spacebars.parse('')), 'HTML.INPUT({selected: ""})'); + test.equal(HTML.toJS(Spacebars.parse('')), + 'HTML.INPUT({selected: ""})'); + test.equal(HTML.toJS(Spacebars.parse('')), + 'HTML.INPUT({selected: ""})'); }); diff --git a/packages/spacebars-compiler/templatetag.js b/packages/spacebars-compiler/templatetag.js index 5a50fde13e..4616f37970 100644 --- a/packages/spacebars-compiler/templatetag.js +++ b/packages/spacebars-compiler/templatetag.js @@ -48,6 +48,7 @@ var starts = { ELSE: makeStacheTagStartRegex(/^\{\{\s*else(?=[\s}])/i), DOUBLE: makeStacheTagStartRegex(/^\{\{\s*(?!\s)/), TRIPLE: makeStacheTagStartRegex(/^\{\{\{\s*(?!\s)/), + BLOCKCOMMENT: makeStacheTagStartRegex(/^\{\{\s*!--/), COMMENT: makeStacheTagStartRegex(/^\{\{\s*!/), INCLUSION: makeStacheTagStartRegex(/^\{\{\s*>\s*(?!\s)/), BLOCKOPEN: makeStacheTagStartRegex(/^\{\{\s*#\s*(?!\s)/), @@ -226,6 +227,7 @@ TemplateTag.parse = function (scannerOrString) { if (run(starts.ELSE)) type = 'ELSE'; else if (run(starts.DOUBLE)) type = 'DOUBLE'; else if (run(starts.TRIPLE)) type = 'TRIPLE'; + else if (run(starts.BLOCKCOMMENT)) type = 'BLOCKCOMMENT'; else if (run(starts.COMMENT)) type = 'COMMENT'; else if (run(starts.INCLUSION)) type = 'INCLUSION'; else if (run(starts.BLOCKOPEN)) type = 'BLOCKOPEN'; @@ -236,7 +238,12 @@ TemplateTag.parse = function (scannerOrString) { var tag = new TemplateTag; tag.type = type; - if (type === 'COMMENT') { + if (type === 'BLOCKCOMMENT') { + var result = run(/^[\s\S]*?--\s*?\}\}/); + if (! result) + error("Unclosed block comment"); + tag.value = result.slice(0, result.lastIndexOf('--')); + } else if (type === 'COMMENT') { var result = run(/^[\s\S]*?\}\}/); if (! result) error("Unclosed comment"); @@ -320,6 +327,9 @@ TemplateTag.parseCompleteTag = function (scannerOrString, position) { if (! result) return result; + if (result.type === 'BLOCKCOMMENT') + return null; + if (result.type === 'COMMENT') return null; diff --git a/packages/spacebars-tests/template_tests.html b/packages/spacebars-tests/template_tests.html index 2eceba5ddf..2f846ea21d 100644 --- a/packages/spacebars-tests/template_tests.html +++ b/packages/spacebars-tests/template_tests.html @@ -676,3 +676,9 @@ Hi there! + diff --git a/packages/spacebars-tests/template_tests.js b/packages/spacebars-tests/template_tests.js index 66443c8430..f3da83e437 100644 --- a/packages/spacebars-tests/template_tests.js +++ b/packages/spacebars-tests/template_tests.js @@ -1877,3 +1877,11 @@ Tinytest.add( } ); +Tinytest.add( + "spacebars - template - block comments should not be displayed", + function (test) { + var tmpl = Template.spacebars_test_block_comment; + var div = renderToDiv(tmpl); + test.equal(canonicalizeHtml(div.innerHTML), ''); + } +);