diff options
| author | Michael Williamson <mike@zwobble.org> | 2014-04-10 14:22:21 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2014-04-10 14:22:21 +0100 |
| commit | dee75b1261646edb99ae01a231036fd9c468fe83 (patch) | |
| tree | f340ce912a525fc5408da7da8c9997c863f8e501 | |
| parent | 7e49d52fb9501a1bdd85d6c9d35d09d50aabb569 (diff) | |
Add basic indentation of HTML
| -rw-r--r-- | lib/html-writer.js | 72 | ||||
| -rw-r--r-- | tests/html-writer.tests.js | 29 |
2 files changed, 98 insertions, 3 deletions
diff --git a/lib/html-writer.js b/lib/html-writer.js index d329438..233e5dd 100644 --- a/lib/html-writer.js +++ b/lib/html-writer.js @@ -3,7 +3,77 @@ var _ = require("underscore"); exports.writer = writer; -function writer() { +function writer(options) { + options = options || {}; + if (options.prettyPrint) { + return prettyWriter(); + } else { + return simpleWriter(); + } +} + + +var indentedElements = { + div: true, + p: true, + ul: true +}; + + +function prettyWriter() { + var indentationLevel = 0; + var stack = []; + + var writer = simpleWriter(); + + function open(tagName, attributes) { + indent(); + stack.push(tagName); + writer.open(tagName, attributes); + if (indentedElements[tagName]) { + indentationLevel++; + } + } + + function close(tagName) { + if (indentedElements[tagName]) { + indentationLevel--; + } + indent(); + stack.pop(); + writer.close(tagName); + } + + function text(value) { + indent(); + writer.text(value); + } + + function insideIndentedElement() { + return stack.length > 0 && indentedElements[stack[stack.length - 1]]; + } + + function indent() { + if (insideIndentedElement()) { + writer.append("\n"); + for (var i = 0; i < indentationLevel; i++) { + writer.append(" "); + } + } + } + + return { + asString: writer.asString, + open: open, + close: close, + text: text, + selfClosing: writer.selfClosing, + append: writer.append + }; +} + + +function simpleWriter() { var fragments = []; function open(tagName, attributes) { diff --git a/tests/html-writer.tests.js b/tests/html-writer.tests.js index 8a40afb..2a22caf 100644 --- a/tests/html-writer.tests.js +++ b/tests/html-writer.tests.js @@ -10,7 +10,7 @@ describe('html-writer', function() { writer.open("p"); writer.text("Hello"); writer.close("p"); - return assert.equal("<p>Hello</p>", writer.asString()); + return assert.equal(writer.asString(), "<p>Hello</p>"); }); test('can nest elements', function() { @@ -23,6 +23,31 @@ describe('html-writer', function() { writer.text("Two"); writer.close("li"); writer.close("ul"); - return assert.equal("<ul><li>One</li><li>Two</li></ul>", writer.asString()); + return assert.equal(writer.asString(), "<ul><li>One</li><li>Two</li></ul>"); + }); + + test('indents element if prettyPrint is true', function() { + var writer = htmlWriter.writer({prettyPrint: true}); + writer.open("ul"); + writer.open("li"); + writer.text("One"); + writer.close("li"); + writer.open("li"); + 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>"); + }); + + test('indents closing element correctly when nested inside another indented element', function() { + var writer = htmlWriter.writer({prettyPrint: true}); + writer.open("div"); + writer.open("div"); + writer.open("div"); + writer.text("Hello"); + writer.close("div"); + writer.close("div"); + writer.close("div"); + return assert.equal(writer.asString(), "<div>\n <div>\n <div>\n Hello\n </div>\n </div>\n</div>"); }); }) |
