From 4163a1ec773f0695baa41000050745df37f789e0 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 13 Oct 2015 21:09:40 +0100 Subject: Fix HTML generation when the HTML path has no fresh elements --- NEWS | 5 +++++ lib/html-generator.js | 6 +++--- tests/html-generator.tests.js | 13 +++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/NEWS b/NEWS index ea24369..34f7672 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,8 @@ +# 0.3.26 + +* Keep HTML elements open correctly when the HTML path contains no fresh + element. + # 0.3.25 * When the styleMap option is passed as a string, ignore lines beginning with #. diff --git a/lib/html-generator.js b/lib/html-generator.js index a20a714..be5079b 100644 --- a/lib/html-generator.js +++ b/lib/html-generator.js @@ -29,7 +29,7 @@ function HtmlGenerator(options) { function satisfyPath(style) { var firstFreshPartIndex = findIndex(style, function(part) { return part.fresh; - }); + }, style.length); var partIndex = 0; while (partIndex < firstFreshPartIndex && stylePartMatchesStackElement(style[partIndex], stack[partIndex])) { @@ -102,11 +102,11 @@ function HtmlGenerator(options) { }; } -function findIndex(array, predicate) { +function findIndex(array, predicate, defaultValue) { for (var i = 0; i < array.length; i++) { if (predicate(array[i])) { return i; } } - return -1; + return defaultValue; } diff --git a/tests/html-generator.tests.js b/tests/html-generator.tests.js index 6921b45..522e010 100644 --- a/tests/html-generator.tests.js +++ b/tests/html-generator.tests.js @@ -40,6 +40,19 @@ describe('HtmlGenerator', function() { return assert.equal(generator.asString(), ""); }); + test('can leave all HTML elements for next style', function() { + var generator = new HtmlGenerator(); + var htmlPath = htmlPaths.elements([ + htmlPaths.element("p", {}, {fresh: false}) + ]); + generator.satisfyPath(htmlPath); + generator.text("Apple"); + generator.satisfyPath(htmlPath); + generator.text("Banana"); + generator.closeAll(); + return assert.equal(generator.asString(), "

AppleBanana

"); + }); + test('can leave some HTML elements for next style', function() { var generator = new HtmlGenerator(); var listPath = htmlPaths.elements([ -- cgit v1.2.3