diff options
| author | Michael Williamson <mike@zwobble.org> | 2014-03-25 12:40:28 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2014-03-25 12:40:28 +0000 |
| commit | f559596a8bb78306e596734bc44057656b6a2214 (patch) | |
| tree | 74264e152b3f5ba60e1a28b95da6a286bea78209 /lib | |
| parent | 714f06a92bccb4118072789558cb29c450ac1baf (diff) | |
Split out HTML writer from HTML generator
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/html-generator.js | 43 | ||||
| -rw-r--r-- | lib/html-writer.js | 58 |
2 files changed, 70 insertions, 31 deletions
diff --git a/lib/html-generator.js b/lib/html-generator.js index eea0fcf..870ee66 100644 --- a/lib/html-generator.js +++ b/lib/html-generator.js @@ -1,32 +1,31 @@ -var util = require("util"); - var _ = require("underscore"); +var htmlWriter = require("./html-writer"); + exports.HtmlGenerator = HtmlGenerator; function HtmlGenerator() { var stack = []; - var fragments = []; + var writer = htmlWriter.writer(); function text(value) { if (value.length > 0) { writeAll(); - fragments.push(escapeHtml(value)) + writer.text(value); } } function asString() { - return fragments.join(""); + return writer.asString(); } function append(otherHtmlGenerator) { - if (otherHtmlGenerator._fragments.length > 0) { + var otherString = otherHtmlGenerator.asString(); + if (otherString.length > 0) { writeAll(); } - otherHtmlGenerator._fragments.forEach(function(fragment) { - fragments.push(fragment); - }); + writer.append(otherString); } function satisfyPath(style) { @@ -59,7 +58,7 @@ function HtmlGenerator() { function close() { var element = stack.pop(); if (element.written) { - fragments.push(util.format("</%s>", element.tagName)); + writer.close(element.tagName); } } @@ -77,19 +76,11 @@ function HtmlGenerator() { function writeElement(element) { element.written = true; - var attributeString = generateAttributeString(element.attributes) - fragments.push(util.format("<%s%s>", element.tagName, attributeString)); + writer.open(element.tagName, element.attributes); } function selfClosing(element) { - var attributeString = generateAttributeString(element.attributes) - fragments.push(util.format("<%s%s />", element.tagName, attributeString)); - } - - function generateAttributeString(attributes) { - return _.map(attributes, function(value, key) { - return util.format(' %s="%s"', key, escapeHtml(value)); - }).join(""); + writer.selfClosing(element.tagName, element.attributes); } return { @@ -100,20 +91,10 @@ function HtmlGenerator() { closeAll: closeAll, asString: asString, append: append, - selfClosing: selfClosing, - _fragments: fragments + selfClosing: selfClosing }; } - -function escapeHtml(value) { - return value - .replace(/&/g, '&') - .replace(/"/g, '"') - .replace(/</g, '<') - .replace(/>/g, '>'); -} - function findIndex(array, predicate) { for (var i = 0; i < array.length; i++) { if (predicate(array[i])) { diff --git a/lib/html-writer.js b/lib/html-writer.js new file mode 100644 index 0000000..d2fb67b --- /dev/null +++ b/lib/html-writer.js @@ -0,0 +1,58 @@ +var util = require("util"); +var _ = require("underscore"); + +exports.writer = writer; + +function writer() { + var fragments = []; + + function open(tagName, attributes) { + var attributeString = generateAttributeString(attributes) + fragments.push(util.format("<%s%s>", tagName, attributeString)); + } + + function close(tagName) { + fragments.push(util.format("</%s>", tagName)); + } + + function selfClosing(tagName, attributes) { + var attributeString = generateAttributeString(attributes) + fragments.push(util.format("<%s%s />", tagName, attributeString)); + } + + function generateAttributeString(attributes) { + return _.map(attributes, function(value, key) { + return util.format(' %s="%s"', key, escapeHtml(value)); + }).join(""); + } + + function text(value) { + fragments.push(escapeHtml(value)) + } + + function append(html) { + fragments.push(html); + } + + function asString() { + return fragments.join(""); + } + + return { + asString: asString, + open: open, + close: close, + text: text, + selfClosing: selfClosing, + append: append + }; +} + + +function escapeHtml(value) { + return value + .replace(/&/g, '&') + .replace(/"/g, '"') + .replace(/</g, '<') + .replace(/>/g, '>'); +} |
