From 92d7cc4c856b160bf8db05d9e1edc2ad22261954 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 31 Mar 2013 12:54:22 +0100 Subject: Ignore empty paragraphs --- lib/html-generator.js | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) (limited to 'lib/html-generator.js') diff --git a/lib/html-generator.js b/lib/html-generator.js index 7a03570..af6cedc 100644 --- a/lib/html-generator.js +++ b/lib/html-generator.js @@ -10,7 +10,10 @@ function HtmlGenerator() { var fragments = []; function text(value) { - fragments.push(escapeHtml(value)) + if (value.length > 0) { + writeAll(); + fragments.push(escapeHtml(value)) + } } function asString() { @@ -24,7 +27,7 @@ function HtmlGenerator() { }); var partIndex = 0; - while (partIndex < firstFreshPartIndex && stylePartMatchesHtmlElement(style[partIndex], stack[partIndex])) { + while (partIndex < firstFreshPartIndex && stylePartMatchesStackElement(style[partIndex], stack[partIndex])) { partIndex++; } while (stack.length > partIndex) { @@ -35,23 +38,22 @@ function HtmlGenerator() { } } - function stylePartMatchesHtmlElement(stylePart, htmlElement) { - return htmlElement && stylePart.tagName === htmlElement.tagName; + function stylePartMatchesStackElement(stylePart, stackElement) { + return stackElement && stylePart.tagName === stackElement.element.tagName; } function push(element) { - stack.push(element); - if (element["class"]) { - var attributeString = util.format(' %s="%s"', "class", element["class"]); - } else { - var attributeString = ""; - } - fragments.push(util.format("<%s%s>", element.tagName, attributeString)); + stack.push({ + element: element, + written: false + }); } function pop() { - var element = stack.pop(); - fragments.push(util.format("", element.tagName)); + var stackElement = stack.pop(); + if (stackElement.written) { + fragments.push(util.format("", stackElement.element.tagName)); + } } function popAll() { @@ -60,6 +62,23 @@ function HtmlGenerator() { } } + function writeAll() { + stack.filter(function(element) { + return !element.written; + }).forEach(writeElement); + } + + function writeElement(stackElement) { + stackElement.written = true; + var element = stackElement.element; + if (element["class"]) { + var attributeString = util.format(' %s="%s"', "class", element["class"]); + } else { + var attributeString = ""; + } + fragments.push(util.format("<%s%s>", element.tagName, attributeString)); + } + return { style: style, text: text, -- cgit v1.2.3