From 2fb7a36230b167aac99fe8d43959833bd3b244dd Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 13 Oct 2015 21:26:50 +0100 Subject: Add element node --- lib/html/ast.js | 14 ++++++++++++-- lib/html/index.js | 1 + 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'lib/html') 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; -- cgit v1.2.3