Skip to content

Commit 72fbf6a

Browse files
droooneyForbesLindesay
authored andcommitted
Apply filters to includes in right-to-left order (#2932)
[fixes #2929]
1 parent 926f7c7 commit 72fbf6a

File tree

12 files changed

+47
-27
lines changed

12 files changed

+47
-27
lines changed

packages/pug-filters/lib/handle-filters.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ function handleFilters(ast, filters, options, filterAliases) {
1919
node.type = 'Text';
2020
node.val = filterWithFallback(node, text, attrs);
2121
} else if (node.type === 'RawInclude' && node.filters.length) {
22-
var firstFilter = node.filters.shift();
22+
var firstFilter = node.filters.pop();
2323
var attrs = getAttributes(firstFilter, options);
2424
var filename = attrs.filename = node.file.fullPath;
2525
var str = node.file.str;
2626
node.type = 'Text';
2727
node.val = filterFileWithFallback(firstFilter, filename, str, attrs);
28-
node.filters.forEach(function (filter) {
28+
node.filters.slice().reverse().forEach(function (filter) {
2929
var attrs = getAttributes(filter, options);
3030
attrs.filename = filename;
3131
node.val = filterWithFallback(filter, node.val, attrs);

packages/pug-filters/test/__snapshots__/index.test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ exports[`test cases/filters.include.input.json 1`] = `
404404
\"type\": \"Text\",
405405
\"line\": 7,
406406
\"filename\": \"filters.include.tokens.json\",
407-
\"val\": \"(function() {\\n var math;\\n\\n math = {\\n square: function(value) {\\n return value * value;\\n }\\n };\\n\\n}).call(this);\\n\"
407+
\"val\": \"<![CDATA[(function() {\\n var math;\\n\\n math = {\\n square: function(value) {\\n return value * value;\\n }\\n };\\n\\n}).call(this);\\n]]>\"
408408
}
409409
],
410410
\"line\": 6,

packages/pug-filters/test/cases/filters.include.input.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@
102102
"line": 7,
103103
"filename": "filters.include.tokens.json",
104104
"filters": [
105+
{
106+
"type": "IncludeFilter",
107+
"name": "cdata",
108+
"attrs": [],
109+
"line": 7,
110+
"filename": "filters.include.tokens.json"
111+
},
105112
{
106113
"type": "IncludeFilter",
107114
"name": "coffee-script",
@@ -150,4 +157,4 @@
150157
],
151158
"line": 0,
152159
"filename": "filters.include.tokens.json"
153-
}
160+
}

packages/pug-filters/test/index.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ testCases.forEach(function (filename) {
3939
handleFilters(JSON.parse(read(filename)), customFilters);
4040
throw new Error('Expected ' + filename + ' to throw an exception.');
4141
} catch (ex) {
42-
if (!ex || !ex.code || !ex.code.indexOf('PUG:') === 0) throw ex;
42+
if (!ex || !ex.code || ex.code.indexOf('PUG:') !== 0) throw ex;
4343
actual = {
4444
msg: ex.msg,
4545
code: ex.code,

packages/pug-filters/test/update-test-cases.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function getError (input, filename) {
1818
handleFilters(input, customFilters);
1919
throw new Error('Expected ' + filename + ' to throw an exception.');
2020
} catch (ex) {
21-
if (!ex || !ex.code || !ex.code.indexOf('PUG:') === 0) throw ex;
21+
if (!ex || !ex.code || ex.code.indexOf('PUG:') !== 0) throw ex;
2222
return {
2323
msg: ex.msg,
2424
code: ex.code,

packages/pug-lexer/test/__snapshots__/index.test.js.snap

+10-4
Original file line numberDiff line numberDiff line change
@@ -6939,28 +6939,34 @@ Array [
69396939
"col": 14,
69406940
"line": 7,
69416941
"type": "filter",
6942+
"val": "cdata",
6943+
},
6944+
Object {
6945+
"col": 20,
6946+
"line": 7,
6947+
"type": "filter",
69426948
"val": "coffee-script",
69436949
},
69446950
Object {
6945-
"col": 28,
6951+
"col": 34,
69466952
"line": 7,
69476953
"type": "start-attributes",
69486954
},
69496955
Object {
6950-
"col": 29,
6956+
"col": 35,
69516957
"line": 7,
69526958
"mustEscape": true,
69536959
"name": "minify",
69546960
"type": "attribute",
69556961
"val": "false",
69566962
},
69576963
Object {
6958-
"col": 41,
6964+
"col": 47,
69596965
"line": 7,
69606966
"type": "end-attributes",
69616967
},
69626968
Object {
6963-
"col": 43,
6969+
"col": 49,
69646970
"line": 7,
69656971
"type": "path",
69666972
"val": "include-filter-coffee.coffee",

packages/pug-lexer/test/cases/filters.include.pug

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ html
44
script
55
include:coffee-script(minify=true) include-filter-coffee.coffee
66
script
7-
include:coffee-script(minify=false) include-filter-coffee.coffee
7+
include:cdata:coffee-script(minify=false) include-filter-coffee.coffee

packages/pug-lexer/test/index.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fs.readdirSync(edir).forEach(function (testCase) {
2424
lex(fs.readFileSync(edir + testCase, 'utf8'), {filename: edir + testCase});
2525
throw new Error('Expected ' + testCase + ' to throw an exception.');
2626
} catch (ex) {
27-
if (!ex || !ex.code || !ex.code.indexOf('PUG:') === 0) throw ex;
27+
if (!ex || !ex.code || ex.code.indexOf('PUG:') !== 0) throw ex;
2828
actual = {
2929
msg: ex.msg,
3030
code: ex.code,

packages/pug-parser/test/__snapshots__/index.test.js.snap

+11-3
Original file line numberDiff line numberDiff line change
@@ -7205,26 +7205,34 @@ Object {
72057205
Object {
72067206
"column": 7,
72077207
"file": Object {
7208-
"column": 43,
7208+
"column": 49,
72097209
"filename": "filters.include.tokens.json",
72107210
"line": 7,
72117211
"path": "include-filter-coffee.coffee",
72127212
"type": "FileReference",
72137213
},
72147214
"filename": "filters.include.tokens.json",
72157215
"filters": Array [
7216+
Object {
7217+
"attrs": Array [],
7218+
"column": 14,
7219+
"filename": "filters.include.tokens.json",
7220+
"line": 7,
7221+
"name": "cdata",
7222+
"type": "IncludeFilter",
7223+
},
72167224
Object {
72177225
"attrs": Array [
72187226
Object {
7219-
"column": 29,
7227+
"column": 35,
72207228
"filename": "filters.include.tokens.json",
72217229
"line": 7,
72227230
"mustEscape": true,
72237231
"name": "minify",
72247232
"val": "false",
72257233
},
72267234
],
7227-
"column": 14,
7235+
"column": 20,
72287236
"filename": "filters.include.tokens.json",
72297237
"line": 7,
72307238
"name": "coffee-script",

packages/pug-parser/test/cases/filters.include.tokens.json

+7-6
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
{"type":"tag","line":6,"col":5,"val":"script"}
1919
{"type":"indent","line":7,"col":1,"val":6}
2020
{"type":"include","line":7,"col":7}
21-
{"type":"filter","line":7,"col":14,"val":"coffee-script"}
22-
{"type":"start-attributes","line":7,"col":28}
23-
{"type":"attribute","line":7,"col":29,"name":"minify","val":"false","mustEscape":true}
24-
{"type":"end-attributes","line":7,"col":41}
25-
{"type":"path","line":7,"col":43,"val":"include-filter-coffee.coffee"}
21+
{"type":"filter","line":7,"col":14,"val":"cdata"}
22+
{"type":"filter","line":7,"col":20,"val":"coffee-script"}
23+
{"type":"start-attributes","line":7,"col":34}
24+
{"type":"attribute","line":7,"col":35,"name":"minify","val":"false","mustEscape":true}
25+
{"type":"end-attributes","line":7,"col":47}
26+
{"type":"path","line":7,"col":49,"val":"include-filter-coffee.coffee"}
2627
{"type":"outdent","line":8,"col":5}
2728
{"type":"outdent","line":8,"col":3}
2829
{"type":"outdent","line":8,"col":1}
29-
{"type":"eos","line":8,"col":1}
30+
{"type":"eos","line":8,"col":1}

packages/pug-strip-comments/test/index.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fs.readdirSync(edir).forEach(function (testCase) {
3939
strip(input);
4040
throw new Error('Expected ' + testCase + ' to throw an exception.');
4141
} catch (ex) {
42-
if (!ex || !ex.code || !ex.code.indexOf('PUG:') === 0) throw ex;
42+
if (!ex || !ex.code || ex.code.indexOf('PUG:') !== 0) throw ex;
4343
expect({
4444
msg: ex.msg,
4545
code: ex.code,

packages/pug/test/__snapshots__/pug.test.js.snap

+3-5
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,9 @@ function pug_rethrow(n, e, r, t) {
7474
return (t == r ? \" > \" : \" \") + t + \"| \" + n;
7575
})
7676
.join(\"\\n\");
77-
throw (
78-
(n.path = e),
79-
(n.message = (e || \"Pug\") + \":\" + r + \"\\n\" + i + \"\\n\\n\" + n.message),
80-
n
81-
);
77+
throw ((n.path = e),
78+
(n.message = (e || \"Pug\") + \":\" + r + \"\\n\" + i + \"\\n\\n\" + n.message),
79+
n);
8280
}
8381
function template(locals) {
8482
var pug_html = \"\",

0 commit comments

Comments
 (0)