UNPKG

stew-select

Version:

CSS selectors that allow regular expressions. Stew is a meatier soup.

334 lines (324 loc) 13 kB
// Generated by CoffeeScript 1.6.3 (function() { var DOMUtil, HOMEDIR, HTMLPARSER_OPTIONS, LIB_DIR, fs, path, should, _this = this; should = require('should'); fs = require('fs'); path = require('path'); HOMEDIR = path.join(__dirname, '..'); LIB_DIR = fs.existsSync(path.join(HOMEDIR, 'lib-cov')) ? path.join(HOMEDIR, 'lib-cov') : path.join(HOMEDIR, 'lib'); DOMUtil = require(path.join(LIB_DIR, 'dom-util')).DOMUtil; HTMLPARSER_OPTIONS = { ignoreWhitespace: false, caseSensitiveTags: false, caseSensitiveAttr: false }; describe("DOMUtil", function() { beforeEach(function(done) { _this.dom_util = new DOMUtil(); return done(); }); afterEach(function(done) { _this.dom_util = null; return done(); }); describe("parse_html", function() { it("produces a DOM tree for the given HTML string", function(done) { return _this.dom_util.parse_html('<html><div>This is an example HTML document</div></html>', function(err, dom) { should.not.exist(err); should.exist(dom); dom.type.should.equal('tag'); dom.name.should.equal('html'); return done(); }); }); it("produces an array of DOM trees when the given HTML string has more than one root", function(done) { return _this.dom_util.parse_html('<div>First div.</div><span>Second span.</span>', function(err, dom) { should.not.exist(err); should.exist(dom); dom.length.should.equal(2); dom[0].type.should.equal('tag'); dom[0].name.should.equal('div'); dom[1].type.should.equal('tag'); dom[1].name.should.equal('span'); return done(); }); }); return it("allows a map of options to be passed", function(done) { return _this.dom_util.parse_html('<html><DIV>This is an example HTML document</div></HTML>', HTMLPARSER_OPTIONS, function(err, dom) { should.not.exist(err); should.exist(dom); dom.type.should.equal('tag'); dom.name.should.equal('html'); return done(); }); }); }); it("as_node converts a nodeset to a single node", function(done) { var nodes; should.not.exist(_this.dom_util.as_node(null)); should.not.exist(_this.dom_util.as_node([])); should.not.exist(_this.dom_util.as_node([null])); nodes = [ { type: 'tag', name: 'html' }, { type: 'tag', name: 'body' }, { type: 'tag', name: 'div' }, { type: 'tag', name: 'span' } ]; _this.dom_util.as_node(nodes).name.should.equal('html'); _this.dom_util.as_node([ { type: 'tag', name: 'html' } ]).name.should.equal('html'); _this.dom_util.as_node({ type: 'tag', name: 'html' }).name.should.equal('html'); return done(); }); it("as_nodeset converts a node to a nodeset", function(done) { var nodes; _this.dom_util.as_nodeset(null).length.should.equal(0); _this.dom_util.as_nodeset([]).length.should.equal(0); _this.dom_util.as_nodeset([null]).length.should.equal(1); nodes = [ { type: 'tag', name: 'html' }, { type: 'tag', name: 'body' }, { type: 'tag', name: 'div' }, { type: 'tag', name: 'span' } ]; _this.dom_util.as_nodeset(nodes).length.should.equal(4); _this.dom_util.as_nodeset(nodes[0]).length.should.equal(1); return done(); }); describe("to_text", function() { it("returns the value of all text descendants", function(done) { var html; html = '<html><div id="A"><span>alpha</span></div><div id="B"><b><i>beta</i></b></div></html>'; return _this.dom_util.parse_html(html, HTMLPARSER_OPTIONS, function(err, dom) { var text; text = _this.dom_util.to_text(dom); text.should.equal('alphabeta'); return done(); }); }); it("handles whitespace between nodes", function(done) { var html; html = '<html><div id="A"> <span>alpha</span></div> <div id="B"><b><i>beta</i> </b></div></html>'; return _this.dom_util.parse_html(html, HTMLPARSER_OPTIONS, function(err, dom) { var text; text = _this.dom_util.to_text(dom); text.should.equal(' alpha beta '); return done(); }); }); it("supports a `filter` function for excluding nodes", function(done) { var html; html = '<html><div id="A"><span>alpha</span></div><div id="B"><b><i>beta</i></b></div></html>'; return _this.dom_util.parse_html(html, HTMLPARSER_OPTIONS, function(err, dom) { var text; text = _this.dom_util.to_text(dom, function(node) { var _ref; return ((_ref = node.attribs) != null ? _ref.id : void 0) !== 'A'; }); text.should.equal('beta'); return done(); }); }); it("supports a `decode` function for converting HTML to text nodes", function(done) { var dom_util, html; dom_util = new DOMUtil({ decode: function(str) { return str.toUpperCase(); } }); html = '<html><div id="A"><span>alpha</span></div><div id="B"><b><i>beta</i></b></div></html>'; return _this.dom_util.parse_html(html, HTMLPARSER_OPTIONS, function(err, dom) { var text; text = dom_util.to_text(dom); text.should.equal('ALPHABETA'); return done(); }); }); return it("is also known as `inner_text`", function(done) { var html; html = '<html><div id="A"><span>alpha</span></div><div id="B"><b><i>beta</i></b></div></html>'; return _this.dom_util.parse_html(html, HTMLPARSER_OPTIONS, function(err, dom) { var text; text = _this.dom_util.inner_text(dom); text.should.equal('alphabeta'); return done(); }); }); }); describe("inner_html", function() { it("returns an HTML representation of the children of the given node", function(done) { var html; html = '<html><div id="A"><span>alpha</span></div><div id="B"><b><i>beta</i></b></div></html>'; return _this.dom_util.parse_html(html, HTMLPARSER_OPTIONS, function(err, dom) { var text; text = _this.dom_util.inner_html(dom); text.should.equal('<div id="A"><span>alpha</span></div><div id="B"><b><i>beta</i></b></div>'); return done(); }); }); return it("handles whitespace between nodes", function(done) { var html; html = '<html><div id="A"> <span>alpha</span></div> <div id="B"><b><i>beta</i> </b></div></html>'; return _this.dom_util.parse_html(html, HTMLPARSER_OPTIONS, function(err, dom) { var text; text = _this.dom_util.inner_html(dom); text.should.equal('<div id="A"> <span>alpha</span></div> <div id="B"><b><i>beta</i> </b></div>'); return done(); }); }); }); describe("to_html", function() { it("returns an HTML representation of the given node", function(done) { var html; html = '<html><div id="A"><span>alpha</span></div><div id="B"><b><i>beta</i></b></div></html>'; return _this.dom_util.parse_html(html, HTMLPARSER_OPTIONS, function(err, dom) { var text; text = _this.dom_util.to_html(dom); text.should.equal('<html><div id="A"><span>alpha</span></div><div id="B"><b><i>beta</i></b></div></html>'); return done(); }); }); return it("handles whitespace between nodes", function(done) { var html; html = '<html><div id="A"> <span>alpha</span></div> <div id="B"><b><i>beta</i> </b></div></html>'; return _this.dom_util.parse_html(html, HTMLPARSER_OPTIONS, function(err, dom) { var text; text = _this.dom_util.to_html(dom); text.should.equal('<html><div id="A"> <span>alpha</span></div> <div id="B"><b><i>beta</i> </b></div></html>'); return done(); }); }); }); return describe("walk_dom", function() { it("performs a depth-first walk of the dom tree", function(done) { var html; html = '<html><div id="A"><span>alpha</span></div><div id="B"><b><i>beta</i></b></div></html>'; return _this.dom_util.parse_html(html, HTMLPARSER_OPTIONS, function(err, dom) { var expected; expected = ['html', 'div', 'span', 'div', 'b', 'i']; _this.dom_util.walk_dom(dom, function(node) { var expected_name; if (node.type === 'tag') { expected_name = expected.shift(); node.name.should.equal(expected_name); } return true; }); expected.length.should.equal(0); return done(); }); }); it("passes parent node to visit function", function(done) { var html; html = '<html><div id="A"><span>alpha</span></div><div id="B"><b><i>beta</i></b></div></html>'; return _this.dom_util.parse_html(html, HTMLPARSER_OPTIONS, function(err, dom) { var expected; expected = [null, 'html', 'div', 'html', 'div', 'b']; _this.dom_util.walk_dom(dom, function(node, node_metadata) { var expected_name; if (node.type === 'tag') { expected_name = expected.shift(); if (expected_name != null) { node_metadata.parent.should.exist; node_metadata.parent.name.should.equal(expected_name); } else { should.not.exist(node_metadata.parent); } } return true; }); expected.length.should.equal(0); return done(); }); }); it("passes path to node to visit function", function(done) { var html; html = '<html><div id="A"><span>alpha</span></div><div id="B"><b><i>beta</i></b></div></html>'; return _this.dom_util.parse_html(html, HTMLPARSER_OPTIONS, function(err, dom) { var expected; expected = [[], ['html'], ['html', 'div'], ['html', 'div', 'span'], ['html'], ['html', 'div'], ['html', 'div', 'b'], ['html', 'div', 'b', 'i']]; _this.dom_util.walk_dom(dom, function(node, node_metadata) { var elt, expected_path, i, _i, _len; if (node.type === 'tag' || node.type === 'text') { expected_path = expected.shift(); expected_path.length.should.equal(node_metadata.path.length); for (i = _i = 0, _len = expected_path.length; _i < _len; i = ++_i) { elt = expected_path[i]; node_metadata.path[i].name.should.equal(elt); } } return true; }); expected.length.should.equal(0); return done(); }); }); it("passes siblings to visit function", function(done) { var html; html = '<html><div id="A"><span>alpha</span></div><div id="B"><b><i>beta</i></b></div></html>'; return _this.dom_util.parse_html(html, HTMLPARSER_OPTIONS, function(err, dom) { var expected; expected = [['html'], ['div', 'div'], ['span'], ['div', 'div'], ['b'], ['i']]; _this.dom_util.walk_dom(dom, function(node, node_metadata) { var elt, expected_sibs, i, _i, _len; if (node.type === 'tag') { expected_sibs = expected.shift(); expected_sibs.length.should.equal(node_metadata.siblings.length); for (i = _i = 0, _len = expected_sibs.length; _i < _len; i = ++_i) { elt = expected_sibs[i]; node_metadata.siblings[i].name.should.equal(elt); } } return true; }); expected.length.should.equal(0); return done(); }); }); return it("passes sibling index to visit function", function(done) { var html; html = '<html><div id="A"><span>alpha</span></div><div id="B"><b><i>beta</i></b></div></html>'; return _this.dom_util.parse_html(html, HTMLPARSER_OPTIONS, function(err, dom) { var expected; expected = [0, 0, 0, 1, 0, 0]; _this.dom_util.walk_dom(dom, function(node, node_metadata) { var expected_index; if (node.type === 'tag') { expected_index = expected.shift(); node_metadata.sib_index.should.equal(expected_index); } return true; }); expected.length.should.equal(0); return done(); }); }); }); }); }).call(this);