diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-07-07 20:25:41 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-07-07 20:25:41 +0100 |
| commit | b5768bced10c6dafe8bc621c6ef01aa0bd9d975c (patch) | |
| tree | f6cd37ba5dad336f72d6a7e9c688eb177de0c23f /lib/html-paths.js | |
| parent | 3047481420f215fb60b0380fa187975948ba54be (diff) | |
Store children in thunks when generating HTML nodes
Diffstat (limited to 'lib/html-paths.js')
| -rw-r--r-- | lib/html-paths.js | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/lib/html-paths.js b/lib/html-paths.js index 04bc655..4021887 100644 --- a/lib/html-paths.js +++ b/lib/html-paths.js @@ -1,5 +1,7 @@ var _ = require("underscore"); +var html = require("./html"); + exports.topLevelElement = topLevelElement; exports.elements = elements; exports.element = element; @@ -9,15 +11,27 @@ function topLevelElement(tagName, attributes) { } function elements(elementStyles) { - return elementStyles.map(function(elementStyle) { + return new HtmlPath(elementStyles.map(function(elementStyle) { if (_.isString(elementStyle)) { return element(elementStyle); } else { return elementStyle; } - }); + })); +} + +function HtmlPath(elements) { + this._elements = elements; } +HtmlPath.prototype.wrap = function wrap(children) { + var result = children(); + for (var index = this._elements.length - 1; index >= 0; index--) { + result = this._elements[index].wrapNodes(result); + } + return result; +}; + function element(tagName, attributes, options) { options = options || {}; return new Element(tagName, attributes, options); @@ -44,4 +58,12 @@ Element.prototype.matchesElement = function(element) { return this.tagNames[element.tagName] && _.isEqual(this.attributes || {}, element.attributes || {}); }; +Element.prototype.wrap = function wrap(generateNodes) { + return this.wrapNodes(generateNodes()); +}; + +Element.prototype.wrapNodes = function wrapNodes(nodes) { + return [html.elementWithTag(this, nodes)]; +}; + exports.empty = elements([]); |
