diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-10-20 09:26:37 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-10-20 09:26:37 +0100 |
| commit | 0fc88bee80c4adbb13ae7fc4c87f9d993bef9135 (patch) | |
| tree | 7f33d012e73a4208f6b26639048f8aef83dad26e | |
| parent | 4bf6655d9bae333b1b41622970d8abfd708c4fd3 (diff) | |
Avoid inserting newlines around inline elements for pretty printing
| -rw-r--r-- | NEWS | 4 | ||||
| -rw-r--r-- | lib/writers/html-writer.js | 9 | ||||
| -rw-r--r-- | tests/writers/html-writer.tests.js | 13 |
3 files changed, 22 insertions, 4 deletions
@@ -1,3 +1,7 @@ +# 0.3.28 + +* Avoid inserting newlines around inline elements when pretty-printing HTML. + # 0.3.27 * Fix recursive collapsing of elements. diff --git a/lib/writers/html-writer.js b/lib/writers/html-writer.js index 456e894..ee7d49f 100644 --- a/lib/writers/html-writer.js +++ b/lib/writers/html-writer.js @@ -16,7 +16,8 @@ function writer(options) { var indentedElements = { div: true, p: true, - ul: true + ul: true, + li: true }; @@ -30,7 +31,9 @@ function prettyWriter() { var writer = simpleWriter(); function open(tagName, attributes) { - indent(); + if (indentedElements[tagName]) { + indent(); + } stack.push(tagName); writer.open(tagName, attributes); if (indentedElements[tagName]) { @@ -42,8 +45,8 @@ function prettyWriter() { function close(tagName) { if (indentedElements[tagName]) { indentationLevel--; + indent(); } - indent(); stack.pop(); writer.close(tagName); } diff --git a/tests/writers/html-writer.tests.js b/tests/writers/html-writer.tests.js index 5e398e8..41d8839 100644 --- a/tests/writers/html-writer.tests.js +++ b/tests/writers/html-writer.tests.js @@ -47,7 +47,18 @@ describe('html-writer', function() { writer.text("Two"); writer.close("li"); writer.close("ul"); - return assert.equal(writer.asString(), "<ul>\n <li>One</li>\n <li>Two</li>\n</ul>"); + return assert.equal(writer.asString(), "<ul>\n <li>\n One\n </li>\n <li>\n Two\n </li>\n</ul>"); + }); + + test('when prettyPrint is true inner elements do not have new lines', function() { + var writer = htmlWriter.writer({prettyPrint: true}); + writer.open("p"); + writer.text("One"); + writer.open("em"); + writer.text("Two"); + writer.close("em"); + writer.close("p"); + return assert.equal(writer.asString(), "<p>\n One<em>Two</em>\n</p>"); }); test('indents closing element correctly when nested inside another indented element', function() { |
