Skip to content

Commit

Permalink
Apply filters to includes in right-to-left order (#2932)
Browse files Browse the repository at this point in the history
[fixes #2929]
  • Loading branch information
droooney authored and ForbesLindesay committed Jan 26, 2018
1 parent 926f7c7 commit 72fbf6a
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 27 deletions.
4 changes: 2 additions & 2 deletions packages/pug-filters/lib/handle-filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ function handleFilters(ast, filters, options, filterAliases) {
node.type = 'Text';
node.val = filterWithFallback(node, text, attrs);
} else if (node.type === 'RawInclude' && node.filters.length) {
var firstFilter = node.filters.shift();
var firstFilter = node.filters.pop();
var attrs = getAttributes(firstFilter, options);
var filename = attrs.filename = node.file.fullPath;
var str = node.file.str;
node.type = 'Text';
node.val = filterFileWithFallback(firstFilter, filename, str, attrs);
node.filters.forEach(function (filter) {
node.filters.slice().reverse().forEach(function (filter) {
var attrs = getAttributes(filter, options);
attrs.filename = filename;
node.val = filterWithFallback(filter, node.val, attrs);
Expand Down
2 changes: 1 addition & 1 deletion packages/pug-filters/test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ exports[`test cases/filters.include.input.json 1`] = `
\"type\": \"Text\",
\"line\": 7,
\"filename\": \"filters.include.tokens.json\",
\"val\": \"(function() {\\n var math;\\n\\n math = {\\n square: function(value) {\\n return value * value;\\n }\\n };\\n\\n}).call(this);\\n\"
\"val\": \"<![CDATA[(function() {\\n var math;\\n\\n math = {\\n square: function(value) {\\n return value * value;\\n }\\n };\\n\\n}).call(this);\\n]]>\"
}
],
\"line\": 6,
Expand Down
9 changes: 8 additions & 1 deletion packages/pug-filters/test/cases/filters.include.input.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@
"line": 7,
"filename": "filters.include.tokens.json",
"filters": [
{
"type": "IncludeFilter",
"name": "cdata",
"attrs": [],
"line": 7,
"filename": "filters.include.tokens.json"
},
{
"type": "IncludeFilter",
"name": "coffee-script",
Expand Down Expand Up @@ -150,4 +157,4 @@
],
"line": 0,
"filename": "filters.include.tokens.json"
}
}
2 changes: 1 addition & 1 deletion packages/pug-filters/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ testCases.forEach(function (filename) {
handleFilters(JSON.parse(read(filename)), customFilters);
throw new Error('Expected ' + filename + ' to throw an exception.');
} catch (ex) {
if (!ex || !ex.code || !ex.code.indexOf('PUG:') === 0) throw ex;
if (!ex || !ex.code || ex.code.indexOf('PUG:') !== 0) throw ex;
actual = {
msg: ex.msg,
code: ex.code,
Expand Down
2 changes: 1 addition & 1 deletion packages/pug-filters/test/update-test-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function getError (input, filename) {
handleFilters(input, customFilters);
throw new Error('Expected ' + filename + ' to throw an exception.');
} catch (ex) {
if (!ex || !ex.code || !ex.code.indexOf('PUG:') === 0) throw ex;
if (!ex || !ex.code || ex.code.indexOf('PUG:') !== 0) throw ex;
return {
msg: ex.msg,
code: ex.code,
Expand Down
14 changes: 10 additions & 4 deletions packages/pug-lexer/test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6939,28 +6939,34 @@ Array [
"col": 14,
"line": 7,
"type": "filter",
"val": "cdata",
},
Object {
"col": 20,
"line": 7,
"type": "filter",
"val": "coffee-script",
},
Object {
"col": 28,
"col": 34,
"line": 7,
"type": "start-attributes",
},
Object {
"col": 29,
"col": 35,
"line": 7,
"mustEscape": true,
"name": "minify",
"type": "attribute",
"val": "false",
},
Object {
"col": 41,
"col": 47,
"line": 7,
"type": "end-attributes",
},
Object {
"col": 43,
"col": 49,
"line": 7,
"type": "path",
"val": "include-filter-coffee.coffee",
Expand Down
2 changes: 1 addition & 1 deletion packages/pug-lexer/test/cases/filters.include.pug
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ html
script
include:coffee-script(minify=true) include-filter-coffee.coffee
script
include:coffee-script(minify=false) include-filter-coffee.coffee
include:cdata:coffee-script(minify=false) include-filter-coffee.coffee
2 changes: 1 addition & 1 deletion packages/pug-lexer/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fs.readdirSync(edir).forEach(function (testCase) {
lex(fs.readFileSync(edir + testCase, 'utf8'), {filename: edir + testCase});
throw new Error('Expected ' + testCase + ' to throw an exception.');
} catch (ex) {
if (!ex || !ex.code || !ex.code.indexOf('PUG:') === 0) throw ex;
if (!ex || !ex.code || ex.code.indexOf('PUG:') !== 0) throw ex;
actual = {
msg: ex.msg,
code: ex.code,
Expand Down
14 changes: 11 additions & 3 deletions packages/pug-parser/test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7205,26 +7205,34 @@ Object {
Object {
"column": 7,
"file": Object {
"column": 43,
"column": 49,
"filename": "filters.include.tokens.json",
"line": 7,
"path": "include-filter-coffee.coffee",
"type": "FileReference",
},
"filename": "filters.include.tokens.json",
"filters": Array [
Object {
"attrs": Array [],
"column": 14,
"filename": "filters.include.tokens.json",
"line": 7,
"name": "cdata",
"type": "IncludeFilter",
},
Object {
"attrs": Array [
Object {
"column": 29,
"column": 35,
"filename": "filters.include.tokens.json",
"line": 7,
"mustEscape": true,
"name": "minify",
"val": "false",
},
],
"column": 14,
"column": 20,
"filename": "filters.include.tokens.json",
"line": 7,
"name": "coffee-script",
Expand Down
13 changes: 7 additions & 6 deletions packages/pug-parser/test/cases/filters.include.tokens.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
{"type":"tag","line":6,"col":5,"val":"script"}
{"type":"indent","line":7,"col":1,"val":6}
{"type":"include","line":7,"col":7}
{"type":"filter","line":7,"col":14,"val":"coffee-script"}
{"type":"start-attributes","line":7,"col":28}
{"type":"attribute","line":7,"col":29,"name":"minify","val":"false","mustEscape":true}
{"type":"end-attributes","line":7,"col":41}
{"type":"path","line":7,"col":43,"val":"include-filter-coffee.coffee"}
{"type":"filter","line":7,"col":14,"val":"cdata"}
{"type":"filter","line":7,"col":20,"val":"coffee-script"}
{"type":"start-attributes","line":7,"col":34}
{"type":"attribute","line":7,"col":35,"name":"minify","val":"false","mustEscape":true}
{"type":"end-attributes","line":7,"col":47}
{"type":"path","line":7,"col":49,"val":"include-filter-coffee.coffee"}
{"type":"outdent","line":8,"col":5}
{"type":"outdent","line":8,"col":3}
{"type":"outdent","line":8,"col":1}
{"type":"eos","line":8,"col":1}
{"type":"eos","line":8,"col":1}
2 changes: 1 addition & 1 deletion packages/pug-strip-comments/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fs.readdirSync(edir).forEach(function (testCase) {
strip(input);
throw new Error('Expected ' + testCase + ' to throw an exception.');
} catch (ex) {
if (!ex || !ex.code || !ex.code.indexOf('PUG:') === 0) throw ex;
if (!ex || !ex.code || ex.code.indexOf('PUG:') !== 0) throw ex;
expect({
msg: ex.msg,
code: ex.code,
Expand Down
8 changes: 3 additions & 5 deletions packages/pug/test/__snapshots__/pug.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,9 @@ function pug_rethrow(n, e, r, t) {
return (t == r ? \" > \" : \" \") + t + \"| \" + n;
})
.join(\"\\n\");
throw (
(n.path = e),
(n.message = (e || \"Pug\") + \":\" + r + \"\\n\" + i + \"\\n\\n\" + n.message),
n
);
throw ((n.path = e),
(n.message = (e || \"Pug\") + \":\" + r + \"\\n\" + i + \"\\n\\n\" + n.message),
n);
}
function template(locals) {
var pug_html = \"\",
Expand Down

0 comments on commit 72fbf6a

Please sign in to comment.