Skip to content

Commit

Permalink
Merge pull request #3 from rvagg/whitespace
Browse files Browse the repository at this point in the history
allow whitespace within tags
  • Loading branch information
amccollum committed May 21, 2012
2 parents 019aa4d + ea08d18 commit bf229cb
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -1 +1,2 @@
node_modules node_modules
test/
16 changes: 8 additions & 8 deletions Cakefile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ task 'test', 'Build and run the test suite', ->
'ln -sf ../src/test/index.html test', 'ln -sf ../src/test/index.html test',
'ln -sf ../src/test/vows.css test', 'ln -sf ../src/test/vows.css test',


'npm install .', 'npm install',
'npm install --dev', 'rm -f node_modules/wings node_modules/vows test/node_modules',
'ln -sfh ender-vows node_modules/vows', 'ln -sf ender-vows node_modules/vows',


'pushd test', 'cd test',
'ln -sfh ../node_modules node_modules', 'ln -sf ../node_modules node_modules',
'ln -sfh .. node_modules/wings', 'ln -sf .. node_modules/wings',
#'node_modules/.bin/ender build ender-vows ..', #'node_modules/.bin/ender build ender-vows ..',
'node_modules/.bin/vows *-test.js', 'node_modules/.bin/vows *-test.js',
'unlink node_modules/wings', 'rm -f node_modules/wings node_modules/vows node_modules',
'popd test', 'cd ..',
] ]
1 change: 0 additions & 1 deletion lib/ender.js
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,3 @@
// Generated by CoffeeScript 1.3.1


(function($) { (function($) {
var renderTemplate; var renderTemplate;
Expand Down
7 changes: 3 additions & 4 deletions lib/wings.js
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,3 @@
// Generated by CoffeeScript 1.3.1


(function(wings) { (function(wings) {
var escapeXML, isArray, parsePattern, renderRawTemplate, replaceBraces, restoreBraces; var escapeXML, isArray, parsePattern, renderRawTemplate, replaceBraces, restoreBraces;
Expand Down Expand Up @@ -35,10 +34,10 @@
} }
}); });
}; };
parsePattern = /\{([:!])\s*([^}]*?)\s*\}([\S\s]+?)\s*\{\/\s*\2\}|\{(\#)[\S\s]+?\#\}|\{([@&]?)\s*([^}]*?)\s*\}/mg; parsePattern = /\{([:!])\s*([^}\s]*?)\s*\}([\S\s]+?)\{\/\s*\2\s*\}|\{(\#)[\S\s]+?\#\}|\{([@&]?)\s*([^}\s]*?)\s*\}/mg;
return renderRawTemplate = function(template, data, links) { return renderRawTemplate = function(template, data, links) {
return template.replace(parsePattern, function(all, sectionOp, sectionName, sectionContent, commentOp, tagOp, tagName) { return template.replace(parsePattern, function(all, sectionOp, sectionName, sectionContent, commentOp, tagOp, tagName) {
var content, i, link, name, op, part, parts, rest, v, value, _i, _len, _ref; var content, i, link, name, op, part, parts, rest, v, value, _len, _ref;
op = sectionOp || commentOp || tagOp; op = sectionOp || commentOp || tagOp;
name = sectionName || tagName; name = sectionName || tagName;
content = sectionContent; content = sectionContent;
Expand All @@ -53,7 +52,7 @@
} }
} else if (isArray(value)) { } else if (isArray(value)) {
parts = []; parts = [];
for (i = _i = 0, _len = value.length; _i < _len; i = ++_i) { for (i = 0, _len = value.length; i < _len; i++) {
v = value[i]; v = value[i];
v['#'] = i; v['#'] = i;
parts.push(renderRawTemplate(content, v, links)); parts.push(renderRawTemplate(content, v, links));
Expand Down
56 changes: 56 additions & 0 deletions src/test/wings-test.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ vows.add 'templates'
'should produce html': (topic) -> 'should produce html': (topic) ->
equal topic, 'This should produce html: <b>bolded</b>' equal topic, 'This should produce html: <b>bolded</b>'


'a template with unescaped tags (whitespace in tag)':
topic: [
t('This should produce html: {& html }', {html: '<b>bolded</b>'}),
t('This should produce html: {& html}', {html: '<b>bolded</b>'}),
t('This should produce html: {&html }', {html: '<b>bolded</b>'}),
t('This should produce html: {& \thtml \t\t}', {html: '<b>bolded</b>'}),
]

'should produce html': (topics) ->
equal topics[0], 'This should produce html: <b>bolded</b>'
equal topics[1], 'This should produce html: <b>bolded</b>'
equal topics[2], 'This should produce html: <b>bolded</b>'
equal topics[3], 'This should produce html: <b>bolded</b>'

'a template with tags having the value 0': 'a template with tags having the value 0':
topic: t('This is a zero: {zero}', {zero: 0}) topic: t('This is a zero: {zero}', {zero: 0})


Expand All @@ -91,6 +105,20 @@ vows.add 'templates'
'should follow the link': (topic) -> 'should follow the link': (topic) ->
equal topic, 'baz' equal topic, 'baz'


'a template with a normal link (whitespace in tag)':
topic: [
t('{@ foo }', {bar: '1baz'}, {foo:'{ bar }'})
t('{@ foo}', {bar: '2baz'}, {foo:'{bar }'}),
t('{@foo }', {bar: '3baz'}, {foo:'{bar }'}),
t('{@ \tfoo\t\t}', {bar: '4baz'}, {foo:'{\tbar\t }'}),
]

'should produce html': (topics) ->
equal topics[0], '1baz'
equal topics[1], '2baz'
equal topics[2], '3baz'
equal topics[3], '4baz'

'a template with a function link': 'a template with a function link':
topic: t('{@foo}', {bar: 'baz'}, {foo: () -> '{bar}'}) topic: t('{@foo}', {bar: 'baz'}, {foo: () -> '{bar}'})


Expand Down Expand Up @@ -118,6 +146,20 @@ vows.add 'templates'
equal topics[4], 'foobar' equal topics[4], 'foobar'
equal topics[5], 'foobar' equal topics[5], 'foobar'


'templates with regular sections (whitespace in tags)':
topic: [
t('{: falsy }foo{/falsy}1{: truthy }bang{/truthy }', {falsy: 0, truthy: 1}),
t('{: falsy}foo{/ falsy}2{: truthy }bang{/truthy }', {falsy: 0, truthy: 1}),
t('{:falsy}foo{/ falsy }3{: truthy }bang{/ truthy }', {falsy: 0, truthy: 1}),
t('{:\tfalsy\t}foo{/falsy}4{:\ttruthy\t}bang{/\ttruthy\t}', {falsy: 0, truthy: 1})
]

'should only include the section when the tag is truthy': (topics) ->
equal topics[0], '1bang'
equal topics[1], '2bang'
equal topics[2], '3bang'
equal topics[3], '4bang'

'templates with inverse sections': 'templates with inverse sections':
topic: [ topic: [
t('{!falsy}foo{/falsy}bar', {falsy: 0}), t('{!falsy}foo{/falsy}bar', {falsy: 0}),
Expand All @@ -137,6 +179,20 @@ vows.add 'templates'
equal topics[4], 'bar' equal topics[4], 'bar'
equal topics[5], 'bar' equal topics[5], 'bar'


'templates with inverse sections (whitespace in tags)':
topic: [
t('{! falsy }foo{/falsy}1{! truthy }bang{/truthy }', {falsy: 0, truthy: 1}),
t('{! falsy}foo{/ falsy}2{! truthy }bang{/truthy }', {falsy: 0, truthy: 1}),
t('{!falsy}foo{/ falsy }3{! truthy }bang{/ truthy }', {falsy: 0, truthy: 1}),
t('{!\tfalsy\t}foo{/falsy}4{!\ttruthy\t}bang{/\ttruthy\t}', {falsy: 0, truthy: 1})
]

'should only include the section when the tag is not truthy': (topics) ->
equal topics[0], 'foo1'
equal topics[1], 'foo2'
equal topics[2], 'foo3'
equal topics[3], 'foo4'

'templates with array sections': 'templates with array sections':
topic: [ topic: [
t('{:array}foo{/array}bar', {array: [1, 2, 3]}), t('{:array}foo{/array}bar', {array: [1, 2, 3]}),
Expand Down
4 changes: 2 additions & 2 deletions src/wings/wings.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
else return s else return s


parsePattern = /// parsePattern = ///
\{([:!]) \s* ([^}]*?) \s* \} ([\S\s]+?) \s* \{/ \s* \2 \} | # sections \{([:!]) \s* ([^}\s]*?) \s* \} ([\S\s]+?) \{/ \s* \2 \s* \} | # sections
\{(\#) [\S\s]+? \#\} | # comments \{(\#) [\S\s]+? \#\} | # comments
\{([@&]?) \s* ([^}]*?) \s* \} # tags \{([@&]?) \s* ([^}\s]*?) \s* \} # tags
///mg ///mg


renderRawTemplate = (template, data, links) -> renderRawTemplate = (template, data, links) ->
Expand Down

0 comments on commit bf229cb

Please sign in to comment.