Permalink
Browse files

hardcode raw tag into parser to be more accurate

  • Loading branch information...
1 parent a36e273 commit 8eece6587fa92474eee3d10e2fdc0c99ad8f6a09 @paularmstrong committed Dec 26, 2011
Showing with 30 additions and 29 deletions.
  1. +20 −2 lib/parser.js
  2. +1 −26 lib/tags.js
  3. +9 −1 tests/tags.test.js
View
@@ -168,7 +168,8 @@ exports.parse = function (data, tags, autoescape) {
lines = 1,
curline = 1,
newlines = null,
- lastToken;
+ lastToken,
+ inRaw = false;
for (i; i < j; i += 1) {
token = rawtokens[i];
@@ -178,6 +179,11 @@ exports.parse = function (data, tags, autoescape) {
lines += newlines.length;
}
+ if (inRaw !== false && !(/\{\%\s*endraw\s*\%\}$/).test(token)) {
+ inRaw += token;
+ continue;
+ }
+
// Ignore empty strings and comments
if (token.length === 0 || commentRegexp.test(token)) {
continue;
@@ -189,6 +195,18 @@ exports.parse = function (data, tags, autoescape) {
parts = token.replace(/^\{% *| *%\}$/g, '').split(' ');
tagname = parts.shift();
+ if (/\{\%\s*endraw\s*\%\}$/.test(token)) {
+ token = inRaw + token.replace(/\{\%\s*endraw\s*\%\}$/, '');
+ inRaw = false;
+ stack[index].push(token);
+ continue;
+ }
+
+ if (/^\{\%\s*raw\s*\%\}/.test(token)) {
+ inRaw = token.replace(/^\{\%\s*raw\s*\%\}/, '');
+ continue;
+ }
+
if (index > 0 && (/^end/).test(tagname)) {
lastToken = _.last(stack[stack.length - 2]);
if ('end' + lastToken.name === tagname) {
@@ -337,4 +355,4 @@ exports.compile = function compile(indent, parentBlock) {
}, this);
return code;
-};
+};
View
@@ -11,6 +11,7 @@ var parser = require('./parser'),
exports['extends'] = {};
exports.block = { ends: true };
exports.parent = {};
+exports.raw = { ends: true };
/**
* Includes another template. The included template will have access to the
@@ -349,29 +350,3 @@ exports.filter = function (indent) {
return '__output += ' + helpers.wrapFilter(value.replace(/\n/g, ''), { name: name, args: args }) + ';\n';
};
exports.filter.ends = true;
-
-exports.raw = function (indent) {
- var out = '';
-
- function reverseParse(token) {
- switch (token.type) {
- case parser.TOKEN_TYPES.LOGIC:
- case parser.TOKEN_TYPES.TEMPLATE:
- out += '__output += "{% ' + token.name + ' ' + token.args.join(' ').replace(/"/g, '\\"') + ' %}";\n';
- _.each(token.tokens, reverseParse);
- if (exports[token.name].ends) {
- out += '__output += "{% end' + token.name + ' %}";\n';
- }
- break;
- case parser.TOKEN_TYPES.VAR:
- out += '__output += "{{ ' + token.name + ' }}";\n';
- break;
- default:
- out += '__output += "' + token.replace(/\n/g, '\\n') + '";\n';
- break;
- }
- }
- _.each(this.tokens, reverseParse);
- return out;
-};
-exports.raw.ends = true;
View
@@ -403,7 +403,7 @@ exports.raw = testCase({
basic: function (test) {
var input = '{{ foo }} {% set bar = "foo" %}{% if foo %}blah: {{ foo }} {% block foobar %}{{ foo }}{% endblock %}{% endif %}',
- tpl = swig.compile('{% raw %}' + input + '{% endraw %}');
+ tpl = swig.compile('{%raw%}' + input + '{% endraw %}');
test.strictEqual(tpl({ foo: 'foo' }), input);
test.done();
},
@@ -414,4 +414,12 @@ exports.raw = testCase({
test.strictEqual(tpl({}), input);
test.done();
},
+
+ 'non-conforming': function (test) {
+ var input = '{{ foo bar %} {% {#',
+ tpl = swig.compile('{% raw %}' + input + '{% endraw %}');
+
+ test.strictEqual(tpl({}), input);
+ test.done();
+ }
});

0 comments on commit 8eece65

Please sign in to comment.