lib/dom.js

tools for rendering the DOM and manipulating DOM nodes

jsdom lets us re-render the DOM from raw HTML

var jsdom = require('jsdom-little').jsdom;

xpath lets us use xpath selectors on the rendered DOM

var xpath = require('xpath'); var dom = module.exports;

Render a DOM from HTML

Parameters:

  • html must be a String.
    (HTML to render)

Returns an Object
(the rendered DOM)

dom.render = function(html) { return new jsdom(html, null, { features: { FetchExternalResources: false, ProcessExternalResources: false } }); }

Clean up an element by stripping out chains of whitespace and newlines.

Parameters:

  • str must be a String.
    (element text)

Returns a String
(the cleaned string)

dom.cleanElement = function(str) { return str.replace(/\s{2,}/mg,' ').trim(); }

Extract a specified attribute from a node

Parameters:

  • node must be a Node.
    (the DOM node)

  • attribute must be a String.
    (the name of the attribute to extract)

Returns a String
(the attribute value)

dom.getAttribute = function(node, attribute) { if (!attribute || attribute == 'text') { return node.textContent; } else if (attribute == 'html') { return node.innerHTML; } else { return node.getAttribute(attribute); } }

Select a DOM node matching an XPath selector

Parameters:

  • selector must be a String.
    (an XPath selector)

  • doc must be an Object.
    (the DOM to search)

Returns a Node
(the selected node)

dom.select = function(selector, doc) { return xpath.select(selector, doc); }