diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-10-13 21:26:50 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-10-13 21:26:50 +0100 |
| commit | 2fb7a36230b167aac99fe8d43959833bd3b244dd (patch) | |
| tree | 18409fc8a2f843e64774e3c32e89e0d5aa32281c /lib/html | |
| parent | 5f67ade998b13797f95ad6ccc2749b7697c8a661 (diff) | |
Add element node
Diffstat (limited to 'lib/html')
| -rw-r--r-- | lib/html/ast.js | 14 | ||||
| -rw-r--r-- | lib/html/index.js | 1 |
2 files changed, 13 insertions, 2 deletions
diff --git a/lib/html/ast.js b/lib/html/ast.js index f5f38da..25f4874 100644 --- a/lib/html/ast.js +++ b/lib/html/ast.js @@ -3,7 +3,16 @@ var _ = require("underscore"); function fragment(children) { return { type: "fragment", - children: collapseFragments(children || []) + children: collapseFragments(children) + }; +} + +function element(tagName, attributes, children) { + return { + type: "element", + tagName: tagName, + attributes: attributes, + children: collapseFragments(children) }; } @@ -15,10 +24,11 @@ function text(value) { } function collapseFragments(elements) { - return _.flatten(elements.map(function(element) { + return _.flatten((elements || []).map(function(element) { return element.type === "fragment" ? element.children : [element]; }), true) } exports.fragment = fragment; +exports.element = element; exports.text = text; diff --git a/lib/html/index.js b/lib/html/index.js index 5e28f7b..f9e957a 100644 --- a/lib/html/index.js +++ b/lib/html/index.js @@ -1,4 +1,5 @@ var ast = require("./ast.js"); exports.fragment = ast.fragment; +exports.element = ast.element; exports.text = ast.text; |
