stew-select
Version:
CSS selectors that allow regular expressions. Stew is a meatier soup.
273 lines (248 loc) • 9.06 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var PredicateFactory, exports,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
PredicateFactory = (function() {
function PredicateFactory() {
this.by_attr_value_pipe_equals = __bind(this.by_attr_value_pipe_equals, this);
this.by_attr_value_predicate = __bind(this.by_attr_value_predicate, this);
this.by_attr_exists_predicate = __bind(this.by_attr_exists_predicate, this);
this.by_id_predicate = __bind(this.by_id_predicate, this);
this.by_class_predicate = __bind(this.by_class_predicate, this);
}
PredicateFactory.prototype.and_predicate = function(predicates) {
return function(node, node_metadata, dom_metadata) {
var predicate, _i, _len;
for (_i = 0, _len = predicates.length; _i < _len; _i++) {
predicate = predicates[_i];
if (!predicate(node, node_metadata, dom_metadata)) {
return false;
}
}
return true;
};
};
PredicateFactory.prototype.or_predicate = function(predicates) {
return function(node, node_metadata, dom_metadata) {
var predicate, _i, _len;
for (_i = 0, _len = predicates.length; _i < _len; _i++) {
predicate = predicates[_i];
if (predicate(node, node_metadata, dom_metadata)) {
return true;
}
}
return false;
};
};
PredicateFactory.prototype.by_attribute_predicate = function(attrname, attrvalue, valuedelim) {
var np, vp;
if (attrvalue == null) {
attrvalue = null;
}
if (valuedelim == null) {
valuedelim = null;
}
if (typeof attrname === 'string') {
np = function(str) {
return str === attrname;
};
} else {
np = function(str) {
return attrname.test(str);
};
}
if (attrvalue === null) {
vp = null;
} else if (typeof attrvalue === 'string') {
attrvalue = attrvalue.replace(/\\\"/g, '"');
vp = function(str) {
return str === attrvalue;
};
} else if ((attrvalue != null ? attrvalue.test : void 0) != null) {
vp = function(str) {
return attrvalue.test(str);
};
}
return function(node) {
var name, token, value, _i, _len, _ref, _ref1;
_ref = node != null ? node.attribs : void 0;
for (name in _ref) {
value = _ref[name];
if (np(name)) {
if (vp === null) {
return true;
} else {
if (valuedelim != null) {
if (value != null) {
_ref1 = value.split(valuedelim);
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
token = _ref1[_i];
if (vp(token)) {
return true;
}
}
}
} else {
if (vp(value)) {
return true;
}
}
}
}
}
return false;
};
};
PredicateFactory.prototype.by_class_predicate = function(klass) {
return this.by_attribute_predicate('class', klass, /\s+/);
};
PredicateFactory.prototype.by_id_predicate = function(id) {
return this.by_attribute_predicate('id', id);
};
PredicateFactory.prototype.by_attr_exists_predicate = function(attrname) {
return this.by_attribute_predicate(attrname, null);
};
PredicateFactory.prototype.by_attr_value_predicate = function(attrname, attrvalue, valuedelim) {
return this.by_attribute_predicate(attrname, attrvalue, valuedelim);
};
PredicateFactory.prototype._escape_for_regexp = function(str) {
return str.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
};
PredicateFactory.prototype.by_attr_value_pipe_equals = function(attrname, attrvalue) {
var modifier, regexp_source;
if (typeof attrvalue === 'string') {
regexp_source = this._escape_for_regexp(attrvalue);
attrvalue = new RegExp("^" + regexp_source + "($|-)");
} else {
regexp_source = attrvalue.source;
modifier = '';
if (attrvalue.ignoreCase) {
modifier += 'i';
}
if (attrvalue.global) {
modifier += 'g';
}
if (attrvalue.multiline) {
modifier += 'm';
}
if (!/^\^/.test(attrvalue.source)) {
regexp_source = "^" + regexp_source;
}
if (!/\(\$\|-\)$/.test(regexp_source)) {
regexp_source = "" + regexp_source + "($|-)";
}
attrvalue = new RegExp(regexp_source, modifier);
}
return this.by_attribute_predicate(attrname, attrvalue);
};
PredicateFactory.prototype.by_tag_predicate = function(name) {
if (typeof name === 'string') {
return function(node) {
return name === node.name;
};
} else {
return function(node) {
return name.test(node.name);
};
}
};
PredicateFactory.prototype.first_child_predicate = function() {
return this._first_child_impl;
};
PredicateFactory.prototype._first_child_impl = function(node, node_metadata, dom_metadata) {
var elt, _i, _len, _ref;
if (node.type === 'tag' && (node_metadata.siblings != null)) {
_ref = node_metadata.siblings;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
elt = _ref[_i];
if (elt.type === 'tag') {
return node._stew_node_id === elt._stew_node_id;
}
}
}
return false;
};
PredicateFactory.prototype.any_tag_predicate = function() {
return this._any_tag_impl;
};
PredicateFactory.prototype._any_tag_impl = function(node) {
return (node != null ? node.type : void 0) === 'tag';
};
PredicateFactory.prototype.descendant_predicate = function(predicates) {
if (predicates.length === 1) {
return predicates[0];
} else {
return function(node, node_metadata, dom_metadata) {
var cloned_path, cloned_predicates;
if (predicates[predicates.length - 1](node, node_metadata, dom_metadata)) {
cloned_path = [].concat(node_metadata.path);
cloned_predicates = [].concat(predicates);
cloned_predicates.pop();
while (cloned_path.length > 0) {
node = cloned_path.pop();
node_metadata = dom_metadata[node._stew_node_id];
if (cloned_predicates[cloned_predicates.length - 1](node, node_metadata, dom_metadata)) {
cloned_predicates.pop();
if (cloned_predicates.length === 0) {
return true;
break;
}
}
}
}
return false;
};
}
};
PredicateFactory.prototype.direct_descendant_predicate = function(parent_selector, child_selector) {
return function(node, node_metadata, dom_metadata) {
var parent, parent_metadata;
if (child_selector(node, node_metadata, dom_metadata)) {
parent = node_metadata.parent;
parent_metadata = dom_metadata[parent._stew_node_id];
return parent_selector(parent, parent_metadata, dom_metadata);
}
return false;
};
};
PredicateFactory.prototype.adjacent_sibling_predicate = function(first, second) {
return function(node, node_metadata, dom_metadata) {
var prev_tag, prev_tag_index;
if (second(node, node_metadata, dom_metadata)) {
prev_tag_index = node_metadata.sib_index - 1;
while (prev_tag_index > 0) {
if (node_metadata.siblings[prev_tag_index].type === 'tag') {
prev_tag = node_metadata.siblings[prev_tag_index];
return first(prev_tag, dom_metadata[prev_tag._stew_node_id], dom_metadata);
} else {
prev_tag_index -= 1;
}
}
}
return false;
};
};
PredicateFactory.prototype.preceding_sibling_predicate = function(first, second) {
return function(node, node_metadata, dom_metadata) {
var index, prev, _i, _len, _ref;
if (second(node, node_metadata, dom_metadata)) {
_ref = node_metadata.siblings;
for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) {
prev = _ref[index];
if (index === node_metadata.sib_index) {
return false;
} else if (prev.type === 'tag') {
if (first(prev, dom_metadata[prev._stew_node_id], dom_metadata)) {
return true;
}
}
}
}
return false;
};
};
return PredicateFactory;
})();
exports = exports != null ? exports : this;
exports.PredicateFactory = PredicateFactory;
}).call(this);