From 5f67ade998b13797f95ad6ccc2749b7697c8a661 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 13 Oct 2015 21:24:38 +0100 Subject: Add HTML text and fragment nodes --- lib/html/ast.js | 24 ++++++++++++++++++++++++ lib/html/index.js | 4 ++++ 2 files changed, 28 insertions(+) create mode 100644 lib/html/ast.js create mode 100644 lib/html/index.js (limited to 'lib/html') diff --git a/lib/html/ast.js b/lib/html/ast.js new file mode 100644 index 0000000..f5f38da --- /dev/null +++ b/lib/html/ast.js @@ -0,0 +1,24 @@ +var _ = require("underscore"); + +function fragment(children) { + return { + type: "fragment", + children: collapseFragments(children || []) + }; +} + +function text(value) { + return { + type: "text", + value: value + }; +} + +function collapseFragments(elements) { + return _.flatten(elements.map(function(element) { + return element.type === "fragment" ? element.children : [element]; + }), true) +} + +exports.fragment = fragment; +exports.text = text; diff --git a/lib/html/index.js b/lib/html/index.js new file mode 100644 index 0000000..5e28f7b --- /dev/null +++ b/lib/html/index.js @@ -0,0 +1,4 @@ +var ast = require("./ast.js"); + +exports.fragment = ast.fragment; +exports.text = ast.text; -- cgit v1.2.3