| Line | Hits | Source |
|---|---|---|
| 1 | // Generated by CoffeeScript 1.6.3 | |
| 2 | 1 | (function() { |
| 3 | 1 | var DOMUtil, exports; |
| 4 | ||
| 5 | 1 | DOMUtil = (function() { |
| 6 | 1 | function DOMUtil(params) { |
| 7 | 66 | var _ref; |
| 8 | 66 | if (params == null) { |
| 9 | 66 | params = {}; |
| 10 | } | |
| 11 | 66 | this.decode = (_ref = params != null ? params['decode'] : void 0) != null ? _ref : function(str) { |
| 12 | 10 | return str; |
| 13 | }; | |
| 14 | } | |
| 15 | ||
| 16 | 1 | DOMUtil.prototype.parse_html = function(html, options, callback) { |
| 17 | 18 | var err, handler, parser, _ref; |
| 18 | 18 | if (typeof options === 'function' && typeof callback !== 'function') { |
| 19 | 4 | _ref = [callback, options], options = _ref[0], callback = _ref[1]; |
| 20 | } | |
| 21 | 18 | if (this.htmlparser == null) { |
| 22 | 18 | try { |
| 23 | 18 | this.htmlparser = require('htmlparser'); |
| 24 | } catch (_error) { | |
| 25 | 0 | err = _error; |
| 26 | 0 | callback(err, null); |
| 27 | } | |
| 28 | } | |
| 29 | 18 | if (this.htmlparser != null) { |
| 30 | 18 | handler = new this.htmlparser.DefaultHandler(function(err, domset) { |
| 31 | 18 | if (err != null) { |
| 32 | 0 | return callback(err, null); |
| 33 | 18 | } else if (Array.isArray(domset) && domset.length <= 1) { |
| 34 | 17 | return callback(null, domset[0]); |
| 35 | } else { | |
| 36 | 1 | return callback(null, domset); |
| 37 | } | |
| 38 | }); | |
| 39 | 18 | parser = new this.htmlparser.Parser(handler, options); |
| 40 | 18 | return parser.parseComplete(html); |
| 41 | } | |
| 42 | }; | |
| 43 | ||
| 44 | 1 | DOMUtil.prototype.as_node = function(nodeset) { |
| 45 | 6 | if (Array.isArray(nodeset)) { |
| 46 | 4 | return nodeset[0]; |
| 47 | } else { | |
| 48 | 2 | return nodeset; |
| 49 | } | |
| 50 | }; | |
| 51 | ||
| 52 | 1 | DOMUtil.prototype.as_nodeset = function(node) { |
| 53 | 138 | if (Array.isArray(node)) { |
| 54 | 123 | return node; |
| 55 | 15 | } else if (node != null) { |
| 56 | 14 | return [node]; |
| 57 | } else { | |
| 58 | 1 | return []; |
| 59 | } | |
| 60 | }; | |
| 61 | ||
| 62 | 1 | DOMUtil.prototype._kt = function() { |
| 63 | 27 | return true; |
| 64 | }; | |
| 65 | ||
| 66 | 1 | DOMUtil.prototype.to_text = function(elt, filter) { |
| 67 | 4 | var buffer, |
| 68 | _this = this; | |
| 69 | 4 | if (filter == null) { |
| 70 | 3 | filter = this._kt; |
| 71 | } | |
| 72 | 4 | buffer = ''; |
| 73 | 4 | this.walk_dom(elt, { |
| 74 | visit: function(node, node_metadata, all_metadata) { | |
| 75 | 33 | if (filter(node, node_metadata, all_metadata)) { |
| 76 | 32 | if ((node != null ? node.type : void 0) === 'text' && ((node != null ? node.raw : void 0) != null)) { |
| 77 | 10 | buffer += _this.decode(node.raw); |
| 78 | } | |
| 79 | 32 | return { |
| 80 | 'continue': true, | |
| 81 | 'visit_children': true | |
| 82 | }; | |
| 83 | } else { | |
| 84 | 1 | return { |
| 85 | 'continue': true, | |
| 86 | 'visit_children': false | |
| 87 | }; | |
| 88 | } | |
| 89 | } | |
| 90 | }); | |
| 91 | 4 | return buffer; |
| 92 | }; | |
| 93 | ||
| 94 | 1 | DOMUtil.prototype.inner_text = function(elt, filter) { |
| 95 | 1 | return this.to_text(elt, filter); |
| 96 | }; | |
| 97 | ||
| 98 | 1 | DOMUtil.prototype.to_html = function(elt) { |
| 99 | 4 | var buffer; |
| 100 | 4 | buffer = ''; |
| 101 | 4 | this.walk_dom(elt, { |
| 102 | visit: function(node) { | |
| 103 | 36 | var name, value, _ref; |
| 104 | 36 | switch (node.type) { |
| 105 | case 'text': | |
| 106 | 14 | buffer += node.raw; |
| 107 | 14 | break; |
| 108 | case 'tag': | |
| 109 | 22 | buffer += "<" + node.name; |
| 110 | 22 | if (node.attribs != null) { |
| 111 | 8 | _ref = node.attribs; |
| 112 | 8 | for (name in _ref) { |
| 113 | 8 | value = _ref[name]; |
| 114 | 8 | buffer += " " + name + "=\"" + value + "\""; |
| 115 | } | |
| 116 | } | |
| 117 | 22 | buffer += ">"; |
| 118 | } | |
| 119 | 36 | return true; |
| 120 | }, | |
| 121 | after_visit: function(node) { | |
| 122 | 36 | switch (node.type) { |
| 123 | case 'tag': | |
| 124 | 22 | buffer += "</" + node.name + ">"; |
| 125 | } | |
| 126 | 36 | return true; |
| 127 | } | |
| 128 | }); | |
| 129 | 4 | return buffer; |
| 130 | }; | |
| 131 | ||
| 132 | 1 | DOMUtil.prototype.inner_html = function(elt) { |
| 133 | 2 | var buffer, node, _i, _len; |
| 134 | 2 | buffer = null; |
| 135 | 2 | if (Array.isArray(elt)) { |
| 136 | 0 | buffer = ''; |
| 137 | 0 | for (_i = 0, _len = elt.length; _i < _len; _i++) { |
| 138 | 0 | node = elt[_i]; |
| 139 | 0 | if (node.children != null) { |
| 140 | 0 | buffer += this.to_html(node.children); |
| 141 | } | |
| 142 | } | |
| 143 | 2 | } else if ((elt != null ? elt.children : void 0) != null) { |
| 144 | 2 | buffer = this.to_html(elt.children); |
| 145 | } | |
| 146 | 2 | return buffer; |
| 147 | }; | |
| 148 | ||
| 149 | 1 | DOMUtil.prototype.walk_dom = function(dom, callbacks) { |
| 150 | 133 | var dom_metadata, node, node_metadata, nodes, should_continue, sib_index, _i, _len, _results; |
| 151 | 133 | if (typeof callbacks === 'function') { |
| 152 | 5 | callbacks = { |
| 153 | visit: callbacks | |
| 154 | }; | |
| 155 | } | |
| 156 | 133 | nodes = this.as_nodeset(dom); |
| 157 | 133 | dom_metadata = []; |
| 158 | 133 | _results = []; |
| 159 | 133 | for (sib_index = _i = 0, _len = nodes.length; _i < _len; sib_index = ++_i) { |
| 160 | 136 | node = nodes[sib_index]; |
| 161 | 136 | node_metadata = { |
| 162 | parent: null, | |
| 163 | path: [], | |
| 164 | siblings: nodes, | |
| 165 | sib_index: sib_index | |
| 166 | }; | |
| 167 | 136 | node._stew_node_id = dom_metadata.length; |
| 168 | 136 | dom_metadata.push(node_metadata); |
| 169 | 136 | should_continue = this._unguarded_walk_dom(node, node_metadata, dom_metadata, callbacks); |
| 170 | 136 | if (!should_continue) { |
| 171 | 5 | break; |
| 172 | } else { | |
| 173 | 131 | _results.push(void 0); |
| 174 | } | |
| 175 | } | |
| 176 | 133 | return _results; |
| 177 | }; | |
| 178 | ||
| 179 | 1 | DOMUtil.prototype._unguarded_walk_dom = function(node, node_metadata, dom_metadata, callbacks) { |
| 180 | 3988 | var child, index, new_node_metadata, new_path, response, should_continue, _i, _len, _ref; |
| 181 | 3988 | response = { |
| 182 | 'continue': true, | |
| 183 | 'visit_children': true | |
| 184 | }; | |
| 185 | 3988 | if (callbacks.visit != null) { |
| 186 | 3988 | response = callbacks.visit(node, node_metadata, dom_metadata); |
| 187 | } | |
| 188 | 3988 | if (response === true || (response != null ? response['continue'] : void 0) === true || ((response != null ? response['continue'] : void 0) == null)) { |
| 189 | 3983 | if ((node.children != null) && (response === true || (response != null ? response['visit_children'] : void 0) === true || ((response != null ? response['visit_children'] : void 0) == null))) { |
| 190 | 1725 | new_path = [].concat(node_metadata.path); |
| 191 | 1725 | new_path.push(node); |
| 192 | 1725 | _ref = node.children; |
| 193 | 1725 | for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) { |
| 194 | 3852 | child = _ref[index]; |
| 195 | 3852 | new_node_metadata = { |
| 196 | parent: node, | |
| 197 | path: new_path, | |
| 198 | siblings: node.children, | |
| 199 | sib_index: index | |
| 200 | }; | |
| 201 | 3852 | child._stew_node_id = dom_metadata.length; |
| 202 | 3852 | dom_metadata.push(new_node_metadata); |
| 203 | 3852 | should_continue = this._unguarded_walk_dom(child, new_node_metadata, dom_metadata, callbacks); |
| 204 | 3852 | if (!should_continue) { |
| 205 | 17 | return false; |
| 206 | } | |
| 207 | } | |
| 208 | } | |
| 209 | 3966 | if (callbacks['after_visit'] != null) { |
| 210 | 36 | response = callbacks.after_visit(node, node_metadata, dom_metadata); |
| 211 | 36 | return response === true || (response != null ? response['continue'] : void 0) === true || ((response != null ? response['continue'] : void 0) == null); |
| 212 | } else { | |
| 213 | 3930 | return true; |
| 214 | } | |
| 215 | } else { | |
| 216 | 5 | return false; |
| 217 | } | |
| 218 | }; | |
| 219 | ||
| 220 | 1 | return DOMUtil; |
| 221 | ||
| 222 | })(); | |
| 223 | ||
| 224 | 1 | exports = exports != null ? exports : this; |
| 225 | ||
| 226 | 1 | exports.DOMUtil = DOMUtil; |
| 227 | ||
| 228 | }).call(this); |
| Line | Hits | Source |
|---|---|---|
| 1 | // Generated by CoffeeScript 1.6.3 | |
| 2 | 1 | (function() { |
| 3 | 1 | var PredicateFactory, exports, |
| 4 | 404 | __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; |
| 5 | ||
| 6 | 1 | PredicateFactory = (function() { |
| 7 | 1 | function PredicateFactory() { |
| 8 | 49 | this.by_attr_value_pipe_equals = __bind(this.by_attr_value_pipe_equals, this); |
| 9 | 49 | this.by_attr_value_predicate = __bind(this.by_attr_value_predicate, this); |
| 10 | 49 | this.by_attr_exists_predicate = __bind(this.by_attr_exists_predicate, this); |
| 11 | 49 | this.by_id_predicate = __bind(this.by_id_predicate, this); |
| 12 | 49 | this.by_class_predicate = __bind(this.by_class_predicate, this); |
| 13 | } | |
| 14 | ||
| 15 | 1 | PredicateFactory.prototype.and_predicate = function(predicates) { |
| 16 | 166 | return function(node, node_metadata, dom_metadata) { |
| 17 | 4106 | var predicate, _i, _len; |
| 18 | 4106 | for (_i = 0, _len = predicates.length; _i < _len; _i++) { |
| 19 | 4421 | predicate = predicates[_i]; |
| 20 | 4421 | if (!predicate(node, node_metadata, dom_metadata)) { |
| 21 | 3760 | return false; |
| 22 | } | |
| 23 | } | |
| 24 | 346 | return true; |
| 25 | }; | |
| 26 | }; | |
| 27 | ||
| 28 | 1 | PredicateFactory.prototype.or_predicate = function(predicates) { |
| 29 | 2 | return function(node, node_metadata, dom_metadata) { |
| 30 | 66 | var predicate, _i, _len; |
| 31 | 66 | for (_i = 0, _len = predicates.length; _i < _len; _i++) { |
| 32 | 128 | predicate = predicates[_i]; |
| 33 | 128 | if (predicate(node, node_metadata, dom_metadata)) { |
| 34 | 8 | return true; |
| 35 | } | |
| 36 | } | |
| 37 | 58 | return false; |
| 38 | }; | |
| 39 | }; | |
| 40 | ||
| 41 | 1 | PredicateFactory.prototype.by_attribute_predicate = function(attrname, attrvalue, valuedelim) { |
| 42 | 159 | var np, vp; |
| 43 | 159 | if (attrvalue == null) { |
| 44 | 8 | attrvalue = null; |
| 45 | } | |
| 46 | 159 | if (valuedelim == null) { |
| 47 | 88 | valuedelim = null; |
| 48 | } | |
| 49 | 159 | if (typeof attrname === 'string') { |
| 50 | 154 | np = function(str) { |
| 51 | 1023 | return str === attrname; |
| 52 | }; | |
| 53 | } else { | |
| 54 | 5 | np = function(str) { |
| 55 | 6 | return attrname.test(str); |
| 56 | }; | |
| 57 | } | |
| 58 | 159 | if (attrvalue === null) { |
| 59 | 8 | vp = null; |
| 60 | 151 | } else if (typeof attrvalue === 'string') { |
| 61 | 74 | attrvalue = attrvalue.replace(/\\\"/g, '"'); |
| 62 | 74 | vp = function(str) { |
| 63 | 240 | return str === attrvalue; |
| 64 | }; | |
| 65 | 77 | } else if ((attrvalue != null ? attrvalue.test : void 0) != null) { |
| 66 | 77 | vp = function(str) { |
| 67 | 202 | return attrvalue.test(str); |
| 68 | }; | |
| 69 | } | |
| 70 | 159 | return function(node) { |
| 71 | 1004 | var name, token, value, _i, _len, _ref, _ref1; |
| 72 | 1004 | _ref = node != null ? node.attribs : void 0; |
| 73 | 1004 | for (name in _ref) { |
| 74 | 1029 | value = _ref[name]; |
| 75 | 1029 | if (np(name)) { |
| 76 | 350 | if (vp === null) { |
| 77 | 5 | return true; |
| 78 | } else { | |
| 79 | 345 | if (valuedelim != null) { |
| 80 | 147 | if (value != null) { |
| 81 | 146 | _ref1 = value.split(valuedelim); |
| 82 | 146 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { |
| 83 | 244 | token = _ref1[_i]; |
| 84 | 244 | if (vp(token)) { |
| 85 | 79 | return true; |
| 86 | } | |
| 87 | } | |
| 88 | } | |
| 89 | } else { | |
| 90 | 198 | if (vp(value)) { |
| 91 | 108 | return true; |
| 92 | } | |
| 93 | } | |
| 94 | } | |
| 95 | } | |
| 96 | } | |
| 97 | 812 | return false; |
| 98 | }; | |
| 99 | }; | |
| 100 | ||
| 101 | 1 | PredicateFactory.prototype.by_class_predicate = function(klass) { |
| 102 | 64 | return this.by_attribute_predicate('class', klass, /\s+/); |
| 103 | }; | |
| 104 | ||
| 105 | 1 | PredicateFactory.prototype.by_id_predicate = function(id) { |
| 106 | 8 | return this.by_attribute_predicate('id', id); |
| 107 | }; | |
| 108 | ||
| 109 | 1 | PredicateFactory.prototype.by_attr_exists_predicate = function(attrname) { |
| 110 | 8 | return this.by_attribute_predicate(attrname, null); |
| 111 | }; | |
| 112 | ||
| 113 | 1 | PredicateFactory.prototype.by_attr_value_predicate = function(attrname, attrvalue, valuedelim) { |
| 114 | 73 | return this.by_attribute_predicate(attrname, attrvalue, valuedelim); |
| 115 | }; | |
| 116 | ||
| 117 | 1 | PredicateFactory.prototype._escape_for_regexp = function(str) { |
| 118 | 10 | return str.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1"); |
| 119 | }; | |
| 120 | ||
| 121 | 1 | PredicateFactory.prototype.by_attr_value_pipe_equals = function(attrname, attrvalue) { |
| 122 | 6 | var modifier, regexp_source; |
| 123 | 6 | if (typeof attrvalue === 'string') { |
| 124 | 2 | regexp_source = this._escape_for_regexp(attrvalue); |
| 125 | 2 | attrvalue = new RegExp("^" + regexp_source + "($|-)"); |
| 126 | } else { | |
| 127 | 4 | regexp_source = attrvalue.source; |
| 128 | 4 | modifier = ''; |
| 129 | 4 | if (attrvalue.ignoreCase) { |
| 130 | 1 | modifier += 'i'; |
| 131 | } | |
| 132 | 4 | if (attrvalue.global) { |
| 133 | 0 | modifier += 'g'; |
| 134 | } | |
| 135 | 4 | if (attrvalue.multiline) { |
| 136 | 0 | modifier += 'm'; |
| 137 | } | |
| 138 | 4 | if (!/^\^/.test(attrvalue.source)) { |
| 139 | 3 | regexp_source = "^" + regexp_source; |
| 140 | } | |
| 141 | 4 | if (!/\(\$\|-\)$/.test(regexp_source)) { |
| 142 | 4 | regexp_source = "" + regexp_source + "($|-)"; |
| 143 | } | |
| 144 | 4 | attrvalue = new RegExp(regexp_source, modifier); |
| 145 | } | |
| 146 | 6 | return this.by_attribute_predicate(attrname, attrvalue); |
| 147 | }; | |
| 148 | ||
| 149 | 1 | PredicateFactory.prototype.by_tag_predicate = function(name) { |
| 150 | 130 | if (typeof name === 'string') { |
| 151 | 118 | return function(node) { |
| 152 | 3073 | return name === node.name; |
| 153 | }; | |
| 154 | } else { | |
| 155 | 12 | return function(node) { |
| 156 | 313 | return name.test(node.name); |
| 157 | }; | |
| 158 | } | |
| 159 | }; | |
| 160 | ||
| 161 | 1 | PredicateFactory.prototype.first_child_predicate = function() { |
| 162 | 3 | return this._first_child_impl; |
| 163 | }; | |
| 164 | ||
| 165 | 1 | PredicateFactory.prototype._first_child_impl = function(node, node_metadata, dom_metadata) { |
| 166 | 9 | var elt, _i, _len, _ref; |
| 167 | 9 | if (node.type === 'tag' && (node_metadata.siblings != null)) { |
| 168 | 9 | _ref = node_metadata.siblings; |
| 169 | 9 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
| 170 | 17 | elt = _ref[_i]; |
| 171 | 17 | if (elt.type === 'tag') { |
| 172 | 9 | return node._stew_node_id === elt._stew_node_id; |
| 173 | } | |
| 174 | } | |
| 175 | } | |
| 176 | 0 | return false; |
| 177 | }; | |
| 178 | ||
| 179 | 1 | PredicateFactory.prototype.any_tag_predicate = function() { |
| 180 | 4 | return this._any_tag_impl; |
| 181 | }; | |
| 182 | ||
| 183 | 1 | PredicateFactory.prototype._any_tag_impl = function(node) { |
| 184 | 79 | return (node != null ? node.type : void 0) === 'tag'; |
| 185 | }; | |
| 186 | ||
| 187 | 1 | PredicateFactory.prototype.descendant_predicate = function(predicates) { |
| 188 | 123 | if (predicates.length === 1) { |
| 189 | 100 | return predicates[0]; |
| 190 | } else { | |
| 191 | 23 | return function(node, node_metadata, dom_metadata) { |
| 192 | 700 | var cloned_path, cloned_predicates; |
| 193 | 700 | if (predicates[predicates.length - 1](node, node_metadata, dom_metadata)) { |
| 194 | 78 | cloned_path = [].concat(node_metadata.path); |
| 195 | 78 | cloned_predicates = [].concat(predicates); |
| 196 | 78 | cloned_predicates.pop(); |
| 197 | 78 | while (cloned_path.length > 0) { |
| 198 | 132 | node = cloned_path.pop(); |
| 199 | 132 | node_metadata = dom_metadata[node._stew_node_id]; |
| 200 | 132 | if (cloned_predicates[cloned_predicates.length - 1](node, node_metadata, dom_metadata)) { |
| 201 | 56 | cloned_predicates.pop(); |
| 202 | 56 | if (cloned_predicates.length === 0) { |
| 203 | 53 | return true; |
| 204 | 0 | break; |
| 205 | } | |
| 206 | } | |
| 207 | } | |
| 208 | } | |
| 209 | 647 | return false; |
| 210 | }; | |
| 211 | } | |
| 212 | }; | |
| 213 | ||
| 214 | 1 | PredicateFactory.prototype.direct_descendant_predicate = function(parent_selector, child_selector) { |
| 215 | 3 | return function(node, node_metadata, dom_metadata) { |
| 216 | 99 | var parent, parent_metadata; |
| 217 | 99 | if (child_selector(node, node_metadata, dom_metadata)) { |
| 218 | 18 | parent = node_metadata.parent; |
| 219 | 18 | parent_metadata = dom_metadata[parent._stew_node_id]; |
| 220 | 18 | return parent_selector(parent, parent_metadata, dom_metadata); |
| 221 | } | |
| 222 | 81 | return false; |
| 223 | }; | |
| 224 | }; | |
| 225 | ||
| 226 | 1 | PredicateFactory.prototype.adjacent_sibling_predicate = function(first, second) { |
| 227 | 5 | return function(node, node_metadata, dom_metadata) { |
| 228 | 165 | var prev_tag, prev_tag_index; |
| 229 | 165 | if (second(node, node_metadata, dom_metadata)) { |
| 230 | 20 | prev_tag_index = node_metadata.sib_index - 1; |
| 231 | 20 | while (prev_tag_index > 0) { |
| 232 | 22 | if (node_metadata.siblings[prev_tag_index].type === 'tag') { |
| 233 | 11 | prev_tag = node_metadata.siblings[prev_tag_index]; |
| 234 | 11 | return first(prev_tag, dom_metadata[prev_tag._stew_node_id], dom_metadata); |
| 235 | } else { | |
| 236 | 11 | prev_tag_index -= 1; |
| 237 | } | |
| 238 | } | |
| 239 | } | |
| 240 | 154 | return false; |
| 241 | }; | |
| 242 | }; | |
| 243 | ||
| 244 | 1 | PredicateFactory.prototype.preceding_sibling_predicate = function(first, second) { |
| 245 | 4 | return function(node, node_metadata, dom_metadata) { |
| 246 | 76 | var index, prev, _i, _len, _ref; |
| 247 | 76 | if (second(node, node_metadata, dom_metadata)) { |
| 248 | 4 | _ref = node_metadata.siblings; |
| 249 | 4 | for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) { |
| 250 | 8 | prev = _ref[index]; |
| 251 | 8 | if (index === node_metadata.sib_index) { |
| 252 | 0 | return false; |
| 253 | 8 | } else if (prev.type === 'tag') { |
| 254 | 4 | if (first(prev, dom_metadata[prev._stew_node_id], dom_metadata)) { |
| 255 | 4 | return true; |
| 256 | } | |
| 257 | } | |
| 258 | } | |
| 259 | } | |
| 260 | 72 | return false; |
| 261 | }; | |
| 262 | }; | |
| 263 | ||
| 264 | 1 | return PredicateFactory; |
| 265 | ||
| 266 | })(); | |
| 267 | ||
| 268 | 1 | exports = exports != null ? exports : this; |
| 269 | ||
| 270 | 1 | exports.PredicateFactory = PredicateFactory; |
| 271 | ||
| 272 | }).call(this); |
| Line | Hits | Source |
|---|---|---|
| 1 | // Generated by CoffeeScript 1.6.3 | |
| 2 | 1 | (function() { |
| 3 | 1 | var DOMUtil, HOMEDIR, LIB_DIR, PredicateFactory, Stew, exports, fs, path; |
| 4 | ||
| 5 | 1 | fs = require('fs'); |
| 6 | ||
| 7 | 1 | path = require('path'); |
| 8 | ||
| 9 | 1 | HOMEDIR = path.join(__dirname, '..'); |
| 10 | ||
| 11 | 1 | LIB_DIR = fs.existsSync(path.join(HOMEDIR, 'lib-cov')) ? path.join(HOMEDIR, 'lib-cov') : path.join(HOMEDIR, 'lib'); |
| 12 | ||
| 13 | 1 | DOMUtil = require(path.join(LIB_DIR, 'dom-util')).DOMUtil; |
| 14 | ||
| 15 | 1 | PredicateFactory = require(path.join(LIB_DIR, 'predicate-factory')).PredicateFactory; |
| 16 | ||
| 17 | 1 | Stew = (function() { |
| 18 | 1 | var _ATTRIBUTES, _ATTR_NAME, _CLASSES, _DEQUOTED_ATTR_VALUE, _ID, _NAME, _NEVERQUOTED_ATTR_VALUE, _OPERATOR, _PSEUDO_CLASS, _SPLIT_ON_WS_REGEXP; |
| 19 | ||
| 20 | 1 | function Stew(dom_util) { |
| 21 | 48 | this.factory = new PredicateFactory(); |
| 22 | 48 | this.dom_util = dom_util != null ? dom_util : new DOMUtil(); |
| 23 | } | |
| 24 | ||
| 25 | 1 | Stew.prototype.select = function(dom, selector, callback) { |
| 26 | 116 | var nodeset, |
| 27 | _this = this; | |
| 28 | 116 | if (typeof selector === 'string') { |
| 29 | 116 | selector = this._parse_selectors(selector); |
| 30 | } | |
| 31 | 116 | if (typeof dom === 'string') { |
| 32 | 2 | if (callback != null) { |
| 33 | 1 | return this.dom_util.parse_html(dom, function(err, dom) { |
| 34 | 1 | if (err != null) { |
| 35 | 0 | return callback(err); |
| 36 | } else { | |
| 37 | 1 | return callback(null, _this._unguarded_select(dom, selector)); |
| 38 | } | |
| 39 | }); | |
| 40 | } else { | |
| 41 | 1 | throw new Error('When select is invoked on a string object, the `callback(err,nodeset)` parameter is required.'); |
| 42 | } | |
| 43 | } else { | |
| 44 | 114 | nodeset = this._unguarded_select(dom, selector); |
| 45 | 114 | if (typeof callback === "function") { |
| 46 | 1 | callback(null, nodeset); |
| 47 | } | |
| 48 | 114 | return nodeset; |
| 49 | } | |
| 50 | }; | |
| 51 | ||
| 52 | 1 | Stew.prototype._unguarded_select = function(dom, predicate) { |
| 53 | 115 | var result, visit; |
| 54 | 115 | result = []; |
| 55 | 115 | visit = function(node, parent, path, siblings, sib_index) { |
| 56 | 3795 | if (predicate(node, parent, path, siblings, sib_index)) { |
| 57 | 218 | result.push(node); |
| 58 | } | |
| 59 | 3795 | return { |
| 60 | 'continue': true, | |
| 61 | 'visit_children': true | |
| 62 | }; | |
| 63 | }; | |
| 64 | 115 | this.dom_util.walk_dom(dom, { |
| 65 | visit: visit | |
| 66 | }); | |
| 67 | 115 | return result; |
| 68 | }; | |
| 69 | ||
| 70 | 1 | Stew.prototype.select_first = function(dom, selector, callback) { |
| 71 | 6 | var node, |
| 72 | _this = this; | |
| 73 | 6 | if (typeof selector === 'string') { |
| 74 | 6 | selector = this._parse_selectors(selector); |
| 75 | } | |
| 76 | 6 | if (typeof dom === 'string') { |
| 77 | 2 | if (callback != null) { |
| 78 | 1 | return this.dom_util.parse_html(dom, function(err, dom) { |
| 79 | 1 | if (err != null) { |
| 80 | 0 | return callback(err); |
| 81 | } else { | |
| 82 | 1 | return callback(null, _this._unguarded_select_first(dom, selector)); |
| 83 | } | |
| 84 | }); | |
| 85 | } else { | |
| 86 | 1 | throw new Error('When select_first is invoked on a string object, the `callback(err,node)` parameter is required.'); |
| 87 | } | |
| 88 | } else { | |
| 89 | 4 | node = this._unguarded_select_first(dom, selector); |
| 90 | 4 | if (typeof callback === "function") { |
| 91 | 1 | callback(null, node); |
| 92 | } | |
| 93 | 4 | return node; |
| 94 | } | |
| 95 | }; | |
| 96 | ||
| 97 | 1 | Stew.prototype._unguarded_select_first = function(dom, predicate) { |
| 98 | 5 | var result, visit; |
| 99 | 5 | result = null; |
| 100 | 5 | visit = function(node, parent, path, siblings, sib_index) { |
| 101 | 84 | if (predicate(node, parent, path, siblings, sib_index)) { |
| 102 | 5 | result = node; |
| 103 | 5 | return { |
| 104 | 'continue': false, | |
| 105 | 'visit_children': false | |
| 106 | }; | |
| 107 | } else { | |
| 108 | 79 | return { |
| 109 | 'continue': true, | |
| 110 | 'visit_children': true | |
| 111 | }; | |
| 112 | } | |
| 113 | }; | |
| 114 | 5 | this.dom_util.walk_dom(dom, { |
| 115 | visit: visit | |
| 116 | }); | |
| 117 | 5 | return result; |
| 118 | }; | |
| 119 | ||
| 120 | 1 | _SPLIT_ON_WS_REGEXP = /([^\"\/\s,\+>]|(\"[^\"]+\")|(\/[^\/]+\/)|(\[[^\]]*\]))+|[,\+~>]/g; |
| 121 | ||
| 122 | 1 | Stew.prototype._split_on_ws_respecting_quotes = function(selector) { |
| 123 | 123 | var result, token; |
| 124 | 123 | result = []; |
| 125 | 123 | while (true) { |
| 126 | 303 | token = _SPLIT_ON_WS_REGEXP.exec(selector); |
| 127 | 303 | if ((token != null ? token[0] : void 0) != null) { |
| 128 | 180 | result.push(token[0]); |
| 129 | } else { | |
| 130 | 123 | break; |
| 131 | } | |
| 132 | } | |
| 133 | 123 | return result; |
| 134 | }; | |
| 135 | ||
| 136 | 1 | Stew.prototype._parse_selectors = function(selectors) { |
| 137 | 123 | var adjacent_operator, child_operator, or_operator, preceding_sibling_operator, predicate, result, selector, _i, _len; |
| 138 | 123 | result = []; |
| 139 | 123 | if (typeof selectors === 'string') { |
| 140 | 123 | selectors = this._split_on_ws_respecting_quotes(selectors); |
| 141 | } | |
| 142 | 123 | child_operator = false; |
| 143 | 123 | adjacent_operator = false; |
| 144 | 123 | preceding_sibling_operator = false; |
| 145 | 123 | or_operator = false; |
| 146 | 123 | for (_i = 0, _len = selectors.length; _i < _len; _i++) { |
| 147 | 180 | selector = selectors[_i]; |
| 148 | 180 | if (selector === '>') { |
| 149 | 3 | child_operator = true; |
| 150 | 177 | } else if (selector === '+') { |
| 151 | 5 | adjacent_operator = true; |
| 152 | 172 | } else if (selector === '~') { |
| 153 | 4 | preceding_sibling_operator = true; |
| 154 | 168 | } else if (selector === ',') { |
| 155 | 2 | or_operator = true; |
| 156 | } else { | |
| 157 | 166 | predicate = this._parse_selector(selector); |
| 158 | 166 | if (child_operator) { |
| 159 | 3 | result.push(this.factory.direct_descendant_predicate(result.pop(), predicate)); |
| 160 | 3 | child_operator = false; |
| 161 | 163 | } else if (adjacent_operator) { |
| 162 | 5 | result.push(this.factory.adjacent_sibling_predicate(result.pop(), predicate)); |
| 163 | 5 | adjacent_operator = false; |
| 164 | 158 | } else if (preceding_sibling_operator) { |
| 165 | 4 | result.push(this.factory.preceding_sibling_predicate(result.pop(), predicate)); |
| 166 | 4 | preceding_sibling_operator = false; |
| 167 | 154 | } else if (or_operator) { |
| 168 | 2 | result.push(this.factory.or_predicate([result.pop(), predicate])); |
| 169 | 2 | or_operator = false; |
| 170 | } else { | |
| 171 | 152 | result.push(predicate); |
| 172 | } | |
| 173 | } | |
| 174 | } | |
| 175 | 123 | if (result.length > 0) { |
| 176 | 123 | result = this.factory.descendant_predicate(result); |
| 177 | } | |
| 178 | 123 | return result; |
| 179 | }; | |
| 180 | ||
| 181 | 1 | Stew.prototype._CSS_SELECTOR_REGEXP = /((\/[^\/]*\/[gmi]*)|(\*|[\w-]+))?(\#((\/[^\/]*\/[gmi]*)|([\w-]+)))?((\.((\/[^\/]*\/[gmi]*)|([\w-]+)))*)((\[((\/[^\/]*\/[gmi]*)|([\w-]+))(((=)|(~=)|(\|=)|(\*=)|(\^=)|(\$=))(("(([^\\"]|(\\"))*)")|((\/[^\/]*\/[gmi]*)|([\w- ]+))))?\])*)(:([\w-]+))?/; |
| 182 | ||
| 183 | 1 | _NAME = 1; |
| 184 | ||
| 185 | 1 | _ID = 4; |
| 186 | ||
| 187 | 1 | _CLASSES = 8; |
| 188 | ||
| 189 | 1 | _ATTRIBUTES = 13; |
| 190 | ||
| 191 | 1 | _PSEUDO_CLASS = 35; |
| 192 | ||
| 193 | 1 | Stew.prototype._ATTRIBUTE_CLAUSE_REGEXP = /(\[((\/[^\/]*\/[gmi]*)|([\w-]+))(((=)|(~=)|(\|=)|(\*=)|(\^=)|(\$=))(("(([^\\"]|(\\"))*)")|((\/[^\/]*\/[gmi]*)|([\w- ]+))))?\])/g; |
| 194 | ||
| 195 | 1 | _ATTR_NAME = 2; |
| 196 | ||
| 197 | 1 | _OPERATOR = 6; |
| 198 | ||
| 199 | 1 | _DEQUOTED_ATTR_VALUE = 15; |
| 200 | ||
| 201 | 1 | _NEVERQUOTED_ATTR_VALUE = 18; |
| 202 | ||
| 203 | 1 | Stew.prototype._parse_selector = function(selector) { |
| 204 | 166 | var attr_match, aval, c, clauses, cs, delim, match, modifier, regexp_source, _i, _len, _ref, _ref1, _ref2, _ref3, _ref4, _ref5, _ref6; |
| 205 | 166 | match = this._CSS_SELECTOR_REGEXP.exec(selector); |
| 206 | 166 | clauses = []; |
| 207 | 166 | if (match[_NAME] != null) { |
| 208 | 134 | if (match[_NAME] === '*') { |
| 209 | 4 | clauses.push(this.factory.any_tag_predicate()); |
| 210 | } else { | |
| 211 | 130 | clauses.push(this.factory.by_tag_predicate(this._to_string_or_regex(match[_NAME]))); |
| 212 | } | |
| 213 | } | |
| 214 | 166 | if (match[_ID] != null) { |
| 215 | 8 | clauses.push(this.factory.by_id_predicate(this._to_string_or_regex(match[_ID].substring(1)))); |
| 216 | } | |
| 217 | 166 | if (((_ref = match[_CLASSES]) != null ? _ref.length : void 0) > 0) { |
| 218 | 23 | cs = match[_CLASSES].split('.'); |
| 219 | 23 | cs.shift(); |
| 220 | 23 | for (_i = 0, _len = cs.length; _i < _len; _i++) { |
| 221 | 28 | c = cs[_i]; |
| 222 | 28 | clauses.push(this.factory.by_class_predicate(this._to_string_or_regex(c))); |
| 223 | } | |
| 224 | } | |
| 225 | 166 | if (((_ref1 = match[_ATTRIBUTES]) != null ? _ref1.length : void 0) > 0) { |
| 226 | 64 | attr_match = this._ATTRIBUTE_CLAUSE_REGEXP.exec(match[_ATTRIBUTES]); |
| 227 | 64 | while (attr_match != null) { |
| 228 | 66 | if ((attr_match[_ATTR_NAME] != null) && (attr_match[_OPERATOR] == null)) { |
| 229 | 4 | clauses.push(this.factory.by_attr_exists_predicate(this._to_string_or_regex(attr_match[_ATTR_NAME]))); |
| 230 | } | |
| 231 | 66 | if ((attr_match[_ATTR_NAME] != null) && (attr_match[_OPERATOR] != null) && ((attr_match[_DEQUOTED_ATTR_VALUE] != null) || (attr_match[_NEVERQUOTED_ATTR_VALUE] != null))) { |
| 232 | 62 | delim = null; |
| 233 | 62 | if (attr_match[_OPERATOR] === '~=') { |
| 234 | 7 | delim = /\s+/; |
| 235 | } | |
| 236 | 62 | if (attr_match[_OPERATOR] === '|=') { |
| 237 | 6 | clauses.push(this.factory.by_attr_value_pipe_equals(this._to_string_or_regex(attr_match[_ATTR_NAME]), this._to_string_or_regex((_ref2 = attr_match[_DEQUOTED_ATTR_VALUE]) != null ? _ref2 : attr_match[_NEVERQUOTED_ATTR_VALUE]))); |
| 238 | 56 | } else if (attr_match[_OPERATOR] === '^=') { |
| 239 | 7 | aval = this._to_string_or_regex((_ref3 = attr_match[_DEQUOTED_ATTR_VALUE]) != null ? _ref3 : attr_match[_NEVERQUOTED_ATTR_VALUE]); |
| 240 | 7 | if (typeof aval === 'string') { |
| 241 | 2 | regexp_source = this.factory._escape_for_regexp(aval); |
| 242 | 2 | aval = new RegExp("^" + regexp_source); |
| 243 | } else { | |
| 244 | 5 | regexp_source = aval.source; |
| 245 | 5 | modifier = ''; |
| 246 | 5 | if (aval.ignoreCase) { |
| 247 | 0 | modifier += 'i'; |
| 248 | } | |
| 249 | 5 | if (aval.global) { |
| 250 | 0 | modifier += 'g'; |
| 251 | } | |
| 252 | 5 | if (aval.multiline) { |
| 253 | 0 | modifier += 'm'; |
| 254 | } | |
| 255 | 5 | if (!/^\^/.test(regexp_source)) { |
| 256 | 4 | aval = new RegExp("^" + regexp_source); |
| 257 | } | |
| 258 | } | |
| 259 | 7 | clauses.push(this.factory.by_attr_value_predicate(this._to_string_or_regex(attr_match[_ATTR_NAME]), aval)); |
| 260 | 49 | } else if (attr_match[_OPERATOR] === '$=') { |
| 261 | 12 | aval = this._to_string_or_regex((_ref4 = attr_match[_DEQUOTED_ATTR_VALUE]) != null ? _ref4 : attr_match[_NEVERQUOTED_ATTR_VALUE]); |
| 262 | 12 | if (typeof aval === 'string') { |
| 263 | 4 | regexp_source = this.factory._escape_for_regexp(aval); |
| 264 | 4 | aval = new RegExp("" + regexp_source + "$"); |
| 265 | } else { | |
| 266 | 8 | regexp_source = aval.source; |
| 267 | 8 | modifier = ''; |
| 268 | 8 | if (aval.ignoreCase) { |
| 269 | 0 | modifier += 'i'; |
| 270 | } | |
| 271 | 8 | if (aval.global) { |
| 272 | 0 | modifier += 'g'; |
| 273 | } | |
| 274 | 8 | if (aval.multiline) { |
| 275 | 0 | modifier += 'm'; |
| 276 | } | |
| 277 | 8 | if (!/\$$/.test(regexp_source)) { |
| 278 | 6 | aval = new RegExp("" + regexp_source + "$"); |
| 279 | } | |
| 280 | } | |
| 281 | 12 | clauses.push(this.factory.by_attr_value_predicate(this._to_string_or_regex(attr_match[_ATTR_NAME]), aval)); |
| 282 | 37 | } else if (attr_match[_OPERATOR] === '*=') { |
| 283 | 4 | aval = this._to_string_or_regex((_ref5 = attr_match[_DEQUOTED_ATTR_VALUE]) != null ? _ref5 : attr_match[_NEVERQUOTED_ATTR_VALUE]); |
| 284 | 4 | if (typeof aval === 'string') { |
| 285 | 2 | regexp_source = this.factory._escape_for_regexp(aval); |
| 286 | 2 | aval = new RegExp(regexp_source); |
| 287 | } | |
| 288 | 4 | clauses.push(this.factory.by_attr_value_predicate(this._to_string_or_regex(attr_match[_ATTR_NAME]), aval)); |
| 289 | } else { | |
| 290 | 33 | clauses.push(this.factory.by_attr_value_predicate(this._to_string_or_regex(attr_match[_ATTR_NAME]), this._to_string_or_regex((_ref6 = attr_match[_DEQUOTED_ATTR_VALUE]) != null ? _ref6 : attr_match[_NEVERQUOTED_ATTR_VALUE]), delim)); |
| 291 | } | |
| 292 | } | |
| 293 | 66 | attr_match = this._ATTRIBUTE_CLAUSE_REGEXP.exec(match[_ATTRIBUTES]); |
| 294 | } | |
| 295 | } | |
| 296 | 166 | if (match[_PSEUDO_CLASS] != null) { |
| 297 | 3 | if (match[_PSEUDO_CLASS] === 'first-child') { |
| 298 | 3 | clauses.push(this.factory.first_child_predicate()); |
| 299 | } | |
| 300 | } | |
| 301 | 166 | if (clauses.length > 0) { |
| 302 | 166 | clauses = this.factory.and_predicate(clauses); |
| 303 | } | |
| 304 | 166 | return clauses; |
| 305 | }; | |
| 306 | ||
| 307 | 1 | Stew.prototype._to_string_or_regex = function(str) { |
| 308 | 294 | var match; |
| 309 | 294 | match = str.match(/^\/(.*)\/([gmi]*)$/); |
| 310 | 294 | if ((match != null ? match[1] : void 0) != null) { |
| 311 | 56 | return new RegExp(match[1], match[2]); |
| 312 | } else { | |
| 313 | 238 | return str; |
| 314 | } | |
| 315 | }; | |
| 316 | ||
| 317 | 1 | return Stew; |
| 318 | ||
| 319 | })(); | |
| 320 | ||
| 321 | 1 | exports = exports != null ? exports : this; |
| 322 | ||
| 323 | 1 | exports.Stew = Stew; |
| 324 | ||
| 325 | 1 | exports.DOMUtil = DOMUtil; |
| 326 | ||
| 327 | }).call(this); |