diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-05-03 15:35:00 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-05-03 15:35:00 +0200 |
commit | de98e0b232509d5f40c135d540a70e415272ff85 (patch) | |
tree | a79222a5b58484ab3b80d18efcaaa7ccc4769b33 /node_modules/css-select | |
parent | e0c9d480a73fa629c1e4a47d3e721f1d2d345406 (diff) | |
download | wallet-core-de98e0b232509d5f40c135d540a70e415272ff85.tar.xz |
node_modules
Diffstat (limited to 'node_modules/css-select')
26 files changed, 1968 insertions, 0 deletions
diff --git a/node_modules/css-select/LICENSE b/node_modules/css-select/LICENSE new file mode 100644 index 000000000..c464f863e --- /dev/null +++ b/node_modules/css-select/LICENSE @@ -0,0 +1,11 @@ +Copyright (c) Felix Böhm +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, +EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/node_modules/css-select/README.md b/node_modules/css-select/README.md new file mode 100644 index 000000000..e36282fe2 --- /dev/null +++ b/node_modules/css-select/README.md @@ -0,0 +1,133 @@ +# css-select [![NPM version](http://img.shields.io/npm/v/css-select.svg)](https://npmjs.org/package/css-select) [![Build Status](https://travis-ci.org/fb55/css-select.svg?branch=master)](http://travis-ci.org/fb55/css-select) [![Downloads](https://img.shields.io/npm/dm/css-select.svg)](https://npmjs.org/package/css-select) [![Coverage](https://coveralls.io/repos/fb55/css-select/badge.svg?branch=master)](https://coveralls.io/r/fb55/css-select) + +a CSS selector compiler/engine + +## What? + +css-select turns CSS selectors into functions that tests if elements match them. When searching for elements, testing is executed "from the top", similar to how browsers execute CSS selectors. + +In its default configuration, css-select queries the DOM structure of the [`domhandler`](https://github.com/fb55/domhandler) module (also known as htmlparser2 DOM). + +__Features:__ + +- Full implementation of CSS3 selectors +- Partial implementation of jQuery/Sizzle extensions +- Very high test coverage +- Pretty good performance + +## Why? + +The traditional approach of executing CSS selectors, named left-to-right execution, is to execute every component of the selector in order, from left to right _(duh)_. The execution of the selector `a b` for example will first query for `a` elements, then search these for `b` elements. (That's the approach of eg. [`Sizzle`](https://github.com/jquery/sizzle), [`nwmatcher`](https://github.com/dperini/nwmatcher/) and [`qwery`](https://github.com/ded/qwery).) + +While this works, it has some downsides: Children of `a`s will be checked multiple times; first, to check if they are also `a`s, then, for every superior `a` once, if they are `b`s. Using [Big O notation](http://en.wikipedia.org/wiki/Big_O_notation), that would be `O(n^(k+1))`, where `k` is the number of descendant selectors (that's the space in the example above). + +The far more efficient approach is to first look for `b` elements, then check if they have superior `a` elements: Using big O notation again, that would be `O(n)`. That's called right-to-left execution. + +And that's what css-select does – and why it's quite performant. + +## How does it work? + +By building a stack of functions. + +_Wait, what?_ + +Okay, so let's suppose we want to compile the selector `a b` again, for right-to-left execution. We start by _parsing_ the selector, which means we turn the selector into an array of the building-blocks of the selector, so we can distinguish them easily. That's what the [`css-what`](https://github.com/fb55/css-what) module is for, if you want to have a look. + +Anyway, after parsing, we end up with an array like this one: + +```js +[ + { type: 'tag', name: 'a' }, + { type: 'descendant' }, + { type: 'tag', name: 'b' } +] +``` + +Actually, this array is wrapped in another array, but that's another story (involving commas in selectors). + +Now that we know the meaning of every part of the selector, we can compile it. That's where it becomes interesting. + +The basic idea is to turn every part of the selector into a function, which takes an element as its only argument. The function checks whether a passed element matches its part of the selector: If it does, the element is passed to the next turned-into-a-function part of the selector, which does the same. If an element is accepted by all parts of the selector, it _matches_ the selector and double rainbow ALL THE WAY. + +As said before, we want to do right-to-left execution with all the big O improvements nonsense, so elements are passed from the rightmost part of the selector (`b` in our example) to the leftmost (~~which would be `c`~~ of course `a`). + +_//TODO: More in-depth description. Implementation details. Build a spaceship._ + +## API + +```js +var CSSselect = require("css-select"); +``` + +#### `CSSselect(query, elems, options)` + +Queries `elems`, returns an array containing all matches. + +- `query` can be either a CSS selector or a function. +- `elems` can be either an array of elements, or a single element. If it is an element, its children will be queried. +- `options` is described below. + +Aliases: `CSSselect.selectAll(query, elems)`, `CSSselect.iterate(query, elems)`. + +#### `CSSselect.compile(query)` + +Compiles the query, returns a function. + +#### `CSSselect.is(elem, query, options)` + +Tests whether or not an element is matched by `query`. `query` can be either a CSS selector or a function. + +#### `CSSselect.selectOne(query, elems, options)` + +Arguments are the same as for `CSSselect(query, elems)`. Only returns the first match, or `null` if there was no match. + +### Options + +- `xmlMode`: When enabled, tag names will be case-sensitive. Default: `false`. +- `strict`: Limits the module to only use CSS3 selectors. Default: `false`. +- `rootFunc`: The last function in the stack, will be called with the last element that's looked at. Should return `true`. + +## Supported selectors + +_As defined by CSS 4 and / or jQuery._ + +* Universal (`*`) +* Tag (`<tagname>`) +* Descendant (` `) +* Child (`>`) +* Parent (`<`) * +* Sibling (`+`) +* Adjacent (`~`) +* Attribute (`[attr=foo]`), with supported comparisons: + * `[attr]` (existential) + * `=` + * `~=` + * `|=` + * `*=` + * `^=` + * `$=` + * `!=` * + * Also, `i` can be added after the comparison to make the comparison case-insensitive (eg. `[attr=foo i]`) * +* Pseudos: + * `:not` + * `:contains` * + * `:icontains` * (case-insensitive version of `:contains`) + * `:has` * + * `:root` + * `:empty` + * `:parent` * + * `:[first|last]-child[-of-type]` + * `:only-of-type`, `:only-child` + * `:nth-[last-]child[-of-type]` + * `:link`, `:visited` (the latter doesn't match any elements) + * `:selected` *, `:checked` + * `:enabled`, `:disabled` + * `:required`, `:optional` + * `:header`, `:button`, `:input`, `:text`, `:checkbox`, `:file`, `:password`, `:reset`, `:radio` etc. * + * `:matches` * + +__*__: Not part of CSS3 + +--- + +License: BSD-like diff --git a/node_modules/css-select/index.js b/node_modules/css-select/index.js new file mode 100644 index 000000000..4179d196b --- /dev/null +++ b/node_modules/css-select/index.js @@ -0,0 +1,59 @@ +"use strict"; + +module.exports = CSSselect; + +var Pseudos = require("./lib/pseudos.js"), + DomUtils = require("domutils"), + findOne = DomUtils.findOne, + findAll = DomUtils.findAll, + getChildren = DomUtils.getChildren, + removeSubsets = DomUtils.removeSubsets, + falseFunc = require("boolbase").falseFunc, + compile = require("./lib/compile.js"), + compileUnsafe = compile.compileUnsafe, + compileToken = compile.compileToken; + +function getSelectorFunc(searchFunc){ + return function select(query, elems, options){ + if(typeof query !== "function") query = compileUnsafe(query, options, elems); + if(!Array.isArray(elems)) elems = getChildren(elems); + else elems = removeSubsets(elems); + return searchFunc(query, elems); + }; +} + +var selectAll = getSelectorFunc(function selectAll(query, elems){ + return (query === falseFunc || !elems || elems.length === 0) ? [] : findAll(query, elems); +}); + +var selectOne = getSelectorFunc(function selectOne(query, elems){ + return (query === falseFunc || !elems || elems.length === 0) ? null : findOne(query, elems); +}); + +function is(elem, query, options){ + return (typeof query === "function" ? query : compile(query, options))(elem); +} + +/* + the exported interface +*/ +function CSSselect(query, elems, options){ + return selectAll(query, elems, options); +} + +CSSselect.compile = compile; +CSSselect.filters = Pseudos.filters; +CSSselect.pseudos = Pseudos.pseudos; + +CSSselect.selectAll = selectAll; +CSSselect.selectOne = selectOne; + +CSSselect.is = is; + +//legacy methods (might be removed) +CSSselect.parse = compile; +CSSselect.iterate = selectAll; + +//hooks +CSSselect._compileUnsafe = compileUnsafe; +CSSselect._compileToken = compileToken; diff --git a/node_modules/css-select/lib/attributes.js b/node_modules/css-select/lib/attributes.js new file mode 100644 index 000000000..a8689c01c --- /dev/null +++ b/node_modules/css-select/lib/attributes.js @@ -0,0 +1,181 @@ +var DomUtils = require("domutils"), + hasAttrib = DomUtils.hasAttrib, + getAttributeValue = DomUtils.getAttributeValue, + falseFunc = require("boolbase").falseFunc; + +//https://github.com/slevithan/XRegExp/blob/master/src/xregexp.js#L469 +var reChars = /[-[\]{}()*+?.,\\^$|#\s]/g; + +/* + attribute selectors +*/ + +var attributeRules = { + __proto__: null, + equals: function(next, data){ + var name = data.name, + value = data.value; + + if(data.ignoreCase){ + value = value.toLowerCase(); + + return function equalsIC(elem){ + var attr = getAttributeValue(elem, name); + return attr != null && attr.toLowerCase() === value && next(elem); + }; + } + + return function equals(elem){ + return getAttributeValue(elem, name) === value && next(elem); + }; + }, + hyphen: function(next, data){ + var name = data.name, + value = data.value, + len = value.length; + + if(data.ignoreCase){ + value = value.toLowerCase(); + + return function hyphenIC(elem){ + var attr = getAttributeValue(elem, name); + return attr != null && + (attr.length === len || attr.charAt(len) === "-") && + attr.substr(0, len).toLowerCase() === value && + next(elem); + }; + } + + return function hyphen(elem){ + var attr = getAttributeValue(elem, name); + return attr != null && + attr.substr(0, len) === value && + (attr.length === len || attr.charAt(len) === "-") && + next(elem); + }; + }, + element: function(next, data){ + var name = data.name, + value = data.value; + + if(/\s/.test(value)){ + return falseFunc; + } + + value = value.replace(reChars, "\\$&"); + + var pattern = "(?:^|\\s)" + value + "(?:$|\\s)", + flags = data.ignoreCase ? "i" : "", + regex = new RegExp(pattern, flags); + + return function element(elem){ + var attr = getAttributeValue(elem, name); + return attr != null && regex.test(attr) && next(elem); + }; + }, + exists: function(next, data){ + var name = data.name; + return function exists(elem){ + return hasAttrib(elem, name) && next(elem); + }; + }, + start: function(next, data){ + var name = data.name, + value = data.value, + len = value.length; + + if(len === 0){ + return falseFunc; + } + + if(data.ignoreCase){ + value = value.toLowerCase(); + + return function startIC(elem){ + var attr = getAttributeValue(elem, name); + return attr != null && attr.substr(0, len).toLowerCase() === value && next(elem); + }; + } + + return function start(elem){ + var attr = getAttributeValue(elem, name); + return attr != null && attr.substr(0, len) === value && next(elem); + }; + }, + end: function(next, data){ + var name = data.name, + value = data.value, + len = -value.length; + + if(len === 0){ + return falseFunc; + } + + if(data.ignoreCase){ + value = value.toLowerCase(); + + return function endIC(elem){ + var attr = getAttributeValue(elem, name); + return attr != null && attr.substr(len).toLowerCase() === value && next(elem); + }; + } + + return function end(elem){ + var attr = getAttributeValue(elem, name); + return attr != null && attr.substr(len) === value && next(elem); + }; + }, + any: function(next, data){ + var name = data.name, + value = data.value; + + if(value === ""){ + return falseFunc; + } + + if(data.ignoreCase){ + var regex = new RegExp(value.replace(reChars, "\\$&"), "i"); + + return function anyIC(elem){ + var attr = getAttributeValue(elem, name); + return attr != null && regex.test(attr) && next(elem); + }; + } + + return function any(elem){ + var attr = getAttributeValue(elem, name); + return attr != null && attr.indexOf(value) >= 0 && next(elem); + }; + }, + not: function(next, data){ + var name = data.name, + value = data.value; + + if(value === ""){ + return function notEmpty(elem){ + return !!getAttributeValue(elem, name) && next(elem); + }; + } else if(data.ignoreCase){ + value = value.toLowerCase(); + + return function notIC(elem){ + var attr = getAttributeValue(elem, name); + return attr != null && attr.toLowerCase() !== value && next(elem); + }; + } + + return function not(elem){ + return getAttributeValue(elem, name) !== value && next(elem); + }; + } +}; + +module.exports = { + compile: function(next, data, options){ + if(options && options.strict && ( + data.ignoreCase || data.action === "not" + )) throw SyntaxError("Unsupported attribute selector"); + return attributeRules[data.action](next, data); + }, + rules: attributeRules +}; diff --git a/node_modules/css-select/lib/compile.js b/node_modules/css-select/lib/compile.js new file mode 100644 index 000000000..91ac5925e --- /dev/null +++ b/node_modules/css-select/lib/compile.js @@ -0,0 +1,192 @@ +/* + compiles a selector to an executable function +*/ + +module.exports = compile; +module.exports.compileUnsafe = compileUnsafe; +module.exports.compileToken = compileToken; + +var parse = require("css-what"), + DomUtils = require("domutils"), + isTag = DomUtils.isTag, + Rules = require("./general.js"), + sortRules = require("./sort.js"), + BaseFuncs = require("boolbase"), + trueFunc = BaseFuncs.trueFunc, + falseFunc = BaseFuncs.falseFunc, + procedure = require("./procedure.json"); + +function compile(selector, options, context){ + var next = compileUnsafe(selector, options, context); + return wrap(next); +} + +function wrap(next){ + return function base(elem){ + return isTag(elem) && next(elem); + }; +} + +function compileUnsafe(selector, options, context){ + var token = parse(selector, options); + return compileToken(token, options, context); +} + +function includesScopePseudo(t){ + return t.type === "pseudo" && ( + t.name === "scope" || ( + Array.isArray(t.data) && + t.data.some(function(data){ + return data.some(includesScopePseudo); + }) + ) + ); +} + +var DESCENDANT_TOKEN = {type: "descendant"}, + SCOPE_TOKEN = {type: "pseudo", name: "scope"}, + PLACEHOLDER_ELEMENT = {}, + getParent = DomUtils.getParent; + +//CSS 4 Spec (Draft): 3.3.1. Absolutizing a Scope-relative Selector +//http://www.w3.org/TR/selectors4/#absolutizing +function absolutize(token, context){ + //TODO better check if context is document + var hasContext = !!context && !!context.length && context.every(function(e){ + return e === PLACEHOLDER_ELEMENT || !!getParent(e); + }); + + + token.forEach(function(t){ + if(t.length > 0 && isTraversal(t[0]) && t[0].type !== "descendant"){ + //don't return in else branch + } else if(hasContext && !includesScopePseudo(t)){ + t.unshift(DESCENDANT_TOKEN); + } else { + return; + } + + t.unshift(SCOPE_TOKEN); + }); +} + +function compileToken(token, options, context){ + token = token.filter(function(t){ return t.length > 0; }); + + token.forEach(sortRules); + + var isArrayContext = Array.isArray(context); + + context = (options && options.context) || context; + + if(context && !isArrayContext) context = [context]; + + absolutize(token, context); + + return token + .map(function(rules){ return compileRules(rules, options, context, isArrayContext); }) + .reduce(reduceRules, falseFunc); +} + +function isTraversal(t){ + return procedure[t.type] < 0; +} + +function compileRules(rules, options, context, isArrayContext){ + var acceptSelf = (isArrayContext && rules[0].name === "scope" && rules[1].type === "descendant"); + return rules.reduce(function(func, rule, index){ + if(func === falseFunc) return func; + return Rules[rule.type](func, rule, options, context, acceptSelf && index === 1); + }, options && options.rootFunc || trueFunc); +} + +function reduceRules(a, b){ + if(b === falseFunc || a === trueFunc){ + return a; + } + if(a === falseFunc || b === trueFunc){ + return b; + } + + return function combine(elem){ + return a(elem) || b(elem); + }; +} + +//:not, :has and :matches have to compile selectors +//doing this in lib/pseudos.js would lead to circular dependencies, +//so we add them here + +var Pseudos = require("./pseudos.js"), + filters = Pseudos.filters, + existsOne = DomUtils.existsOne, + isTag = DomUtils.isTag, + getChildren = DomUtils.getChildren; + + +function containsTraversal(t){ + return t.some(isTraversal); +} + +filters.not = function(next, token, options, context){ + var opts = { + xmlMode: !!(options && options.xmlMode), + strict: !!(options && options.strict) + }; + + if(opts.strict){ + if(token.length > 1 || token.some(containsTraversal)){ + throw new SyntaxError("complex selectors in :not aren't allowed in strict mode"); + } + } + + var func = compileToken(token, opts, context); + + if(func === falseFunc) return next; + if(func === trueFunc) return falseFunc; + + return function(elem){ + return !func(elem) && next(elem); + }; +}; + +filters.has = function(next, token, options){ + var opts = { + xmlMode: !!(options && options.xmlMode), + strict: !!(options && options.strict) + }; + + //FIXME: Uses an array as a pointer to the current element (side effects) + var context = token.some(containsTraversal) ? [PLACEHOLDER_ELEMENT] : null; + + var func = compileToken(token, opts, context); + + if(func === falseFunc) return falseFunc; + if(func === trueFunc) return function(elem){ + return getChildren(elem).some(isTag) && next(elem); + }; + + func = wrap(func); + + if(context){ + return function has(elem){ + return next(elem) && ( + (context[0] = elem), existsOne(func, getChildren(elem)) + ); + }; + } + + return function has(elem){ + return next(elem) && existsOne(func, getChildren(elem)); + }; +}; + +filters.matches = function(next, token, options, context){ + var opts = { + xmlMode: !!(options && options.xmlMode), + strict: !!(options && options.strict), + rootFunc: next + }; + + return compileToken(token, opts, context); +}; diff --git a/node_modules/css-select/lib/general.js b/node_modules/css-select/lib/general.js new file mode 100644 index 000000000..fbc960fe9 --- /dev/null +++ b/node_modules/css-select/lib/general.js @@ -0,0 +1,89 @@ +var DomUtils = require("domutils"), + isTag = DomUtils.isTag, + getParent = DomUtils.getParent, + getChildren = DomUtils.getChildren, + getSiblings = DomUtils.getSiblings, + getName = DomUtils.getName; + +/* + all available rules +*/ +module.exports = { + __proto__: null, + + attribute: require("./attributes.js").compile, + pseudo: require("./pseudos.js").compile, + + //tags + tag: function(next, data){ + var name = data.name; + return function tag(elem){ + return getName(elem) === name && next(elem); + }; + }, + + //traversal + descendant: function(next, rule, options, context, acceptSelf){ + return function descendant(elem){ + + if (acceptSelf && next(elem)) return true; + + var found = false; + + while(!found && (elem = getParent(elem))){ + found = next(elem); + } + + return found; + }; + }, + parent: function(next, data, options){ + if(options && options.strict) throw SyntaxError("Parent selector isn't part of CSS3"); + + return function parent(elem){ + return getChildren(elem).some(test); + }; + + function test(elem){ + return isTag(elem) && next(elem); + } + }, + child: function(next){ + return function child(elem){ + var parent = getParent(elem); + return !!parent && next(parent); + }; + }, + sibling: function(next){ + return function sibling(elem){ + var siblings = getSiblings(elem); + + for(var i = 0; i < siblings.length; i++){ + if(isTag(siblings[i])){ + if(siblings[i] === elem) break; + if(next(siblings[i])) return true; + } + } + + return false; + }; + }, + adjacent: function(next){ + return function adjacent(elem){ + var siblings = getSiblings(elem), + lastElement; + + for(var i = 0; i < siblings.length; i++){ + if(isTag(siblings[i])){ + if(siblings[i] === elem) break; + lastElement = siblings[i]; + } + } + + return !!lastElement && next(lastElement); + }; + }, + universal: function(next){ + return next; + } +};
\ No newline at end of file diff --git a/node_modules/css-select/lib/procedure.json b/node_modules/css-select/lib/procedure.json new file mode 100644 index 000000000..e836de117 --- /dev/null +++ b/node_modules/css-select/lib/procedure.json @@ -0,0 +1,11 @@ +{ + "universal": 50, + "tag": 30, + "attribute": 1, + "pseudo": 0, + "descendant": -1, + "child": -1, + "parent": -1, + "sibling": -1, + "adjacent": -1 +} diff --git a/node_modules/css-select/lib/pseudos.js b/node_modules/css-select/lib/pseudos.js new file mode 100644 index 000000000..f6774ecfc --- /dev/null +++ b/node_modules/css-select/lib/pseudos.js @@ -0,0 +1,393 @@ +/* + pseudo selectors + + --- + + they are available in two forms: + * filters called when the selector + is compiled and return a function + that needs to return next() + * pseudos get called on execution + they need to return a boolean +*/ + +var DomUtils = require("domutils"), + isTag = DomUtils.isTag, + getText = DomUtils.getText, + getParent = DomUtils.getParent, + getChildren = DomUtils.getChildren, + getSiblings = DomUtils.getSiblings, + hasAttrib = DomUtils.hasAttrib, + getName = DomUtils.getName, + getAttribute= DomUtils.getAttributeValue, + getNCheck = require("nth-check"), + checkAttrib = require("./attributes.js").rules.equals, + BaseFuncs = require("boolbase"), + trueFunc = BaseFuncs.trueFunc, + falseFunc = BaseFuncs.falseFunc; + +//helper methods +function getFirstElement(elems){ + for(var i = 0; elems && i < elems.length; i++){ + if(isTag(elems[i])) return elems[i]; + } +} + +function getAttribFunc(name, value){ + var data = {name: name, value: value}; + return function attribFunc(next){ + return checkAttrib(next, data); + }; +} + +function getChildFunc(next){ + return function(elem){ + return !!getParent(elem) && next(elem); + }; +} + +var filters = { + contains: function(next, text){ + return function contains(elem){ + return next(elem) && getText(elem).indexOf(text) >= 0; + }; + }, + icontains: function(next, text){ + var itext = text.toLowerCase(); + return function icontains(elem){ + return next(elem) && + getText(elem).toLowerCase().indexOf(itext) >= 0; + }; + }, + + //location specific methods + "nth-child": function(next, rule){ + var func = getNCheck(rule); + + if(func === falseFunc) return func; + if(func === trueFunc) return getChildFunc(next); + + return function nthChild(elem){ + var siblings = getSiblings(elem); + + for(var i = 0, pos = 0; i < siblings.length; i++){ + if(isTag(siblings[i])){ + if(siblings[i] === elem) break; + else pos++; + } + } + + return func(pos) && next(elem); + }; + }, + "nth-last-child": function(next, rule){ + var func = getNCheck(rule); + + if(func === falseFunc) return func; + if(func === trueFunc) return getChildFunc(next); + + return function nthLastChild(elem){ + var siblings = getSiblings(elem); + + for(var pos = 0, i = siblings.length - 1; i >= 0; i--){ + if(isTag(siblings[i])){ + if(siblings[i] === elem) break; + else pos++; + } + } + + return func(pos) && next(elem); + }; + }, + "nth-of-type": function(next, rule){ + var func = getNCheck(rule); + + if(func === falseFunc) return func; + if(func === trueFunc) return getChildFunc(next); + + return function nthOfType(elem){ + var siblings = getSiblings(elem); + + for(var pos = 0, i = 0; i < siblings.length; i++){ + if(isTag(siblings[i])){ + if(siblings[i] === elem) break; + if(getName(siblings[i]) === getName(elem)) pos++; + } + } + + return func(pos) && next(elem); + }; + }, + "nth-last-of-type": function(next, rule){ + var func = getNCheck(rule); + + if(func === falseFunc) return func; + if(func === trueFunc) return getChildFunc(next); + + return function nthLastOfType(elem){ + var siblings = getSiblings(elem); + + for(var pos = 0, i = siblings.length - 1; i >= 0; i--){ + if(isTag(siblings[i])){ + if(siblings[i] === elem) break; + if(getName(siblings[i]) === getName(elem)) pos++; + } + } + + return func(pos) && next(elem); + }; + }, + + //TODO determine the actual root element + root: function(next){ + return function(elem){ + return !getParent(elem) && next(elem); + }; + }, + + scope: function(next, rule, options, context){ + if(!context || context.length === 0){ + //equivalent to :root + return filters.root(next); + } + + if(context.length === 1){ + //NOTE: can't be unpacked, as :has uses this for side-effects + return function(elem){ + return context[0] === elem && next(elem); + }; + } + + return function(elem){ + return context.indexOf(elem) >= 0 && next(elem); + }; + }, + + //jQuery extensions (others follow as pseudos) + checkbox: getAttribFunc("type", "checkbox"), + file: getAttribFunc("type", "file"), + password: getAttribFunc("type", "password"), + radio: getAttribFunc("type", "radio"), + reset: getAttribFunc("type", "reset"), + image: getAttribFunc("type", "image"), + submit: getAttribFunc("type", "submit") +}; + +//while filters are precompiled, pseudos get called when they are needed +var pseudos = { + empty: function(elem){ + return !getChildren(elem).some(function(elem){ + return isTag(elem) || elem.type === "text"; + }); + }, + + "first-child": function(elem){ + return getFirstElement(getSiblings(elem)) === elem; + }, + "last-child": function(elem){ + var siblings = getSiblings(elem); + + for(var i = siblings.length - 1; i >= 0; i--){ + if(siblings[i] === elem) return true; + if(isTag(siblings[i])) break; + } + + return false; + }, + "first-of-type": function(elem){ + var siblings = getSiblings(elem); + + for(var i = 0; i < siblings.length; i++){ + if(isTag(siblings[i])){ + if(siblings[i] === elem) return true; + if(getName(siblings[i]) === getName(elem)) break; + } + } + + return false; + }, + "last-of-type": function(elem){ + var siblings = getSiblings(elem); + + for(var i = siblings.length-1; i >= 0; i--){ + if(isTag(siblings[i])){ + if(siblings[i] === elem) return true; + if(getName(siblings[i]) === getName(elem)) break; + } + } + + return false; + }, + "only-of-type": function(elem){ + var siblings = getSiblings(elem); + + for(var i = 0, j = siblings.length; i < j; i++){ + if(isTag(siblings[i])){ + if(siblings[i] === elem) continue; + if(getName(siblings[i]) === getName(elem)) return false; + } + } + + return true; + }, + "only-child": function(elem){ + var siblings = getSiblings(elem); + + for(var i = 0; i < siblings.length; i++){ + if(isTag(siblings[i]) && siblings[i] !== elem) return false; + } + + return true; + }, + + //:matches(a, area, link)[href] + link: function(elem){ + return hasAttrib(elem, "href"); + }, + visited: falseFunc, //seems to be a valid implementation + //TODO: :any-link once the name is finalized (as an alias of :link) + + //forms + //to consider: :target + + //:matches([selected], select:not([multiple]):not(> option[selected]) > option:first-of-type) + selected: function(elem){ + if(hasAttrib(elem, "selected")) return true; + else if(getName(elem) !== "option") return false; + + //the first <option> in a <select> is also selected + var parent = getParent(elem); + + if( + !parent || + getName(parent) !== "select" || + hasAttrib(parent, "multiple") + ) return false; + + var siblings = getChildren(parent), + sawElem = false; + + for(var i = 0; i < siblings.length; i++){ + if(isTag(siblings[i])){ + if(siblings[i] === elem){ + sawElem = true; + } else if(!sawElem){ + return false; + } else if(hasAttrib(siblings[i], "selected")){ + return false; + } + } + } + + return sawElem; + }, + //https://html.spec.whatwg.org/multipage/scripting.html#disabled-elements + //:matches( + // :matches(button, input, select, textarea, menuitem, optgroup, option)[disabled], + // optgroup[disabled] > option), + // fieldset[disabled] * //TODO not child of first <legend> + //) + disabled: function(elem){ + return hasAttrib(elem, "disabled"); + }, + enabled: function(elem){ + return !hasAttrib(elem, "disabled"); + }, + //:matches(:matches(:radio, :checkbox)[checked], :selected) (TODO menuitem) + checked: function(elem){ + return hasAttrib(elem, "checked") || pseudos.selected(elem); + }, + //:matches(input, select, textarea)[required] + required: function(elem){ + return hasAttrib(elem, "required"); + }, + //:matches(input, select, textarea):not([required]) + optional: function(elem){ + return !hasAttrib(elem, "required"); + }, + + //jQuery extensions + + //:not(:empty) + parent: function(elem){ + return !pseudos.empty(elem); + }, + //:matches(h1, h2, h3, h4, h5, h6) + header: function(elem){ + var name = getName(elem); + return name === "h1" || + name === "h2" || + name === "h3" || + name === "h4" || + name === "h5" || + name === "h6"; + }, + + //:matches(button, input[type=button]) + button: function(elem){ + var name = getName(elem); + return name === "button" || + name === "input" && + getAttribute(elem, "type") === "button"; + }, + //:matches(input, textarea, select, button) + input: function(elem){ + var name = getName(elem); + return name === "input" || + name === "textarea" || + name === "select" || + name === "button"; + }, + //input:matches(:not([type!='']), [type='text' i]) + text: function(elem){ + var attr; + return getName(elem) === "input" && ( + !(attr = getAttribute(elem, "type")) || + attr.toLowerCase() === "text" + ); + } +}; + +function verifyArgs(func, name, subselect){ + if(subselect === null){ + if(func.length > 1 && name !== "scope"){ + throw new SyntaxError("pseudo-selector :" + name + " requires an argument"); + } + } else { + if(func.length === 1){ + throw new SyntaxError("pseudo-selector :" + name + " doesn't have any arguments"); + } + } +} + +//FIXME this feels hacky +var re_CSS3 = /^(?:(?:nth|last|first|only)-(?:child|of-type)|root|empty|(?:en|dis)abled|checked|not)$/; + +module.exports = { + compile: function(next, data, options, context){ + var name = data.name, + subselect = data.data; + + if(options && options.strict && !re_CSS3.test(name)){ + throw SyntaxError(":" + name + " isn't part of CSS3"); + } + + if(typeof filters[name] === "function"){ + verifyArgs(filters[name], name, subselect); + return filters[name](next, subselect, options, context); + } else if(typeof pseudos[name] === "function"){ + var func = pseudos[name]; + verifyArgs(func, name, subselect); + + if(next === trueFunc) return func; + + return function pseudoArgs(elem){ + return func(elem, subselect) && next(elem); + }; + } else { + throw new SyntaxError("unmatched pseudo-class :" + name); + } + }, + filters: filters, + pseudos: pseudos +}; diff --git a/node_modules/css-select/lib/sort.js b/node_modules/css-select/lib/sort.js new file mode 100644 index 000000000..835332459 --- /dev/null +++ b/node_modules/css-select/lib/sort.js @@ -0,0 +1,80 @@ +module.exports = sortByProcedure; + +/* + sort the parts of the passed selector, + as there is potential for optimization + (some types of selectors are faster than others) +*/ + +var procedure = require("./procedure.json"); + +var attributes = { + __proto__: null, + exists: 10, + equals: 8, + not: 7, + start: 6, + end: 6, + any: 5, + hyphen: 4, + element: 4 +}; + +function sortByProcedure(arr){ + var procs = arr.map(getProcedure); + for(var i = 1; i < arr.length; i++){ + var procNew = procs[i]; + + if(procNew < 0) continue; + + for(var j = i - 1; j >= 0 && procNew < procs[j]; j--){ + var token = arr[j + 1]; + arr[j + 1] = arr[j]; + arr[j] = token; + procs[j + 1] = procs[j]; + procs[j] = procNew; + } + } +} + +function getProcedure(token){ + var proc = procedure[token.type]; + + if(proc === procedure.attribute){ + proc = attributes[token.action]; + + if(proc === attributes.equals && token.name === "id"){ + //prefer ID selectors (eg. #ID) + proc = 9; + } + + if(token.ignoreCase){ + //ignoreCase adds some overhead, prefer "normal" token + //this is a binary operation, to ensure it's still an int + proc >>= 1; + } + } else if(proc === procedure.pseudo){ + if(!token.data){ + proc = 3; + } else if(token.name === "has" || token.name === "contains"){ + proc = 0; //expensive in any case + } else if(token.name === "matches" || token.name === "not"){ + proc = 0; + for(var i = 0; i < token.data.length; i++){ + //TODO better handling of complex selectors + if(token.data[i].length !== 1) continue; + var cur = getProcedure(token.data[i][0]); + //avoid executing :has or :contains + if(cur === 0){ + proc = 0; + break; + } + if(cur > proc) proc = cur; + } + if(token.data.length > 1 && proc > 0) proc -= 1; + } else { + proc = 1; + } + } + return proc; +} diff --git a/node_modules/css-select/node_modules/domutils/.npmignore b/node_modules/css-select/node_modules/domutils/.npmignore new file mode 100644 index 000000000..3c3629e64 --- /dev/null +++ b/node_modules/css-select/node_modules/domutils/.npmignore @@ -0,0 +1 @@ +node_modules diff --git a/node_modules/css-select/node_modules/domutils/LICENSE b/node_modules/css-select/node_modules/domutils/LICENSE new file mode 100644 index 000000000..c464f863e --- /dev/null +++ b/node_modules/css-select/node_modules/domutils/LICENSE @@ -0,0 +1,11 @@ +Copyright (c) Felix Böhm +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, +EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/node_modules/css-select/node_modules/domutils/index.js b/node_modules/css-select/node_modules/domutils/index.js new file mode 100644 index 000000000..13f334420 --- /dev/null +++ b/node_modules/css-select/node_modules/domutils/index.js @@ -0,0 +1,14 @@ +var DomUtils = module.exports; + +[ + require("./lib/stringify"), + require("./lib/traversal"), + require("./lib/manipulation"), + require("./lib/querying"), + require("./lib/legacy"), + require("./lib/helpers") +].forEach(function(ext){ + Object.keys(ext).forEach(function(key){ + DomUtils[key] = ext[key].bind(DomUtils); + }); +}); diff --git a/node_modules/css-select/node_modules/domutils/lib/helpers.js b/node_modules/css-select/node_modules/domutils/lib/helpers.js new file mode 100644 index 000000000..57056f632 --- /dev/null +++ b/node_modules/css-select/node_modules/domutils/lib/helpers.js @@ -0,0 +1,141 @@ +// removeSubsets +// Given an array of nodes, remove any member that is contained by another. +exports.removeSubsets = function(nodes) { + var idx = nodes.length, node, ancestor, replace; + + // Check if each node (or one of its ancestors) is already contained in the + // array. + while (--idx > -1) { + node = ancestor = nodes[idx]; + + // Temporarily remove the node under consideration + nodes[idx] = null; + replace = true; + + while (ancestor) { + if (nodes.indexOf(ancestor) > -1) { + replace = false; + nodes.splice(idx, 1); + break; + } + ancestor = ancestor.parent; + } + + // If the node has been found to be unique, re-insert it. + if (replace) { + nodes[idx] = node; + } + } + + return nodes; +}; + +// Source: http://dom.spec.whatwg.org/#dom-node-comparedocumentposition +var POSITION = { + DISCONNECTED: 1, + PRECEDING: 2, + FOLLOWING: 4, + CONTAINS: 8, + CONTAINED_BY: 16 +}; + +// Compare the position of one node against another node in any other document. +// The return value is a bitmask with the following values: +// +// document order: +// > There is an ordering, document order, defined on all the nodes in the +// > document corresponding to the order in which the first character of the +// > XML representation of each node occurs in the XML representation of the +// > document after expansion of general entities. Thus, the document element +// > node will be the first node. Element nodes occur before their children. +// > Thus, document order orders element nodes in order of the occurrence of +// > their start-tag in the XML (after expansion of entities). The attribute +// > nodes of an element occur after the element and before its children. The +// > relative order of attribute nodes is implementation-dependent./ +// Source: +// http://www.w3.org/TR/DOM-Level-3-Core/glossary.html#dt-document-order +// +// @argument {Node} nodaA The first node to use in the comparison +// @argument {Node} nodeB The second node to use in the comparison +// +// @return {Number} A bitmask describing the input nodes' relative position. +// See http://dom.spec.whatwg.org/#dom-node-comparedocumentposition for +// a description of these values. +var comparePos = exports.compareDocumentPosition = function(nodeA, nodeB) { + var aParents = []; + var bParents = []; + var current, sharedParent, siblings, aSibling, bSibling, idx; + + if (nodeA === nodeB) { + return 0; + } + + current = nodeA; + while (current) { + aParents.unshift(current); + current = current.parent; + } + current = nodeB; + while (current) { + bParents.unshift(current); + current = current.parent; + } + + idx = 0; + while (aParents[idx] === bParents[idx]) { + idx++; + } + + if (idx === 0) { + return POSITION.DISCONNECTED; + } + + sharedParent = aParents[idx - 1]; + siblings = sharedParent.children; + aSibling = aParents[idx]; + bSibling = bParents[idx]; + + if (siblings.indexOf(aSibling) > siblings.indexOf(bSibling)) { + if (sharedParent === nodeB) { + return POSITION.FOLLOWING | POSITION.CONTAINED_BY; + } + return POSITION.FOLLOWING; + } else { + if (sharedParent === nodeA) { + return POSITION.PRECEDING | POSITION.CONTAINS; + } + return POSITION.PRECEDING; + } +}; + +// Sort an array of nodes based on their relative position in the document and +// remove any duplicate nodes. If the array contains nodes that do not belong +// to the same document, sort order is unspecified. +// +// @argument {Array} nodes Array of DOM nodes +// +// @returns {Array} collection of unique nodes, sorted in document order +exports.uniqueSort = function(nodes) { + var idx = nodes.length, node, position; + + nodes = nodes.slice(); + + while (--idx > -1) { + node = nodes[idx]; + position = nodes.indexOf(node); + if (position > -1 && position < idx) { + nodes.splice(idx, 1); + } + } + nodes.sort(function(a, b) { + var relative = comparePos(a, b); + if (relative & POSITION.PRECEDING) { + return -1; + } else if (relative & POSITION.FOLLOWING) { + return 1; + } + return 0; + }); + + return nodes; +}; diff --git a/node_modules/css-select/node_modules/domutils/lib/legacy.js b/node_modules/css-select/node_modules/domutils/lib/legacy.js new file mode 100644 index 000000000..43bd446db --- /dev/null +++ b/node_modules/css-select/node_modules/domutils/lib/legacy.js @@ -0,0 +1,87 @@ +var ElementType = require("domelementtype"); +var isTag = exports.isTag = ElementType.isTag; + +exports.testElement = function(options, element){ + for(var key in options){ + if(!options.hasOwnProperty(key)); + else if(key === "tag_name"){ + if(!isTag(element) || !options.tag_name(element.name)){ + return false; + } + } else if(key === "tag_type"){ + if(!options.tag_type(element.type)) return false; + } else if(key === "tag_contains"){ + if(isTag(element) || !options.tag_contains(element.data)){ + return false; + } + } else if(!element.attribs || !options[key](element.attribs[key])){ + return false; + } + } + return true; +}; + +var Checks = { + tag_name: function(name){ + if(typeof name === "function"){ + return function(elem){ return isTag(elem) && name(elem.name); }; + } else if(name === "*"){ + return isTag; + } else { + return function(elem){ return isTag(elem) && elem.name === name; }; + } + }, + tag_type: function(type){ + if(typeof type === "function"){ + return function(elem){ return type(elem.type); }; + } else { + return function(elem){ return elem.type === type; }; + } + }, + tag_contains: function(data){ + if(typeof data === "function"){ + return function(elem){ return !isTag(elem) && data(elem.data); }; + } else { + return function(elem){ return !isTag(elem) && elem.data === data; }; + } + } +}; + +function getAttribCheck(attrib, value){ + if(typeof value === "function"){ + return function(elem){ return elem.attribs && value(elem.attribs[attrib]); }; + } else { + return function(elem){ return elem.attribs && elem.attribs[attrib] === value; }; + } +} + +function combineFuncs(a, b){ + return function(elem){ + return a(elem) || b(elem); + }; +} + +exports.getElements = function(options, element, recurse, limit){ + var funcs = Object.keys(options).map(function(key){ + var value = options[key]; + return key in Checks ? Checks[key](value) : getAttribCheck(key, value); + }); + + return funcs.length === 0 ? [] : this.filter( + funcs.reduce(combineFuncs), + element, recurse, limit + ); +}; + +exports.getElementById = function(id, element, recurse){ + if(!Array.isArray(element)) element = [element]; + return this.findOne(getAttribCheck("id", id), element, recurse !== false); +}; + +exports.getElementsByTagName = function(name, element, recurse, limit){ + return this.filter(Checks.tag_name(name), element, recurse, limit); +}; + +exports.getElementsByTagType = function(type, element, recurse, limit){ + return this.filter(Checks.tag_type(type), element, recurse, limit); +}; diff --git a/node_modules/css-select/node_modules/domutils/lib/manipulation.js b/node_modules/css-select/node_modules/domutils/lib/manipulation.js new file mode 100644 index 000000000..6b44cbc5a --- /dev/null +++ b/node_modules/css-select/node_modules/domutils/lib/manipulation.js @@ -0,0 +1,77 @@ +exports.removeElement = function(elem){ + if(elem.prev) elem.prev.next = elem.next; + if(elem.next) elem.next.prev = elem.prev; + + if(elem.parent){ + var childs = elem.parent.children; + childs.splice(childs.lastIndexOf(elem), 1); + } +}; + +exports.replaceElement = function(elem, replacement){ + var prev = replacement.prev = elem.prev; + if(prev){ + prev.next = replacement; + } + + var next = replacement.next = elem.next; + if(next){ + next.prev = replacement; + } + + var parent = replacement.parent = elem.parent; + if(parent){ + var childs = parent.children; + childs[childs.lastIndexOf(elem)] = replacement; + } +}; + +exports.appendChild = function(elem, child){ + child.parent = elem; + + if(elem.children.push(child) !== 1){ + var sibling = elem.children[elem.children.length - 2]; + sibling.next = child; + child.prev = sibling; + child.next = null; + } +}; + +exports.append = function(elem, next){ + var parent = elem.parent, + currNext = elem.next; + + next.next = currNext; + next.prev = elem; + elem.next = next; + next.parent = parent; + + if(currNext){ + currNext.prev = next; + if(parent){ + var childs = parent.children; + childs.splice(childs.lastIndexOf(currNext), 0, next); + } + } else if(parent){ + parent.children.push(next); + } +}; + +exports.prepend = function(elem, prev){ + var parent = elem.parent; + if(parent){ + var childs = parent.children; + childs.splice(childs.lastIndexOf(elem), 0, prev); + } + + if(elem.prev){ + elem.prev.next = prev; + } + + prev.parent = parent; + prev.prev = elem.prev; + prev.next = elem; + elem.prev = prev; +}; + + diff --git a/node_modules/css-select/node_modules/domutils/lib/querying.js b/node_modules/css-select/node_modules/domutils/lib/querying.js new file mode 100644 index 000000000..17e3aa9bd --- /dev/null +++ b/node_modules/css-select/node_modules/domutils/lib/querying.js @@ -0,0 +1,94 @@ +var isTag = require("domelementtype").isTag; + +module.exports = { + filter: filter, + find: find, + findOneChild: findOneChild, + findOne: findOne, + existsOne: existsOne, + findAll: findAll +}; + +function filter(test, element, recurse, limit){ + if(!Array.isArray(element)) element = [element]; + + if(typeof limit !== "number" || !isFinite(limit)){ + limit = Infinity; + } + return find(test, element, recurse !== false, limit); +} + +function find(test, elems, recurse, limit){ + var result = [], childs; + + for(var i = 0, j = elems.length; i < j; i++){ + if(test(elems[i])){ + result.push(elems[i]); + if(--limit <= 0) break; + } + + childs = elems[i].children; + if(recurse && childs && childs.length > 0){ + childs = find(test, childs, recurse, limit); + result = result.concat(childs); + limit -= childs.length; + if(limit <= 0) break; + } + } + + return result; +} + +function findOneChild(test, elems){ + for(var i = 0, l = elems.length; i < l; i++){ + if(test(elems[i])) return elems[i]; + } + + return null; +} + +function findOne(test, elems){ + var elem = null; + + for(var i = 0, l = elems.length; i < l && !elem; i++){ + if(!isTag(elems[i])){ + continue; + } else if(test(elems[i])){ + elem = elems[i]; + } else if(elems[i].children.length > 0){ + elem = findOne(test, elems[i].children); + } + } + + return elem; +} + +function existsOne(test, elems){ + for(var i = 0, l = elems.length; i < l; i++){ + if( + isTag(elems[i]) && ( + test(elems[i]) || ( + elems[i].children.length > 0 && + existsOne(test, elems[i].children) + ) + ) + ){ + return true; + } + } + + return false; +} + +function findAll(test, elems){ + var result = []; + for(var i = 0, j = elems.length; i < j; i++){ + if(!isTag(elems[i])) continue; + if(test(elems[i])) result.push(elems[i]); + + if(elems[i].children.length > 0){ + result = result.concat(findAll(test, elems[i].children)); + } + } + return result; +} diff --git a/node_modules/css-select/node_modules/domutils/lib/stringify.js b/node_modules/css-select/node_modules/domutils/lib/stringify.js new file mode 100644 index 000000000..e3f2f393f --- /dev/null +++ b/node_modules/css-select/node_modules/domutils/lib/stringify.js @@ -0,0 +1,22 @@ +var ElementType = require("domelementtype"), + getOuterHTML = require("dom-serializer"), + isTag = ElementType.isTag; + +module.exports = { + getInnerHTML: getInnerHTML, + getOuterHTML: getOuterHTML, + getText: getText +}; + +function getInnerHTML(elem, opts){ + return elem.children ? elem.children.map(function(elem){ + return getOuterHTML(elem, opts); + }).join("") : ""; +} + +function getText(elem){ + if(Array.isArray(elem)) return elem.map(getText).join(""); + if(isTag(elem) || elem.type === ElementType.CDATA) return getText(elem.children); + if(elem.type === ElementType.Text) return elem.data; + return ""; +} diff --git a/node_modules/css-select/node_modules/domutils/lib/traversal.js b/node_modules/css-select/node_modules/domutils/lib/traversal.js new file mode 100644 index 000000000..ffdfeb696 --- /dev/null +++ b/node_modules/css-select/node_modules/domutils/lib/traversal.js @@ -0,0 +1,24 @@ +var getChildren = exports.getChildren = function(elem){ + return elem.children; +}; + +var getParent = exports.getParent = function(elem){ + return elem.parent; +}; + +exports.getSiblings = function(elem){ + var parent = getParent(elem); + return parent ? getChildren(parent) : [elem]; +}; + +exports.getAttributeValue = function(elem, name){ + return elem.attribs && elem.attribs[name]; +}; + +exports.hasAttrib = function(elem, name){ + return !!elem.attribs && hasOwnProperty.call(elem.attribs, name); +}; + +exports.getName = function(elem){ + return elem.name; +}; diff --git a/node_modules/css-select/node_modules/domutils/package.json b/node_modules/css-select/node_modules/domutils/package.json new file mode 100644 index 000000000..0daf95540 --- /dev/null +++ b/node_modules/css-select/node_modules/domutils/package.json @@ -0,0 +1,46 @@ +{ + "name": "domutils", + "version": "1.5.1", + "description": "utilities for working with htmlparser2's dom", + "main": "index.js", + "directories": { + "test": "tests" + }, + "scripts": { + "test": "mocha test/tests/**.js && jshint index.js test/**/*.js lib/*.js" + }, + "repository": { + "type": "git", + "url": "git://github.com/FB55/domutils.git" + }, + "keywords": [ + "dom", + "htmlparser2" + ], + "dependencies": { + "dom-serializer": "0", + "domelementtype": "1" + }, + "devDependencies": { + "htmlparser2": "~3.3.0", + "domhandler": "2", + "jshint": "~2.3.0", + "mocha": "~1.15.1" + }, + "author": "Felix Boehm <me@feedic.com>", + "jshintConfig": { + "proto": true, + "unused": true, + "eqnull": true, + "undef": true, + "quotmark": "double", + "eqeqeq": true, + "trailing": true, + "node": true, + "globals": { + "describe": true, + "it": true, + "beforeEach": true + } + } +} diff --git a/node_modules/css-select/node_modules/domutils/readme.md b/node_modules/css-select/node_modules/domutils/readme.md new file mode 100644 index 000000000..9ccdda608 --- /dev/null +++ b/node_modules/css-select/node_modules/domutils/readme.md @@ -0,0 +1 @@ +utilities for working with htmlparser2's dom diff --git a/node_modules/css-select/node_modules/domutils/test/fixture.js b/node_modules/css-select/node_modules/domutils/test/fixture.js new file mode 100644 index 000000000..9bd791b7d --- /dev/null +++ b/node_modules/css-select/node_modules/domutils/test/fixture.js @@ -0,0 +1,6 @@ +var makeDom = require("./utils").makeDom; +var markup = Array(21).join( + "<?xml><tag1 id='asdf'> <script>text</script> <!-- comment --> <tag2> text </tag1>" +); + +module.exports = makeDom(markup); diff --git a/node_modules/css-select/node_modules/domutils/test/tests/helpers.js b/node_modules/css-select/node_modules/domutils/test/tests/helpers.js new file mode 100644 index 000000000..2e30afb69 --- /dev/null +++ b/node_modules/css-select/node_modules/domutils/test/tests/helpers.js @@ -0,0 +1,89 @@ +var makeDom = require("../utils").makeDom; +var helpers = require("../.."); +var assert = require("assert"); + +describe("helpers", function() { + describe("removeSubsets", function() { + var removeSubsets = helpers.removeSubsets; + var dom = makeDom("<div><p><span></span></p><p></p></div>")[0]; + + it("removes identical trees", function() { + var matches = removeSubsets([dom, dom]); + assert.equal(matches.length, 1); + }); + + it("Removes subsets found first", function() { + var matches = removeSubsets([dom, dom.children[0].children[0]]); + assert.equal(matches.length, 1); + }); + + it("Removes subsets found last", function() { + var matches = removeSubsets([dom.children[0], dom]); + assert.equal(matches.length, 1); + }); + + it("Does not remove unique trees", function() { + var matches = removeSubsets([dom.children[0], dom.children[1]]); + assert.equal(matches.length, 2); + }); + }); + + describe("compareDocumentPosition", function() { + var compareDocumentPosition = helpers.compareDocumentPosition; + var markup = "<div><p><span></span></p><a></a></div>"; + var dom = makeDom(markup)[0]; + var p = dom.children[0]; + var span = p.children[0]; + var a = dom.children[1]; + + it("reports when the first node occurs before the second indirectly", function() { + assert.equal(compareDocumentPosition(span, a), 2); + }); + + it("reports when the first node contains the second", function() { + assert.equal(compareDocumentPosition(p, span), 10); + }); + + it("reports when the first node occurs after the second indirectly", function() { + assert.equal(compareDocumentPosition(a, span), 4); + }); + + it("reports when the first node is contained by the second", function() { + assert.equal(compareDocumentPosition(span, p), 20); + }); + + it("reports when the nodes belong to separate documents", function() { + var other = makeDom(markup)[0].children[0].children[0]; + + assert.equal(compareDocumentPosition(span, other), 1); + }); + + it("reports when the nodes are identical", function() { + assert.equal(compareDocumentPosition(span, span), 0); + }); + }); + + describe("uniqueSort", function() { + var uniqueSort = helpers.uniqueSort; + var dom, p, span, a; + + beforeEach(function() { + dom = makeDom("<div><p><span></span></p><a></a></div>")[0]; + p = dom.children[0]; + span = p.children[0]; + a = dom.children[1]; + }); + + it("leaves unique elements untouched", function() { + assert.deepEqual(uniqueSort([p, a]), [p, a]); + }); + + it("removes duplicate elements", function() { + assert.deepEqual(uniqueSort([p, a, p]), [p, a]); + }); + + it("sorts nodes in document order", function() { + assert.deepEqual(uniqueSort([a, dom, span, p]), [dom, p, span, a]); + }); + }); +}); diff --git a/node_modules/css-select/node_modules/domutils/test/tests/legacy.js b/node_modules/css-select/node_modules/domutils/test/tests/legacy.js new file mode 100644 index 000000000..87fabfab2 --- /dev/null +++ b/node_modules/css-select/node_modules/domutils/test/tests/legacy.js @@ -0,0 +1,119 @@ +var DomUtils = require("../.."); +var fixture = require("../fixture"); +var assert = require("assert"); + +// Set up expected structures +var expected = { + idAsdf: fixture[1], + tag2: [], + typeScript: [] +}; +for (var idx = 0; idx < 20; ++idx) { + expected.tag2.push(fixture[idx*2 + 1].children[5]); + expected.typeScript.push(fixture[idx*2 + 1].children[1]); +} + +describe("legacy", function() { + describe("getElements", function() { + var getElements = DomUtils.getElements; + it("returns the node with the specified ID", function() { + assert.deepEqual( + getElements({ id: "asdf" }, fixture, true, 1), + [expected.idAsdf] + ); + }); + it("returns empty array for unknown IDs", function() { + assert.deepEqual(getElements({ id: "asdfs" }, fixture, true), []); + }); + it("returns the nodes with the specified tag name", function() { + assert.deepEqual( + getElements({ tag_name:"tag2" }, fixture, true), + expected.tag2 + ); + }); + it("returns empty array for unknown tag names", function() { + assert.deepEqual( + getElements({ tag_name : "asdfs" }, fixture, true), + [] + ); + }); + it("returns the nodes with the specified tag type", function() { + assert.deepEqual( + getElements({ tag_type: "script" }, fixture, true), + expected.typeScript + ); + }); + it("returns empty array for unknown tag types", function() { + assert.deepEqual( + getElements({ tag_type: "video" }, fixture, true), + [] + ); + }); + }); + + describe("getElementById", function() { + var getElementById = DomUtils.getElementById; + it("returns the specified node", function() { + assert.equal( + expected.idAsdf, + getElementById("asdf", fixture, true) + ); + }); + it("returns `null` for unknown IDs", function() { + assert.equal(null, getElementById("asdfs", fixture, true)); + }); + }); + + describe("getElementsByTagName", function() { + var getElementsByTagName = DomUtils.getElementsByTagName; + it("returns the specified nodes", function() { + assert.deepEqual( + getElementsByTagName("tag2", fixture, true), + expected.tag2 + ); + }); + it("returns empty array for unknown tag names", function() { + assert.deepEqual( + getElementsByTagName("tag23", fixture, true), + [] + ); + }); + }); + + describe("getElementsByTagType", function() { + var getElementsByTagType = DomUtils.getElementsByTagType; + it("returns the specified nodes", function() { + assert.deepEqual( + getElementsByTagType("script", fixture, true), + expected.typeScript + ); + }); + it("returns empty array for unknown tag types", function() { + assert.deepEqual( + getElementsByTagType("video", fixture, true), + [] + ); + }); + }); + + describe("getOuterHTML", function() { + var getOuterHTML = DomUtils.getOuterHTML; + it("Correctly renders the outer HTML", function() { + assert.equal( + getOuterHTML(fixture[1]), + "<tag1 id=\"asdf\"> <script>text</script> <!-- comment --> <tag2> text </tag2></tag1>" + ); + }); + }); + + describe("getInnerHTML", function() { + var getInnerHTML = DomUtils.getInnerHTML; + it("Correctly renders the inner HTML", function() { + assert.equal( + getInnerHTML(fixture[1]), + " <script>text</script> <!-- comment --> <tag2> text </tag2>" + ); + }); + }); + +}); diff --git a/node_modules/css-select/node_modules/domutils/test/tests/traversal.js b/node_modules/css-select/node_modules/domutils/test/tests/traversal.js new file mode 100644 index 000000000..f500e089b --- /dev/null +++ b/node_modules/css-select/node_modules/domutils/test/tests/traversal.js @@ -0,0 +1,17 @@ +var makeDom = require("../utils").makeDom; +var traversal = require("../.."); +var assert = require("assert"); + +describe("traversal", function() { + describe("hasAttrib", function() { + var hasAttrib = traversal.hasAttrib; + + it("doesn't throw on text nodes", function() { + var dom = makeDom("textnode"); + assert.doesNotThrow(function() { + hasAttrib(dom[0], "some-attrib"); + }); + }); + + }); +}); diff --git a/node_modules/css-select/node_modules/domutils/test/utils.js b/node_modules/css-select/node_modules/domutils/test/utils.js new file mode 100644 index 000000000..676e8f68f --- /dev/null +++ b/node_modules/css-select/node_modules/domutils/test/utils.js @@ -0,0 +1,9 @@ +var htmlparser = require("htmlparser2"); + +exports.makeDom = function(markup) { + var handler = new htmlparser.DomHandler(), + parser = new htmlparser.Parser(handler); + parser.write(markup); + parser.done(); + return handler.dom; +}; diff --git a/node_modules/css-select/package.json b/node_modules/css-select/package.json new file mode 100644 index 000000000..dc14b25e3 --- /dev/null +++ b/node_modules/css-select/package.json @@ -0,0 +1,61 @@ +{ + "name": "css-select", + "version": "1.2.0", + "description": "a CSS selector compiler/engine", + "author": "Felix Boehm <me@feedic.com>", + "keywords": [ + "css", + "selector", + "sizzle" + ], + "repository": { + "type": "git", + "url": "git://github.com/fb55/css-select.git" + }, + "files": [ + "index.js", + "lib" + ], + "dependencies": { + "css-what": "2.1", + "domutils": "1.5.1", + "boolbase": "~1.0.0", + "nth-check": "~1.0.1" + }, + "devDependencies": { + "htmlparser2": "*", + "cheerio-soupselect": "*", + "mocha": "*", + "mocha-lcov-reporter": "*", + "coveralls": "*", + "istanbul": "*", + "expect.js": "*", + "jshint": "2" + }, + "scripts": { + "test": "mocha && npm run lint", + "lint": "jshint index.js lib/*.js test/*.js", + "lcov": "istanbul cover _mocha --report lcovonly -- -R spec", + "coveralls": "npm run lint && npm run lcov && (cat coverage/lcov.info | coveralls || exit 0)" + }, + "license": "BSD-like", + "jshintConfig": { + "eqeqeq": true, + "freeze": true, + "latedef": "nofunc", + "noarg": true, + "nonbsp": true, + "quotmark": "double", + "undef": true, + "unused": true, + "trailing": true, + "eqnull": true, + "proto": true, + "smarttabs": true, + "node": true, + "globals": { + "describe": true, + "it": true + } + } +} |