diff options
| author | Michael Williamson <mike@zwobble.org> | 2013-03-31 12:54:22 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2013-03-31 12:54:22 +0100 |
| commit | 92d7cc4c856b160bf8db05d9e1edc2ad22261954 (patch) | |
| tree | d4c66545e68f8dca00d4c1f5949f311136eb4eea /lib | |
| parent | 24b17947c668bc1f3392aa332804d9b57f76618a (diff) | |
Ignore empty paragraphs
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/html-generator.js | 45 |
1 files changed, 32 insertions, 13 deletions
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("</%s>", element.tagName)); + var stackElement = stack.pop(); + if (stackElement.written) { + fragments.push(util.format("</%s>", 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, |
