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 | |
| parent | 24b17947c668bc1f3392aa332804d9b57f76618a (diff) | |
Ignore empty paragraphs
| -rw-r--r-- | lib/html-generator.js | 45 | ||||
| -rw-r--r-- | tests/documents.tests.js | 10 | ||||
| -rw-r--r-- | tests/html-generator.tests.js | 13 |
3 files changed, 55 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, diff --git a/tests/documents.tests.js b/tests/documents.tests.js index d5db03a..06faf10 100644 --- a/tests/documents.tests.js +++ b/tests/documents.tests.js @@ -17,6 +17,16 @@ describe('DocumentConverter', function() { }); }); + test('ignores empty paragraphs', function() { + var document = new documents.Document([ + paragraphOfText("") + ]); + var converter = new DocumentConverter(); + return converter.convertToHtml(document).then(function(result) { + assert.equal(result.html, ""); + }); + }); + test('can use non-default HTML element for unstyled paragraphs', function() { var document = new documents.Document([ paragraphOfText("Hello.") diff --git a/tests/html-generator.tests.js b/tests/html-generator.tests.js index f71ea59..4510351 100644 --- a/tests/html-generator.tests.js +++ b/tests/html-generator.tests.js @@ -24,6 +24,19 @@ describe('HtmlGenerator', function() { return assert.equal(generator.asString(), "<p><span>Hello!</span></p>"); }); + test('elements with no text are not generated', function() { + var generator = new HtmlGenerator(); + generator.style(styles.elements(["p", "span"])); + return assert.equal(generator.asString(), ""); + }); + + test('generates empty string if text is empty string', function() { + var generator = new HtmlGenerator(); + generator.style(styles.elements(["p", "span"])); + generator.text("") + return assert.equal(generator.asString(), ""); + }); + test('can leave some HTML elements for next style', function() { var generator = new HtmlGenerator(); var listStyle = styles.elements([ |
