Skip to content

Commit b006086

Browse files
committed
make sure our copying of a found pattern does not destroy the template
fixes #461 fixes #444
1 parent 5e90b80 commit b006086

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

core/lib/pattern_assembler.js

-1
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,6 @@ var pattern_assembler = function () {
429429
//create a copy of the partial so as to not pollute it after the getPartial call.
430430
var partialPattern = getPartial(partial, patternlab);
431431
var cleanPartialPattern = JSON5.parse(JSON5.stringify(partialPattern));
432-
cleanPartialPattern.extendedTemplate = cleanPartialPattern.template;
433432

434433
//if partial has style modifier data, replace the styleModifier value
435434
if (currentPattern.stylePartials && currentPattern.stylePartials.length > 0) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{> test-foo }}

test/pattern_assembler_tests.js

+69-1
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,77 @@
364364
//assert
365365
var expectedCleanValue = '<span class="test_base {{styleModifier}}"> {{message}} </span>';
366366
var expectedSetValue = '<span class="test_base test_1"> {{message}} </span>';
367-
test.equals(anotherPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedSetValue.trim());
367+
368+
//this is the "atom" - it should remain unchanged
368369
test.equals(atomPattern.template.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedCleanValue.trim());
369370
test.equals(atomPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedCleanValue.trim());
371+
372+
// this is the style modifier pattern, which should resolve correctly
373+
test.equals(anotherPattern.template.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), '{{> test-styled-atom:test_1 }}');
374+
test.equals(anotherPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedSetValue.trim());
375+
376+
test.done();
377+
},
378+
'processPatternRecursive - ensure deep-nesting works' : function(test){
379+
//arrange
380+
var fs = require('fs-extra');
381+
var pattern_assembler = new pa();
382+
var patterns_dir = './test/files/_patterns';
383+
384+
var pl = {};
385+
pl.config = {
386+
paths: {
387+
source: {
388+
patterns: patterns_dir
389+
}
390+
},
391+
outputFileSuffixes: {
392+
rendered : ''
393+
}
394+
};
395+
pl.data = {};
396+
pl.data.link = {};
397+
pl.config.debug = false;
398+
pl.patterns = [];
399+
pl.partials = {};
400+
401+
var atomPattern = new Pattern('00-test/01-bar.mustache');
402+
atomPattern.template = fs.readFileSync(patterns_dir + '/00-test/01-bar.mustache', 'utf8');
403+
atomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(atomPattern);
404+
atomPattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(atomPattern);
405+
406+
var templatePattern = new Pattern('00-test/00-foo.mustache');
407+
templatePattern.template = fs.readFileSync(patterns_dir + '/00-test/00-foo.mustache', 'utf8');
408+
templatePattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(templatePattern);
409+
templatePattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(templatePattern);
410+
411+
var pagesPattern = new Pattern('00-test/14-inception.mustache');
412+
pagesPattern.template = fs.readFileSync(patterns_dir + '/00-test/14-inception.mustache', 'utf8');
413+
pagesPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(pagesPattern);
414+
pagesPattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(pagesPattern);
415+
416+
pattern_assembler.addPattern(atomPattern, pl);
417+
pattern_assembler.addPattern(templatePattern, pl);
418+
pattern_assembler.addPattern(pagesPattern, pl);
419+
420+
//act
421+
pattern_assembler.process_pattern_recursive('00-test' + path.sep + '14-inception.mustache', pl, {});
422+
423+
//assert
424+
var expectedCleanValue = 'bar';
425+
var expectedSetValue = 'bar';
426+
427+
//this is the "atom" - it should remain unchanged
428+
test.equals(atomPattern.template.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedCleanValue);
429+
test.equals(atomPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedCleanValue);
430+
431+
//this is the "template pattern" - it should have an updated extendedTemplate but an unchanged template
432+
test.equals(templatePattern.template.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), '{{> test-bar }}');
433+
test.equals(templatePattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedSetValue.trim());
434+
435+
//this is the "pages pattern" - it should have an updated extendedTemplate equal to the template pattern but an unchanged template
436+
test.equals(pagesPattern.template.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), '{{> test-foo }}');
437+
test.equals(pagesPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedSetValue.trim());
370438
test.done();
371439
},
372440
'setState - applies any patternState matching the pattern' : function(test){

0 commit comments

Comments
 (0)