Skip to content
This repository was archived by the owner on Dec 10, 2019. It is now read-only.

Commit b03bc45

Browse files
committed
New partial finding regex for Mustache that accounts for regular syntax,
verbose syntax, style modifier syntax, pattern parameter syntax, and single and double quoted strings in pattern parameters with escaped quotes. Making this hurt my brain very badly. Update the appropriate unit test with all the test cases used to develop the regex, plus some flown in from other unit tests.
1 parent 0ffb2fe commit b03bc45

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

builder/pattern_engines/engine_mustache.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
engineFileExtension: '.mustache',
2020

2121
// regexes, stored here so they're only compiled once
22-
findPartialsRE: /{{>([ ])?([A-Za-z0-9-]+)(?:\:[A-Za-z0-9-]+)?(?:(| )\(.*)?([ ])?}}/g,
22+
findPartialsRE: /{{>\s*((?:\d+-[\w-]+\/)+(\d+-[\w-]+\.\w+)|[A-Za-z0-9-]+)(\:[A-Za-z0-9-]+)?(\(\s*\w+\s*:\s*(?:'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*")\))?\s*}}/g,
2323
findPartialsWithStyleModifiersRE: /{{>([ ])?([\w\-\.\/~]+)(?!\()(\:[A-Za-z0-9-_]+)+(?:(| )\(.*)?([ ])?}}/g,
2424
findPartialsWithPatternParametersRE: /{{>([ ])?([\w\-\.\/~]+)(?:\:[A-Za-z0-9-_]+)?(?:(| )\(.*)+([ ])?}}/g,
2525
findListItemsRE: /({{#( )?)(list(I|i)tems.)(one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty)( )?}}/g,

test/pattern_assembler_tests.js

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,47 @@
77

88
exports['pattern_assembler'] = {
99
'find_pattern_partials finds partials' : function(test){
10-
test.expect(3);
10+
test.expect(13);
1111

1212
//setup current pattern from what we would have during execution
13-
var currentPattern = new of.oPattern(
13+
var currentPattern = of.oPattern.create(
1414
'/home/fakeuser/pl/source/_patterns/01-molecules/00-testing/00-test-mol.mustache', // abspath
1515
'01-molecules\\00-testing', // subdir
1616
'00-test-mol.mustache', // filename,
17-
null // data
17+
null, // data
18+
{
19+
template: "{{> molecules-comment-header}}asdfasdf" +
20+
"{{> molecules-comment-header}}" +
21+
"{{> \n molecules-comment-header\n}}" +
22+
"{{> }}" +
23+
"{{> molecules-weird-spacing }}" +
24+
"{{> molecules-ba_d-cha*rs }}" +
25+
"{{> molecules-single-comment(description: 'A life isn\\'t like a garden. Perfect moments can be had, but not preserved, except in memory.') }}" +
26+
'{{> molecules-single-comment(description: "A life is like a \\"garden\\". Perfect moments can be had, but not preserved, except in memory.") }}' +
27+
"{{> molecules-single-comment:foo }}" +
28+
"{{> 01-molecules/06-components/03-comment-header.mustache }}" +
29+
"{{> 01-molecules/06-components/02-single-comment.mustache(description: 'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.') }}" +
30+
"{{> molecules-single-comment:foo }}" +
31+
"{{>atoms-error(message: 'That\\'s no moon...')}}" +
32+
'{{>atoms-error(message: \'That\\\'s no moon...\')}}'
33+
}
1834
);
19-
currentPattern.template = "<h1>{{> molecules-comment-header}}</h1><div>{{> molecules-single-comment(description: 'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.') }}</div>";
2035

2136
var results = currentPattern.findPartials();
22-
test.equals(results.length, 2);
23-
test.equals(results[0], '{{> molecules-comment-header}}');
24-
test.equals(results[1], '{{> molecules-single-comment(description: \'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.\') }}');
25-
37+
console.log(results);
38+
test.equals(results.length, 12);
39+
test.equals(results[0], "{{> molecules-comment-header}}");
40+
test.equals(results[1], "{{> molecules-comment-header}}");
41+
test.equals(results[2], "{{> \n molecules-comment-header\n}}");
42+
test.equals(results[3], "{{> molecules-weird-spacing }}");
43+
test.equals(results[4], "{{> molecules-single-comment(description: 'A life isn\\'t like a garden. Perfect moments can be had, but not preserved, except in memory.') }}");
44+
test.equals(results[5], '{{> molecules-single-comment(description: "A life is like a \\"garden\\". Perfect moments can be had, but not preserved, except in memory.") }}');
45+
test.equals(results[6], "{{> molecules-single-comment:foo }}");
46+
test.equals(results[7], "{{> 01-molecules/06-components/03-comment-header.mustache }}");
47+
test.equals(results[8], "{{> 01-molecules/06-components/02-single-comment.mustache(description: 'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.') }}");
48+
test.equals(results[9], "{{> molecules-single-comment:foo }}");
49+
test.equals(results[10], "{{>atoms-error(message: 'That\\'s no moon...')}}");
50+
test.equals(results[11], "{{>atoms-error(message: 'That\\'s no moon...')}}");
2651
test.done();
2752
},
2853

@@ -36,12 +61,12 @@
3661
'00-test-mol.mustache', // filename,
3762
null // data
3863
);
39-
currentPattern.template = "<h1>{{> 01-molecules/06-components/03-comment-header.mustache }}</h1><div>{{> 01-molecules/06-components/02-single-comment(description: 'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.') }}</div>";
64+
currentPattern.template = "<h1>{{> 01-molecules/06-components/03-comment-header.mustache }}</h1><div>{{> 01-molecules/06-components/02-single-comment.mustache(description: 'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.') }}</div>";
4065

4166
var results = currentPattern.findPartials();
4267
test.equals(results.length, 2);
4368
test.equals(results[0], '{{> 01-molecules/06-components/03-comment-header.mustache }}');
44-
test.equals(results[1], '{{> 01-molecules/06-components/02-single-comment(description: \'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.\') }}');
69+
test.equals(results[1], '{{> 01-molecules/06-components/02-single-comment.mustache(description: \'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.\') }}');
4570
test.done();
4671
},
4772

0 commit comments

Comments
 (0)