[Pkg-javascript-commits] [node-css-select] 01/05: Import Upstream version 1.2.0

Paolo Greppi paolog-guest at moszumanska.debian.org
Fri Dec 16 15:32:22 UTC 2016


This is an automated email from the git hooks/post-receive script.

paolog-guest pushed a commit to branch master
in repository node-css-select.

commit a3d79963e7159aad0cd208a2758a61f5f9a6354b
Author: Paolo Greppi <paolo.greppi at libpf.com>
Date:   Fri Dec 16 14:23:14 2016 +0000

    Import Upstream version 1.2.0
---
 .travis.yml                        |    6 +
 LICENSE                            |   11 +
 README.md                          |  133 +++
 browser_functions.js               |   67 ++
 index.js                           |   59 ++
 lib/attributes.js                  |  181 ++++
 lib/compile.js                     |  192 ++++
 lib/general.js                     |   89 ++
 lib/procedure.json                 |   11 +
 lib/pseudos.js                     |  393 +++++++
 lib/sort.js                        |   80 ++
 package.json                       |   61 ++
 test/api.js                        |  150 +++
 test/attributes.js                 |   84 ++
 test/icontains.js                  |   86 ++
 test/mocha.opts                    |    2 +
 test/nwmatcher/LICENSE             |   22 +
 test/nwmatcher/index.js            |  467 +++++++++
 test/nwmatcher/test.html           |   92 ++
 test/qwery/index.html              |  132 +++
 test/qwery/index.js                |  549 ++++++++++
 test/sizzle/data/fries.xml         |    1 +
 test/sizzle/data/index.html        |  247 +++++
 test/sizzle/data/testinit.js       |   87 ++
 test/sizzle/selector.js            | 1197 +++++++++++++++++++++
 test/test.js                       |   22 +
 test/tools/bench.js                |   10 +
 test/tools/docs/W3C_Selectors.html | 2034 ++++++++++++++++++++++++++++++++++++
 test/tools/helper.js               |   51 +
 test/tools/slickspeed.js           |   76 ++
 30 files changed, 6592 insertions(+)

diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..577ee23
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,6 @@
+language: node_js
+node_js:
+  - "0.10"
+  - 0.11
+
+script: npm run coveralls
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..c464f86
--- /dev/null
+++ b/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 B [...]
+EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..e36282f
--- /dev/null
+++ b/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/browser_functions.js b/browser_functions.js
new file mode 100644
index 0000000..024540d
--- /dev/null
+++ b/browser_functions.js
@@ -0,0 +1,67 @@
+function isTag(elem){
+	return elem.nodeType === 1;
+}
+function getChildren(elem){
+	return Array.prototype.slice.call(elem.childNodes, 0);
+}
+function getParent(elem){
+	return elem.parentElement;
+}
+
+module.exports = {
+	isTag: isTag,
+	getSiblings: function(elem){
+		var parent = getParent(elem);
+		return parent && getChildren(parent);
+	},
+	getChildren: getChildren,
+	getParent: getParent,
+	getAttributeValue: function(elem, name){
+		return elem.attributes[name].value;
+	},
+	hasAttrib: function(elem, name){
+		return name in elem.attributes;
+	},
+	getName: function(elem){
+		return elem.tagName.toLowerCase();
+	},
+	findOne: function findOne(test, arr){
+		var elem = null;
+
+		for(var i = 0, l = arr.length; i < l && !elem; i++){
+			if(test(arr[i])){
+				elem = arr[i];
+			} else {
+				var childs = getChildren(arr[i]);
+				if(childs && childs.length > 0){
+					elem = findOne(test, childs);
+				}
+			}
+		}
+
+		return elem;
+	},
+	findAll: 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]);
+			var childs = getChildren(elems[i]);
+			if(childs) result = result.concat(findAll(test, childs));
+		}
+		return result;
+	},
+	//https://github.com/ded/qwery/blob/master/pseudos/qwery-pseudos.js#L47-54
+	getText: function getText(elem) {
+		var str = "",
+		    childs = getChildren(elem);
+
+		if(!childs) return str;
+
+		for(var i = 0; i < childs.length; i++){
+			if(isTag(childs[i])) str += elem.textContent || elem.innerText || getText(childs[i]);
+		}
+
+		return str;
+	}
+};
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..4179d19
--- /dev/null
+++ b/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/lib/attributes.js b/lib/attributes.js
new file mode 100644
index 0000000..a8689c0
--- /dev/null
+++ b/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/lib/compile.js b/lib/compile.js
new file mode 100644
index 0000000..91ac592
--- /dev/null
+++ b/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/lib/general.js b/lib/general.js
new file mode 100644
index 0000000..fbc960f
--- /dev/null
+++ b/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/lib/procedure.json b/lib/procedure.json
new file mode 100644
index 0000000..e836de1
--- /dev/null
+++ b/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/lib/pseudos.js b/lib/pseudos.js
new file mode 100644
index 0000000..f6774ec
--- /dev/null
+++ b/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/lib/sort.js b/lib/sort.js
new file mode 100644
index 0000000..8353324
--- /dev/null
+++ b/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/package.json b/package.json
new file mode 100644
index 0000000..dc14b25
--- /dev/null
+++ b/package.json
@@ -0,0 +1,61 @@
+{
+  "name": "css-select",
+  "version": "1.2.0",
+  "description": "a CSS selector compiler/engine",
+  "author": "Felix Boehm <me at 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
+    }
+  }
+}
diff --git a/test/api.js b/test/api.js
new file mode 100644
index 0000000..a40b53f
--- /dev/null
+++ b/test/api.js
@@ -0,0 +1,150 @@
+var CSSselect = require(".."),
+    makeDom = require("htmlparser2").parseDOM,
+    bools = require("boolbase"),
+    assert = require("assert");
+
+var dom = makeDom("<div id=foo><p>foo</p></div>")[0],
+    xmlDom = makeDom("<DiV id=foo><P>foo</P></DiV>", {xmlMode: true})[0];
+
+describe("API", function(){
+	describe("removes duplicates", function(){
+		it("between identical trees", function(){
+			var matches = CSSselect.selectAll("div", [dom, dom]);
+			assert.equal(matches.length, 1, "Removes duplicate matches");
+		});
+		it("between a superset and subset", function(){
+			var matches = CSSselect.selectAll("p", [dom, dom.children[0]]);
+			assert.equal(matches.length, 1, "Removes duplicate matches");
+		});
+		it("betweeen a subset and superset", function(){
+			var matches = CSSselect.selectAll("p", [dom.children[0], dom]);
+			assert.equal(matches.length, 1, "Removes duplicate matches");
+		});
+	});
+
+	describe("can be queried by function", function(){
+		it("in `is`", function(){
+			assert(CSSselect.is(dom, function(elem){
+				return elem.attribs.id === "foo";
+			}));
+		});
+		//probably more cases should be added here
+	});
+
+	describe("selectAll", function(){
+		it("should query array elements directly when they have no parents", function() {
+			var divs = [dom];
+			assert.deepEqual(CSSselect("div", divs), divs);
+		});
+		it("should query array elements directly when they have parents", function() {
+			var ps = CSSselect("p", [dom]);
+			assert.deepEqual(CSSselect("p", ps), ps);
+		});
+	});
+
+	describe("unsatisfiable and universally valid selectors", function(){
+		it("in :not", function(){
+			var func = CSSselect._compileUnsafe(":not(*)");
+			assert.equal(func, bools.falseFunc);
+			func = CSSselect._compileUnsafe(":not(:nth-child(-1n-1))");
+			assert.equal(func, bools.trueFunc);
+			func = CSSselect._compileUnsafe(":not(:not(:not(*)))");
+			assert.equal(func, bools.falseFunc);
+		});
+
+		it("in :has", function(){
+			var matches = CSSselect.selectAll(":has(*)", [dom]);
+			assert.equal(matches.length, 1);
+			assert.equal(matches[0], dom);
+			var func = CSSselect._compileUnsafe(":has(:nth-child(-1n-1))");
+			assert.equal(func, bools.falseFunc);
+		});
+
+		it("should skip unsatisfiable", function(){
+			var func = CSSselect._compileUnsafe("* :not(*) foo");
+			assert.equal(func, bools.falseFunc);
+		});
+
+		it("should promote universally valid", function(){
+			var func = CSSselect._compileUnsafe("*, foo");
+			assert.equal(func, bools.trueFunc);
+		});
+	});
+
+	describe(":matches", function(){
+		it("should select multiple elements", function(){
+			var matches = CSSselect.selectAll(":matches(p, div)", [dom]);
+			assert.equal(matches.length, 2);
+			matches = CSSselect.selectAll(":matches(div, :not(div))", [dom]);
+			assert.equal(matches.length, 2);
+			matches = CSSselect.selectAll(":matches(boo, baa, tag, div, foo, bar, baz)", [dom]);
+			assert.equal(matches.length, 1);
+			assert.equal(matches[0], dom);
+		});
+
+		it("should strip quotes", function(){
+			var matches = CSSselect.selectAll(":matches('p, div')", [dom]);
+			assert.equal(matches.length, 2);
+			matches = CSSselect.selectAll(":matches(\"p, div\")", [dom]);
+			assert.equal(matches.length, 2);
+		});
+	});
+
+	describe("parent selector (<)", function(){
+		it("should select the right element", function(){
+			var matches = CSSselect.selectAll("p < div", [dom]);
+			assert.equal(matches.length, 1);
+			assert.equal(matches[0], dom);
+		});
+		it("should not select nodes without children", function(){
+			var matches = CSSselect.selectAll("p < div", [dom]);
+			assert.deepEqual(matches, CSSselect.selectAll("* < *", [dom]));
+		});
+	});
+
+	describe("selectOne", function(){
+		it("should select elements in traversal order", function(){
+			var match = CSSselect.selectOne("p", [dom]);
+			assert.equal(match, dom.children[0]);
+			match = CSSselect.selectOne(":contains(foo)", [dom]);
+			assert.equal(match, dom);
+		});
+		it("should take shortcuts when applicable", function(){
+			//TODO this is currently only visible in coverage reports
+			var match = CSSselect.selectOne(bools.falseFunc, [dom]);
+			assert.equal(match, null);
+			match = CSSselect.selectOne("*", []);
+			assert.equal(match, null);
+		});
+	});
+
+	describe("options", function(){
+		var opts = {xmlMode: true};
+		it("should recognize xmlMode in :has and :not", function(){
+			assert(CSSselect.is(xmlDom, "DiV:has(P)",   opts));
+			assert(CSSselect.is(xmlDom, "DiV:not(div)", opts));
+			assert(CSSselect.is(xmlDom.children[0], "DiV:has(P) :not(p)", opts));
+		});
+
+		it("should be strict", function(){
+			var opts = {strict: true};
+			assert.throws(CSSselect.compile.bind(null, ":checkbox", opts), SyntaxError);
+			assert.throws(CSSselect.compile.bind(null, "[attr=val i]", opts), SyntaxError);
+			assert.throws(CSSselect.compile.bind(null, "[attr!=val]", opts), SyntaxError);
+			assert.throws(CSSselect.compile.bind(null, "[attr!=val i]", opts), SyntaxError);
+			assert.throws(CSSselect.compile.bind(null, "foo < bar", opts), SyntaxError);
+			assert.throws(CSSselect.compile.bind(null, ":not(:parent)", opts), SyntaxError);
+			assert.throws(CSSselect.compile.bind(null, ":not(a > b)", opts), SyntaxError);
+			assert.throws(CSSselect.compile.bind(null, ":not(a, b)",  opts), SyntaxError);
+		});
+
+		it("should recognize contexts", function(){
+			var div = CSSselect("div", [dom]),
+			    p = CSSselect("p", [dom]);
+
+			assert.equal(CSSselect.selectOne("div", div, {context: div}), div[0]);
+			assert.equal(CSSselect.selectOne("div", div, {context: p}), null);
+			assert.deepEqual(CSSselect.selectAll("p", div, {context: div}), p);
+		});
+	});
+});
diff --git a/test/attributes.js b/test/attributes.js
new file mode 100644
index 0000000..254be83
--- /dev/null
+++ b/test/attributes.js
@@ -0,0 +1,84 @@
+var CSSselect = require("../"),
+    makeDom = require("htmlparser2").parseDOM,
+    falseFunc = require("boolbase").falseFunc,
+    assert = require("assert");
+
+var dom = makeDom("<div><div data-foo=\"In the end, it doesn't really matter.\"></div><div data-foo=\"Indeed-that's a delicate matter.\">");
+
+describe("Attributes", function(){
+	describe("ignore case", function(){
+		it("should for =", function(){
+			var matches = CSSselect.selectAll("[data-foo=\"indeed-that's a delicate matter.\" i]", dom);
+			assert.equal(matches.length, 1);
+			assert.deepEqual(matches, [dom[0].children[1]]);
+			matches = CSSselect.selectAll("[data-foo=\"inDeeD-THAT's a DELICATE matteR.\" i]", dom);
+			assert.deepEqual(matches, [dom[0].children[1]]);
+		});
+
+		it("should for ^=", function(){
+			var matches = CSSselect.selectAll("[data-foo^=IN i]", dom);
+			assert.equal(matches.length, 2);
+			assert.deepEqual(matches, dom[0].children);
+			matches = CSSselect.selectAll("[data-foo^=in i]", dom);
+			assert.deepEqual(matches, dom[0].children);
+			matches = CSSselect.selectAll("[data-foo^=iN i]", dom);
+			assert.deepEqual(matches, dom[0].children);
+		});
+
+		it("should for $=", function(){
+			var matches = CSSselect.selectAll("[data-foo$=\"MATTER.\" i]", dom);
+			assert.equal(matches.length, 2);
+			assert.deepEqual(matches, dom[0].children);
+			matches = CSSselect.selectAll("[data-foo$=\"matter.\" i]", dom);
+			assert.deepEqual(matches, dom[0].children);
+			matches = CSSselect.selectAll("[data-foo$=\"MaTtEr.\" i]", dom);
+			assert.deepEqual(matches, dom[0].children);
+		});
+
+		it("should for !=", function(){
+			var matches = CSSselect.selectAll("[data-foo!=\"indeed-that's a delicate matter.\" i]", dom);
+			assert.equal(matches.length, 1);
+			assert.deepEqual(matches, [dom[0].children[0]]);
+			matches = CSSselect.selectAll("[data-foo!=\"inDeeD-THAT's a DELICATE matteR.\" i]", dom);
+			assert.deepEqual(matches, [dom[0].children[0]]);
+		});
+
+		it("should for *=", function(){
+			var matches = CSSselect.selectAll("[data-foo*=IT i]", dom);
+			assert.equal(matches.length, 1);
+			assert.deepEqual(matches, [dom[0].children[0]]);
+			matches = CSSselect.selectAll("[data-foo*=tH i]", dom);
+			assert.deepEqual(matches, dom[0].children);
+		});
+
+		it("should for |=", function(){
+			var matches = CSSselect.selectAll("[data-foo|=indeed i]", dom);
+			assert.equal(matches.length, 1);
+			assert.deepEqual(matches, [dom[0].children[1]]);
+			matches = CSSselect.selectAll("[data-foo|=inDeeD i]", dom);
+			assert.deepEqual(matches, [dom[0].children[1]]);
+		});
+
+		it("should for ~=", function(){
+			var matches = CSSselect.selectAll("[data-foo~=IT i]", dom);
+			assert.equal(matches.length, 1);
+			assert.deepEqual(matches, [dom[0].children[0]]);
+			matches = CSSselect.selectAll("[data-foo~=dElIcAtE i]", dom);
+			assert.deepEqual(matches, [dom[0].children[1]]);
+		});
+	});
+
+	describe("no matches", function(){
+		it("should for ~=", function(){
+			assert.equal(CSSselect._compileUnsafe("[foo~='baz bar']"), falseFunc);
+		});
+
+		it("should for $=", function(){
+			assert.equal(CSSselect._compileUnsafe("[foo$='']"), falseFunc);
+		});
+
+		it("should for *=", function(){
+			assert.equal(CSSselect._compileUnsafe("[foo*='']"), falseFunc);
+		});
+	});
+});
\ No newline at end of file
diff --git a/test/icontains.js b/test/icontains.js
new file mode 100644
index 0000000..748c764
--- /dev/null
+++ b/test/icontains.js
@@ -0,0 +1,86 @@
+var CSSselect = require("../"),
+    makeDom = require("htmlparser2").parseDOM,
+    assert = require("assert");
+
+var dom = makeDom("<div><p>In the end, it doesn't really Matter.</p><div>Indeed-that's a delicate matter.</div>");
+
+describe("icontains", function(){
+	describe("ignore case", function(){
+		it("should match full string", function(){
+			var matches = CSSselect.selectAll(":icontains(indeed-that's a delicate matter.)", dom);
+			assert.equal(matches.length, 2);
+			assert.deepEqual(matches, [dom[0], dom[0].children[1]]);
+			matches = CSSselect.selectAll(":icontains(inDeeD-THAT's a DELICATE matteR.)", dom);
+			assert.equal(matches.length, 2);
+			assert.deepEqual(matches, [dom[0], dom[0].children[1]]);
+		});
+
+		it("should match substring", function(){
+			var matches = CSSselect.selectAll(":icontains(indeed)", dom);
+			assert.equal(matches.length, 2);
+			assert.deepEqual(matches, [dom[0], dom[0].children[1]]);
+			matches = CSSselect.selectAll(":icontains(inDeeD)", dom);
+			assert.equal(matches.length, 2);
+			assert.deepEqual(matches, [dom[0], dom[0].children[1]]);
+		});
+
+		it("should match specific element", function(){
+			var matches = CSSselect.selectAll("p:icontains(matter)", dom);
+			assert.equal(matches.length, 1);
+			assert.deepEqual(matches, [dom[0].children[0]]);
+			matches = CSSselect.selectAll("p:icontains(mATter)", dom);
+			assert.equal(matches.length, 1);
+			assert.deepEqual(matches, [dom[0].children[0]]);
+		});
+
+		it("should match multiple elements", function(){
+			var matches = CSSselect.selectAll(":icontains(matter)", dom);
+			assert.equal(matches.length, 3);
+			assert.deepEqual(matches, [dom[0], dom[0].children[0],
+                dom[0].children[1]]);
+			matches = CSSselect.selectAll(":icontains(mATter)", dom);
+			assert.equal(matches.length, 3);
+			assert.deepEqual(matches, [dom[0], dom[0].children[0],
+                dom[0].children[1]]);
+		});
+
+		it("should match empty string", function(){
+            var matches = CSSselect.selectAll(":icontains()", dom);
+			assert.equal(matches.length, 3);
+			assert.deepEqual(matches, [dom[0], dom[0].children[0],
+                dom[0].children[1]]);
+		});
+
+		it("should match quoted string", function(){
+            var matches = CSSselect.selectAll(":icontains('')", dom);
+			assert.equal(matches.length, 3);
+			assert.deepEqual(matches, [dom[0], dom[0].children[0],
+                dom[0].children[1]]);
+            matches = CSSselect.selectAll("p:icontains('matter')", dom);
+			assert.equal(matches.length, 1);
+			assert.deepEqual(matches, [dom[0].children[0]]);
+            matches = CSSselect.selectAll("p:icontains(\"matter\")", dom);
+			assert.equal(matches.length, 1);
+			assert.deepEqual(matches, [dom[0].children[0]]);
+		});
+
+		it("should match whitespace", function(){
+			var matches = CSSselect.selectAll(":icontains( matter)", dom);
+			assert.equal(matches.length, 3);
+			assert.deepEqual(matches, [dom[0], dom[0].children[0],
+                dom[0].children[1]]);
+			matches = CSSselect.selectAll(":icontains( mATter)", dom);
+			assert.equal(matches.length, 3);
+			assert.deepEqual(matches, [dom[0], dom[0].children[0],
+                dom[0].children[1]]);
+		});
+    });
+
+	describe("no matches", function(){
+		it("should not match", function(){
+            var matches = CSSselect.selectAll("p:icontains(indeed)", dom);
+			assert.equal(matches.length, 0);
+		});
+
+    });
+});
diff --git a/test/mocha.opts b/test/mocha.opts
new file mode 100644
index 0000000..af53e24
--- /dev/null
+++ b/test/mocha.opts
@@ -0,0 +1,2 @@
+--check-leaks
+--reporter spec
diff --git a/test/nwmatcher/LICENSE b/test/nwmatcher/LICENSE
new file mode 100644
index 0000000..d7ac905
--- /dev/null
+++ b/test/nwmatcher/LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2007-2013 Diego Perini (http://www.iport.it)
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
diff --git a/test/nwmatcher/index.js b/test/nwmatcher/index.js
new file mode 100644
index 0000000..a478b03
--- /dev/null
+++ b/test/nwmatcher/index.js
@@ -0,0 +1,467 @@
+/*
+	taken from https://github.com/dperini/nwmatcher/blob/master/test/scotch/test.js
+*/
+
+var DomUtils = require("htmlparser2").DomUtils,
+	helper = require("../tools/helper.js"),
+	assert = require("assert"),
+	path = require("path"),
+	document = helper.getDocument(path.join(__dirname, "test.html")),
+	CSSselect = helper.CSSselect;
+
+//Prototype's `$` function
+function getById(element){
+	if(arguments.length === 1){
+		if(typeof element === "string"){
+			return DomUtils.getElementById(element, document);
+		}
+		return element;
+	}
+	else return Array.prototype.map.call(arguments, function(elem){
+			return getById(elem);
+		});
+}
+
+//NWMatcher methods
+var select = function(query, doc){
+	if(arguments.length === 1 || typeof doc === "undefined") doc = document;
+	else if(typeof doc === "string") doc = select(doc);
+	return CSSselect(query, doc);
+}, match = CSSselect.is;
+
+var validators = {
+	assert: assert,
+	assertEqual: assert.equal,
+	assertEquivalent: assert.deepEqual,
+	refute: function refute(a, msg){
+		assert(!a, msg);
+	},
+	assertThrowsException: function(){} //not implemented
+};
+
+var runner = {
+	__name: "",
+	addGroup: function(name){
+		this.__name = name;
+		return this;
+	},
+	addTests: function(_, tests){
+		if(this.__name){
+			describe(this.__name, run);
+			this.__name = "";
+		} else run();
+
+		function run(){
+			Object.keys(tests).forEach(function(name){
+				it(name, function(){
+					tests[name].call(validators);
+				});
+			});
+		}
+	}
+}
+
+var RUN_BENCHMARKS = false;
+//The tests...
+(function(runner){
+	runner.addGroup("Basic Selectors").addTests(null, {
+		"*": function(){
+			//Universal selector
+			var results = [], nodes = document.getElementsByTagName("*"), index = 0, length = nodes.length, node;
+			//Collect all element nodes, excluding comments (IE)
+			for(; index < length; index++){
+				if((node = nodes[index]).tagName !== "!"){
+					results[results.length] = node;
+				}
+			}
+			this.assertEquivalent(select("*"), results, "Comment nodes should be ignored.");
+		},
+		"E": function(){
+			//Type selector
+			var results = [], index = 0, nodes = document.getElementsByTagName("li");
+			while((results[index] = nodes[index++])){}
+			results.length--;
+			//  this.assertEquivalent(select("li"), results); //TODO
+			this.assertEqual(select("strong", getById("fixtures"))[0], getById("strong"));
+			this.assertEquivalent(select("nonexistent"), []);
+		},
+		"#id": function(){
+			//ID selector
+			this.assertEqual(select("#fixtures")[0], getById("fixtures"));
+			this.assertEquivalent(select("nonexistent"), []);
+			this.assertEqual(select("#troubleForm")[0], getById("troubleForm"));
+		},
+		".class": function(){
+			//Class selector
+			this.assertEquivalent(select(".first"), getById('p', 'link_1', 'item_1'));
+			this.assertEquivalent(select(".second"), []);
+		},
+		"E#id": function(){
+			this.assertEqual(select("strong#strong")[0], getById("strong"));
+			this.assertEquivalent(select("p#strong"), []);
+		},
+		"E.class": function(){
+			var secondLink = getById("link_2");
+			this.assertEquivalent(select('a.internal'), getById('link_1', 'link_2'));
+			this.assertEqual(select('a.internal.highlight')[0], secondLink);
+			this.assertEqual(select('a.highlight.internal')[0], secondLink);
+			this.assertEquivalent(select('a.highlight.internal.nonexistent'), []);
+		},
+		"#id.class": function(){
+			var secondLink = getById('link_2');
+			this.assertEqual(select('#link_2.internal')[0], secondLink);
+			this.assertEqual(select('.internal#link_2')[0], secondLink);
+			this.assertEqual(select('#link_2.internal.highlight')[0], secondLink);
+			this.assertEquivalent(select('#link_2.internal.nonexistent'), []);
+		},
+		"E#id.class": function(){
+			var secondLink = getById('link_2');
+			this.assertEqual(select('a#link_2.internal')[0], secondLink);
+			this.assertEqual(select('a.internal#link_2')[0], secondLink);
+			this.assertEqual(select('li#item_1.first')[0], getById("item_1"));
+			this.assertEquivalent(select('li#item_1.nonexistent'), []);
+			this.assertEquivalent(select('li#item_1.first.nonexistent'), []);
+		}
+	});
+
+	runner.addGroup("Attribute Selectors").addTests(null, {
+		"[foo]": function(){
+			this.assertEquivalent(select('[href]', document.body), select('a[href]', document.body));
+			this.assertEquivalent(select('[class~=internal]'), select('a[class~="internal"]'));
+			this.assertEquivalent(select('[id]'), select('*[id]'));
+			this.assertEquivalent(select('[type=radio]'), getById('checked_radio', 'unchecked_radio'));
+			this.assertEquivalent(select('[type=checkbox]'), select('*[type=checkbox]'));
+			this.assertEquivalent(select('[title]'), getById('with_title', 'commaParent'));
+			this.assertEquivalent(select('#troubleForm [type=radio]'), select('#troubleForm *[type=radio]'));
+			this.assertEquivalent(select('#troubleForm [type]'), select('#troubleForm *[type]'));
+		},
+		"E[foo]": function(){
+			this.assertEquivalent(select('h1[class]'), select('#fixtures h1'), "h1[class]");
+			this.assertEquivalent(select('h1[CLASS]'), select('#fixtures h1'), "h1[CLASS]");
+			this.assertEqual(select('li#item_3[class]')[0], getById('item_3'), "li#item_3[class]");
+			this.assertEquivalent(select('#troubleForm2 input[name="brackets[5][]"]'), getById('chk_1', 'chk_2'));
+			//Brackets in attribute value
+			this.assertEqual(select('#troubleForm2 input[name="brackets[5][]"]:checked')[0], getById('chk_1'));
+			//Space in attribute value
+			this.assertEqual(select('cite[title="hello world!"]')[0], getById('with_title'));
+			//Namespaced attributes
+			//  this.assertEquivalent(select('[xml:lang]'), [document.documentElement, getById("item_3")]);
+			//  this.assertEquivalent(select('*[xml:lang]'), [document.documentElement, getById("item_3")]);
+		},
+		'E[foo="bar"]': function(){
+			this.assertEquivalent(select('a[href="#"]'), getById('link_1', 'link_2', 'link_3'));
+			this.assertThrowsException(/Error/, function(){
+				select('a[href=#]');
+			});
+			this.assertEqual(select('#troubleForm2 input[name="brackets[5][]"][value="2"]')[0], getById('chk_2'));
+		},
+		'E[foo~="bar"]': function(){
+			this.assertEquivalent(select('a[class~="internal"]'), getById('link_1', 'link_2'), "a[class~=\"internal\"]");
+			this.assertEquivalent(select('a[class~=internal]'), getById('link_1', 'link_2'), "a[class~=internal]");
+			this.assertEqual(select('a[class~=external][href="#"]')[0], getById('link_3'), 'a[class~=external][href="#"]');
+		},/*
+		'E[foo|="en"]': function(){
+			this.assertEqual(select('*[xml:lang|="es"]')[0], getById('item_3'));
+			this.assertEqual(select('*[xml:lang|="ES"]')[0], getById('item_3'));
+		},*/
+		'E[foo^="bar"]': function(){
+			this.assertEquivalent(select('div[class^=bro]'), getById('father', 'uncle'), 'matching beginning of string');
+			this.assertEquivalent(select('#level1 *[id^="level2_"]'), getById('level2_1', 'level2_2', 'level2_3'));
+			this.assertEquivalent(select('#level1 *[id^=level2_]'), getById('level2_1', 'level2_2', 'level2_3'));
+			if(RUN_BENCHMARKS){
+				this.wait(function(){
+					this.benchmark(function(){
+						select('#level1 *[id^=level2_]');
+					}, 1000);
+				}, 500);
+			}
+		},
+		'E[foo$="bar"]': function(){
+			this.assertEquivalent(select('div[class$=men]'), getById('father', 'uncle'), 'matching end of string');
+			this.assertEquivalent(select('#level1 *[id$="_1"]'), getById('level2_1', 'level3_1'));
+			this.assertEquivalent(select('#level1 *[id$=_1]'), getById('level2_1', 'level3_1'));
+			if(RUN_BENCHMARKS){
+				this.wait(function(){
+					this.benchmark(function(){
+						select('#level1 *[id$=_1]');
+					}, 1000);
+				}, 500);
+			}
+		},
+		'E[foo*="bar"]': function(){
+			this.assertEquivalent(select('div[class*="ers m"]'), getById('father', 'uncle'), 'matching substring');
+			this.assertEquivalent(select('#level1 *[id*="2"]'), getById('level2_1', 'level3_2', 'level2_2', 'level2_3'));
+			this.assertThrowsException(/Error/, function(){
+				select('#level1 *[id*=2]');
+			});
+			if(RUN_BENCHMARKS){
+				this.wait(function(){
+					this.benchmark(function(){
+						select('#level1 *[id*=2]');
+					}, 1000);
+				}, 500);
+			}
+		},
+
+	// *** these should throw SYNTAX_ERR ***
+
+		'E[id=-1]': function(){
+			this.assertThrowsException(/Error/, function(){
+				select('#level1 *[id=-1]');
+			});
+			if(RUN_BENCHMARKS){
+				this.wait(function(){
+					this.benchmark(function(){
+						select('#level1 *[id=9]');
+					}, 1000);
+				}, 500);
+			}
+		},
+		'E[class=-45deg]': function(){
+			this.assertThrowsException(/Error/, function(){
+				select('#level1 *[class=-45deg]');
+			});
+			if(RUN_BENCHMARKS){
+				this.wait(function(){
+					this.benchmark(function(){
+						select('#level1 *[class=-45deg]');
+					}, 1000);
+				}, 500);
+			}
+		},
+		'E[class=8mm]': function(){
+			this.assertThrowsException(/Error/, function(){
+				select('#level1 *[class=8mm]');
+			});
+			if(RUN_BENCHMARKS){
+				this.wait(function(){
+					this.benchmark(function(){
+						select('#level1 *[class=8mm]');
+					}, 1000);
+				}, 500);
+			}
+		}
+
+	});
+
+	runner.addGroup("Structural pseudo-classes").addTests(null, {
+		"E:first-child": function(){
+			this.assertEqual(select('#level1>*:first-child')[0], getById('level2_1'));
+			this.assertEquivalent(select('#level1 *:first-child'), getById('level2_1', 'level3_1', 'level_only_child'));
+			this.assertEquivalent(select('#level1>div:first-child'), []);
+			this.assertEquivalent(select('#level1 span:first-child'), getById('level2_1', 'level3_1'));
+			this.assertEquivalent(select('#level1:first-child'), []);
+			if(RUN_BENCHMARKS){
+				this.wait(function(){
+					this.benchmark(function(){
+						select('#level1 *:first-child');
+					}, 1000);
+				}, 500);
+			}
+		},
+		"E:last-child": function(){
+			this.assertEqual(select('#level1>*:last-child')[0], getById('level2_3'));
+			this.assertEquivalent(select('#level1 *:last-child'), getById('level3_2', 'level_only_child', 'level2_3'));
+			this.assertEqual(select('#level1>div:last-child')[0], getById('level2_3'));
+			this.assertEqual(select('#level1 div:last-child')[0], getById('level2_3'));
+			this.assertEquivalent(select('#level1>span:last-child'), []);
+			if(RUN_BENCHMARKS){
+				this.wait(function(){
+					this.benchmark(function(){
+						select('#level1 *:last-child');
+					}, 1000);
+				}, 500);
+			}
+		},
+		"E:nth-child(n)": function(){
+			this.assertEqual(select('#p *:nth-child(3)')[0], getById('link_2'));
+			this.assertEqual(select('#p a:nth-child(3)')[0], getById('link_2'), 'nth-child');
+			this.assertEquivalent(select('#list > li:nth-child(n+2)'), getById('item_2', 'item_3'));
+			this.assertEquivalent(select('#list > li:nth-child(-n+2)'), getById('item_1', 'item_2'));
+		},
+		"E:nth-of-type(n)": function(){
+			this.assertEqual(select('#p a:nth-of-type(2)')[0], getById('link_2'), 'nth-of-type');
+			this.assertEqual(select('#p a:nth-of-type(1)')[0], getById('link_1'), 'nth-of-type');
+		},
+		"E:nth-last-of-type(n)": function(){
+			this.assertEqual(select('#p a:nth-last-of-type(1)')[0], getById('link_2'), 'nth-last-of-type');
+		},
+		"E:first-of-type": function(){
+			this.assertEqual(select('#p a:first-of-type')[0], getById('link_1'), 'first-of-type');
+		},
+		"E:last-of-type": function(){
+			this.assertEqual(select('#p a:last-of-type')[0], getById('link_2'), 'last-of-type');
+		},
+		"E:only-child": function(){
+			this.assertEqual(select('#level1 *:only-child')[0], getById('level_only_child'));
+			//Shouldn't return anything
+			this.assertEquivalent(select('#level1>*:only-child'), []);
+			this.assertEquivalent(select('#level1:only-child'), []);
+			this.assertEquivalent(select('#level2_2 :only-child:not(:last-child)'), []);
+			this.assertEquivalent(select('#level2_2 :only-child:not(:first-child)'), []);
+			if(RUN_BENCHMARKS){
+				this.wait(function(){
+					this.benchmark(function(){
+						select('#level1 *:only-child');
+					}, 1000);
+				}, 500);
+			}
+		},
+		"E:empty": function(){
+			getById('level3_1').children = [];
+			if(document.createEvent){
+				this.assertEquivalent(select('#level1 *:empty'), getById('level3_1', 'level3_2', 'level2_3'), '#level1 *:empty');
+				this.assertEquivalent(select('#level_only_child:empty'), [], 'newlines count as content!');
+			}else{
+				this.assertEqual(select('#level3_1:empty')[0], getById('level3_1'), 'IE forced empty content!');
+				//this.skip("IE forced empty content!");
+			}
+			//Shouldn't return anything
+			this.assertEquivalent(select('span:empty > *'), []);
+		}
+	});
+
+	runner.addTests(null, {
+		"E:not(s)": function(){
+			//Negation pseudo-class
+			this.assertEquivalent(select('a:not([href="#"])'), []);
+			this.assertEquivalent(select('div.brothers:not(.brothers)'), []);
+			this.assertEquivalent(select('a[class~=external]:not([href="#"])'), [], 'a[class~=external][href!="#"]');
+			this.assertEqual(select('#p a:not(:first-of-type)')[0], getById('link_2'), 'first-of-type');
+			this.assertEqual(select('#p a:not(:last-of-type)')[0], getById('link_1'), 'last-of-type');
+			this.assertEqual(select('#p a:not(:nth-of-type(1))')[0], getById('link_2'), 'nth-of-type');
+			this.assertEqual(select('#p a:not(:nth-last-of-type(1))')[0], getById('link_1'), 'nth-last-of-type');
+			this.assertEqual(select('#p a:not([rel~=nofollow])')[0], getById('link_2'), 'attribute 1');
+			this.assertEqual(select('#p a:not([rel^=external])')[0], getById('link_2'), 'attribute 2');
+			this.assertEqual(select('#p a:not([rel$=nofollow])')[0], getById('link_2'), 'attribute 3');
+			this.assertEqual(select('#p a:not([rel$="nofollow"]) > em')[0], getById('em'), 'attribute 4');
+			this.assertEqual(select('#list li:not(#item_1):not(#item_3)')[0], getById('item_2'), 'adjacent :not clauses');
+			this.assertEqual(select('#grandfather > div:not(#uncle) #son')[0], getById('son'));
+			this.assertEqual(select('#p a:not([rel$="nofollow"]) em')[0], getById('em'), 'attribute 4 + all descendants');
+			this.assertEqual(select('#p a:not([rel$="nofollow"])>em')[0], getById('em'), 'attribute 4 (without whitespace)');
+		}
+	});
+
+	runner.addGroup("UI element states pseudo-classes").addTests(null, {
+		"E:disabled": function(){
+			this.assertEqual(select('#troubleForm > p > *:disabled')[0], getById('disabled_text_field'));
+		},
+		"E:checked": function(){
+			this.assertEquivalent(select('#troubleForm *:checked'), getById('checked_box', 'checked_radio'));
+		}
+	});
+
+	runner.addGroup("Combinators").addTests(null, {
+		"E F": function(){
+			//Descendant
+			this.assertEquivalent(select('#fixtures a *'), getById('em2', 'em', 'span'));
+			this.assertEqual(select('div#fixtures p')[0], getById("p"));
+		},
+		"E + F": function(){
+			//Adjacent sibling
+			this.assertEqual(select('div.brothers + div.brothers')[0], getById("uncle"));
+			this.assertEqual(select('div.brothers + div')[0], getById('uncle'));
+			this.assertEqual(select('#level2_1+span')[0], getById('level2_2'));
+			this.assertEqual(select('#level2_1 + span')[0], getById('level2_2'));
+			this.assertEqual(select('#level2_1 + *')[0], getById('level2_2'));
+			this.assertEquivalent(select('#level2_2 + span'), []);
+			this.assertEqual(select('#level3_1 + span')[0], getById('level3_2'));
+			this.assertEqual(select('#level3_1 + *')[0], getById('level3_2'));
+			this.assertEquivalent(select('#level3_2 + *'), []);
+			this.assertEquivalent(select('#level3_1 + em'), []);
+			if(RUN_BENCHMARKS){
+				this.wait(function(){
+					this.benchmark(function(){
+						select('#level3_1 + span');
+					}, 1000);
+				}, 500);
+			}
+		},
+		"E > F": function(){
+			//Child
+			this.assertEquivalent(select('p.first > a'), getById('link_1', 'link_2'));
+			this.assertEquivalent(select('div#grandfather > div'), getById('father', 'uncle'));
+			this.assertEquivalent(select('#level1>span'), getById('level2_1', 'level2_2'));
+			this.assertEquivalent(select('#level1 > span'), getById('level2_1', 'level2_2'));
+			this.assertEquivalent(select('#level2_1 > *'), getById('level3_1', 'level3_2'));
+			this.assertEquivalent(select('div > #nonexistent'), []);
+			if(RUN_BENCHMARKS){
+				this.wait(function(){
+					this.benchmark(function(){
+						select('#level1 > span');
+					}, 1000);
+				}, 500);
+			}
+		},
+		"E ~ F": function(){
+			//General sibling
+			this.assertEqual(select('h1 ~ ul')[0], getById('list'));
+			this.assertEquivalent(select('#level2_2 ~ span'), []);
+			this.assertEquivalent(select('#level3_2 ~ *'), []);
+			this.assertEquivalent(select('#level3_1 ~ em'), []);
+			this.assertEquivalent(select('div ~ #level3_2'), []);
+			this.assertEquivalent(select('div ~ #level2_3'), []);
+			this.assertEqual(select('#level2_1 ~ span')[0], getById('level2_2'));
+			this.assertEquivalent(select('#level2_1 ~ *'), getById('level2_2', 'level2_3'));
+			this.assertEqual(select('#level3_1 ~ #level3_2')[0], getById('level3_2'));
+			this.assertEqual(select('span ~ #level3_2')[0], getById('level3_2'));
+			if(RUN_BENCHMARKS){
+				this.wait(function(){
+					this.benchmark(function(){
+						select('#level2_1 ~ span');
+					}, 1000);
+				}, 500);
+			}
+		}
+	});
+
+	runner.addTests(null, {
+		"NW.Dom.match": function(){
+			var element = getById('dupL1');
+			//Assertions
+			this.assert(match(element, 'span'));
+			this.assert(match(element, "span#dupL1"));
+			this.assert(match(element, "div > span"), "child combinator");
+			this.assert(match(element, "#dupContainer span"), "descendant combinator");
+			this.assert(match(element, "#dupL1"), "ID only");
+			this.assert(match(element, "span.span_foo"), "class name 1");
+			this.assert(match(element, "span.span_bar"), "class name 2");
+			this.assert(match(element, "span:first-child"), "first-child pseudoclass");
+			//Refutations
+			this.refute(match(element, "span.span_wtf"), "bogus class name");
+			this.refute(match(element, "#dupL2"), "different ID");
+			this.refute(match(element, "div"), "different tag name");
+			this.refute(match(element, "span span"), "different ancestry");
+			this.refute(match(element, "span > span"), "different parent");
+			this.refute(match(element, "span:nth-child(5)"), "different pseudoclass");
+			//Misc.
+			this.refute(match(getById('link_2'), 'a[rel^=external]'));
+			this.assert(match(getById('link_1'), 'a[rel^=external]'));
+			this.assert(match(getById('link_1'), 'a[rel^="external"]'));
+			this.assert(match(getById('link_1'), "a[rel^='external']"));
+		},
+		"Equivalent Selectors": function(){
+			this.assertEquivalent(select('div.brothers'), select('div[class~=brothers]'));
+			this.assertEquivalent(select('div.brothers'), select('div[class~=brothers].brothers'));
+			this.assertEquivalent(select('div:not(.brothers)'), select('div:not([class~=brothers])'));
+			this.assertEquivalent(select('li ~ li'), select('li:not(:first-child)'));
+			this.assertEquivalent(select('ul > li'), select('ul > li:nth-child(n)'));
+			this.assertEquivalent(select('ul > li:nth-child(even)'), select('ul > li:nth-child(2n)'));
+			this.assertEquivalent(select('ul > li:nth-child(odd)'), select('ul > li:nth-child(2n+1)'));
+			this.assertEquivalent(select('ul > li:first-child'), select('ul > li:nth-child(1)'));
+			this.assertEquivalent(select('ul > li:last-child'), select('ul > li:nth-last-child(1)'));
+			/* Opera 10 does not accept values > 128 as a parameter to :nth-child
+			See <http://operawiki.info/ArtificialLimits> */
+			this.assertEquivalent(select('ul > li:nth-child(n-128)'), select('ul > li'));
+			this.assertEquivalent(select('ul>li'), select('ul > li'));
+			this.assertEquivalent(select('#p a:not([rel$="nofollow"])>em'), select('#p a:not([rel$="nofollow"]) > em'));
+		},
+		"Multiple Selectors": function(){
+			//The next two assertions should return document-ordered lists of matching elements --Diego Perini
+			//  this.assertEquivalent(select('#list, .first,*[xml:lang="es-us"] , #troubleForm'), getById('p', 'link_1', 'list', 'item_1', 'item_3', 'troubleForm'));
+			//  this.assertEquivalent(select('#list, .first, *[xml:lang="es-us"], #troubleForm'), getById('p', 'link_1', 'list', 'item_1', 'item_3', 'troubleForm'));
+			this.assertEquivalent(select('form[title*="commas,"], input[value="#commaOne,#commaTwo"]'), getById('commaParent', 'commaChild'));
+			this.assertEquivalent(select('form[title*="commas,"], input[value="#commaOne,#commaTwo"]'), getById('commaParent', 'commaChild'));
+		}
+	});
+}(runner));
diff --git a/test/nwmatcher/test.html b/test/nwmatcher/test.html
new file mode 100644
index 0000000..f74daba
--- /dev/null
+++ b/test/nwmatcher/test.html
@@ -0,0 +1,92 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+  <title>NWMatcher Tests</title>
+  <link rel="stylesheet" type="text/css" href="assets/style.css" media="screen" />
+  <script type="text/javascript" src="../../src/nwmatcher.js"></script>
+  <script type="text/javascript" src="scotch.js"></script>
+  <script type="text/javascript" src="test.js"></script>
+</head>
+<body>
+  <div id="container">
+    <div id="testlog" class="log"></div>
+    <!-- Test elements -->
+    <div id="fixtures" style="display: none;">
+      <h1 class="title">Some title <span>here</span></h1>
+      <p id="p" class="first summary">
+        <strong id="strong">This</strong> is a short blurb
+        <a id="link_1" class="first internal" rel="external nofollow" href="#">with a <em id="em2">link</em></a> or
+        <a id="link_2" class="internal highlight" href="#"><em id="em">two</em></a>.
+        Or <cite id="with_title" title="hello world!">a citation</cite>.
+      </p>
+      <ul id="list">
+        <li id="item_1" class="first"><a id="link_3" href="#" class="external"><span id="span">Another link</span></a></li>
+        <li id="item_2">Some text</li>
+        <li id="item_3" xml:lang="es-us" class="">Otra cosa</li>
+      </ul>
+
+      <!-- This form has a field with the name "id"; its "ID" property won't be "troubleForm" -->
+      <form id="troubleForm" action="">
+        <p>
+          <input type="hidden" name="id" id="hidden" />
+          <input type="text" name="disabled_text_field" id="disabled_text_field" disabled="disabled" />
+          <input type="text" name="enabled_text_field" id="enabled_text_field" />
+          <input type="checkbox" name="checkboxes" id="checked_box" checked="checked" value="Checked" />
+          <input type="checkbox" name="checkboxes" id="unchecked_box" value="Unchecked"/>
+          <input type="radio" name="radiobuttons" id="checked_radio" checked="checked" value="Checked" />
+          <input type="radio" name="radiobuttons" id="unchecked_radio" value="Unchecked" />
+        </p>
+      </form>
+
+      <form id="troubleForm2" action="">
+        <p>
+          <input type="checkbox" name="brackets[5][]" id="chk_1" checked="checked" value="1" />
+          <input type="checkbox" name="brackets[5][]" id="chk_2" value="2" />
+        </p>
+      </form>
+
+      <div id="level1">
+        <span id="level2_1">
+          <span id="level3_1"></span>
+          <!-- This comment should be ignored by the adjacent selector -->
+          <span id="level3_2"></span>
+        </span>
+        <span id="level2_2">
+          <em id="level_only_child">
+          </em>
+        </span>
+        <div id="level2_3"></div>
+      </div> <!-- #level1 -->
+
+      <div id="dupContainer">
+        <span id="dupL1" class="span_foo span_bar">
+          <span id="dupL2">
+            <span id="dupL3">
+              <span id="dupL4">
+                <span id="dupL5"></span>
+              </span>
+            </span>
+          </span>
+        </span>
+      </div> <!-- #dupContainer -->
+
+      <div id="grandfather"> grandfather
+        <div id="father" class="brothers men"> father
+          <div id="son"> son </div>
+        </div>
+        <div id="uncle" class="brothers men"> uncle </div>
+      </div>
+
+      <form id="commaParent" title="commas,are,good" action="">
+        <p>
+          <input type="hidden" id="commaChild" name="foo" value="#commaOne,#commaTwo" />
+          <input type="hidden" id="commaTwo" name="foo2" value="oops" />
+        </p>
+      </form>
+      <div id="counted_container"><div class="is_counted"></div></div>
+    </div>
+  </div>
+</body>
+</html>
diff --git a/test/qwery/index.html b/test/qwery/index.html
new file mode 100644
index 0000000..d7bfb28
--- /dev/null
+++ b/test/qwery/index.html
@@ -0,0 +1,132 @@
+<!DOCTYPE HTML>
+<html lang="en-us">
+  <head>
+    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
+    <title>Qwery tests</title>
+    <style type="text/css">
+      #fixtures {
+        position: absolute;
+        top: -9999px;
+      }
+    </style>
+    <link rel="stylesheet" href="../node_modules/sink-test/src/sink.css" type="text/css">
+    <script src="../node_modules/sink-test/src/sink.js"></script>
+    <script src="../src/qwery.js"></script>
+    <script src="../pseudos/qwery-pseudos.js"></script>
+    <script type="text/javascript">
+      var Q = qwery
+    </script>
+  </head>
+  <body>
+    <h1>Qwery Tests</h1>
+    <div id="fixtures">
+      <ol id="list">
+        <li>hello</li>
+        <li>world</li>
+        <ol>
+          <li>world</li>
+          <li id="attr-child-boosh" attr="boosh">hello</li>
+        </ol>
+        <li>humans</li>
+      </ol>
+      <div id="spaced-tokens">
+        <p><em><a href="#"></a></em></p>
+        <p></p>
+      </div>
+      <div id="pseudos">
+        <div class="odd pseudos pseudo-1"></div>
+        <div class="even pseudos pseudo-2"></div>
+        <div class="odd"></div>
+        <div class="even"></div>
+        <a class="odd"></a>
+        <div class="even"></div>
+        <div class="odd"></div>
+      </div>
+      <div foo="bar"></div>
+      <div class="a"></div>
+      <div class="class-with-dashes"></div>
+      <div id="boosh">
+        <!-- comment -->
+        <!-- comment -->
+        <div class="a b">
+          <div class="d e" test="fg" id="booshTest"></div>
+          <!-- comment -->
+          <em nopass="copyrighters" rel="copyright booshrs" test="f g"></em>
+          <span class="h i a"></span>
+        </div>
+        <!-- comment -->
+      </div>
+      <div id="lonelyBoosh"></div>
+      <div id="attr-test1" -data-attr></div>
+      <div id="attr-test2" -data-attr></div>
+      <div id="attr-test3" class="found you" -data-attr title="whatup duders"></div>
+      <div id="attributes">
+        <div test="one" unique-test="baz" id="attr-test-1"></div>
+        <div test="two-foo" id="attr-test-2"></div>
+        <div test=" three " id="attr-test-3"></div>
+        <a href="#aname" id="attr-test-4">aname</a>
+      </div>
+      <div class="idless">
+        <div class="tokens" title="one" id="token-one"></div>
+        <div class="tokens" title="one two" id="token-two"></div>
+        <div class="tokens" title="one two three #%" id="token-three">
+          <a href="foo"  id="token-four">
+            <div id="token-five"></div>
+          </a>
+        </div>
+      </div>
+      <div id="order-matters" class="order-matters">
+        <p class="order-matters"></p>
+        <a class="order-matters">
+          <em class="order-matters"></em><b class="order-matters"></b>
+        </a>
+      </div>
+      <div id="direct-descend" class="oogabooga">
+        <div></div>
+        <div class="direct-descend">
+          <span></span>
+          <div class="direct-descend">
+            <div class="lvl2" id="toodeep"><span></span></div>
+          </div>
+	  <div class="direct-descend"><span></span></div>
+          <div class="lvl2" id="l2">
+            <span></span>
+            <div class="direct-descend"><span></span></div>
+          </div>
+          <div class="lvl2" id="l3"></div>
+        </div>
+        <div class="ignoreme"></div>
+        <div class="direct-descend">
+          <div class="direct-descend"></div>
+          <div class="lvl2" id="l4"></div>
+        </div>
+        <div></div>
+      </div>
+      <div id="sibling-selector"></div>
+      <div class="sibling-selector" id="sib1">
+       <div class="sibling-selector"></div>
+       <div class="sibling-selector"></div>
+      </div>
+      <div class="sibling-selector" id="sib2">
+       <div class="sibling-selector">
+         <div class="sibling-selector"></div>
+       </div>
+      </div>
+      <div class="parent">
+        <h1 class="sibling oldest"></h1>
+        <h2 class="sibling older"></h2>
+        <h3 class="sibling middle"></h3>
+        <h4 class="sibling younger"></h4>
+        <h5 class="sibling youngest"></h5>
+      </div>
+      <form>
+        <button></button>
+        <input type="text">
+        <input type="hidden">
+      </form>
+    </div>
+     <ol id="tests"></ol>
+     <iframe id="frame" style="width: 0; height: 0; margin-left: -1000px;"></iframe>
+     <script src="tests.js"></script>
+  </body>
+</html>
diff --git a/test/qwery/index.js b/test/qwery/index.js
new file mode 100644
index 0000000..3ac3500
--- /dev/null
+++ b/test/qwery/index.js
@@ -0,0 +1,549 @@
+"use strict";
+
+var expect = require("expect.js"),
+    DomUtils = require("htmlparser2").DomUtils,
+	helper = require("../tools/helper.js"),
+	path = require("path"),
+	document = helper.getDocument(path.join(__dirname, "index.html")),
+	CSSselect = helper.CSSselect;
+
+var location = {hash: ""};
+CSSselect.pseudos.target = function(elem){
+	return elem.attribs && elem.attribs.id === location.hash.substr(1);
+};
+
+//---
+
+/*
+	The following is taken from https://github.com/ded/qwery/blob/master/tests/tests.js
+*/
+
+CSSselect.pseudos.humanoid = function(e) { return CSSselect.is(e, 'li:contains(human)') || CSSselect.is(e, 'ol:contains(human)'); };
+
+var frag = helper.getDOM(
+	'<root><div class="d i v">' +
+		'<p id="oooo"><em></em><em id="emem"></em></p>' +
+	'</div>' +
+	'<p id="sep">' +
+		'<div class="a"><span></span></div>' +
+	'</p></root>'
+);
+
+var doc = helper.getDOM(
+	'<root><div id="hsoob">' +
+		'<div class="a b">' +
+			'<div class="d e sib" test="fg" id="booshTest"><p><span id="spanny"></span></p></div>' +
+			'<em nopass="copyrighters" rel="copyright booshrs" test="f g" class="sib"></em>' +
+			'<span class="h i a sib"></span>' +
+		'</div>' +
+		'<p class="odd"></p>' +
+	'</div>' +
+	'<div id="lonelyHsoob"></div></root>'
+);
+
+var el = DomUtils.getElementById('attr-child-boosh', document);
+
+var pseudos = DomUtils.getElementById('pseudos', document).children.filter(DomUtils.isTag);
+
+module.exports = {
+
+'Contexts': {
+
+	'should be able to pass optional context': function () {
+		expect(CSSselect('.a', document)).to.have.length(3); //no context found 3 elements (.a)
+		expect(CSSselect('.a', CSSselect('#boosh', document))).to.have.length(2); //context found 2 elements (#boosh .a)
+	},
+
+/*
+	'should be able to pass string as context': function() {
+		expect(CSSselect('.a', '#boosh')).to.have.length(2); //context found 2 elements(.a, #boosh)
+		expect(CSSselect('.a', '.a')).to.be.empty(); //context found 0 elements(.a, .a)
+		expect(CSSselect('.a', '.b')).to.have.length(1); //context found 1 elements(.a, .b)
+		expect(CSSselect('.a', '#boosh .b')).to.have.length(1); //context found 1 elements(.a, #boosh .b)
+		expect(CSSselect('.b', '#boosh .b')).to.be.empty(); //context found 0 elements(.b, #boosh .b)
+	},
+*/
+/*
+	'should be able to pass qwery result as context': function() {
+		expect(CSSselect('.a', CSSselect('#boosh', document))).to.have.length(2); //context found 2 elements(.a, #boosh)
+		expect(CSSselect('.a', CSSselect('.a', document))).to.be.empty(); //context found 0 elements(.a, .a)
+		expect(CSSselect('.a', CSSselect('.b', document))).to.have.length(1); //context found 1 elements(.a, .b)
+		expect(CSSselect('.a', CSSselect('#boosh .b', document))).to.have.length(1); //context found 1 elements(.a, #boosh .b)
+		expect(CSSselect('.b', CSSselect('#boosh .b', document))).to.be.empty(); //context found 0 elements(.b, #boosh .b)
+	},
+*/
+	'should not return duplicates from combinators': function () {
+		expect(CSSselect('#boosh,#boosh', document)).to.have.length(1); //two booshes dont make a thing go right
+		expect(CSSselect('#boosh,.apples,#boosh', document)).to.have.length(1); //two booshes and an apple dont make a thing go right
+	},
+
+	'byId sub-queries within context': function() {
+		expect(CSSselect('#booshTest', CSSselect('#boosh', document))).to.have.length(1); //found "#id #id"
+		expect(CSSselect('.a.b #booshTest', CSSselect('#boosh', document))).to.have.length(1); //found ".class.class #id"
+		expect(CSSselect('.a>#booshTest', CSSselect('#boosh', document))).to.have.length(1); //found ".class>#id"
+		expect(CSSselect('>.a>#booshTest', CSSselect('#boosh', document))).to.have.length(1); //found ">.class>#id"
+		expect(CSSselect('#boosh', CSSselect('#booshTest', document)).length).to.not.be.ok(); //shouldn't find #boosh (ancestor) within #booshTest (descendent)
+		expect(CSSselect('#boosh', CSSselect('#lonelyBoosh', document)).length).to.not.be.ok(); //shouldn't find #boosh within #lonelyBoosh (unrelated)
+	}
+},
+
+'CSS 1': {
+	'get element by id': function () {
+		var result = CSSselect('#boosh', document);
+		expect(result[0]).to.be.ok(); //found element with id=boosh
+		expect(CSSselect('h1', document)[0]).to.be.ok(); //found 1 h1
+	},
+
+	'byId sub-queries': function() {
+		expect(CSSselect('#boosh #booshTest', document)).to.have.length(1); //found "#id #id"
+		expect(CSSselect('.a.b #booshTest', document)).to.have.length(1); //found ".class.class #id"
+		expect(CSSselect('#boosh>.a>#booshTest', document)).to.have.length(1); //found "#id>.class>#id"
+		expect(CSSselect('.a>#booshTest', document)).to.have.length(1); //found ".class>#id"
+	},
+
+	'get elements by class': function () {
+		expect(CSSselect('#boosh .a', document)).to.have.length(2); //found two elements
+		expect(CSSselect('#boosh div.a', document)[0]).to.be.ok(); //found one element
+		expect(CSSselect('#boosh div', document)).to.have.length(2); //found two {div} elements
+		expect(CSSselect('#boosh span', document)[0]).to.be.ok(); //found one {span} element
+		expect(CSSselect('#boosh div div', document)[0]).to.be.ok(); //found a single div
+		expect(CSSselect('a.odd', document)).to.have.length(1); //found single a
+	},
+
+	'combos': function () {
+		expect(CSSselect('#boosh div,#boosh span', document)).to.have.length(3); //found 2 divs and 1 span
+	},
+
+	'class with dashes': function() {
+		expect(CSSselect('.class-with-dashes', document)).to.have.length(1); //found something
+	},
+
+	'should ignore comment nodes': function() {
+		expect(CSSselect('#boosh *', document)).to.have.length(4); //found only 4 elements under #boosh
+	},
+
+	'deep messy relationships': function() {
+		// these are mostly characterised by a combination of tight relationships and loose relationships
+		// on the right side of the query it's easy to find matches but they tighten up quickly as you
+		// go to the left
+		// they are useful for making sure the dom crawler doesn't stop short or over-extend as it works
+		// up the tree the crawl needs to be comprehensive
+		expect(CSSselect('div#fixtures > div a', document)).to.have.length(5); //found four results for "div#fixtures > div a"
+		expect(CSSselect('.direct-descend > .direct-descend .lvl2', document)).to.have.length(1); //found one result for ".direct-descend > .direct-descend .lvl2"
+		expect(CSSselect('.direct-descend > .direct-descend div', document)).to.have.length(1); //found one result for ".direct-descend > .direct-descend div"
+		expect(CSSselect('.direct-descend > .direct-descend div', document)).to.have.length(1); //found one result for ".direct-descend > .direct-descend div"
+		expect(CSSselect('div#fixtures div ~ a div', document)).to.be.empty(); //found no results for odd query
+		expect(CSSselect('.direct-descend > .direct-descend > .direct-descend ~ .lvl2', document)).to.be.empty(); //found no results for another odd query
+	}
+},
+
+'CSS 2': {
+
+	'get elements by attribute': function () {
+		var wanted = CSSselect('#boosh div[test]', document)[0];
+		var expected = DomUtils.getElementById('booshTest', document);
+		expect(wanted).to.be(expected); //found attribute
+		expect(CSSselect('#boosh div[test=fg]', document)[0]).to.be(expected); //found attribute with value
+		expect(CSSselect('em[rel~="copyright"]', document)).to.have.length(1); //found em[rel~="copyright"]
+		expect(CSSselect('em[nopass~="copyright"]', document)).to.be.empty(); //found em[nopass~="copyright"]
+	},
+
+	'should not throw error by attribute selector': function () {
+		expect(CSSselect('[foo^="bar"]', document)).to.have.length(1); //found 1 element
+	},
+
+	'crazy town': function () {
+		var el = DomUtils.getElementById('attr-test3', document);
+		expect(CSSselect('div#attr-test3.found.you[title="whatup duders"]', document)[0]).to.be(el); //found the right element
+	}
+
+},
+
+'attribute selectors': {
+
+	/* CSS 2 SPEC */
+
+	'[attr]': function () {
+		var expected = DomUtils.getElementById('attr-test-1', document);
+		expect(CSSselect('#attributes div[unique-test]', document)[0]).to.be(expected); //found attribute with [attr]
+	},
+
+	'[attr=val]': function () {
+		var expected = DomUtils.getElementById('attr-test-2', document);
+		expect(CSSselect('#attributes div[test="two-foo"]', document)[0]).to.be(expected); //found attribute with =
+		expect(CSSselect("#attributes div[test='two-foo']", document)[0]).to.be(expected); //found attribute with =
+		expect(CSSselect('#attributes div[test=two-foo]', document)[0]).to.be(expected); //found attribute with =
+	},
+
+	'[attr~=val]': function () {
+		var expected = DomUtils.getElementById('attr-test-3', document);
+		expect(CSSselect('#attributes div[test~=three]', document)[0]).to.be(expected); //found attribute with ~=
+	},
+
+	'[attr|=val]': function () {
+		var expected = DomUtils.getElementById('attr-test-2', document);
+		expect(CSSselect('#attributes div[test|="two-foo"]', document)[0]).to.be(expected); //found attribute with |=
+		expect(CSSselect('#attributes div[test|=two]', document)[0]).to.be(expected); //found attribute with |=
+	},
+
+	'[href=#x] special case': function () {
+		var expected = DomUtils.getElementById('attr-test-4', document);
+		expect(CSSselect('#attributes a[href="#aname"]', document)[0]).to.be(expected); //found attribute with href=#x
+	},
+
+	/* CSS 3 SPEC */
+
+	'[attr^=val]': function () {
+		var expected = DomUtils.getElementById('attr-test-2', document);
+		expect(CSSselect('#attributes div[test^=two]', document)[0]).to.be(expected); //found attribute with ^=
+	},
+
+	'[attr$=val]': function () {
+		var expected = DomUtils.getElementById('attr-test-2', document);
+		expect(CSSselect('#attributes div[test$=foo]', document)[0]).to.be(expected); //found attribute with $=
+	},
+
+	'[attr*=val]': function () {
+		var expected = DomUtils.getElementById('attr-test-3', document);
+		expect(CSSselect('#attributes div[test*=hree]', document)[0]).to.be(expected); //found attribute with *=
+	},
+
+	'direct descendants': function () {
+		expect(CSSselect('#direct-descend > .direct-descend', document)).to.have.length(2); //found two direct descendents
+		expect(CSSselect('#direct-descend > .direct-descend > .lvl2', document)).to.have.length(3); //found three second-level direct descendents
+	},
+
+	'sibling elements': function () {
+		expect(CSSselect('#sibling-selector ~ .sibling-selector', document)).to.have.length(2); //found two siblings
+		expect(CSSselect('#sibling-selector ~ div.sibling-selector', document)).to.have.length(2); //found two siblings
+		expect(CSSselect('#sibling-selector + div.sibling-selector', document)).to.have.length(1); //found one sibling
+		expect(CSSselect('#sibling-selector + .sibling-selector', document)).to.have.length(1); //found one sibling
+
+		expect(CSSselect('.parent .oldest ~ .sibling', document)).to.have.length(4); //found four younger siblings
+		expect(CSSselect('.parent .middle ~ .sibling', document)).to.have.length(2); //found two younger siblings
+		expect(CSSselect('.parent .middle ~ h4', document)).to.have.length(1); //found next sibling by tag
+		expect(CSSselect('.parent .middle ~ h4.younger', document)).to.have.length(1); //found next sibling by tag and class
+		expect(CSSselect('.parent .middle ~ h3', document)).to.be.empty(); //an element can't be its own sibling
+		expect(CSSselect('.parent .middle ~ h2', document)).to.be.empty(); //didn't find an older sibling
+		expect(CSSselect('.parent .youngest ~ .sibling', document)).to.be.empty(); //found no younger siblings
+
+		expect(CSSselect('.parent .oldest + .sibling', document)).to.have.length(1); //found next sibling
+		expect(CSSselect('.parent .middle + .sibling', document)).to.have.length(1); //found next sibling
+		expect(CSSselect('.parent .middle + h4', document)).to.have.length(1); //found next sibling by tag
+		expect(CSSselect('.parent .middle + h3', document)).to.be.empty(); //an element can't be its own sibling
+		expect(CSSselect('.parent .middle + h2', document)).to.be.empty(); //didn't find an older sibling
+		expect(CSSselect('.parent .youngest + .sibling', document)).to.be.empty(); //found no younger siblings
+	}
+
+},
+
+/*
+'Uniq': {
+	'duplicates arent found in arrays': function () {
+		expect(CSSselect.uniq(['a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'd', 'e'])).to.have.length(5); //result should be a, b, c, d, e
+		expect(CSSselect.uniq(['a', 'b', 'c', 'c', 'c'])).to.have.length(3); //result should be a, b, c
+	}
+},
+*/
+
+
+'element-context queries': {
+
+/*
+	'relationship-first queries': function() {
+		expect(CSSselect('> .direct-descend', CSSselect('#direct-descend', document))).to.have.length(2); //found two direct descendents using > first
+		expect(CSSselect('~ .sibling-selector', CSSselect('#sibling-selector', document))).to.have.length(2); //found two siblings with ~ first
+		expect(CSSselect('+ .sibling-selector', CSSselect('#sibling-selector', document))).to.have.length(1); //found one sibling with + first
+		expect(CSSselect('> .tokens a', CSSselect('.idless', document)[0])).to.have.length(1); //found one sibling from a root with no id
+	},
+*/
+
+	// should be able to query on an element that hasn't been inserted into the dom
+	'detached fragments': function() {
+		expect(CSSselect('.a span', frag)).to.have.length(1); //should find child elements of fragment
+		expect(CSSselect('> div p em', frag)).to.have.length(2); //should find child elements of fragment, relationship first
+	},
+
+	'byId sub-queries within detached fragment': function () {
+		expect(CSSselect('#emem', frag)).to.have.length(1); //found "#id" in fragment
+		expect(CSSselect('.d.i #emem', frag)).to.have.length(1); //found ".class.class #id" in fragment
+		expect(CSSselect('.d #oooo #emem', frag)).to.have.length(1); //found ".class #id #id" in fragment
+		expect(CSSselect('> div #oooo', frag)).to.have.length(1); //found "> .class #id" in fragment
+		expect(CSSselect('#oooo', CSSselect('#emem', frag)).length).to.not.be.ok(); //shouldn't find #oooo (ancestor) within #emem (descendent)
+		expect(CSSselect('#sep', CSSselect('#emem', frag)).length).to.not.be.ok(); //shouldn't find #sep within #emem (unrelated)
+	},
+
+
+	'exclude self in match': function() {
+		expect(CSSselect('.order-matters', CSSselect('#order-matters', document)[0])).to.have.length(4); //should not include self in element-context queries
+	},
+
+
+	// because form's have .length
+	'forms can be used as contexts': function() {
+		expect(CSSselect('*', CSSselect('form', document)[0])).to.have.length(3); //found 3 elements under <form>
+	}
+},
+
+'tokenizer': {
+
+	'should not get weird tokens': function () {
+		expect(CSSselect('div .tokens[title="one"]', document)[0]).to.be(DomUtils.getElementById('token-one', document)); //found div .tokens[title="one"]
+		expect(CSSselect('div .tokens[title="one two"]', document)[0]).to.be(DomUtils.getElementById('token-two', document)); //found div .tokens[title="one two"]
+		expect(CSSselect('div .tokens[title="one two three #%"]', document)[0]).to.be(DomUtils.getElementById('token-three', document)); //found div .tokens[title="one two three #%"]
+		expect(CSSselect("div .tokens[title='one two three #%'] a", document)[0]).to.be(DomUtils.getElementById('token-four', document)); //found div .tokens[title=\'one two three #%\'] a
+		expect(CSSselect('div .tokens[title="one two three #%"] a[href$=foo] div', document)[0]).to.be(DomUtils.getElementById('token-five', document)); //found div .tokens[title="one two three #%"] a[href=foo] div
+	}
+
+},
+
+'interesting syntaxes': {
+	'should parse bad selectors': function () {
+		expect(CSSselect('#spaced-tokens    p    em    a', document).length).to.be.ok(); //found element with funny tokens
+	}
+},
+
+'order matters': {
+
+	// <div id="order-matters">
+	//   <p class="order-matters"></p>
+	//   <a class="order-matters">
+	//     <em class="order-matters"></em><b class="order-matters"></b>
+	//   </a>
+	// </div>
+
+	'the order of elements return matters': function () {
+		function tag(el) {
+			return el.name.toLowerCase();
+		}
+		var els = CSSselect('#order-matters .order-matters', document);
+		expect(tag(els[0])).to.be('p'); //first element matched is a {p} tag
+		expect(tag(els[1])).to.be('a'); //first element matched is a {a} tag
+		expect(tag(els[2])).to.be('em'); //first element matched is a {em} tag
+		expect(tag(els[3])).to.be('b'); //first element matched is a {b} tag
+	}
+
+},
+
+'pseudo-selectors': {
+	':contains': function() {
+		expect(CSSselect('li:contains(humans)', document)).to.have.length(1); //found by "element:contains(text)"
+		expect(CSSselect(':contains(humans)', document)).to.have.length(5); //found by ":contains(text)", including all ancestors
+		// * is an important case, can cause weird errors
+		expect(CSSselect('*:contains(humans)', document)).to.have.length(5); //found by "*:contains(text)", including all ancestors
+		expect(CSSselect('ol:contains(humans)', document)).to.have.length(1); //found by "ancestor:contains(text)"
+	},
+
+	':not': function() {
+		expect(CSSselect('.odd:not(div)', document)).to.have.length(1); //found one .odd :not an <a>
+	},
+
+	':first-child': function () {
+		expect(CSSselect('#pseudos div:first-child', document)[0]).to.be(pseudos[0]); //found first child
+		expect(CSSselect('#pseudos div:first-child', document)).to.have.length(1); //found only 1
+	},
+
+	':last-child': function () {
+		var all = DomUtils.getElementsByTagName('div', pseudos);
+		expect(CSSselect('#pseudos div:last-child', document)[0]).to.be(all[all.length - 1]); //found last child
+		expect(CSSselect('#pseudos div:last-child', document)).to.have.length(1); //found only 1
+	},
+
+	'ol > li[attr="boosh"]:last-child': function () {
+		var expected = DomUtils.getElementById('attr-child-boosh', document);
+		expect(CSSselect('ol > li[attr="boosh"]:last-child', document)).to.have.length(1); //only 1 element found
+		expect(CSSselect('ol > li[attr="boosh"]:last-child', document)[0]).to.be(expected); //found correct element
+	},
+
+	':nth-child(odd|even|x)': function () {
+		var second = DomUtils.getElementsByTagName('div', pseudos)[1];
+		expect(CSSselect('#pseudos :nth-child(odd)', document)).to.have.length(4); //found 4 odd elements
+		expect(CSSselect('#pseudos div:nth-child(odd)', document)).to.have.length(3); //found 3 odd elements with div tag
+		expect(CSSselect('#pseudos div:nth-child(even)', document)).to.have.length(3); //found 3 even elements with div tag
+		expect(CSSselect('#pseudos div:nth-child(2)', document)[0]).to.be(second); //found 2nd nth-child of pseudos
+	},
+
+	':nth-child(expr)': function () {
+		var fifth = DomUtils.getElementsByTagName('a', pseudos)[0];
+		var sixth = DomUtils.getElementsByTagName('div', pseudos)[4];
+
+		expect(CSSselect('#pseudos :nth-child(3n+1)', document)).to.have.length(3); //found 3 elements
+		expect(CSSselect('#pseudos :nth-child(+3n-2)', document)).to.have.length(3); //found 3 elements'
+		expect(CSSselect('#pseudos :nth-child(-n+6)', document)).to.have.length(6); //found 6 elements
+		expect(CSSselect('#pseudos :nth-child(-n+5)', document)).to.have.length(5); //found 5 elements
+		expect(CSSselect('#pseudos :nth-child(3n+2)', document)[1]).to.be(fifth); //second :nth-child(3n+2) is the fifth child
+		expect(CSSselect('#pseudos :nth-child(3n)', document)[1]).to.be(sixth); //second :nth-child(3n) is the sixth child
+	},
+
+	':nth-last-child(odd|even|x)': function () {
+		var second = DomUtils.getElementsByTagName('div', pseudos)[1];
+		expect(CSSselect('#pseudos :nth-last-child(odd)', document)).to.have.length(4); //found 4 odd elements
+		expect(CSSselect('#pseudos div:nth-last-child(odd)', document)).to.have.length(3); //found 3 odd elements with div tag
+		expect(CSSselect('#pseudos div:nth-last-child(even)', document)).to.have.length(3); //found 3 even elements with div tag
+		expect(CSSselect('#pseudos div:nth-last-child(6)', document)[0]).to.be(second); //6th nth-last-child should be 2nd of 7 elements
+	},
+
+	':nth-last-child(expr)': function () {
+		var third = DomUtils.getElementsByTagName('div', pseudos)[2];
+
+		expect(CSSselect('#pseudos :nth-last-child(3n+1)', document)).to.have.length(3); //found 3 elements
+		expect(CSSselect('#pseudos :nth-last-child(3n-2)', document)).to.have.length(3); //found 3 elements
+		expect(CSSselect('#pseudos :nth-last-child(-n+6)', document)).to.have.length(6); //found 6 elements
+		expect(CSSselect('#pseudos :nth-last-child(-n+5)', document)).to.have.length(5); //found 5 elements
+		expect(CSSselect('#pseudos :nth-last-child(3n+2)', document)[0]).to.be(third); //first :nth-last-child(3n+2) is the third child
+	},
+
+	':nth-of-type(expr)': function () {
+		var a = DomUtils.getElementsByTagName('a', pseudos)[0];
+
+		expect(CSSselect('#pseudos div:nth-of-type(3n+1)', document)).to.have.length(2); //found 2 div elements
+		expect(CSSselect('#pseudos a:nth-of-type(3n+1)', document)).to.have.length(1); //found 1 a element
+		expect(CSSselect('#pseudos a:nth-of-type(3n+1)', document)[0]).to.be(a); //found the right a element
+		expect(CSSselect('#pseudos a:nth-of-type(3n)', document)).to.be.empty(); //no matches for every third a
+		expect(CSSselect('#pseudos a:nth-of-type(odd)', document)).to.have.length(1); //found the odd a
+		expect(CSSselect('#pseudos a:nth-of-type(1)', document)).to.have.length(1); //found the first a
+	},
+
+	':nth-last-of-type(expr)': function () {
+		var second = DomUtils.getElementsByTagName('div', pseudos)[1];
+
+		expect(CSSselect('#pseudos div:nth-last-of-type(3n+1)', document)).to.have.length(2); //found 2 div elements
+		expect(CSSselect('#pseudos a:nth-last-of-type(3n+1)', document)).to.have.length(1); //found 1 a element
+		expect(CSSselect('#pseudos div:nth-last-of-type(5)', document)[0]).to.be(second); //5th nth-last-of-type should be 2nd of 7 elements
+	},
+
+	':first-of-type': function () {
+		expect(CSSselect('#pseudos a:first-of-type', document)[0]).to.be(DomUtils.getElementsByTagName('a', pseudos)[0]); //found first a element
+		expect(CSSselect('#pseudos a:first-of-type', document)).to.have.length(1); //found only 1
+	},
+
+	':last-of-type': function () {
+		var all = DomUtils.getElementsByTagName('div', pseudos);
+		expect(CSSselect('#pseudos div:last-of-type', document)[0]).to.be(all[all.length - 1]); //found last div element
+		expect(CSSselect('#pseudos div:last-of-type', document)).to.have.length(1); //found only 1
+	},
+
+	':only-of-type': function () {
+		expect(CSSselect('#pseudos a:only-of-type', document)[0]).to.be(DomUtils.getElementsByTagName('a', pseudos)[0]); //found the only a element
+		expect(CSSselect('#pseudos a:first-of-type', document)).to.have.length(1); //found only 1
+	},
+
+	':target': function () {
+		location.hash = '';
+		expect(CSSselect('#pseudos:target', document)).to.be.empty(); //#pseudos is not the target
+		location.hash = '#pseudos';
+		expect(CSSselect('#pseudos:target', document)).to.have.length(1); //now #pseudos is the target
+		location.hash = '';
+	},
+
+	'custom pseudos': function() {
+		// :humanoid implemented just for testing purposes
+		expect(CSSselect(':humanoid', document)).to.have.length(2); //selected using custom pseudo
+	}
+
+},
+
+/*
+'argument types': {
+
+	'should be able to pass in nodes as arguments': function () {
+		var el = DomUtils.getElementById('boosh', document);
+		expect(CSSselect(el)[0]).to.be(el); //CSSselect(el)[0] == el
+		expect(CSSselect(el, 'body')[0]).to.be(el); //CSSselect(el, 'body')[0] == el
+		expect(CSSselect(el, document)[0]).to.be(el); //CSSselect(el, document)[0] == el
+		expect(CSSselect(window)[0]).to.be(window); //CSSselect(window)[0] == window
+		expect(CSSselect(document)[0]).to.be(document); //CSSselect(document)[0] == document
+	},
+
+	'should be able to pass in an array of results as arguments': function () {
+		var el = DomUtils.getElementById('boosh', document);
+		var result = CSSselect([CSSselect('#boosh', document), CSSselect(document), CSSselect(window)]);
+		expect(result).to.have.length(3); //3 elements in the combined set
+		expect(result[0]).to.be(el); //result[0] == el
+		expect(result[1]).to.be(document); //result[0] == document
+		expect(result[2]).to.be(window); //result[0] == window
+		expect(CSSselect([CSSselect('#pseudos div.odd', document), CSSselect('#pseudos div.even', document)])).to.have.length(6); //found all the odd and even divs
+	}
+
+},
+*/
+
+'is()': {
+	'simple selectors': function () {
+		expect(CSSselect.is(el, 'li')).to.be.ok(); //tag
+		expect(CSSselect.is(el, '*')).to.be.ok(); //wildcard
+		expect(CSSselect.is(el, '#attr-child-boosh')).to.be.ok(); //#id
+		expect(CSSselect.is(el, '[attr]')).to.be.ok(); //[attr]
+		expect(CSSselect.is(el, '[attr=boosh]')).to.be.ok(); //[attr=val]
+		expect(CSSselect.is(el, 'div')).to.not.be.ok(); //wrong tag
+		expect(CSSselect.is(el, '#foo')).to.not.be.ok(); //wrong #id
+		expect(CSSselect.is(el, '[foo]')).to.not.be.ok(); //wrong [attr]
+		expect(CSSselect.is(el, '[attr=foo]')).to.not.be.ok(); //wrong [attr=val]
+	},
+	'selector sequences': function () {
+		expect(CSSselect.is(el, 'li#attr-child-boosh[attr=boosh]')).to.be.ok(); //tag#id[attr=val]
+		expect(CSSselect.is(el, 'div#attr-child-boosh[attr=boosh]')).to.not.be.ok(); //wrong tag#id[attr=val]
+	},
+	'selector sequences combinators': function () {
+		expect(CSSselect.is(el, 'ol li')).to.be.ok(); //tag tag
+		expect(CSSselect.is(el, 'ol>li')).to.be.ok(); //tag>tag
+		expect(CSSselect.is(el, 'ol>li+li')).to.be.ok(); //tab>tag+tag
+		expect(CSSselect.is(el, 'ol#list li#attr-child-boosh[attr=boosh]')).to.be.ok(); //tag#id tag#id[attr=val]
+		expect(CSSselect.is(el, 'ol#list>li#attr-child-boosh[attr=boosh]')).to.not.be.ok(); //wrong tag#id>tag#id[attr=val]
+		expect(CSSselect.is(el, 'ol ol li#attr-child-boosh[attr=boosh]')).to.be.ok(); //tag tag tag#id[attr=val]
+		expect(CSSselect.is(CSSselect('#token-four', document)[0], 'div#fixtures>div a')).to.be.ok(); //tag#id>tag tag where ambiguous middle tag requires backtracking
+	},
+	'pseudos': function() {
+		//TODO: more tests!
+		expect(CSSselect.is(el, 'li:contains(hello)')).to.be.ok(); //matching :contains(text)
+		expect(CSSselect.is(el, 'li:contains(human)')).to.not.be.ok(); //non-matching :contains(text)
+		expect(CSSselect.is(CSSselect('#list>li', document)[2], ':humanoid')).to.be.ok(); //matching custom pseudo
+		expect(CSSselect.is(CSSselect('#list>li', document)[1], ':humanoid')).to.not.be.ok(); //non-matching custom pseudo
+	},
+	'context': function () {
+		expect(CSSselect.is(el, 'li#attr-child-boosh[attr=boosh]', {context: CSSselect('#list', document)[0]})).to.be.ok(); //context
+		expect(CSSselect.is(el, 'ol#list li#attr-child-boosh[attr=boosh]', {context: CSSselect('#boosh', document)[0]})).to.not.be.ok(); //wrong context
+	}
+},
+
+'selecting elements in other documents': {
+	'get element by id': function () {
+		var result = CSSselect('#hsoob', doc);
+		expect(result[0]).to.be.ok(); //found element with id=hsoob
+	},
+
+	'get elements by class': function () {
+		expect(CSSselect('#hsoob .a', doc)).to.have.length(2); //found two elements
+		expect(CSSselect('#hsoob div.a', doc)[0]).to.be.ok(); //found one element
+		expect(CSSselect('#hsoob div', doc)).to.have.length(2); //found two {div} elements
+		expect(CSSselect('#hsoob span', doc)[0]).to.be.ok(); //found one {span} element
+		expect(CSSselect('#hsoob div div', doc)[0]).to.be.ok(); //found a single div
+		expect(CSSselect('p.odd', doc)).to.have.length(1); //found single br
+	},
+
+	'complex selectors': function () {
+		expect(CSSselect('.d ~ .sib', doc)).to.have.length(2); //found one ~ sibling
+		expect(CSSselect('.a .d + .sib', doc)).to.have.length(1); //found 2 + siblings
+		expect(CSSselect('#hsoob > div > .h', doc)).to.have.length(1); //found span using child selectors
+		expect(CSSselect('.a .d ~ .sib[test="f g"]', doc)).to.have.length(1); //found 1 ~ sibling with test attribute
+	},
+
+	'byId sub-queries': function () {
+		expect(CSSselect('#hsoob #spanny', doc)).to.have.length(1); //found "#id #id" in frame
+		expect(CSSselect('.a #spanny', doc)).to.have.length(1); //found ".class #id" in frame
+		expect(CSSselect('.a #booshTest #spanny', doc)).to.have.length(1); //found ".class #id #id" in frame
+		expect(CSSselect('> #hsoob', doc)).to.have.length(1) //found "> #id" in frame
+	},
+
+	'byId sub-queries within sub-context': function () {
+		expect(CSSselect('#spanny', CSSselect('#hsoob', doc))).to.have.length(1); //found "#id -> #id" in frame
+		expect(CSSselect('.a #spanny', CSSselect('#hsoob', doc))).to.have.length(1); //found ".class #id" in frame
+		expect(CSSselect('.a #booshTest #spanny', CSSselect('#hsoob', doc))).to.have.length(1); //found ".class #id #id" in frame
+		expect(CSSselect('.a > #booshTest', CSSselect('#hsoob', doc))).to.have.length(1); //found "> .class #id" in frame
+		expect(CSSselect('#booshTest', CSSselect('#spanny', doc)).length).to.not.be.ok(); //shouldn't find #booshTest (ancestor) within #spanny (descendent)
+		expect(CSSselect('#booshTest', CSSselect('#lonelyHsoob', doc)).length).to.not.be.ok(); //shouldn't find #booshTest within #lonelyHsoob (unrelated)
+	}
+
+}
+
+};
diff --git a/test/sizzle/data/fries.xml b/test/sizzle/data/fries.xml
new file mode 100644
index 0000000..8528575
--- /dev/null
+++ b/test/sizzle/data/fries.xml
@@ -0,0 +1 @@
+<?xml version=1.0 encoding=UTF-8?> <soap:Envelope xmlns:soap=http://schemas.xmlsoap.org/soap/envelope/ xmlns:xsd=http://www.w3.org/2001/XMLSchema xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance> <soap:Body> <jsconf xmlns=http://www.example.com/ns1> <response xmlns:ab=http://www.example.com/ns2> <meta> <component id=seite1 class=component> <properties xmlns:cd=http://www.example.com/ns3> <property name=prop1> <thing /> <value>1</value> </property> <property name=prop2> <thing att=some [...]
diff --git a/test/sizzle/data/index.html b/test/sizzle/data/index.html
new file mode 100755
index 0000000..ad7db53
--- /dev/null
+++ b/test/sizzle/data/index.html
@@ -0,0 +1,247 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr" id="html">
+<head>
+	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+	<title>Sizzle Test Suite</title>
+	<link rel="Stylesheet" media="screen" href="../bower_components/qunit/qunit/qunit.css" />
+	<script type="text/javascript" src="../bower_components/qunit/qunit/qunit.js"></script>
+	<script type="text/javascript" src="data/testinit.js"></script>
+	<script type="text/javascript" src="jquery.js"></script>
+	<script type="text/javascript" src="../dist/sizzle.js"></script>
+	<script type="text/javascript" src="unit/selector.js"></script>
+	<script type="text/javascript" src="unit/utilities.js"></script>
+	<script type="text/javascript" src="unit/extending.js"></script>
+</head>
+
+<body id="body">
+	<div id="qunit">
+		<h1 id="qunit-header">jQuery Test Suite</h1>
+		<h2 id="qunit-banner"></h2>
+		<div id="qunit-testrunner-toolbar"></div>
+		<h2 id="qunit-userAgent"></h2>
+	</div>
+
+	<!-- Test HTML -->
+	<dl id="dl" style="position:absolute;top:-32767px;left:-32767px;width:1px">
+	<div id="qunit-fixture">
+		<p id="firstp">See <a id="simon1" href="http://simon.incutio.com/archive/2003/03/25/#getElementsBySelector" rel="bookmark">this blog entry</a> for more information.</p>
+		<p id="ap">
+			Here are some [links] in a normal paragraph: <a id="google" href="http://www.google.com/" title="Google!">Google</a>,
+			<a id="groups" href="http://groups.google.com/" class="GROUPS">Google Groups (Link)</a>.
+			This link has <code id="code1"><a href="http://smin" id="anchor1">class="blog"</a></code>:
+			<a href="http://diveintomark.org/" class="blog" hreflang="en" id="mark">diveintomark</a>
+
+		</p>
+		<div id="foo">
+			<p id="sndp">Everything inside the red border is inside a div with <code>id="foo"</code>.</p>
+			<p lang="en" id="en">This is a normal link: <a id="yahoo" href="http://www.yahoo.com/" class="blogTest">Yahoo</a></p>
+			<p id="sap">This link has <code><a href="#2" id="anchor2">class="blog"</a></code>: <a href="http://simon.incutio.com/" class="blog link" id="simon">Simon Willison's Weblog</a></p>
+
+		</div>
+		<div id="nothiddendiv" style="height:1px;background:white;" class="nothiddendiv">
+			<div id="nothiddendivchild"></div>
+		</div>
+		<span id="name+value"></span>
+		<p id="first">Try them out:</p>
+		<ul id="firstUL"></ul>
+		<ol id="empty"><!-- comment --></ol>
+		<form id="form" action="formaction">
+			<label for="action" id="label-for">Action:</label>
+			<input type="text" name="action" value="Test" id="text1" maxlength="30"/>
+			<input type="text" name="text2" value="Test" id="text2" disabled="disabled"/>
+			<input type="radio" name="radio1" id="radio1" value="on"/>
+
+			<input type="radio" name="radio2" id="radio2" checked="checked"/>
+			<input type="checkbox" name="check" id="check1" checked="checked"/>
+			<input type="checkbox" id="check2" value="on"/>
+
+			<input type="hidden" name="hidden" id="hidden1"/>
+			<input type="text" style="display:none;" name="foo[bar]" id="hidden2"/>
+
+			<input type="text" id="name" name="name" value="name" />
+			<input type="search" id="search" name="search" value="search" />
+
+			<button id="button" name="button" type="button">Button</button>
+
+			<textarea id="area1" maxlength="30">foobar</textarea>
+
+			<select name="select1" id="select1">
+				<option id="option1a" class="emptyopt" value="">Nothing</option>
+				<option id="option1b" value="1">1</option>
+				<option id="option1c" value="2">2</option>
+				<option id="option1d" value="3">3</option>
+			</select>
+			<select name="select2" id="select2">
+				<option id="option2a" class="emptyopt" value="">Nothing</option>
+				<option id="option2b" value="1">1</option>
+				<option id="option2c" value="2">2</option>
+				<option id="option2d" selected="selected" value="3">3</option>
+			</select>
+			<select name="select3" id="select3" multiple="multiple">
+				<option id="option3a" class="emptyopt" value="">Nothing</option>
+				<option id="option3b" selected="selected" value="1">1</option>
+				<option id="option3c" selected="selected" value="2">2</option>
+				<option id="option3d" value="3">3</option>
+				<option id="option3e">no value</option>
+			</select>
+			<select name="select4" id="select4" multiple="multiple">
+				<optgroup disabled="disabled">
+					<option id="option4a" class="emptyopt" value="">Nothing</option>
+					<option id="option4b" disabled="disabled" selected="selected" value="1">1</option>
+					<option id="option4c" selected="selected" value="2">2</option>
+				</optgroup>
+				<option selected="selected" disabled="disabled" id="option4d" value="3">3</option>
+				<option id="option4e">no value</option>
+			</select>
+			<select name="select5" id="select5">
+				<option id="option5a" value="3">1</option>
+				<option id="option5b" value="2">2</option>
+				<option id="option5c" value="1">3</option>
+			</select>
+
+			<object id="object1" codebase="stupid">
+				<param name="p1" value="x1" />
+				<param name="p2" value="x2" />
+			</object>
+
+			<span id="台北Táiběi"></span>
+			<span id="台北" lang="中文"></span>
+			<span id="utf8class1" class="台北Táiběi 台北"></span>
+			<span id="utf8class2" class="台北"></span>
+			<span id="foo:bar" class="foo:bar"><span id="foo_descendent"></span></span>
+			<span id="test.foo[5]bar" class="test.foo[5]bar"></span>
+
+			<foo_bar id="foobar">test element</foo_bar>
+		</form>
+		<b id="floatTest">Float test.</b>
+		<iframe id="iframe" name="iframe"></iframe>
+		<form id="lengthtest">
+			<input type="text" id="length" name="test"/>
+			<input type="text" id="idTest" name="id"/>
+		</form>
+		<table id="table"></table>
+
+		<form id="name-tests">
+			<!-- Inputs with a grouped name attribute. -->
+			<input name="types[]" id="types_all" type="checkbox" value="all" />
+			<input name="types[]" id="types_anime" type="checkbox" value="anime" />
+			<input name="types[]" id="types_movie" type="checkbox" value="movie" />
+		</form>
+
+		<form id="testForm" action="#" method="get">
+			<textarea name="T3" rows="2" cols="15">?
+Z</textarea>
+			<input type="hidden" name="H1" value="x" />
+			<input type="hidden" name="H2" />
+			<input name="PWD" type="password" value="" />
+			<input name="T1" type="text" />
+			<input name="T2" type="text" value="YES" readonly="readonly" />
+			<input type="checkbox" name="C1" value="1" />
+			<input type="checkbox" name="C2" />
+			<input type="radio" name="R1" value="1" />
+			<input type="radio" name="R1" value="2" />
+			<input type="text" name="My Name" value="me" />
+			<input type="reset" name="reset" value="NO" />
+			<select name="S1">
+				<option value="abc">ABC</option>
+				<option value="abc">ABC</option>
+				<option value="abc">ABC</option>
+			</select>
+			<select name="S2" multiple="multiple" size="3">
+				<option value="abc">ABC</option>
+				<option value="abc">ABC</option>
+				<option value="abc">ABC</option>
+			</select>
+			<select name="S3">
+				<option selected="selected">YES</option>
+			</select>
+			<select name="S4">
+				<option value="" selected="selected">NO</option>
+			</select>
+			<input type="submit" name="sub1" value="NO" />
+			<input type="submit" name="sub2" value="NO" />
+			<input type="image" name="sub3" value="NO" />
+			<button name="sub4" type="submit" value="NO">NO</button>
+			<input name="D1" type="text" value="NO" disabled="disabled" />
+			<input type="checkbox" checked="checked" disabled="disabled" name="D2" value="NO" />
+			<input type="radio" name="D3" value="NO" checked="checked" disabled="disabled" />
+			<select name="D4" disabled="disabled">
+				<option selected="selected" value="NO">NO</option>
+			</select>
+			<input id="list-test" type="text" />
+			<datalist id="datalist">
+				<option value="option"></option>
+			</datalist>
+		</form>
+		<div id="moretests">
+			<form>
+				<div id="checkedtest" style="display:none;">
+					<input type="radio" name="checkedtestradios" checked="checked"/>
+					<input type="radio" name="checkedtestradios" value="on"/>
+					<input type="checkbox" name="checkedtestcheckboxes" checked="checked"/>
+					<input type="checkbox" name="checkedtestcheckboxes" />
+				</div>
+			</form>
+			<div id="nonnodes"><span>hi</span> there <!-- mon ami --></div>
+			<div id="t2037">
+				<div><div class="hidden">hidden</div></div>
+			</div>
+			<div id="t6652">
+				<div></div>
+			</div>
+			<div id="t12087">
+				<input type="hidden" id="el12087" data-comma="0,1"/>
+			</div>
+			<div id="no-clone-exception"><object><embed></embed></object></div>
+			<div id="names-group">
+				<span id="name-is-example" name="example"></span>
+				<span id="name-is-div" name="div"></span>
+			</div>
+			<script id="script-no-src"></script>
+			<script id="script-src" src="http://src.test/path"></script>
+			<div id="id-name-tests">
+				<a id="tName1ID" name="tName1"><span></span></a>
+				<a id="tName2ID" name="tName2"><span></span></a>
+				<div id="tName1"><span id="tName1-span">C</span></div>
+			</div>
+		</div>
+
+		<div id="tabindex-tests">
+			<ol id="listWithTabIndex" tabindex="5">
+				<li id="foodWithNegativeTabIndex" tabindex="-1">Rice</li>
+				<li id="foodNoTabIndex">Beans</li>
+				<li>Blinis</li>
+				<li>Tofu</li>
+			</ol>
+
+			<div id="divWithNoTabIndex">I'm hungry. I should...</div>
+			<span>...</span><a href="#" id="linkWithNoTabIndex">Eat lots of food</a><span>...</span> |
+			<span>...</span><a href="#" id="linkWithTabIndex" tabindex="2">Eat a little food</a><span>...</span> |
+			<span>...</span><a href="#" id="linkWithNegativeTabIndex" tabindex="-1">Eat no food</a><span>...</span>
+			<span>...</span><a id="linkWithNoHrefWithNoTabIndex">Eat a burger</a><span>...</span>
+			<span>...</span><a id="linkWithNoHrefWithTabIndex" tabindex="1">Eat some funyuns</a><span>...</span>
+			<span>...</span><a id="linkWithNoHrefWithNegativeTabIndex" tabindex="-1">Eat some funyuns</a><span>...</span>
+		</div>
+
+		<div id="liveHandlerOrder">
+			<span id="liveSpan1"><a href="#" id="liveLink1"></a></span>
+			<span id="liveSpan2"><a href="#" id="liveLink2"></a></span>
+		</div>
+
+		<div id="siblingTest">
+			<em id="siblingfirst">1</em>
+			<em id="siblingnext">2</em>
+			<em id="siblingthird">
+				<em id="siblingchild">
+					<em id="siblinggrandchild">
+						<em id="siblinggreatgrandchild"></em>
+					</em>
+				</em>
+			</em>
+			<span id="siblingspan"></span>
+		</div>​
+	</div>
+	</dl>
+	<br id="last"/>
+</body>
+</html>
diff --git a/test/sizzle/data/testinit.js b/test/sizzle/data/testinit.js
new file mode 100755
index 0000000..c3cd0e1
--- /dev/null
+++ b/test/sizzle/data/testinit.js
@@ -0,0 +1,87 @@
+var assert = require("assert"),
+    util = require("util"),
+    helper = require("../../tools/helper.js"),
+    CSSselect = helper.CSSselect,
+    path = require("path"),
+    docPath = path.join(__dirname, "index.html"),
+    document = null;
+
+
+//in this module, the only use-case is to compare arrays of
+function deepEqual(a, b, msg){
+	try {
+		assert.deepEqual(a, b, msg);
+	} catch(e) {
+		function getId(n){return n && n.attribs.id; }
+		e.actual = JSON.stringify(a.map(getId), 0, 2);
+		e.expected = JSON.stringify(b.map(getId), 0, 2);
+		throw e;
+	}
+}
+
+function loadDoc(){
+	return document = helper.getDocument(docPath);
+}
+
+module.exports = {
+	q: q,
+	t: t,
+	loadDoc: loadDoc,
+	createWithFriesXML: createWithFriesXML
+};
+
+/**
+ * Returns an array of elements with the given IDs
+ * @example q("main", "foo", "bar")
+ * @result [<div id="main">, <span id="foo">, <input id="bar">]
+ */
+function q() {
+	var r = [],
+		i = 0;
+
+	for ( ; i < arguments.length; i++ ) {
+		r.push( document.getElementById( arguments[i] ) );
+	}
+	return r;
+}
+
+/**
+ * Asserts that a select matches the given IDs
+ * @param {String} a - Assertion name
+ * @param {String} b - Sizzle selector
+ * @param {String} c - Array of ids to construct what is expected
+ * @example t("Check for something", "//[a]", ["foo", "baar"]);
+ * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
+ */
+function t( a, b, c ) {
+	var f = CSSselect(b, document),
+		s = "",
+		i = 0;
+
+	for ( ; i < f.length; i++ ) {
+		s += ( s && "," ) + '"' + f[ i ].id + '"';
+	}
+
+	deepEqual(f, q.apply( q, c ), a + " (" + b + ")");
+}
+
+/**
+ * Add random number to url to stop caching
+ *
+ * @example url("data/test.html")
+ * @result "data/test.html?10538358428943"
+ *
+ * @example url("data/test.php?foo=bar")
+ * @result "data/test.php?foo=bar&10538358345554"
+ */
+function url( value ) {
+	return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000);
+}
+
+var xmlDoc = helper.getDOMFromPath(path.join(__dirname, "fries.xml"), { xmlMode: true });
+var filtered = xmlDoc.filter(function(t){return t.type === "tag"});
+xmlDoc.lastChild = filtered[filtered.length - 1];
+
+function createWithFriesXML() {
+	return xmlDoc;
+}
diff --git a/test/sizzle/selector.js b/test/sizzle/selector.js
new file mode 100755
index 0000000..31f745b
--- /dev/null
+++ b/test/sizzle/selector.js
@@ -0,0 +1,1197 @@
+var DomUtils = require("domutils"),
+	helper = require("../tools/helper.js"),
+	CSSselect = helper.CSSselect,
+	assert = require("assert"),
+	raises = assert.throws,
+	equal = assert.equal,
+	deepEqual = assert.deepEqual,
+	ok = assert.ok,
+	testInit = require("./data/testinit.js"),
+	q = testInit.q,
+	t = testInit.t,
+	document = testInit.loadDoc(),
+	createWithFriesXML = testInit.createWithFriesXML,
+	expect = function(){},
+	test = it;
+
+function Sizzle(str, doc){
+	return CSSselect(str, doc || document);
+}
+
+Sizzle.matches = function(selector, elements){
+	return elements.filter(CSSselect.compile(selector));
+};
+
+Sizzle.matchesSelector = CSSselect.is;
+
+function jQuery(dom){
+	if(typeof dom === "string") dom = helper.getDOM(dom);
+	var ret = {
+		appendTo: function(elem){
+			if(typeof elem === "string") elem = Sizzle(elem)[0];
+			dom.forEach(function(child){
+				DomUtils.appendChild(elem, child);
+			});
+			return this;
+		},
+		remove: function(){
+			dom.forEach(DomUtils.removeElement);
+			return this;
+		},
+		prev: function(){
+			dom = dom.map(function(elem){
+				return elem.prev;
+			});
+			return this;
+		},
+		before: function(str){
+			dom.forEach(function(elem){
+				helper.getDOM(str).forEach(function(child){
+					DomUtils.prepend(elem, child);
+				});
+			});
+			return this;
+		}
+	};
+
+	dom.forEach(function(elem, i){
+		ret[i] = elem;
+	});
+
+	return ret;
+}
+
+function asyncTest(name, _, func){
+	it(name, func);
+}
+
+beforeEach(function(){
+	document = testInit.loadDoc();
+});
+
+// #### NOTE: ####
+// jQuery should not be used in this module
+// except for DOM manipulation
+// If jQuery is mandatory for the selection, move the test to jquery/test/unit/selector.js
+// Use t() or Sizzle()
+// ###############
+
+/*
+	======== QUnit Reference ========
+	http://docs.jquery.com/QUnit
+
+	Test methods:
+		expect(numAssertions)
+		stop()
+		start()
+			note: QUnit's eventual addition of an argument to stop/start is ignored in this test suite
+			so that start and stop can be passed as callbacks without worrying about
+				their parameters
+	Test assertions:
+		ok(value, [message])
+		equal(actual, expected, [message])
+		notEqual(actual, expected, [message])
+		deepEqual(actual, expected, [message])
+		notDeepEqual(actual, expected, [message])
+		strictEqual(actual, expected, [message])
+		notStrictEqual(actual, expected, [message])
+		raises(block, [expected], [message])
+
+	======== testinit.js reference ========
+	See data/testinit.js
+
+	q(...);
+		Returns an array of elements with the given IDs
+		@example q("main", "foo", "bar") => [<div id="main">, <span id="foo">, <input id="bar">]
+
+	t( testName, selector, [ "array", "of", "ids" ] );
+		Asserts that a select matches the given IDs
+		@example t("Check for something", "//[a]", ["foo", "baar"]);
+
+	url( "some/url.php" );
+		Add random number to url to stop caching
+		@example url("data/test.html") => "data/test.html?10538358428943"
+		@example url("data/test.php?foo=bar") => "data/test.php?foo=bar&10538358345554"
+*/
+
+test("element", function() {
+	expect( 38 );
+
+	var form, all, good, i, obj1, lengthtest,
+		siblingTest, iframe, iframeDoc, html;
+
+	equal( Sizzle("").length, 0, "Empty selector returns an empty array" );
+	deepEqual( Sizzle("div", document.createTextNode("")), [], "Text element as context fails silently" );
+	form = document.getElementById("form");
+	ok( !Sizzle.matchesSelector( form, "" ), "Empty string passed to matchesSelector does not match" );
+	equal( Sizzle(" ").length, 0, "Empty selector returns an empty array" );
+	equal( Sizzle("\t").length, 0, "Empty selector returns an empty array" );
+
+	ok( Sizzle("*").length >= 30, "Select all" );
+	all = Sizzle("*");
+	good = true;
+	for ( i = 0; i < all.length; i++ ) {
+		if ( all[i].nodeType === 8 ) {
+			good = false;
+		}
+	}
+	ok( good, "Select all elements, no comment nodes" );
+	t( "Element Selector", "html", ["html"] );
+	t( "Element Selector", "body", ["body"] );
+	t( "Element Selector", "#qunit-fixture p", ["firstp","ap","sndp","en","sap","first"] );
+
+	t( "Leading space", " #qunit-fixture p", ["firstp","ap","sndp","en","sap","first"] );
+	t( "Leading tab", "\t#qunit-fixture p", ["firstp","ap","sndp","en","sap","first"] );
+	t( "Leading carriage return", "\r#qunit-fixture p", ["firstp","ap","sndp","en","sap","first"] );
+	t( "Leading line feed", "\n#qunit-fixture p", ["firstp","ap","sndp","en","sap","first"] );
+	t( "Leading form feed", "\f#qunit-fixture p", ["firstp","ap","sndp","en","sap","first"] );
+	t( "Trailing space", "#qunit-fixture p ", ["firstp","ap","sndp","en","sap","first"] );
+	t( "Trailing tab", "#qunit-fixture p\t", ["firstp","ap","sndp","en","sap","first"] );
+	t( "Trailing carriage return", "#qunit-fixture p\r", ["firstp","ap","sndp","en","sap","first"] );
+	t( "Trailing line feed", "#qunit-fixture p\n", ["firstp","ap","sndp","en","sap","first"] );
+	t( "Trailing form feed", "#qunit-fixture p\f", ["firstp","ap","sndp","en","sap","first"] );
+
+	t( "Parent Element", "dl ol", ["empty", "listWithTabIndex"] );
+	t( "Parent Element (non-space descendant combinator)", "dl\tol", ["empty", "listWithTabIndex"] );
+	obj1 = document.getElementById("object1");
+	equal( Sizzle("param", obj1).length, 2, "Object/param as context" );
+
+	deepEqual( Sizzle("select", form), q("select1","select2","select3","select4","select5"), "Finding selects with a context." );
+
+	// Check for unique-ness and sort order
+	deepEqual( Sizzle("p, div p"), Sizzle("p"), "Check for duplicates: p, div p" );
+
+	t( "Checking sort order", "h2, h1", ["qunit-header", "qunit-banner", "qunit-userAgent"] );
+	//  t( "Checking sort order", "h2:first, h1:first", ["qunit-header", "qunit-banner"] );
+	t( "Checking sort order", "#qunit-fixture p, #qunit-fixture p a", ["firstp", "simon1", "ap", "google", "groups", "anchor1", "mark", "sndp", "en", "yahoo", "sap", "anchor2", "simon", "first"] );
+
+	// Test Conflict ID
+	lengthtest = document.getElementById("lengthtest");
+	deepEqual( Sizzle("#idTest", lengthtest), q("idTest"), "Finding element with id of ID." );
+	deepEqual( Sizzle("[name='id']", lengthtest), q("idTest"), "Finding element with id of ID." );
+	deepEqual( Sizzle("input[id='idTest']", lengthtest), q("idTest"), "Finding elements with id of ID." );
+
+	siblingTest = document.getElementById("siblingTest"); // TODO
+	deepEqual( Sizzle("div em", siblingTest), [], "Element-rooted QSA does not select based on document context" );
+	deepEqual( Sizzle("div em, div em, div em:not(div em)", siblingTest), [], "Element-rooted QSA does not select based on document context" );
+	deepEqual( Sizzle("div em, em\\,", siblingTest), [], "Escaped commas do not get treated with an id in element-rooted QSA" );
+
+	iframe = document.getElementById("iframe");
+	//iframeDoc.open();
+	iframe.children = helper.getDOM("<body><p id='foo'>bar</p></body>");
+	iframe.children.forEach(function(e){ e.parent = iframe; });
+	//iframeDoc.close();
+	deepEqual(
+		Sizzle( "p:contains(bar)", iframe ),
+		[ DomUtils.getElementById("foo", iframe.children) ],
+		"Other document as context"
+	);
+	iframe.children = [];
+
+	html = "";
+	for ( i = 0; i < 100; i++ ) {
+		html = "<div>" + html + "</div>";
+	}
+	html = jQuery( html ).appendTo( document.body );
+	ok( !!Sizzle("body div div div").length, "No stack or performance problems with large amounts of descendents" );
+	ok( !!Sizzle("body>div div div").length, "No stack or performance problems with large amounts of descendents" );
+	html.remove();
+
+	// Real use case would be using .watch in browsers with window.watch (see Issue #157)
+	var elem = document.createElement("tostring");
+	elem.attribs.id = "toString";
+	var siblings = q("qunit-fixture")[0].children;
+	siblings.push( elem );
+	t( "Element name matches Object.prototype property", "tostring#toString", ["toString"] );
+	siblings.pop();
+});
+
+test("XML Document Selectors", function() {
+	var xml = createWithFriesXML();
+	expect( 11 );
+
+	equal( Sizzle("foo_bar", xml).length, 1, "Element Selector with underscore" );
+	equal( Sizzle(".component", xml).length, 1, "Class selector" );
+	equal( Sizzle("[class*=component]", xml).length, 1, "Attribute selector for class" );
+	equal( Sizzle("property[name=prop2]", xml).length, 1, "Attribute selector with name" );
+	equal( Sizzle("[name=prop2]", xml).length, 1, "Attribute selector with name" );
+	equal( Sizzle("#seite1", xml).length, 1, "Attribute selector with ID" );
+	equal( Sizzle("component#seite1", xml).length, 1, "Attribute selector with ID" );
+	equal( Sizzle.matches( "#seite1", Sizzle("component", xml) ).length, 1, "Attribute selector filter with ID" );
+	equal( Sizzle("meta property thing", xml).length, 2, "Descendent selector and dir caching" );
+	ok( Sizzle.matchesSelector( xml.lastChild, "soap\\:Envelope", { xmlMode: true } ), "Check for namespaced element" );
+
+	xml = helper.getDOM("<?xml version='1.0' encoding='UTF-8'?><root><elem id='1'/></root>", { xmlMode: true });
+	equal( Sizzle( "elem:not(:has(*))", xml ).length, 1,
+		"Non-qSA path correctly handles numeric ids (jQuery #14142)" );
+});
+
+test("broken", function() {
+	expect( 26 );
+
+	var attrbad,
+		broken = function( name, selector ) {
+			raises(function() {
+				// Setting context to null here somehow avoids QUnit's window.error handling
+				// making the e & e.message correct
+				// For whatever reason, without this,
+				// Sizzle.error will be called but no error will be seen in oldIE
+				Sizzle.call( null, selector );
+			}, SyntaxError, name + ": " + selector );
+		};
+
+	broken( "Broken Selector", "[" );
+	broken( "Broken Selector", "(" );
+	broken( "Broken Selector", "{" );
+	//  broken( "Broken Selector", "<" );
+	broken( "Broken Selector", "()" );
+	//  broken( "Broken Selector", "<>" );
+	broken( "Broken Selector", "{}" );
+	broken( "Broken Selector", "," );
+	broken( "Broken Selector", ",a" );
+	broken( "Broken Selector", "a," );
+	// Hangs on IE 9 if regular expression is inefficient
+	broken( "Broken Selector", "[id=012345678901234567890123456789");
+	broken( "Doesn't exist", ":visble" );
+	broken( "Nth-child", ":nth-child" );
+	// Sigh again. IE 9 thinks this is also a real selector
+	// not super critical that we fix this case
+	broken( "Nth-child", ":nth-child(-)" );
+	// Sigh. WebKit thinks this is a real selector in qSA
+	// They've already fixed this and it'll be coming into
+	// current browsers soon. Currently, Safari 5.0 still has this problem
+	broken( "Nth-child", ":nth-child(asdf)", [] );
+	broken( "Nth-child", ":nth-child(2n+-0)" );
+	broken( "Nth-child", ":nth-child(2+0)" );
+	broken( "Nth-child", ":nth-child(- 1n)" );
+	broken( "Nth-child", ":nth-child(-1 n)" );
+	broken( "First-child", ":first-child(n)" );
+	broken( "Last-child", ":last-child(n)" );
+	broken( "Only-child", ":only-child(n)" );
+	broken( "Nth-last-last-child", ":nth-last-last-child(1)" );
+	broken( "First-last-child", ":first-last-child" );
+	broken( "Last-last-child", ":last-last-child" );
+	broken( "Only-last-child", ":only-last-child" );
+
+	// Make sure attribute value quoting works correctly. See: #6093
+	attrbad = jQuery("<input type='hidden' value='2' name='foo.baz' id='attrbad1'/><input type='hidden' value='2' name='foo[baz]' id='attrbad2'/>").appendTo("#qunit-fixture");
+
+	broken( "Attribute not escaped", "input[name=foo.baz]", [] );
+	// Shouldn't be matching those inner brackets
+	broken( "Attribute not escaped", "input[name=foo[baz]]", [] );
+});
+
+test("id", function() {
+	expect( 34 );
+
+	var fiddle, a;
+
+	t( "ID Selector", "#body", ["body"] );
+	t( "ID Selector w/ Element", "body#body", ["body"] );
+	t( "ID Selector w/ Element", "ul#first", [] );
+	t( "ID selector with existing ID descendant", "#firstp #simon1", ["simon1"] );
+	t( "ID selector with non-existant descendant", "#firstp #foobar", [] );
+	t( "ID selector using UTF8", "#台北Táiběi", ["台北Táiběi"] );
+	t( "Multiple ID selectors using UTF8", "#台北Táiběi, #台北", ["台北Táiběi","台北"] );
+	t( "Descendant ID selector using UTF8", "div #台北", ["台北"] );
+	t( "Child ID selector using UTF8", "form > #台北", ["台北"] );
+
+	t( "Escaped ID", "#foo\\:bar", ["foo:bar"] );
+	t( "Escaped ID with descendent", "#foo\\:bar span:not(:input)", ["foo_descendent"] );
+	t( "Escaped ID", "#test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
+	t( "Descendant escaped ID", "div #foo\\:bar", ["foo:bar"] );
+	t( "Descendant escaped ID", "div #test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
+	t( "Child escaped ID", "form > #foo\\:bar", ["foo:bar"] );
+	t( "Child escaped ID", "form > #test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
+
+	fiddle = jQuery("<div id='fiddle\\Foo'><span id='fiddleSpan'></span></div>").appendTo("#qunit-fixture");
+	//  deepEqual( Sizzle( "> span", Sizzle("#fiddle\\\\Foo")[0] ), q([ "fiddleSpan" ]), "Escaped ID as context" );
+	fiddle.remove();
+
+	t( "ID Selector, child ID present", "#form > #radio1", ["radio1"] ); // bug #267
+	t( "ID Selector, not an ancestor ID", "#form #first", [] );
+	t( "ID Selector, not a child ID", "#form > #option1a", [] );
+
+	t( "All Children of ID", "#foo > *", ["sndp", "en", "sap"] );
+	t( "All Children of ID with no children", "#firstUL > *", [] );
+
+	equal( Sizzle("#tName1")[0].attribs.id, "tName1", "ID selector with same value for a name attribute" );
+	t( "ID selector non-existing but name attribute on an A tag",         "#tName2",      [] );
+	t( "Leading ID selector non-existing but name attribute on an A tag", "#tName2 span", [] );
+	t( "Leading ID selector existing, retrieving the child",              "#tName1 span", ["tName1-span"] );
+	equal( Sizzle("div > div #tName1")[0].attribs.id, Sizzle("#tName1-span")[0].parent.attribs.id, "Ending with ID" );
+
+	a = jQuery("<a id='backslash\\foo'></a>").appendTo("#qunit-fixture");
+	t( "ID Selector contains backslash", "#backslash\\\\foo", ["backslash\\foo"] );
+
+	t( "ID Selector on Form with an input that has a name of 'id'", "#lengthtest", ["lengthtest"] );
+
+	t( "ID selector with non-existant ancestor", "#asdfasdf #foobar", [] ); // bug #986
+
+	deepEqual( Sizzle("div#form", document.body), [], "ID selector within the context of another element" );
+
+	t( "Underscore ID", "#types_all", ["types_all"] );
+	t( "Dash ID", "#qunit-fixture", ["qunit-fixture"] );
+
+	t( "ID with weird characters in it", "#name\\+value", ["name+value"] );
+});
+
+test("class", function() {
+	expect( 26 );
+
+	t( "Class Selector", ".blog", ["mark","simon"] );
+	t( "Class Selector", ".GROUPS", ["groups"] );
+	t( "Class Selector", ".blog.link", ["simon"] );
+	t( "Class Selector w/ Element", "a.blog", ["mark","simon"] );
+	t( "Parent Class Selector", "p .blog", ["mark","simon"] );
+
+	t( "Class selector using UTF8", ".台北Táiběi", ["utf8class1"] );
+	t( "Class selector using UTF8", ".台北", ["utf8class1","utf8class2"] );
+	t( "Class selector using UTF8", ".台北Táiběi.台北", ["utf8class1"] );
+	t( "Class selector using UTF8", ".台北Táiběi, .台北", ["utf8class1","utf8class2"] );
+	t( "Descendant class selector using UTF8", "div .台北Táiběi", ["utf8class1"] );
+	t( "Child class selector using UTF8", "form > .台北Táiběi", ["utf8class1"] );
+
+	t( "Escaped Class", ".foo\\:bar", ["foo:bar"] );
+	t( "Escaped Class", ".test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
+	t( "Descendant escaped Class", "div .foo\\:bar", ["foo:bar"] );
+	t( "Descendant escaped Class", "div .test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
+	t( "Child escaped Class", "form > .foo\\:bar", ["foo:bar"] );
+	t( "Child escaped Class", "form > .test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
+
+	var div = document.createElement("div");
+	div.children = helper.getDOM("<div class='test e'></div><div class='test'></div>");
+	div.children.forEach(function(e){
+		e.parent = div;
+	});
+	deepEqual( Sizzle(".e", div), [ div.children[0] ], "Finding a second class." );
+
+	var lastChild = div.children[div.children.length - 1];
+	lastChild.attribs.class = "e";
+
+	deepEqual( Sizzle(".e", div), [ div.children[0], lastChild ], "Finding a modified class." );
+
+	ok( !Sizzle.matchesSelector( div, ".null"), ".null does not match an element with no class" );
+	ok( !Sizzle.matchesSelector( div.children[0], ".null div"), ".null does not match an element with no class" );
+	div.attribs.class = "null";
+	ok( Sizzle.matchesSelector( div, ".null"), ".null matches element with class 'null'" );
+	ok( Sizzle.matchesSelector( div.children[0], ".null div"), "caching system respects DOM changes" );
+	ok( !Sizzle.matchesSelector( document, ".foo" ), "testing class on document doesn't error" );
+	//ok( !Sizzle.matchesSelector( window, ".foo" ), "testing class on window doesn't error" );
+
+	lastChild.attribs.class += " hasOwnProperty toString";
+	deepEqual( Sizzle(".e.hasOwnProperty.toString", div), [ lastChild ], "Classes match Object.prototype properties" );
+
+	div = jQuery("<div><svg width='200' height='250' version='1.1' xmlns='http://www.w3.org/2000/svg'><rect x='10' y='10' width='30' height='30' class='foo'></rect></svg></div>")[0];
+	equal( Sizzle(".foo", div).length, 1, "Class selector against SVG" );
+});
+
+test("name", function() {
+	expect( 13 );
+
+	var form;
+
+	t( "Name selector", "input[name=action]", ["text1"] );
+	t( "Name selector with single quotes", "input[name='action']", ["text1"] );
+	t( "Name selector with double quotes", "input[name=\"action\"]", ["text1"] );
+
+	t( "Name selector non-input", "[name=example]", ["name-is-example"] );
+	t( "Name selector non-input", "[name=div]", ["name-is-div"] );
+	t( "Name selector non-input", "*[name=iframe]", ["iframe"] );
+
+	t( "Name selector for grouped input", "input[name='types[]']", ["types_all", "types_anime", "types_movie"] );
+
+	form = document.getElementById("form");
+	deepEqual( Sizzle("input[name=action]", form), q("text1"), "Name selector within the context of another element" );
+	deepEqual( Sizzle("input[name='foo[bar]']", form), q("hidden2"), "Name selector for grouped form element within the context of another element" );
+
+	form = jQuery("<form><input name='id'/></form>").appendTo("body");
+	equal( Sizzle("input", form[0]).length, 1, "Make sure that rooted queries on forms (with possible expandos) work." );
+
+	form.remove();
+
+	t( "Find elements that have similar IDs", "[name=tName1]", ["tName1ID"] );
+	t( "Find elements that have similar IDs", "[name=tName2]", ["tName2ID"] );
+	t( "Find elements that have similar IDs", "#tName2ID", ["tName2ID"] );
+});
+
+test("multiple", function() {
+	expect(6);
+
+	t( "Comma Support", "h2, #qunit-fixture p", ["qunit-banner","qunit-userAgent","firstp","ap","sndp","en","sap","first"] );
+	t( "Comma Support", "h2 , #qunit-fixture p", ["qunit-banner","qunit-userAgent","firstp","ap","sndp","en","sap","first"] );
+	t( "Comma Support", "h2 , #qunit-fixture p", ["qunit-banner","qunit-userAgent","firstp","ap","sndp","en","sap","first"] );
+	t( "Comma Support", "h2,#qunit-fixture p", ["qunit-banner","qunit-userAgent","firstp","ap","sndp","en","sap","first"] );
+	t( "Comma Support", "h2,#qunit-fixture p ", ["qunit-banner","qunit-userAgent","firstp","ap","sndp","en","sap","first"] );
+	t( "Comma Support", "h2\t,\r#qunit-fixture p\n", ["qunit-banner","qunit-userAgent","firstp","ap","sndp","en","sap","first"] );
+});
+
+test("child and adjacent", function() {
+	expect( 42 );
+
+	var siblingFirst, en, nothiddendiv;
+
+	t( "Child", "p > a", ["simon1","google","groups","mark","yahoo","simon"] );
+	t( "Child", "p> a", ["simon1","google","groups","mark","yahoo","simon"] );
+	t( "Child", "p >a", ["simon1","google","groups","mark","yahoo","simon"] );
+	t( "Child", "p>a", ["simon1","google","groups","mark","yahoo","simon"] );
+	t( "Child w/ Class", "p > a.blog", ["mark","simon"] );
+	t( "All Children", "code > *", ["anchor1","anchor2"] );
+	t( "All Grandchildren", "p > * > *", ["anchor1","anchor2"] );
+	t( "Adjacent", "#qunit-fixture a + a", ["groups", "tName2ID"] );
+	t( "Adjacent", "#qunit-fixture a +a", ["groups", "tName2ID"] );
+	t( "Adjacent", "#qunit-fixture a+ a", ["groups", "tName2ID"] );
+	t( "Adjacent", "#qunit-fixture a+a", ["groups", "tName2ID"] );
+	t( "Adjacent", "p + p", ["ap","en","sap"] );
+	t( "Adjacent", "p#firstp + p", ["ap"] );
+	t( "Adjacent", "p[lang=en] + p", ["sap"] );
+	t( "Adjacent", "a.GROUPS + code + a", ["mark"] );
+	t( "Comma, Child, and Adjacent", "#qunit-fixture a + a, code > a", ["groups","anchor1","anchor2","tName2ID"] );
+	t( "Element Preceded By", "#qunit-fixture p ~ div", ["foo", "nothiddendiv", "moretests","tabindex-tests", "liveHandlerOrder", "siblingTest"] );
+	t( "Element Preceded By", "#first ~ div", ["moretests","tabindex-tests", "liveHandlerOrder", "siblingTest"] );
+	t( "Element Preceded By", "#groups ~ a", ["mark"] );
+	t( "Element Preceded By", "#length ~ input", ["idTest"] );
+	t( "Element Preceded By", "#siblingfirst ~ em", ["siblingnext", "siblingthird"] );
+	t( "Element Preceded By (multiple)", "#siblingTest em ~ em ~ em ~ span", ["siblingspan"] );
+	t( "Element Preceded By, Containing", "#liveHandlerOrder ~ div em:contains('1')", ["siblingfirst"] );
+
+	siblingFirst = document.getElementById("siblingfirst");
+
+	//deepEqual( Sizzle("~ em", siblingFirst), q("siblingnext", "siblingthird"), "Element Preceded By with a context." );
+	//deepEqual( Sizzle("+ em", siblingFirst), q("siblingnext"), "Element Directly Preceded By with a context." );
+	//deepEqual( Sizzle("~ em:first", siblingFirst), q("siblingnext"), "Element Preceded By positional with a context." );
+
+	en = document.getElementById("en");
+	//deepEqual( Sizzle("+ p, a", en), q("yahoo", "sap"), "Compound selector with context, beginning with sibling test." );
+	//deepEqual( Sizzle("a, + p", en), q("yahoo", "sap"), "Compound selector with context, containing sibling test." );
+
+	t( "Multiple combinators selects all levels", "#siblingTest em *", ["siblingchild", "siblinggrandchild", "siblinggreatgrandchild"] );
+	t( "Multiple combinators selects all levels", "#siblingTest > em *", ["siblingchild", "siblinggrandchild", "siblinggreatgrandchild"] );
+	t( "Multiple sibling combinators doesn't miss general siblings", "#siblingTest > em:first-child + em ~ span", ["siblingspan"] );
+	t( "Combinators are not skipped when mixing general and specific", "#siblingTest > em:contains('x') + em ~ span", [] );
+
+	equal( Sizzle("#listWithTabIndex").length, 1, "Parent div for next test is found via ID (#8310)" );
+	//equal( Sizzle("#listWithTabIndex li:eq(2) ~ li").length, 1, "Find by general sibling combinator (#8310)" );
+	equal( Sizzle("#__sizzle__").length, 0, "Make sure the temporary id assigned by sizzle is cleared out (#8310)" );
+	equal( Sizzle("#listWithTabIndex").length, 1, "Parent div for previous test is still found via ID (#8310)" );
+
+	t( "Verify deep class selector", "div.blah > p > a", [] );
+
+	t( "No element deep selector", "div.foo > span > a", [] );
+
+	nothiddendiv = document.getElementById("nothiddendiv");
+	//deepEqual( Sizzle("> :first", nothiddendiv), q("nothiddendivchild"), "Verify child context positional selector" );
+	//deepEqual( Sizzle("> :eq(0)", nothiddendiv), q("nothiddendivchild"), "Verify child context positional selector" );
+	//deepEqual( Sizzle("> *:first", nothiddendiv), q("nothiddendivchild"), "Verify child context positional selector" );
+
+	t( "Non-existant ancestors", ".fototab > .thumbnails > a", [] );
+});
+
+test("attributes", function() {
+	expect( 76 );
+
+	var opt, input, attrbad, div;
+
+	t( "Attribute Exists", "#qunit-fixture a[title]", ["google"] );
+	t( "Attribute Exists (case-insensitive)", "#qunit-fixture a[TITLE]", ["google"] );
+	t( "Attribute Exists", "#qunit-fixture *[title]", ["google"] );
+	t( "Attribute Exists", "#qunit-fixture [title]", ["google"] );
+	t( "Attribute Exists", "#qunit-fixture a[ title ]", ["google"] );
+
+	t( "Boolean attribute exists", "#select2 option[selected]", ["option2d"]);
+	t( "Boolean attribute equals", "#select2 option[selected='selected']", ["option2d"]);
+
+	t( "Attribute Equals", "#qunit-fixture a[rel='bookmark']", ["simon1"] );
+	t( "Attribute Equals", "#qunit-fixture a[rel='bookmark']", ["simon1"] );
+	t( "Attribute Equals", "#qunit-fixture a[rel=bookmark]", ["simon1"] );
+	t( "Attribute Equals", "#qunit-fixture a[href='http://www.google.com/']", ["google"] );
+	t( "Attribute Equals", "#qunit-fixture a[ rel = 'bookmark' ]", ["simon1"] );
+	t( "Attribute Equals Number", "#qunit-fixture option[value=1]", ["option1b","option2b","option3b","option4b","option5c"] );
+	t( "Attribute Equals Number", "#qunit-fixture li[tabIndex=-1]", ["foodWithNegativeTabIndex"] );
+
+	document.getElementById("anchor2").href = "#2";
+	t( "href Attribute", "p a[href^=#]", ["anchor2"] );
+	t( "href Attribute", "p a[href*=#]", ["simon1", "anchor2"] );
+
+	t( "for Attribute", "form label[for]", ["label-for"] );
+	t( "for Attribute in form", "#form [for=action]", ["label-for"] );
+
+	t( "Attribute containing []", "input[name^='foo[']", ["hidden2"] );
+	t( "Attribute containing []", "input[name^='foo[bar]']", ["hidden2"] );
+	t( "Attribute containing []", "input[name*='[bar]']", ["hidden2"] );
+	t( "Attribute containing []", "input[name$='bar]']", ["hidden2"] );
+	t( "Attribute containing []", "input[name$='[bar]']", ["hidden2"] );
+	t( "Attribute containing []", "input[name$='foo[bar]']", ["hidden2"] );
+	t( "Attribute containing []", "input[name*='foo[bar]']", ["hidden2"] );
+
+	deepEqual( Sizzle( "input[data-comma='0,1']" ), [ document.getElementById("el12087") ], "Without context, single-quoted attribute containing ','" );
+	deepEqual( Sizzle( "input[data-comma=\"0,1\"]" ), [ document.getElementById("el12087") ], "Without context, double-quoted attribute containing ','" );
+	deepEqual( Sizzle( "input[data-comma='0,1']", document.getElementById("t12087") ), [ document.getElementById("el12087") ], "With context, single-quoted attribute containing ','" );
+	deepEqual( Sizzle( "input[data-comma=\"0,1\"]", document.getElementById("t12087") ), [ document.getElementById("el12087") ], "With context, double-quoted attribute containing ','" );
+
+	t( "Multiple Attribute Equals", "#form input[type='radio'], #form input[type='hidden']", ["radio1", "radio2", "hidden1"] );
+	t( "Multiple Attribute Equals", "#form input[type='radio'], #form input[type=\"hidden\"]", ["radio1", "radio2", "hidden1"] );
+	t( "Multiple Attribute Equals", "#form input[type='radio'], #form input[type=hidden]", ["radio1", "radio2", "hidden1"] );
+
+	t( "Attribute selector using UTF8", "span[lang=中文]", ["台北"] );
+
+	t( "Attribute Begins With", "a[href ^= 'http://www']", ["google","yahoo"] );
+	t( "Attribute Ends With", "a[href $= 'org/']", ["mark"] );
+	t( "Attribute Contains", "a[href *= 'google']", ["google","groups"] );
+	t( "Attribute Is Not Equal", "#ap a[hreflang!='en']", ["google","groups","anchor1"] );
+
+	opt = document.getElementById("option1a");
+	opt.attribs.test = "";
+
+	ok( Sizzle.matchesSelector( opt, "[id*=option1][type!=checkbox]" ), "Attribute Is Not Equal Matches" );
+	ok( Sizzle.matchesSelector( opt, "[id*=option1]" ), "Attribute With No Quotes Contains Matches" );
+	ok( Sizzle.matchesSelector( opt, "[test=]" ), "Attribute With No Quotes No Content Matches" );
+	ok( !Sizzle.matchesSelector( opt, "[test^='']" ), "Attribute with empty string value does not match startsWith selector (^=)" );
+	ok( Sizzle.matchesSelector( opt, "[id=option1a]" ), "Attribute With No Quotes Equals Matches" );
+	ok( Sizzle.matchesSelector( document.getElementById("simon1"), "a[href*=#]" ), "Attribute With No Quotes Href Contains Matches" );
+
+	t( "Empty values", "#select1 option[value='']", ["option1a"] );
+	t( "Empty values", "#select1 option[value!='']", ["option1b","option1c","option1d"] );
+
+	t( "Select options via :selected", "#select1 option:selected", ["option1a"] );
+	t( "Select options via :selected", "#select2 option:selected", ["option2d"] );
+	t( "Select options via :selected", "#select3 option:selected", ["option3b", "option3c"] );
+	t( "Select options via :selected", "select[name='select2'] option:selected", ["option2d"] );
+
+	t( "Grouped Form Elements", "input[name='foo[bar]']", ["hidden2"] );
+
+	input = document.getElementById("text1");
+	input.attribs.title = "Don't click me";
+
+	ok( Sizzle.matchesSelector( input, "input[title=\"Don't click me\"]" ), "Quote within attribute value does not mess up tokenizer" );
+
+	// Uncomment if the boolHook is removed
+	// var check2 = document.getElementById("check2");
+	// check2.checked = true;
+	// ok( !Sizzle.matches("[checked]", [ check2 ] ), "Dynamic boolean attributes match when they should with Sizzle.matches (#11115)" );
+
+	// jQuery #12303
+	input.attribs["data-pos"] = ":first";
+	ok( Sizzle.matchesSelector( input, "input[data-pos=\\:first]"), "POS within attribute value is treated as an attribute value" );
+	ok( Sizzle.matchesSelector( input, "input[data-pos=':first']"), "POS within attribute value is treated as an attribute value" );
+	ok( Sizzle.matchesSelector( input, ":input[data-pos=':first']"), "POS within attribute value after pseudo is treated as an attribute value" );
+	delete input.attribs["data-pos"];
+
+	// Make sure attribute value quoting works correctly. See jQuery #6093; #6428; #13894
+	// Use seeded results to bypass querySelectorAll optimizations
+	attrbad = jQuery(
+		"<input type='hidden' id='attrbad_space' name='foo bar'/>" +
+		"<input type='hidden' id='attrbad_dot' value='2' name='foo.baz'/>" +
+		"<input type='hidden' id='attrbad_brackets' value='2' name='foo[baz]'/>" +
+		"<input type='hidden' id='attrbad_injection' data-attr='foo_baz']'/>" +
+		"<input type='hidden' id='attrbad_quote' data-attr='''/>" +
+		"<input type='hidden' id='attrbad_backslash' data-attr='\'/>" +
+		"<input type='hidden' id='attrbad_backslash_quote' data-attr='\''/>" +
+		"<input type='hidden' id='attrbad_backslash_backslash' data-attr='\\'/>" +
+		"<input type='hidden' id='attrbad_unicode' data-attr='&#x4e00;'/>"
+	).appendTo("#qunit-fixture");
+
+	t( "Underscores don't need escaping", "input[id=types_all]", ["types_all"] );
+
+	deepEqual( Sizzle( "input[name=foo\\ bar]", null, null, attrbad ), q("attrbad_space"),
+		"Escaped space" );
+	deepEqual( Sizzle( "input[name=foo\\.baz]", null, null, attrbad ), q("attrbad_dot"),
+		"Escaped dot" );
+	deepEqual( Sizzle( "input[name=foo\\[baz\\]]", null, null, attrbad ), q("attrbad_brackets"),
+		"Escaped brackets" );
+	//  deepEqual( Sizzle( "input[data-attr='foo_baz\\']']", null, null, attrbad ), q("attrbad_injection"),
+	//	"Escaped quote + right bracket" );
+
+	//  deepEqual( Sizzle( "input[data-attr='\\'']", null, null, attrbad ), q("attrbad_quote"),
+	//	"Quoted quote" );
+	//  deepEqual( Sizzle( "input[data-attr='\\\\']", null, null, attrbad ), q("attrbad_backslash"),
+	//	"Quoted backslash" );
+	//  deepEqual( Sizzle( "input[data-attr='\\\\\\'']", null, null, attrbad ), q("attrbad_backslash_quote"),
+	//	"Quoted backslash quote" );
+	//  deepEqual( Sizzle( "input[data-attr='\\\\\\\\']", null, null, attrbad ), q("attrbad_backslash_backslash"),
+	//	"Quoted backslash backslash" );
+
+	//  deepEqual( Sizzle( "input[data-attr='\\5C\\\\']", null, null, attrbad ), q("attrbad_backslash_backslash"),
+	//	"Quoted backslash backslash (numeric escape)" );
+	//  deepEqual( Sizzle( "input[data-attr='\\5C \\\\']", null, null, attrbad ), q("attrbad_backslash_backslash"),
+	//	"Quoted backslash backslash (numeric escape with trailing space)" );
+	//  deepEqual( Sizzle( "input[data-attr='\\5C\t\\\\']", null, null, attrbad ), q("attrbad_backslash_backslash"),
+	//	"Quoted backslash backslash (numeric escape with trailing tab)" );
+	//  deepEqual( Sizzle( "input[data-attr='\\04e00']", null, null, attrbad ), q("attrbad_unicode"),
+	//	"Long numeric escape (BMP)" );*/
+	document.getElementById("attrbad_unicode").attribs["data-attr"] = "\uD834\uDF06A";
+	// It was too much code to fix Safari 5.x Supplemental Plane crashes (see ba5f09fa404379a87370ec905ffa47f8ac40aaa3)
+	deepEqual( Sizzle( "input[data-attr='\\01D306A']", null, null, attrbad ), q("attrbad_unicode"),
+		"Long numeric escape (non-BMP)" );
+
+	attrbad.remove();
+
+	t( "input[type=text]", "#form input[type=text]", ["text1", "text2", "hidden2", "name"] );
+	t( "input[type=search]", "#form input[type=search]", ["search"] );
+	t( "script[src] (jQuery #13777)", "#moretests script[src]", ["script-src"] );
+
+	// #3279
+	div = document.createElement("div");
+	div.children = helper.getDOM("<div id='foo' xml:test='something'></div>");
+
+	deepEqual( Sizzle( "[xml\\:test]", div ), [ div.children[0] ], "Finding by attribute with escaped characters." );
+
+	div = document.getElementById("foo");
+	t( "Object.prototype property \"constructor\" (negative)", "[constructor]", [] );
+	t( "Gecko Object.prototype property \"watch\" (negative)", "[watch]", [] );
+	div.attribs.constructor = "foo";
+	div.attribs.watch = "bar";
+	t( "Object.prototype property \"constructor\"", "[constructor='foo']", ["foo"] );
+	t( "Gecko Object.prototype property \"watch\"", "[watch='bar']", ["foo"] );
+
+	t( "Value attribute is retrieved correctly", "input[value=Test]", ["text1", "text2"] );
+});
+
+test("pseudo - (parent|empty)", function() {
+	expect( 3 );
+	t( "Empty", "ul:empty", ["firstUL"] );
+	t( "Empty with comment node", "ol:empty", ["empty"] );
+	t( "Is A Parent", "#qunit-fixture p:parent", ["firstp","ap","sndp","en","sap","first"] );
+});
+
+test("pseudo - (first|last|only)-(child|of-type)", function() {
+	expect( 12 );
+
+	t( "First Child", "p:first-child", ["firstp","sndp"] );
+	t( "First Child (leading id)", "#qunit-fixture p:first-child", ["firstp","sndp"] );
+	t( "First Child (leading class)", ".nothiddendiv div:first-child", ["nothiddendivchild"] );
+	t( "First Child (case-insensitive)", "#qunit-fixture p:FIRST-CHILD", ["firstp","sndp"] );
+
+	t( "Last Child", "p:last-child", ["sap"] );
+	t( "Last Child (leading id)", "#qunit-fixture a:last-child", ["simon1","anchor1","mark","yahoo","anchor2","simon","liveLink1","liveLink2"] );
+
+	t( "Only Child", "#qunit-fixture a:only-child", ["simon1","anchor1","yahoo","anchor2","liveLink1","liveLink2"] );
+
+	t( "First-of-type", "#qunit-fixture > p:first-of-type", ["firstp"] );
+	t( "Last-of-type", "#qunit-fixture > p:last-of-type", ["first"] );
+	t( "Only-of-type", "#qunit-fixture > :only-of-type", ["name+value", "firstUL", "empty", "floatTest", "iframe", "table"] );
+
+	// Verify that the child position isn't being cached improperly
+	var secondChildren = jQuery(Sizzle("p:nth-child(2)")).before("<div></div>");
+
+	t( "No longer second child", "p:nth-child(2)", [] );
+	secondChildren.prev().remove();
+	t( "Restored second child", "p:nth-child(2)", ["ap","en"] );
+});
+
+test("pseudo - nth-child", function() {
+	expect( 30 );
+
+	t( "Nth-child", "p:nth-child(1)", ["firstp","sndp"] );
+	t( "Nth-child (with whitespace)", "p:nth-child( 1 )", ["firstp","sndp"] );
+	t( "Nth-child (case-insensitive)", "#select1 option:NTH-child(3)", ["option1c"] );
+	t( "Not nth-child", "#qunit-fixture p:not(:nth-child(1))", ["ap","en","sap","first"] );
+
+	t( "Nth-child(2)", "#qunit-fixture form#form > *:nth-child(2)", ["text1"] );
+	t( "Nth-child(2)", "#qunit-fixture form#form > :nth-child(2)", ["text1"] );
+
+	t( "Nth-child(-1)", "#select1 option:nth-child(-1)", [] );
+	t( "Nth-child(3)", "#select1 option:nth-child(3)", ["option1c"] );
+	//  t( "Nth-child(0n+3)", "#select1 option:nth-child(0n+3)", ["option1c"] );
+	t( "Nth-child(1n+0)", "#select1 option:nth-child(1n+0)", ["option1a", "option1b", "option1c", "option1d"] );
+	t( "Nth-child(1n)", "#select1 option:nth-child(1n)", ["option1a", "option1b", "option1c", "option1d"] );
+	t( "Nth-child(n)", "#select1 option:nth-child(n)", ["option1a", "option1b", "option1c", "option1d"] );
+	t( "Nth-child(even)", "#select1 option:nth-child(even)", ["option1b", "option1d"] );
+	t( "Nth-child(odd)", "#select1 option:nth-child(odd)", ["option1a", "option1c"] );
+	t( "Nth-child(2n)", "#select1 option:nth-child(2n)", ["option1b", "option1d"] );
+	t( "Nth-child(2n+1)", "#select1 option:nth-child(2n+1)", ["option1a", "option1c"] );
+	t( "Nth-child(2n + 1)", "#select1 option:nth-child(2n + 1)", ["option1a", "option1c"] );
+	t( "Nth-child(+2n + 1)", "#select1 option:nth-child(+2n + 1)", ["option1a", "option1c"] );
+	t( "Nth-child(3n)", "#select1 option:nth-child(3n)", ["option1c"] );
+	t( "Nth-child(3n+1)", "#select1 option:nth-child(3n+1)", ["option1a", "option1d"] );
+	t( "Nth-child(3n+2)", "#select1 option:nth-child(3n+2)", ["option1b"] );
+	t( "Nth-child(3n+3)", "#select1 option:nth-child(3n+3)", ["option1c"] );
+	t( "Nth-child(3n-1)", "#select1 option:nth-child(3n-1)", ["option1b"] );
+	t( "Nth-child(3n-2)", "#select1 option:nth-child(3n-2)", ["option1a", "option1d"] );
+	t( "Nth-child(3n-3)", "#select1 option:nth-child(3n-3)", ["option1c"] );
+	t( "Nth-child(3n+0)", "#select1 option:nth-child(3n+0)", ["option1c"] );
+	t( "Nth-child(-1n+3)", "#select1 option:nth-child(-1n+3)", ["option1a", "option1b", "option1c"] );
+	t( "Nth-child(-n+3)", "#select1 option:nth-child(-n+3)", ["option1a", "option1b", "option1c"] );
+	t( "Nth-child(-1n + 3)", "#select1 option:nth-child(-1n + 3)", ["option1a", "option1b", "option1c"] );
+
+	//  deepEqual( Sizzle( ":nth-child(n)", null, null, [ document.createElement("a") ].concat( q("ap") ) ), q("ap"), "Seeded nth-child" );
+});
+
+test("pseudo - nth-last-child", function() {
+	expect( 30 );
+
+	t( "Nth-last-child", "form:nth-last-child(5)", ["testForm"] );
+	t( "Nth-last-child (with whitespace)", "form:nth-last-child( 5 )", ["testForm"] );
+	t( "Nth-last-child (case-insensitive)", "#select1 option:NTH-last-child(3)", ["option1b"] );
+	t( "Not nth-last-child", "#qunit-fixture p:not(:nth-last-child(1))", ["firstp", "ap", "sndp", "en", "first"] );
+
+	t( "Nth-last-child(-1)", "#select1 option:nth-last-child(-1)", [] );
+	t( "Nth-last-child(3)", "#select1 :nth-last-child(3)", ["option1b"] );
+	t( "Nth-last-child(3)", "#select1 *:nth-last-child(3)", ["option1b"] );
+	t( "Nth-last-child(3)", "#select1 option:nth-last-child(3)", ["option1b"] );
+	//  t( "Nth-last-child(0n+3)", "#select1 option:nth-last-child(0n+3)", ["option1b"] );
+	t( "Nth-last-child(1n+0)", "#select1 option:nth-last-child(1n+0)", ["option1a", "option1b", "option1c", "option1d"] );
+	t( "Nth-last-child(1n)", "#select1 option:nth-last-child(1n)", ["option1a", "option1b", "option1c", "option1d"] );
+	t( "Nth-last-child(n)", "#select1 option:nth-last-child(n)", ["option1a", "option1b", "option1c", "option1d"] );
+	t( "Nth-last-child(even)", "#select1 option:nth-last-child(even)", ["option1a", "option1c"] );
+	t( "Nth-last-child(odd)", "#select1 option:nth-last-child(odd)", ["option1b", "option1d"] );
+	t( "Nth-last-child(2n)", "#select1 option:nth-last-child(2n)", ["option1a", "option1c"] );
+	t( "Nth-last-child(2n+1)", "#select1 option:nth-last-child(2n+1)", ["option1b", "option1d"] );
+	t( "Nth-last-child(2n + 1)", "#select1 option:nth-last-child(2n + 1)", ["option1b", "option1d"] );
+	t( "Nth-last-child(+2n + 1)", "#select1 option:nth-last-child(+2n + 1)", ["option1b", "option1d"] );
+	t( "Nth-last-child(3n)", "#select1 option:nth-last-child(3n)", ["option1b"] );
+	t( "Nth-last-child(3n+1)", "#select1 option:nth-last-child(3n+1)", ["option1a", "option1d"] );
+	t( "Nth-last-child(3n+2)", "#select1 option:nth-last-child(3n+2)", ["option1c"] );
+	t( "Nth-last-child(3n+3)", "#select1 option:nth-last-child(3n+3)", ["option1b"] );
+	t( "Nth-last-child(3n-1)", "#select1 option:nth-last-child(3n-1)", ["option1c"] );
+	t( "Nth-last-child(3n-2)", "#select1 option:nth-last-child(3n-2)", ["option1a", "option1d"] );
+	t( "Nth-last-child(3n-3)", "#select1 option:nth-last-child(3n-3)", ["option1b"] );
+	t( "Nth-last-child(3n+0)", "#select1 option:nth-last-child(3n+0)", ["option1b"] );
+	t( "Nth-last-child(-1n+3)", "#select1 option:nth-last-child(-1n+3)", ["option1b", "option1c", "option1d"] );
+	t( "Nth-last-child(-n+3)", "#select1 option:nth-last-child(-n+3)", ["option1b", "option1c", "option1d"] );
+	t( "Nth-last-child(-1n + 3)", "#select1 option:nth-last-child(-1n + 3)", ["option1b", "option1c", "option1d"] );
+
+	//  deepEqual( Sizzle( ":nth-last-child(n)", null, null, [ document.createElement("a") ].concat( q("ap") ) ), q("ap"), "Seeded nth-last-child" );
+});
+
+test("pseudo - nth-of-type", function() {
+	expect( 9 );
+	t( "Nth-of-type(-1)", ":nth-of-type(-1)", [] );
+	t( "Nth-of-type(3)", "#ap :nth-of-type(3)", ["mark"] );
+	t( "Nth-of-type(n)", "#ap :nth-of-type(n)", ["google", "groups", "code1", "anchor1", "mark"] );
+	t( "Nth-of-type(0n+3)", "#ap :nth-of-type(0n+3)", ["mark"] );
+	t( "Nth-of-type(2n)", "#ap :nth-of-type(2n)", ["groups"] );
+	t( "Nth-of-type(even)", "#ap :nth-of-type(even)", ["groups"] );
+	t( "Nth-of-type(2n+1)", "#ap :nth-of-type(2n+1)", ["google", "code1", "anchor1", "mark"] );
+	t( "Nth-of-type(odd)", "#ap :nth-of-type(odd)", ["google", "code1", "anchor1", "mark"] );
+	t( "Nth-of-type(-n+2)", "#qunit-fixture > :nth-of-type(-n+2)", ["firstp", "ap", "foo", "nothiddendiv", "name+value", "firstUL", "empty", "form", "floatTest", "iframe", "lengthtest", "table"] );
+});
+
+test("pseudo - nth-last-of-type", function() {
+	expect( 9 );
+	t( "Nth-last-of-type(-1)", ":nth-last-of-type(-1)", [] );
+	t( "Nth-last-of-type(3)", "#ap :nth-last-of-type(3)", ["google"] );
+	t( "Nth-last-of-type(n)", "#ap :nth-last-of-type(n)", ["google", "groups", "code1", "anchor1", "mark"] );
+	t( "Nth-last-of-type(0n+3)", "#ap :nth-last-of-type(0n+3)", ["google"] );
+	t( "Nth-last-of-type(2n)", "#ap :nth-last-of-type(2n)", ["groups"] );
+	t( "Nth-last-of-type(even)", "#ap :nth-last-of-type(even)", ["groups"] );
+	t( "Nth-last-of-type(2n+1)", "#ap :nth-last-of-type(2n+1)", ["google", "code1", "anchor1", "mark"] );
+	t( "Nth-last-of-type(odd)", "#ap :nth-last-of-type(odd)", ["google", "code1", "anchor1", "mark"] );
+	t( "Nth-last-of-type(-n+2)", "#qunit-fixture > :nth-last-of-type(-n+2)", ["ap", "name+value", "first", "firstUL", "empty", "floatTest", "iframe", "table", "name-tests", "testForm", "liveHandlerOrder", "siblingTest"] );
+});
+
+test("pseudo - has", function() {
+	expect( 3 );
+
+	t( "Basic test", "p:has(a)", ["firstp","ap","en","sap"] );
+	t( "Basic test (irrelevant whitespace)", "p:has( a )", ["firstp","ap","en","sap"] );
+	t( "Nested with overlapping candidates", "#qunit-fixture div:has(div:has(div:not([id])))", [ "moretests", "t2037" ] );
+});
+
+test("pseudo - misc", function() {
+	expect( 39 );
+
+	var select, tmp, input;
+
+	t( "Headers", ":header", ["qunit-header", "qunit-banner", "qunit-userAgent"] );
+	t( "Headers(case-insensitive)", ":Header", ["qunit-header", "qunit-banner", "qunit-userAgent"] );
+	t( "Multiple matches with the same context (cache check)", "#form select:has(option:first-child:contains('o'))", ["select1", "select2", "select3", "select4"] );
+
+	ok( Sizzle("#qunit-fixture :not(:has(:has(*)))").length, "All not grandparents" );
+
+	select = document.getElementById("select1");
+	ok( Sizzle.matchesSelector( select, ":has(option)" ), "Has Option Matches" );
+
+	ok( Sizzle("a:contains('')").length, "Empty string contains" );
+	t( "Text Contains", "a:contains(Google)", ["google","groups"] );
+	t( "Text Contains", "a:contains(Google Groups)", ["groups"] );
+
+	t( "Text Contains", "a:contains('Google Groups (Link)')", ["groups"] );
+	t( "Text Contains", "a:contains(\"(Link)\")", ["groups"] );
+	t( "Text Contains", "a:contains(Google Groups (Link))", ["groups"] );
+	t( "Text Contains", "a:contains((Link))", ["groups"] );
+
+
+	tmp = document.createElement("div");
+	tmp.attribs.id = "tmp_input";
+	document.body.children.push( tmp );
+
+	[ "button", "submit", "reset" ].forEach(function( type ) {
+		var els = jQuery(
+			"<input id='input_%' type='%'/><button id='button_%' type='%'>test</button>"
+			.replace( /%/g, type )
+		).appendTo( tmp );
+
+		t( "Input Buttons :" + type, "#tmp_input :" + type, [ "input_" + type, "button_" + type ] );
+
+		ok( Sizzle.matchesSelector( els[0], ":" + type ), "Input Matches :" + type );
+		ok( Sizzle.matchesSelector( els[1], ":" + type ), "Button Matches :" + type );
+	});
+
+	document.body.children.pop();
+
+	// Recreate tmp
+	tmp = document.createElement("div");
+	tmp.attribs.id = "tmp_input";
+	tmp.children = helper.getDOM("<span>Hello I am focusable.</span>");
+	// Setting tabIndex should make the element focusable
+	// http://dev.w3.org/html5/spec/single-page.html#focus-management
+	document.body.children.push( tmp );
+	tmp.tabIndex = 0;
+	//tmp.focus();
+	if ( document.activeElement !== tmp || (document.hasFocus && !document.hasFocus()) ||
+		(document.querySelectorAll && !document.querySelectorAll("div:focus").length) ) {
+		ok( true, "The div was not focused. Skip checking the :focus match." );
+		ok( true, "The div was not focused. Skip checking the :focus match." );
+	} else {
+		t( "tabIndex element focused", ":focus", [ "tmp_input" ] );
+		ok( Sizzle.matchesSelector( tmp, ":focus" ), ":focus matches tabIndex div" );
+	}
+
+	// Blur tmp
+	//tmp.blur();
+	//document.body.focus();
+	//ok( !Sizzle.matchesSelector( tmp, ":focus" ), ":focus doesn't match tabIndex div" );
+	document.body.children.pop();
+
+	// Input focus/active
+	input = document.createElement("input");
+	input.attribs.type = "text";
+	input.attribs.id = "focus-input";
+
+	document.body.children.push( input );
+	//input.focus();
+
+	// Inputs can't be focused unless the document has focus
+	if ( document.activeElement !== input || (document.hasFocus && !document.hasFocus()) ||
+		(document.querySelectorAll && !document.querySelectorAll("input:focus").length) ) {
+		ok( true, "The input was not focused. Skip checking the :focus match." );
+		ok( true, "The input was not focused. Skip checking the :focus match." );
+	} else {
+		t( "Element focused", "input:focus", [ "focus-input" ] );
+		ok( Sizzle.matchesSelector( input, ":focus" ), ":focus matches" );
+	}
+
+	//input.blur();
+
+	// When IE is out of focus, blur does not work. Force it here.
+	if ( document.activeElement === input ) {
+		document.body.focus();
+	}
+
+	//ok( !Sizzle.matchesSelector( input, ":focus" ), ":focus doesn't match" );
+	document.body.children.pop();
+
+
+
+	deepEqual(
+		Sizzle( "[id='select1'] *:not(:last-child), [id='select2'] *:not(:last-child)", q("qunit-fixture")[0] ),
+		q( "option1a", "option1b", "option1c", "option2a", "option2b", "option2c" ),
+		"caching system tolerates recursive selection"
+	);
+
+	// Tokenization edge cases
+	t( "Sequential pseudos", "#qunit-fixture p:has(:contains(mark)):has(code)", ["ap"] );
+	t( "Sequential pseudos", "#qunit-fixture p:has(:contains(mark)):has(code):contains(This link)", ["ap"] );
+
+	t( "Pseudo argument containing ')'", "p:has(>a.GROUPS[src!=')'])", ["ap"] );
+	t( "Pseudo argument containing ')'", "p:has(>a.GROUPS[src!=')'])", ["ap"] );
+	t( "Pseudo followed by token containing ')'", "p:contains(id=\"foo\")[id!=\\)]", ["sndp"] );
+	t( "Pseudo followed by token containing ')'", "p:contains(id=\"foo\")[id!=')']", ["sndp"] );
+
+	t( "Multi-pseudo", "#ap:has(*), #ap:has(*)", ["ap"] );
+	//t( "Multi-positional", "#ap:gt(0), #ap:lt(1)", ["ap"] );
+	t( "Multi-pseudo with leading nonexistent id", "#nonexistent:has(*), #ap:has(*)", ["ap"] );
+	//t( "Multi-positional with leading nonexistent id", "#nonexistent:gt(0), #ap:lt(1)", ["ap"] );
+
+	t( "Tokenization stressor", "a[class*=blog]:not(:has(*, :contains(!)), :contains(!)), br:contains(]), p:contains(]), :not(:empty):not(:parent)", ["ap", "mark","yahoo","simon"] );
+});
+
+
+test("pseudo - :not", function() {
+	expect( 43 );
+
+	t( "Not", "a.blog:not(.link)", ["mark"] );
+	//t( ":not() with :first", "#foo p:not(:first) .link", ["simon"] );
+
+	t( "Not - multiple", "#form option:not(:contains(Nothing),#option1b,:selected)", ["option1c", "option1d", "option2b", "option2c", "option3d", "option3e", "option4e", "option5b", "option5c"] );
+	t( "Not - recursive", "#form option:not(:not(:selected))[id^='option3']", [ "option3b", "option3c"] );
+
+	t( ":not() failing interior", "#qunit-fixture p:not(.foo)", ["firstp","ap","sndp","en","sap","first"] );
+	t( ":not() failing interior", "#qunit-fixture p:not(div.foo)", ["firstp","ap","sndp","en","sap","first"] );
+	t( ":not() failing interior", "#qunit-fixture p:not(p.foo)", ["firstp","ap","sndp","en","sap","first"] );
+	t( ":not() failing interior", "#qunit-fixture p:not(#blargh)", ["firstp","ap","sndp","en","sap","first"] );
+	t( ":not() failing interior", "#qunit-fixture p:not(div#blargh)", ["firstp","ap","sndp","en","sap","first"] );
+	t( ":not() failing interior", "#qunit-fixture p:not(p#blargh)", ["firstp","ap","sndp","en","sap","first"] );
+
+	t( ":not Multiple", "#qunit-fixture p:not(a)", ["firstp","ap","sndp","en","sap","first"] );
+	t( ":not Multiple", "#qunit-fixture p:not( a )", ["firstp","ap","sndp","en","sap","first"] );
+	t( ":not Multiple", "#qunit-fixture p:not( p )", [] );
+	t( ":not Multiple", "#qunit-fixture p:not(a, b)", ["firstp","ap","sndp","en","sap","first"] );
+	t( ":not Multiple", "#qunit-fixture p:not(a, b, div)", ["firstp","ap","sndp","en","sap","first"] );
+	t( ":not Multiple", "p:not(p)", [] );
+	t( ":not Multiple", "p:not(a,p)", [] );
+	t( ":not Multiple", "p:not(p,a)", [] );
+	t( ":not Multiple", "p:not(a,p,b)", [] );
+	t( ":not Multiple", ":input:not(:image,:input,:submit)", [] );
+	t( ":not Multiple", "#qunit-fixture p:not(:has(a), :nth-child(1))", ["first"] );
+
+	t( "No element not selector", ".container div:not(.excluded) div", [] );
+
+	t( ":not() Existing attribute", "#form select:not([multiple])", ["select1", "select2", "select5"]);
+	t( ":not() Equals attribute", "#form select:not([name=select1])", ["select2", "select3", "select4","select5"]);
+	t( ":not() Equals quoted attribute", "#form select:not([name='select1'])", ["select2", "select3", "select4", "select5"]);
+
+	t( ":not() Multiple Class", "#foo a:not(.blog)", ["yahoo", "anchor2"] );
+	t( ":not() Multiple Class", "#foo a:not(.link)", ["yahoo", "anchor2"] );
+	t( ":not() Multiple Class", "#foo a:not(.blog.link)", ["yahoo", "anchor2"] );
+
+	t( ":not chaining (compound)", "#qunit-fixture div[id]:not(:has(div, span)):not(:has(*))", ["nothiddendivchild", "divWithNoTabIndex"] );
+	t( ":not chaining (with attribute)", "#qunit-fixture form[id]:not([action$='formaction']):not(:button)", ["lengthtest", "name-tests", "testForm"] );
+	t( ":not chaining (colon in attribute)", "#qunit-fixture form[id]:not([action='form:action']):not(:button)", ["form", "lengthtest", "name-tests", "testForm"] );
+	t( ":not chaining (colon in attribute and nested chaining)", "#qunit-fixture form[id]:not([action='form:action']:button):not(:input)", ["form", "lengthtest", "name-tests", "testForm"] );
+	t( ":not chaining", "#form select:not(.select1):contains(Nothing) > option:not(option)", [] );
+
+	/*
+	t( "positional :not()", "#foo p:not(:last)", ["sndp", "en"] );
+	t( "positional :not() prefix", "#foo p:not(:last) a", ["yahoo"] );
+	t( "compound positional :not()", "#foo p:not(:first, :last)", ["en"] );
+	t( "compound positional :not()", "#foo p:not(:first, :even)", ["en"] );
+	t( "compound positional :not()", "#foo p:not(:first, :odd)", ["sap"] );
+	t( "reordered compound positional :not()", "#foo p:not(:odd, :first)", ["sap"] );
+
+	t( "positional :not() with pre-filter", "#foo p:not([id]:first)", ["en", "sap"] );
+	t( "positional :not() with post-filter", "#foo p:not(:first[id])", ["en", "sap"] );
+	t( "positional :not() with pre-filter", "#foo p:not([lang]:first)", ["sndp", "sap"] );
+	t( "positional :not() with post-filter", "#foo p:not(:first[lang])", ["sndp", "en", "sap"] );
+	*/
+});
+
+/*
+test("pseudo - position", function() {
+	expect( 33 );
+
+	t( "First element", "div:first", ["qunit"] );
+	t( "First element(case-insensitive)", "div:fiRst", ["qunit"] );
+	t( "nth Element", "#qunit-fixture p:nth(1)", ["ap"] );
+	t( "First Element", "#qunit-fixture p:first", ["firstp"] );
+	t( "Last Element", "p:last", ["first"] );
+	t( "Even Elements", "#qunit-fixture p:even", ["firstp","sndp","sap"] );
+	t( "Odd Elements", "#qunit-fixture p:odd", ["ap","en","first"] );
+	t( "Position Equals", "#qunit-fixture p:eq(1)", ["ap"] );
+	t( "Position Equals (negative)", "#qunit-fixture p:eq(-1)", ["first"] );
+	t( "Position Greater Than", "#qunit-fixture p:gt(0)", ["ap","sndp","en","sap","first"] );
+	t( "Position Less Than", "#qunit-fixture p:lt(3)", ["firstp","ap","sndp"] );
+
+	t( "Check position filtering", "div#nothiddendiv:eq(0)", ["nothiddendiv"] );
+	t( "Check position filtering", "div#nothiddendiv:last", ["nothiddendiv"] );
+	t( "Check position filtering", "div#nothiddendiv:not(:gt(0))", ["nothiddendiv"] );
+	t( "Check position filtering", "#foo > :not(:first)", ["en", "sap"] );
+	t( "Check position filtering", "#qunit-fixture select > :not(:gt(2))", ["option1a", "option1b", "option1c"] );
+	t( "Check position filtering", "#qunit-fixture select:lt(2) :not(:first)", ["option1b", "option1c", "option1d", "option2a", "option2b", "option2c", "option2d"] );
+	t( "Check position filtering", "div.nothiddendiv:eq(0)", ["nothiddendiv"] );
+	t( "Check position filtering", "div.nothiddendiv:last", ["nothiddendiv"] );
+	t( "Check position filtering", "div.nothiddendiv:not(:lt(0))", ["nothiddendiv"] );
+
+	t( "Check element position", "#qunit-fixture div div:eq(0)", ["nothiddendivchild"] );
+	t( "Check element position", "#select1 option:eq(3)", ["option1d"] );
+	t( "Check element position", "#qunit-fixture div div:eq(10)", ["names-group"] );
+	t( "Check element position", "#qunit-fixture div div:first", ["nothiddendivchild"] );
+	t( "Check element position", "#qunit-fixture div > div:first", ["nothiddendivchild"] );
+	t( "Check element position", "#dl div:first div:first", ["foo"] );
+	t( "Check element position", "#dl div:first > div:first", ["foo"] );
+	t( "Check element position", "div#nothiddendiv:first > div:first", ["nothiddendivchild"] );
+	t( "Chained pseudo after a pos pseudo", "#listWithTabIndex li:eq(0):contains(Rice)", ["foodWithNegativeTabIndex"] );
+
+	t( "Check sort order with POS and comma", "#qunit-fixture em>em>em>em:first-child,div>em:first", ["siblingfirst", "siblinggreatgrandchild"] );
+
+	t( "Isolated position", ":last", ["last"] );
+
+	deepEqual( Sizzle( "*:lt(2) + *", null, [], Sizzle("#qunit-fixture > p") ), q("ap"), "Seeded pos with trailing relative" );
+
+	// jQuery #12526
+	var context = jQuery("#qunit-fixture").append("<div id='jquery12526'></div>")[0];
+	deepEqual( Sizzle( ":last", context ), q("jquery12526"), "Post-manipulation positional" );
+});
+*/
+
+test("pseudo - form", function() {
+	expect( 10 );
+
+	var extraTexts = jQuery("<input id=\"impliedText\"/><input id=\"capitalText\" type=\"TEXT\">").appendTo("#form");
+
+	t( "Form element :input", "#form :input", ["text1", "text2", "radio1", "radio2", "check1", "check2", "hidden1", "hidden2", "name", "search", "button", "area1", "select1", "select2", "select3", "select4", "select5", "impliedText", "capitalText"] );
+	t( "Form element :radio", "#form :radio", ["radio1", "radio2"] );
+	t( "Form element :checkbox", "#form :checkbox", ["check1", "check2"] );
+	t( "Form element :text", "#form :text", ["text1", "text2", "hidden2", "name", "impliedText", "capitalText"] );
+	t( "Form element :radio:checked", "#form :radio:checked", ["radio2"] );
+	t( "Form element :checkbox:checked", "#form :checkbox:checked", ["check1"] );
+	t( "Form element :radio:checked, :checkbox:checked", "#form :radio:checked, #form :checkbox:checked", ["radio2", "check1"] );
+
+	t( "Selected Option Element", "#form option:selected", ["option1a","option2d","option3b","option3c","option4b","option4c","option4d","option5a"] );
+	t( "Selected Option Element are also :checked", "#form option:checked", ["option1a","option2d","option3b","option3c","option4b","option4c","option4d","option5a"] );
+	t( "Hidden inputs should be treated as enabled. See QSA test.", "#hidden1:enabled", ["hidden1"] );
+
+	extraTexts.remove();
+});
+
+test("pseudo - :target and :root", function() {
+	expect( 2 );
+	/* // TODO add shim from qwery tests
+	// Target
+	var oldHash,
+	$link = jQuery("<a/>").attr({
+		href: "#",
+		id: "new-link"
+	}).appendTo("#qunit-fixture");
+
+	oldHash = window.location.hash;
+	window.location.hash = "new-link";
+
+	t( ":target", ":target", ["new-link"] );
+
+	$link.remove();
+	window.location.hash = oldHash;*/
+
+	// Root
+	equal( Sizzle(":root")[0], document.documentElement, ":root selector" );
+});
+
+/*
+// TODO
+test("pseudo - :lang", function() {
+	expect( 105 );
+
+	var docElem = document.documentElement,
+		docXmlLang = docElem.getAttribute("xml:lang"),
+		docLang = docElem.lang,
+		foo = document.getElementById("foo"),
+		anchor = document.getElementById("anchor2"),
+		xml = createWithFriesXML(),
+		testLang = function( text, elem, container, lang, extra ) {
+			var message,
+				full = lang + "-" + extra;
+
+			message = "lang=" + lang + " " + text;
+			container.setAttribute( container.ownerDocument.documentElement.nodeName === "HTML" ? "lang" : "xml:lang", lang );
+			assertMatch( message, elem, ":lang(" + lang + ")" );
+			assertMatch( message, elem, ":lang(" + mixCase(lang) + ")" );
+			assertNoMatch( message, elem, ":lang(" + full + ")" );
+			assertNoMatch( message, elem, ":lang(" + mixCase(full) + ")" );
+			assertNoMatch( message, elem, ":lang(" + lang + "-)" );
+			assertNoMatch( message, elem, ":lang(" + full + "-)" );
+			assertNoMatch( message, elem, ":lang(" + lang + "glish)" );
+			assertNoMatch( message, elem, ":lang(" + full + "glish)" );
+
+			message = "lang=" + full + " " + text;
+			container.setAttribute( container.ownerDocument.documentElement.nodeName === "HTML" ? "lang" : "xml:lang", full );
+			assertMatch( message, elem, ":lang(" + lang + ")" );
+			assertMatch( message, elem, ":lang(" + mixCase(lang) + ")" );
+			assertMatch( message, elem, ":lang(" + full + ")" );
+			assertMatch( message, elem, ":lang(" + mixCase(full) + ")" );
+			assertNoMatch( message, elem, ":lang(" + lang + "-)" );
+			assertNoMatch( message, elem, ":lang(" + full + "-)" );
+			assertNoMatch( message, elem, ":lang(" + lang + "glish)" );
+			assertNoMatch( message, elem, ":lang(" + full + "glish)" );
+		},
+		mixCase = function( str ) {
+			var ret = str.split(""),
+				i = ret.length;
+			while ( i-- ) {
+				if ( i & 1 ) {
+					ret[i] = ret[i].toUpperCase();
+				}
+			}
+			return ret.join("");
+		},
+		assertMatch = function( text, elem, selector ) {
+			ok( Sizzle.matchesSelector( elem, selector ), text + " match " + selector );
+		},
+		assertNoMatch = function( text, elem, selector ) {
+			ok( !Sizzle.matchesSelector( elem, selector ), text + " fail " + selector );
+		};
+
+	// Prefixing and inheritance
+	ok( Sizzle.matchesSelector( docElem, ":lang(" + docElem.lang + ")" ), "starting :lang" );
+	testLang( "document", anchor, docElem, "en", "us" );
+	testLang( "grandparent", anchor, anchor.parentNode.parentNode, "yue", "hk" );
+	ok( !Sizzle.matchesSelector( anchor, ":lang(en), :lang(en-us)" ),
+		":lang does not look above an ancestor with specified lang" );
+	testLang( "self", anchor, anchor, "es", "419" );
+	ok( !Sizzle.matchesSelector( anchor, ":lang(en), :lang(en-us), :lang(yue), :lang(yue-hk)" ),
+		":lang does not look above self with specified lang" );
+
+	// Searching by language tag
+	anchor.parentNode.parentNode.lang = "arab";
+	anchor.parentNode.lang = anchor.parentNode.id = "ara-sa";
+	anchor.lang = "ara";
+	deepEqual( Sizzle( ":lang(ara)", foo ), [ anchor.parentNode, anchor ], "Find by :lang" );
+
+	// Selector validity
+	anchor.parentNode.lang = "ara";
+	anchor.lang = "ara\\b";
+	deepEqual( Sizzle( ":lang(ara\\b)", foo ), [], ":lang respects backslashes" );
+	deepEqual( Sizzle( ":lang(ara\\\\b)", foo ), [ anchor ], ":lang respects escaped backslashes" );
+	raises(function() {
+		Sizzle.call( null, "dl:lang(c++)" );
+	}, function( e ) {
+		return e.message.indexOf("Syntax error") >= 0;
+	}, ":lang value must be a valid identifier" );
+
+	// XML
+	foo = jQuery( "response", xml )[0];
+	anchor = jQuery( "#seite1", xml )[0];
+	testLang( "XML document", anchor, xml.documentElement, "en", "us" );
+	testLang( "XML grandparent", anchor, foo, "yue", "hk" );
+	ok( !Sizzle.matchesSelector( anchor, ":lang(en), :lang(en-us)" ),
+		"XML :lang does not look above an ancestor with specified lang" );
+	testLang( "XML self", anchor, anchor, "es", "419" );
+	ok( !Sizzle.matchesSelector( anchor, ":lang(en), :lang(en-us), :lang(yue), :lang(yue-hk)" ),
+		"XML :lang does not look above self with specified lang" );
+
+	// Cleanup
+	if ( docXmlLang == null ) {
+		docElem.removeAttribute("xml:lang");
+	} else {
+		docElem.setAttribute( "xml:lang", docXmlLang );
+	}
+	docElem.lang = docLang;
+});
+*/
+
+test("caching", function() {
+	expect( 1 );
+	Sizzle( ":not(code)", document.getElementById("ap") );
+	deepEqual( Sizzle( ":not(code)", document.getElementById("foo") ), q("sndp", "en", "yahoo", "sap", "anchor2", "simon"), "Reusing selector with new context" );
+});
+/*
+asyncTest( "Iframe dispatch should not affect Sizzle, see jQuery #13936", 1, function() {
+	var i = 0,
+		thrown = false,
+		iframe = document.getElementById("iframe"),
+		iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
+
+	jQuery( iframe ).on( "load", function() {
+		var doc;
+
+		try {
+			i++;
+			doc = this.contentDocument || this.contentWindow.document;
+			Sizzle( "form", doc ).pop().submit();
+
+		} catch ( e ) {
+			thrown = true;
+		}
+
+		if ( i === 2 ) {
+			jQuery( this ).off("load");
+			ok( !thrown, "Iframe reload should not affect Sizzle, see jQuery #13936" );
+			start();
+		}
+	});
+
+	iframeDoc.open();
+	iframeDoc.write("<body><form></form></body>");
+	iframeDoc.close();
+});
+*/
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..99a486d
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,22 @@
+describe("nwmatcher", function(){
+	require("./nwmatcher/");
+});
+
+describe("sizzle", function(){
+	describe("selector", function(){
+		require("./sizzle/selector");
+	});
+});
+
+describe("qwery", function(){
+	exportsRun(require("./qwery/"));
+});
+
+function exportsRun(mod){
+	Object.keys(mod).forEach(function(name){
+		if(typeof mod[name] === "object") describe(name, function(){
+				exportsRun(mod[name]);
+			});
+		else it(name, mod[name]);
+	});
+}
\ No newline at end of file
diff --git a/test/tools/bench.js b/test/tools/bench.js
new file mode 100644
index 0000000..ef251b1
--- /dev/null
+++ b/test/tools/bench.js
@@ -0,0 +1,10 @@
+var ben = require("ben"),
+	testString = "doo, *#foo > elem.bar[class$=bAz i]:not([ id *= \"2\" ]):nth-child(2n)",
+	helper = require("./helper.js"),
+	CSSselect = helper.CSSselect,
+	compile = CSSselect.compile,
+	dom = helper.getDefaultDom();
+
+//console.log("Parsing took:", ben(1e5, function(){compile(testString);}));
+var compiled = compile(testString);
+console.log("Executing took:", ben(1e6, function(){CSSselect(compiled, dom);})*1e3);
\ No newline at end of file
diff --git a/test/tools/docs/W3C_Selectors.html b/test/tools/docs/W3C_Selectors.html
new file mode 100644
index 0000000..09c67f3
--- /dev/null
+++ b/test/tools/docs/W3C_Selectors.html
@@ -0,0 +1,2034 @@
+<!-- http://www.w3.org/TR/2001/CR-css3-selectors-20011113/ -->
+<html><head><title>Selectors</title>
+<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
+<meta http-equiv="Content-Style-Type" content="text/css">
+<meta http-equiv="Content-Language" content="en">
+<link href="default.css" type="text/css" rel="stylesheet">
+<style type="text/css">
+pre {
+	border-right: medium none; padding-right: 0.3cm; border-top: medium none; padding-left: 0.3cm; font-size: 92%; padding-bottom: 0px; margin: 1em 1cm; border-left: medium none; padding-top: 0px; border-bottom: medium none; white-space: pre; background-color: #d5d5d5
+}
+.code {
+	font-family: monospace
+}
+table.selectorsReview th {
+	background: gray; color: white
+}
+table.selectorsReview th .pattern {
+	width: 20%; font-family: monospace
+}
+table.selectorsReview th .meaning {
+	width: 45%
+}
+table.selectorsReview tr .described {
+	WIDTH: 25%
+}
+table.selectorsReview tr .origin {
+	width: 10%; text-align: center
+}
+table.tprofile th.title {
+	background: gray; color: white
+}
+table.tprofile th {
+	width: 29%
+}
+table.tprofile td {
+	width: 71%
+}
+.toc {
+	list-style-type: none
+}
+.subtoc ul {
+	list-style-type: none
+}
+.subtoc ol {
+	list-style-type: none
+}
+.profile {
+	margin: 1cm
+}
+.editorNote {
+	color: red; font-style: italic
+}
+.e-mail {
+	font-size: 90%
+}
+h1 {
+	font-size: 200%
+}
+h2 {
+	font-size: 170%
+}
+h3 {
+	font-size: 150%
+}
+h4 {
+	font-size: 130%
+}
+h5 {
+	font-size: 120%
+}
+h6 {
+	font-size: 110%
+}
+ul.changes {
+	font-size: smaller
+}
+table.selectorsReview {
+	font-size: smaller; border-collapse: collapse
+}
+.figure {
+	text-align: center
+}
+</style>
+<link href="http://www.w3.org/StyleSheets/TR/W3C-CR.css" type="text/css" rel="stylesheet">
+</head>
+<body>
+<div class="head"><p><a href="http://www.w3.org/"><img height="48" alt="W3C" src="http://www.w3.org/Icons/w3c_home" width="72"></a>
+</p><h1><span class="modulename">Selectors</span></h1>
+  <h2>W3C Candidate Recommendation 13 November 2001</h2>
+  <dl>
+    <dt>This version:
+    </dt><dd><a href="http://www.w3.org/TR/2001/CR-css3-selectors-20011113">
+                 http://www.w3.org/TR/2001/CR-css3-selectors-20011113</a>
+    </dd><dt>Latest version:
+    </dt><dd><a href="http://www.w3.org/TR/css3-selectors">
+                 http://www.w3.org/TR/css3-selectors</a>
+    </dd><dt>Previous version:
+    </dt><dd><a href="http://www.w3.org/TR/2001/WD-css3-selectors-20010126">
+                 http://www.w3.org/TR/2001/WD-css3-selectors-20010126</a>
+    </dd><dt><a name="editors-list"></a>Editors:
+    </dt><dd><a href="mailto:glazman at netscape.com">Daniel Glazman</a> (<span class="company"><a href="http://www.netscape.com/">Netscape/AOL</a></span>)
+    </dd><dd><a href="mailto:tantekc at microsoft.com">Tantek Çelik</a> (<span class="company"><a href="http://www.microsoft.com/">Microsoft Corporation</a></span>)
+    </dd><dd><a href="mailto:ian at hixie.ch">Ian Hickson</a>
+    </dd><dd>Peter Linss (former editor, formerly of <span class="company"><a href="http://www.netscape.com/">Netscape/AOL</a></span>)
+    </dd><dd>John Williams (former editor, <span class="company"><a href="http://www.quark.com/">Quark, Inc.</a></span>) 
+  </dd></dl>
+<p class="copyright"><a href="http://www.w3.org/Consortium/Legal/ipr-notice-20000612#Copyright">Copyright</a>
+©2001 <a href="http://www.w3.org/"><abbr title="World Wide Web Consortium">W3C</abbr></a><sup>®</sup> (<a href="http://www.lcs.mit.edu/"><abbr title="Massachusetts Institute of Technology">MIT</abbr></a>, <a href="http://www.inria.fr/"><abbr lang="fr" title="Institut National de Recherche en Informatique et Automatique">INRIA</abbr></a>,
+<a href="http://www.keio.ac.jp/">Keio</a>), All Rights Reserved. W3C <a href="http://www.w3.org/Consortium/Legal/ipr-notice-20000612#Legal_Disclaimer">liability</a>,
+<a href="http://www.w3.org/Consortium/Legal/ipr-notice-20000612#W3C_Trademarks">trademark</a>,
+<a href="http://www.w3.org/Consortium/Legal/copyright-documents-19990405">document
+use</a> and <a href="http://www.w3.org/Consortium/Legal/copyright-software-19980720">software
+licensing</a> rules apply.
+</p><hr title="Separator for header">
+</div>
+
+<h2><a name="abstract"></a>Abstract</h2>
+<p><acronym title="Cascading Style Sheets">CSS</acronym> (Cascading Style Sheets) is a language for describing the rendering of
+  <acronym title="Hypertext Markup Language">HTML</acronym> and <acronym title="Extensible Markup Language">XML</acronym>
+  documents on screen, on paper, in speech, etc. To bind style properties
+  to elements in the document, CSS uses <em>selectors,</em> which are patterns
+  that match one or more elements. This document describes the selectors that are proposed
+  for CSS level 3. It includes and extends the selectors of CSS level 2. 
+</p><h2><a name="status"></a>Status of this document</h2>
+<p>This document is one of the "modules" of the upcoming CSS3 specification. It
+  not only describes the selectors that already exist in <a href="#CSS1"><abbr title="CSS level 1">CSS1</abbr></a> and <a href="#CSS2"><abbr title="CSS level 2">CSS2</abbr></a>,
+  but also proposes new selectors for <abbr title="CSS level 3">CSS3</abbr> as well as for
+  other languages that may need them. The CSS Working Group doesn't expect that all
+  implementations of CSS3 will have to implement all selectors. Instead,
+  there will probably be a small number of variants of CSS3, so-called "profiles".
+  For example, it may be that only a profile for non-interactive user agents
+  will include all of the proposed selectors. 
+</p><p>This specification is being put forth as a <a href="http://www.w3.org/TR/#About">Candidate
+  Recommendation</a> by the <a href="http://www.w3.org/Style/Group">CSS Working
+  Group</a>. This document is a revision of the <a href="http://www.w3.org/TR/2001/WD-css3-selectors-20010126">Working
+  Draft dated 2001 January 26</a>, and has incorporated suggestions received
+  during last call review, comments, and further deliberations of the W3C CSS
+  Working Group.
+</p><p>The duration of Candidate Recommendation is expected to last approximately
+  six months (ending <strong>May, 2002</strong>). All persons are encouraged
+  to review and implement this specification and return comments to the (<a href="http://lists.w3.org/Archives/Public/www-style/">archived</a>) public mailing
+  list <a href="http://www.w3.org/Mail/Lists.html#www-style">www-style</a> (see <a href="http://www.w3.org/Mail/Request">instructions</a>).
+  W3C Members can also send comments directly to the CSS Working Group. 
+</p><p>Should this specification prove impossible to implement, the Working Group
+  will return the document to Working Draft status and make necessary changes.
+  Otherwise, the Working Group anticipates asking the W3C Director to advance
+  this document to Proposed Recommendation.
+</p><p>This is still a draft document and may be updated, replaced, or obsoleted by
+  other documents at any time. It is inappropriate to cite a W3C Candidate Recommendation
+  as other than "work in progress." A list of current W3C working drafts
+  can be found at <a href="http://www.w3.org/TR">http://www.w3.org/TR</a>.<br>
+  <br>
+  This document may be available in <a href="http://www.w3.org/Style/css3-selectors-updates/translations">translation</a>.
+  The English version of this specification is the only normative version.
+</p><h2><a name="dependencies"></a>Dependencies with other CSS3 Modules</h2>
+<ul>
+  <li>General Syntax
+  </li><li>Value Assignment, Cascade and Inheritance
+  </li><li>Generated Content / Markers
+  </li><li>User Interface
+</li></ul>
+<div class="subtoc">
+<h2><a name="contents">Table of contents</a></h2>
+<ul class="toc">
+  <li class="tocline2"><a href="#context">1.
+  Context</a>
+  <ul>
+    <li><a href="#changesFromCSS2">1.1
+    Changes from CSS2</a> </li></ul>
+  </li><li class="tocline2"><a href="#selectors">2.
+  Selectors</a>
+  </li><li class="tocline2"><a href="#casesens">3.
+  Case sensitivity</a>
+  </li><li class="tocline2"><a href="#selector-syntax">4. Selector
+  syntax</a>
+  </li><li class="tocline2"><a href="#grouping">5.
+  Groups of selectors</a>
+  </li><li class="tocline2"><a href="#simple-selectors">6. Simple
+  selectors</a>
+  <ul class="toc">
+    <li class="tocline3"><a href="#type-selectors">6.1 Type
+    selectors</a>
+    <ul class="toc">
+      <li class="tocline4"><a href="#typenmsp">6.1.1 Type selectors
+      and Namespaces</a> </li></ul>
+    </li><li class="tocline3"><a href="#universal-selector">6.2 Universal
+    selector</a>
+    <ul>
+      <li><a href="#univnmsp">6.2.1
+      Universal selector and Namespaces</a> </li></ul>
+    </li><li class="tocline3"><a href="#attribute-selectors">6.3
+    Attribute selectors</a>
+    <ul class="toc">
+      <li class="tocline4"><a href="#attribute-representation">6.3.1
+      Representation of attributes and attributes values</a>
+      </li><li><a href="#attribute-substrings">6.3.2
+      Substring matching attribute selectors</a>
+      </li><li class="tocline4"><a href="#attrnmsp">6.3.3 Attribute
+      selectors and Namespaces</a>
+      </li><li class="tocline4"><a href="#def-values">6.3.4 Default
+      attribute values in DTDs</a> </li></ul>
+    </li><li class="tocline3"><a href="#class-html">6.4 Class
+    selectors</a>
+    </li><li class="tocline3"><a href="#id-selectors">6.5 ID
+    selectors</a>
+    </li><li class="tocline3"><a href="#pseudo-classes">6.6
+    Pseudo-classes</a>
+    <ul class="toc">
+      <li class="tocline4"><a href="#dynamic-pseudos">6.6.1 Dynamic
+      pseudo-classes</a>
+      </li><li class="tocline4"><a href="#target-pseudo">6.6.2 The
+      :target pseudo-class</a>
+      </li><li class="tocline4"><a href="#lang-pseudo">6.6.3 The :lang()
+      pseudo-class</a>
+      </li><li class="tocline4"><a href="#UIstates">6.6.4 UI element
+      states pseudo-classes</a>
+      </li><li class="tocline4"><a href="#structural-pseudos">6.6.5
+      Structural pseudo-classes</a>
+      <ul>
+        <li><a href="#root-pseudo">:root
+        pseudo-class</a>
+        </li><li><a href="#nth-child-pseudo">:nth-child()
+        pseudo-class</a>
+        </li><li><a href="#nth-last-child-pseudo">:nth-last-child()</a>
+
+        </li><li><a href="#nth-of-type-pseudo">:nth-of-type()
+        pseudo-class</a>
+        </li><li><a href="#nth-last-of-type-pseudo">:nth-last-of-type()</a>
+
+        </li><li><a href="#first-child-pseudo">:first-child
+        pseudo-class</a>
+        </li><li><a href="#last-child-pseudo">:last-child
+        pseudo-class</a>
+        </li><li><a href="#first-of-type-pseudo">:first-of-type
+        pseudo-class</a>
+        </li><li><a href="#last-of-type-pseudo">:last-of-type
+        pseudo-class</a>
+        </li><li><a href="#only-child-pseudo">:only-child
+        pseudo-class</a>
+        </li><li><a href="#only-of-type-pseudo">:only-of-type
+        pseudo-class</a>
+        </li><li><a href="#empty-pseudo">:empty
+        pseudo-class</a> </li></ul>
+      </li><li class="tocline4"><a href="#content-selectors">6.6.6
+      Content pseudo-class</a>
+      </li><li><a href="#negation">6.6.7 The
+      negation pseudo-class</a> </li></ul></li></ul>
+  </li><li><a href="#pseudo-elements">7.
+  Pseudo-elements</a>
+  <ul>
+    <li><a href="#first-line">7.1 The
+    :first-line pseudo-element</a>
+    </li><li><a href="#first-letter">7.2 The
+    :first-letter pseudo-element</a>
+    </li><li><a href="#UIfragments">7.3 UI
+    element fragments pseudo-elements</a>
+    </li><li><a href="#gen-content">7.4 The
+    :before and :after pseudo-elements</a> </li></ul>
+  </li><li class="tocline2"><a href="#combinators">8. Combinators</a>
+  <ul class="toc">
+    <li class="tocline3"><a href="#descendant-combinators">8.1
+    Descendant combinators</a>
+    </li><li class="tocline3"><a href="#child-combinators">8.2 Child
+    combinators</a>
+    </li><li class="tocline3"><a href="#adjacent-combinators">8.3
+    Adjacent sibling combinators</a>
+    <ul class="toc">
+      <li class="tocline4"><a href="#adjacent-d-combinators">8.3.1
+      Adjacent direct combinators</a>
+      </li><li class="tocline4"><a href="#adjacent-i-combinators">8.3.2
+      Adjacent indirect combinators</a> </li></ul></li></ul>
+  </li><li class="tocline2"><a href="#specificity">9. Calculating a
+  selector's specificity</a>
+  </li><li class="tocline2"><a href="#w3cselgrammar">10. The grammar of
+  <span class="modulename">Selectors</span></a>
+  <ul class="toc">
+    <li class="tocline3"><a href="#grammar">10.1 Grammar</a>
+    </li><li class="tocline3"><a href="#lex">10.2
+    Lexical scanner</a> </li></ul>
+  </li><li class="tocline2"><a href="#downlevel">11. Namespaces and
+  Down-Level clients</a>
+  </li><li class="tocline2"><a href="#profiling">12. Profiles</a>
+  </li><li><a href="#Conformance">13. Conformance
+  and Requirements</a>
+  </li><li><a href="#Tests">14. Tests</a>
+  </li><li><a href="#ACKS">15.
+  Acknowledgements</a>
+  </li><li class="tocline2"><a href="#references">16. References</a> <!--<li class="tocline2"><a href="#changes">Changes from previous version</a>--></li></ul></div>
+<h2><a name="context">1. Context</a></h2>
+<p>Members of the CSS+FP Working Group proposed during the Clamart meeting to
+modularize the CSS specification. 
+</p><p>This modularization, and the externalization of the general syntax of CSS
+will reduce the size of the specification and allow new specifications
+to use selectors and/or CSS general syntax. For instance, behaviors or tree
+transformations. 
+</p><p>This specification contains its own <a href="#Tests">test cases</a>, one test per concept introduced in this document.
+  These tests are not full conformance tests but are intended to provide users
+  with a way to check if a part of this specification is implemented <i>ad minima</i>
+  or is not implemented at all.
+</p><h3><a name="changesFromCSS2"></a>1.1 Changes from CSS2</h3>
+<p>The main differences between the selectors in CSS2 and those in
+ <span class="modulename">Selectors</span> are:
+</p><ul>
+  <li>the list of basic definitions (selector, group of selectors, simple
+  selector, etc.) has been clarified
+  </li><li>an optional namespace component is now allowed in type element selectors,
+  the universal selector and attribute selectors
+  </li><li>a new combinator
+  </li><li>new simple selectors including substring matching attribute selectors, and new
+  pseudo-classes
+  </li><li>new pseudo-elements, and introduction of the "::" convention for pseudo-elements
+  </li><li>a rewriting of the selectors grammar
+  </li><li>profiles to be added to specifications integrating <span class="modulename">Selectors</span> and
+  defining the set of selectors which is actually supported by each
+  specification
+  </li><li><span class="modulename">Selectors</span> are now a CSS3 Module and an independent specification.
+  Other  specifications can now refer to this document independently of CSS
+  </li><li>the specification now contains its own test suite. </li>
+</ul>
+<h2><a name="selectors"></a>2. Selectors</h2>
+<p>A <span class="propernoun">Selector</span> represents a structure. This structure can be used
+as a condition (e.g. in a CSS rule) that determines which elements
+a selector matches in the document tree, or as a flat description of the
+HTML or XML fragment corresponding to that structure. 
+</p><p><span class="propernoun">Selectors</span> may range from simple element names to rich contextual
+representations. 
+</p><p>The following table summarizes <span class="propernoun">Selector</span> syntax: 
+</p><table class="selectorsreview" width="100%" border="1">
+  <tbody>
+  <tr>
+    <th class="pattern">Pattern</th>
+    <th class="meaning">Meaning</th>
+    <th class="described">Described in section</th>
+    <th class="origin">First defined in CSS level</th></tr>
+  <tr>
+    <td class="pattern">*</td>
+    <td class="meaning">any element</td>
+    <td class="described"><a href="#universal-selector">Universal
+      selector</a></td>
+    <td class="origin">2</td></tr>
+  <tr>
+    <td class="pattern">E</td>
+    <td class="meaning">an element of type E</td>
+    <td class="described"><a href="#type-selectors">Type selector</a></td>
+    <td class="origin">1</td></tr>
+  <tr>
+    <td class="pattern">E[foo]</td>
+    <td class="meaning">an E element with a "foo" attribute</td>
+    <td class="described"><a href="#attribute-selectors">Attribute
+      selectors</a></td>
+    <td class="origin">2</td></tr>
+  <tr>
+    <td class="pattern">E[foo="bar"]</td>
+    <td class="meaning">an E element whose "foo" attribute value is exactly
+      equal to "bar"</td>
+    <td class="described"><a href="#attribute-selectors">Attribute
+      selectors</a></td>
+    <td class="origin">2</td></tr>
+  <tr>
+    <td class="pattern">E[foo~="bar"]</td>
+    <td class="meaning">an E element whose "foo" attribute value is a list of
+      space-separated values, one of which is exactly equal to "bar"</td>
+    <td class="described"><a href="#attribute-selectors">Attribute
+      selectors</a></td>
+    <td class="origin">2</td></tr>
+  <tr>
+    <td class="pattern">E[foo^="bar"]</td>
+    <td class="meaning">an E element whose "foo" attribute value begins exactly
+      with the string "bar"</td>
+    <td class="described"><a href="#attribute-selectors">Attribute
+      selectors</a></td>
+    <td class="origin">3</td></tr>
+  <tr>
+    <td class="pattern">E[foo$="bar"]</td>
+    <td class="meaning">an E element whose "foo" attribute value ends exactly
+      with the string "bar"</td>
+    <td class="described"><a href="#attribute-selectors">Attribute
+      selectors</a></td>
+    <td class="origin">3</td></tr>
+  <tr>
+    <td class="pattern">E[foo*="bar"]</td>
+    <td class="meaning">an E element whose "foo" attribute value contains the
+      substring "bar"</td>
+    <td class="described"><a href="#attribute-selectors">Attribute
+      selectors</a></td>
+    <td class="origin">3</td></tr>
+  <tr>
+    <td class="pattern">E[hreflang|="en"]</td>
+    <td class="meaning">an E element whose "hreflang" attribute has a hyphen-separated
+      list of values beginning (from the left) with "en"</td>
+    <td class="described"><a href="#attribute-selectors">Attribute
+      selectors</a></td>
+    <td class="origin">2</td></tr>
+  <tr>
+    <td class="pattern">E:root</td>
+    <td class="meaning">an E element, root of the document</td>
+    <td class="described"><a href="#structural-pseudos">Structural
+      pseudo-classes</a></td>
+    <td class="origin">3</td></tr>
+  <tr>
+    <td class="pattern">E:nth-child(n)</td>
+    <td class="meaning">an E element, the n-th child of its parent</td>
+    <td class="described"><a href="#structural-pseudos">Structural
+      pseudo-classes</a></td>
+    <td class="origin">3</td></tr>
+  <tr>
+    <td class="pattern">E:nth-last-child(n)</td>
+    <td class="meaning">an E element, the n-th child of its parent, counting
+      from the last one</td>
+    <td class="described"><a href="#structural-pseudos">Structural
+      pseudo-classes</a></td>
+    <td class="origin">3</td></tr>
+  <tr>
+    <td class="pattern">E:nth-of-type(n)</td>
+    <td class="meaning">an E element, the n-th sibling of its type</td>
+    <td class="described"><a href="#structural-pseudos">Structural
+      pseudo-classes</a></td>
+    <td class="origin">3</td></tr>
+  <tr>
+    <td class="pattern">E:nth-last-of-type(n)</td>
+    <td class="meaning">an E element, the n-th sibling of its type, counting
+      from the last one</td>
+    <td class="described"><a href="#structural-pseudos">Structural
+      pseudo-classes</a></td>
+    <td class="origin">3</td></tr>
+  <tr>
+    <td class="pattern">E:first-child</td>
+    <td class="meaning">an E element, first child of its parent</td>
+    <td class="described"><a href="#structural-pseudos">Structural
+      pseudo-classes</a></td>
+    <td class="origin">2</td></tr>
+  <tr>
+    <td class="pattern">E:last-child</td>
+    <td class="meaning">an E element, last child of its parent</td>
+    <td class="described"><a href="#structural-pseudos">Structural
+      pseudo-classes</a></td>
+    <td class="origin">3</td></tr>
+  <tr>
+    <td class="pattern">E:first-of-type</td>
+    <td class="meaning">an E element, first sibling of its type</td>
+    <td class="described"><a href="#structural-pseudos">Structural
+      pseudo-classes</a></td>
+    <td class="origin">3</td></tr>
+  <tr>
+    <td class="pattern">E:last-of-type</td>
+    <td class="meaning">an E element, last sibling of its type</td>
+    <td class="described"><a href="#structural-pseudos">Structural
+      pseudo-classes</a></td>
+    <td class="origin">3</td></tr>
+  <tr>
+    <td class="pattern">E:only-child</td>
+    <td class="meaning">an E element, only child of its parent</td>
+    <td class="described"><a href="#structural-pseudos">Structural
+      pseudo-classes</a></td>
+    <td class="origin">3</td></tr>
+  <tr>
+    <td class="pattern">E:only-of-type</td>
+    <td class="meaning">an E element, only sibling of its type</td>
+    <td class="described"><a href="#structural-pseudos">Structural
+      pseudo-classes</a></td>
+    <td class="origin">3</td></tr>
+  <tr>
+    <td class="pattern">E:empty</td>
+    <td class="meaning">an E element that has no children (including text
+    nodes)</td>
+    <td class="described"><a href="#structural-pseudos">Structural
+      pseudo-classes</a></td>
+    <td class="origin">3</td></tr>
+  <tr>
+    <td class="pattern">E:link <br>E:visited</td>
+    <td class="meaning">an E element being the source anchor of a hyperlink of
+      which the target is not yet visited (:link) or already visited
+    (:visited)</td>
+    <td class="described"><a href="#link">The link
+      pseudo-classes</a></td>
+    <td class="origin">1</td></tr>
+  <tr>
+    <td class="pattern">E:active <br>E:hover <br>E:focus</td>
+    <td class="meaning">an E element during certain user actions</td>
+    <td class="described"><a href="#useraction-pseudos">The user
+      action pseudo-classes</a></td>
+    <td class="origin">1 and 2</td></tr>
+  <tr>
+    <td class="pattern">E:target</td>
+    <td class="meaning">an E element being the target of the referring URI</td>
+    <td class="described"><a href="#target-pseudo">The target
+      pseudo-class</a></td>
+    <td class="origin">3</td></tr>
+  <tr>
+    <td class="pattern">E:lang(fr)</td>
+    <td class="meaning">an element of type E in language "fr" (the document
+      language specifies how language is determined)</td>
+    <td class="described"><a href="#lang-pseudo">The :lang()
+      pseudo-class </a></td>
+    <td class="origin">2</td></tr>
+  <tr>
+    <td class="pattern">E:enabled<br>E:disabled </td>
+    <td class="meaning">a user interface element E which is enabled or
+    disabled</td>
+    <td class="described"><a href="#UIstates">The UI element states
+      pseudo-classes</a></td>
+    <td class="origin">3</td></tr>
+  <tr>
+    <td class="pattern">E:checked<br>E:indeterminate </td>
+    <td class="meaning">a user interface element E which is checked or in an
+      indeterminate state (for instance a radio-button or checkbox)</td>
+    <td class="described"><a href="#UIstates">The UI element states
+      pseudo-classes</a></td>
+    <td class="origin">3</td></tr>
+  <tr>
+    <td class="pattern">E:contains("foo")</td>
+    <td class="meaning">an E element containing the substring "foo" in its textual
+      contents</td>
+    <td class="described"><a href="#content-selectors">Content
+      pseudo-class</a></td>
+    <td class="origin">3</td></tr>
+  <tr>
+    <td class="pattern">E::first-line</td>
+    <td class="meaning">the first formatted line of an E element</td>
+    <td class="described"><a href="#first-line">The :first-line
+      pseudo-element</a></td>
+    <td class="origin">1</td></tr>
+  <tr>
+    <td class="pattern">E::first-letter</td>
+    <td class="meaning">the first formatted letter of an E element</td>
+    <td class="described"><a href="#first-letter">The :first-letter
+      pseudo-element</a></td>
+    <td class="origin">1</td></tr>
+  <tr>
+    <td class="pattern">E::selection</td>
+    <td class="meaning">the portion of an E element that is currently
+      selected/highlighted by the user</td>
+    <td class="described"><a href="#UIfragments">The UI element
+      fragments pseudo-elements</a></td>
+    <td class="origin">3</td></tr>
+  <tr>
+    <td class="pattern">E::before</td>
+    <td class="meaning">generated content before an E element</td>
+    <td class="described"><a href="#gen-content">The :before
+      pseudo-element</a></td>
+    <td class="origin">2</td></tr>
+  <tr>
+    <td class="pattern">E::after</td>
+    <td class="meaning">generated content after an E element</td>
+    <td class="described"><a href="#gen-content">The :after
+      pseudo-element</a></td>
+    <td class="origin">2</td></tr>
+  <tr>
+    <td class="pattern">E.warning</td>
+    <td class="meaning">an E element whose class is
+"warning" (the document language specifies how class is determined).</td>
+    <td class="described"><a href="#class-html">Class
+    selectors</a></td>
+    <td class="origin">1</td></tr>
+  <tr>
+    <td class="pattern">E#myid</td>
+    <td class="meaning">an E element with ID equal to "myid".</td>
+    <td class="described"><a href="#id-selectors">ID
+    selectors</a></td>
+    <td class="origin">1</td></tr>
+  <tr>
+    <td class="pattern">E:not(s)</td>
+    <td class="meaning">an E element that does not match simple selector s</td>
+    <td class="described"><a href="#negation">Negation
+      pseudo-class</a></td>
+    <td class="origin">3</td></tr>
+  <tr>
+    <td class="pattern">E F</td>
+    <td class="meaning">an F element descendant of an E element</td>
+    <td class="described"><a href="#descendant-combinators">Descendant
+      combinator</a></td>
+    <td class="origin">1</td></tr>
+  <tr>
+    <td class="pattern">E > F</td>
+    <td class="meaning">an F element child of an E element</td>
+    <td class="described"><a href="#child-combinators">Child
+      combinator</a></td>
+    <td class="origin">2</td></tr>
+  <tr>
+    <td class="pattern">E + F</td>
+    <td class="meaning">an F element immediately preceded by an E element</td>
+    <td class="described"><a href="#adjacent-d-combinators">Direct
+      adjacent combinator</a></td>
+    <td class="origin">2</td></tr>
+  <tr>
+    <td class="pattern">E ~ F</td>
+    <td class="meaning">an F element preceded by an E element</td>
+    <td class="described"><a href="#adjacent-i-combinators">Indirect
+      adjacent combinator</a></td>
+    <td class="origin">3</td></tr></tbody></table>
+<p>The meaning of each selector is derived from the table above by
+ prepending "matches" to the contents of each cell of the "Meaning" column.
+</p><h2><a name="casesens">3. Case sensitivity</a></h2>
+<p>The case-sensitivity of document language element names in selectors depends
+on the document language. For example, in HTML, element names are
+case-insensitive, but in XML they are case-sensitive. 
+</p><p>The case-sensitivity of attribute names and attribute values in attribute
+selectors also depends on the document language.
+</p><h2><a name="selector-syntax">4. Selector syntax</a></h2>
+<p>A <dfn><a name="selector">selector</a></dfn> is a chain of one or more <a href="#sequence">sequences of simple
+selectors</a> separated by <a href="#combinators">combinators</a>. 
+</p><p>A <dfn><a name="sequence">sequence of simple selectors</a></dfn> is a chain
+  of <a href="#simple-selectors-dfn">simple selectors</a> that are not separated by a
+  <a href="#combinators">combinator</a>. It always begins with a <a href="#type-selectors">type selector</a> or a <a href="#universal-selector">universal
+  selector</a>. No other type selector or universal selector is allowed in the
+  sequence. 
+</p><p>A <dfn><a name="simple-selectors-dfn"></a><a href="#simple-selectors">simple selector</a></dfn> is either a <a href="#type-selectors">type selector</a>, <a href="#universal-selector">universal selector</a>, <a href="#attribute-selectors">attribute selector</a>, <a href="#id-selectors">ID
+  selector</a>, <a href="#content-selectors">content selector</a>, or <a href="#pseudo-classes">pseudo-class</a>. One <a href="#pseudo-elements">pseudo-element</a> may be appended to the last sequence
+  of simple selectors.
+</p><p><dfn>Combinators</dfn> are: white space, "greater-than sign" (<code>></code>),
+  "plus sign" (<code>+</code>) and "tilde" (<code>~</code>).
+  White space may appear between a combinator and the simple selectors around
+  it. <a name="whitespace"></a>Only the characters "space" (Unicode code 32), "tab"
+  (9), "line feed" (10), "carriage return" (13), and "form feed" (12) can occur
+  in white space. Other space-like characters, such as "em-space" (8195) and "ideographic
+  space" (12288), are never part of white space.
+</p><p>The elements of the document tree represented by a selector are called <dfn><a name="subject"></a>subjects
+  of the selector</dfn>. A selector consisting of a single sequence of simple
+  selectors represents any element satisfying its requirements. Prepending another
+  sequence of simple selectors and a combinator to a sequence imposes additional
+  matching constraints, so the subjects of a selector are always a subset of the
+  elements represented by the rightmost sequence of simple selectors.
+</p><p><strong><em>Note</em></strong><em>: an empty selector, containing no sequence
+  of simple selectors and no combinator, is an <a href="#Conformance">invalid
+  selector</a>.</em>
+</p><h2><a name="grouping">5. Groups of selectors</a></h2>
+<p>When several selectors share the same declarations, they may be grouped into
+a comma-separated list. 
+</p><div class="example">CSS example(s):
+<p>In this example, we condense three rules with identical declarations into
+one. Thus, </p><pre>h1 { font-family: sans-serif }
+h2 { font-family: sans-serif }
+h3 { font-family: sans-serif }</pre>is equivalent to: <pre>h1, h2, h3 { font-family: sans-serif }</pre></div>
+<p><b>Warning</b>: the equivalence is true in this example because all selectors
+  are valid selectors. If just one of these selectors is invalid, the entire group
+  of selectors is invalid thus invalidating the rule for all three heading elements,
+  whereas only one of the three individual heading rules would be invalid.
+  
+</p><h2><a name="simple-selectors">6. Simple selectors</a></h2>
+<h3><a name="type-selectors">6.1 Type selector</a></h3>
+<p>A <dfn>type selector</dfn> is the name of a document language element
+type. A type selector represents an instance of the element type in the document
+tree. 
+</p><div class="example">Example:
+  <p>The following selector represents an <code>h1</code> element in the document
+tree: </p><pre>h1</pre></div>
+<h4><a name="typenmsp">6.1.1 Type selectors and Namespaces</a></h4>
+<p>Type selectors allow an optional namespace (<a href="#XMLNAMES">[XML-NAMES]</a>) component.
+  A namespace prefix that has been previously declared
+  may be prepended to the element name separated by the namespace separator
+  "vertical bar" (<code>|</code>). The namespace component may be left
+  empty to indicate that the selector is only to represent elements with no declared
+  namespace. Furthermore, an asterisk may be used for the namespace prefix, indicating
+  that the selector represents elements in any namespace (including elements
+  with no namespace). Element type selectors that have no namespace component
+  (no namespace separator), represent elements without regard
+  to the element's namespace (equivalent to "<code>*|</code>") unless a default
+  namespace has been declared. In that case, the selector will represent only
+  elements in the default namespace.
+</p><p>Note : a type selector containing a namespace prefix that has not been previously
+ declared is an <a href="#Conformance">invalid</a> selector.
+ The mechanism for declaring a namespace prefix is left up to the language
+ implementing <span class="modulename">Selectors</span>.
+ In CSS, such a mechanism is defined in the General Syntax module.
+
+<!--<p>An alternative approach would be to define element type selectors that have
+  no namespace component to match only elements that have no namespace (unless
+  a default namespace has been declared in the CSS). This would make the selector
+  "<code>h1</code>" equivalent to the selector "<code>|h1</code>" as opposed to
+  "<code>*|h1</code>". The downside to this approach is that legacy style sheets
+  (those written without any namespace constructs) will fail to match in all XML
+  documents where namespaces are used throughout, e.g. all XHTML documents. -->
+</p><p>It should be noted that if a namespace prefix used in a selector has not been
+  previously declared, then the selector must be considered invalid and the entire
+  style rule will be ignored in accordance with the <a href="#Conformance">standard
+  error handling rules</a>. 
+</p><p>It should further be noted that in a namespace aware client, element type
+selectors will only match against the <a href="http://www.w3.org/TR/REC-xml-names/#NT-LocalPart">local part</a> of the
+element's <a href="http://www.w3.org/TR/REC-xml-names/#ns-qualnames">qualified
+name</a>. See <a href="#downlevel">below</a>
+for notes about matching behaviors in down-level clients. 
+</p><p>In summary:
+</p><dl>
+  <dt><code>ns|E</code>
+  </dt><dd>elements with name E in namespace ns
+  </dd><dt><code>*|E</code>
+  </dt><dd>elements with name E in any namespace, including those without any
+  declared namespace
+  </dd><dt><code>|E</code>
+  </dt><dd>elements with name E without any declared namespace
+  </dd><dt><code>E</code>
+  </dt><dd>if no default namespace has been specified, this is equivalent to *|E.
+  Otherwise it is equivalent to ns|E where ns is the default namespace. </dd></dl>
+<div class="example">
+<p>CSS examples: 
+  </p><pre>@namespace foo url(http://www.example.com);
+
+foo|h1 { color: blue }
+
+foo|* { color: yellow }
+
+|h1 { color: red }
+
+*|h1 { color: green }
+
+h1 { color: green }</pre>
+  <p>The first rule will match only <code>h1</code> elements in the "http://www.example.com"
+    namespace. 
+  </p><p>The second rule will match all elements in the "http://www.example.com" namespace.
+  
+</p><p>The third rule will match only <code>h1</code> elements without any declared
+namespace. 
+</p><p>The fourth rule will match <code>h1</code> elements in any namespace (including
+those without any declared namespace).
+</p><p>The last rule is equivalent to the fourth rule because no default namespace
+has been defined.</p></div>
+<h3><a name="universal-selector">6.2 Universal selector</a> </h3>
+<p>The <dfn>universal selector</dfn>, written "asterisk" (<code>*</code>),
+  represents the qualified name of any element type. It represents then any single
+  element in the document tree in any namespace (including those without any declared
+  namespace) if no default namespace has been specified. If a default namespace
+  has been specified, see <a href="#univnmsp">Universal selector and Namespaces</a> below.
+</p><p>If the universal selector is not the only component of a sequence of simple
+selectors, the <code>*</code> may be omitted. For example: 
+</p><div class="example">
+<ul>
+  <li><code>*[hreflang|=en]</code> and <code>[hreflang|=en]</code> are equivalent,
+  </li><li><code>*.warning</code> and <code>.warning</code> are equivalent,
+  </li><li><code>*#myid</code> and <code>#myid</code> are equivalent. </li></ul></div>
+<p><b>Note</b>: it is recommended that the <code>*</code>, representing the
+universal selector, not be omitted.
+</p><h4><a name="univnmsp">6.2.1 Universal selector and Namespaces</a></h4>
+<p>The universal selector allows an optional namespace component.
+</p><dl>
+  <dt><code>ns|*</code>
+  </dt><dd>all elements in namespace ns
+  </dd><dt><code>*|*</code>
+  </dt><dd>all elements
+  </dd><dt><code>|*</code>
+  </dt><dd>all elements without any declared namespace
+  </dd><dt><code>*</code>
+  </dt><dd>if no default namespace has been specified, this is equivalent to *|*.
+  Otherwise it is equivalent to ns|* where ns is the default namespace. </dd></dl>
+<p>Note: a universal selector containing a namespace prefix that has not been
+ previously declared is an <a href="#Conformance">invalid</a> selector.
+ The mechanism for declaring a namespace prefix is left up to the language
+ implementing <span class="modulename">Selectors</span>.
+ In CSS, such a mechanism is defined in the General Syntax module.
+
+</p><h3><a name="attribute-selectors">6.3 Attribute selectors</a></h3>
+<p><span class="propernoun">Selectors</span> allow the representation of an element's attributes.
+
+</p><h4><a name="attribute-representation">6.3.1 Attribute presence and values
+selectors</a></h4>
+<p>CSS2 introduced four attribute selectors:
+</p><dl>
+  <dt><code>[att]</code>
+  </dt><dd>Represents the <code>att</code> attribute, whatever the value of the
+  attribute.
+  </dd><dt><code>[att=val]</code>
+  </dt><dd>Represents the <code>att</code> attribute with value exactly "val".
+  </dd><dt><code>[att~=val]</code>
+  </dt><dd>Represents the <code>att</code> attribute whose value is a space-separated list of words,
+  one of which is exactly "val". If this selector is used, the
+  words in the value must not contain spaces (since they are separated by
+  spaces).
+  </dd><dt><code>[att|=val]</code>
+  </dt><dd>Represents the <code>att</code> attribute, its value either being exactly "val" or
+  beginning with "val" immediately followed by "-".
+  This is primarily intended to allow language subcode matches
+  (e.g., the <code>hreflang</code> attribute on the <code>link</code> element in HTML)
+  as described in RFC 3066 (<a class="noxref" href="#rfc3066" rel="biblioentry">[RFC3066]</a>).
+   Note: for <code>lang</code> (or <code>xml:lang</code>) language subcode matching,
+   please see <a href="#lang-pseudo">the <code>:lang</code> pseudo-class</a>.
+  
+</dd></dl>
+<p>Attribute values must be identifiers or strings. The case-sensitivity of
+attribute names and values in selectors depends on the document language. 
+</p><div class="example">Examples:
+  <p>For example, the following attribute selector represents an <code>h1</code>
+element that carries the <code>title</code> attribute, whatever its value: </p><pre>h1[title]</pre>
+  <p>In the following example, the selector represents a <code>span</code> element
+whose <code>class</code> attribute has exactly the value "example": </p><pre>span[class=example]</pre>
+  Multiple attribute selectors can be used to represent several attributes of
+  an element, or several conditions on the same attribute.
+  <p>Here, the selector represents a <code>span</code> element whose <code>hello</code>
+attribute has exactly the value "Cleveland" and whose <code>goodbye</code> attribute
+has exactly the value "Columbus": </p><pre>span[hello="Cleveland"][goodbye="Columbus"]</pre>
+  <p>The following selectors illustrate the differences between "=" and "~=".
+    The first selector will represent, for example, the value "copyright copyleft
+    copyeditor" on a <code>rel</code> attribute. The second selector will only
+    represent an <code>a</code> element with an <code>href</code> attribute having
+    the exact value "http://www.w3.org/".
+  </p><pre>a[rel~="copyright"]
+a[href="http://www.w3.org/"]</pre>
+  <p>The following selector represents a <code>link</code> element whose
+  <code>hreflang</code> attribute is exactly "fr".
+  </p><pre>link[hreflang=fr]</pre>
+  <p>The following selector represents a <code>link</code> element for which the
+    values of the <code>hreflang</code> attribute begins with "en", including
+    "en", "en-US", and "en-cockney":
+  </p><pre>link[hreflang|="en"]</pre>
+  <p>Similarly, the following selectors represents a <code>DIALOGUE</code> element
+whenever it has one of two different values for an attribute <code>character</code>:
+</p><pre>DIALOGUE[character=romeo] 
+
+DIALOGUE[character=juliet]</pre></div>
+<h4><a name="attribute-substrings"></a>6.3.2 Substring matching attribute
+selectors</h4>
+<p>Three additional attribute selectors are provided
+ for matching substrings in the value of an attribute:
+</p><dl>
+  <dt><code>[att^=val]</code>
+  </dt><dd>Represents the <code>att</code> attribute whose value begins with
+  the prefix "val"
+  </dd><dt><code>[att$=val]</code>
+  </dt><dd>Represents the <code>att</code> attribute whose value ends with the
+  suffix "val"
+  </dd><dt><code>[att*=val]</code>
+  </dt><dd>Represents the <code>att</code> attribute whose value contains at least
+  one instance of the substring "val" </dd></dl>
+<p>Attribute values must be identifiers or strings. The case-sensitivity of
+attribute names in selectors depends on the document language. 
+</p><p>Example:
+</p><p>The following selector represents an HTML <code>object</code>, referencing an
+image:</p><pre>object[type^="image/"]
+</pre>
+<p>The following selector represents an HTML anchor <code>a</code> with an
+  <code>href</code> attribute whose value ends with ".html".
+</p><pre>a[href$=".html"]</pre>
+<p>The following selector represents a HTML paragraph with a <code>title</code>
+attribute whose value contains the substring "hello"</p><pre>p[title*="hello"] </pre>
+<h4><a name="attrnmsp">6.3.3 Attribute selectors and Namespaces</a></h4>
+<p>Attribute selectors allow an optional namespace component to the attribute
+  name. A namespace prefix that has been previously declared may be prepended
+  to the attribute name separated by the namespace separator
+  "vertical bar" (<code>|</code>). In keeping with the Namespaces in
+  the XML recommendation, default namespaces do not apply to attributes, therefore
+  attribute selectors without a namespace component apply only to attributes that
+  have no declared namespace (equivalent to "<code>|attr</code>"). An asterisk
+  may be used for the namespace prefix indicating that the selector is to match
+  all attribute names without regard to the attribute's namespace.
+</p><p>Note : an attribute
+ selector with an attribute name containing a namespace prefix that has
+ not been previously declared is an <a href="#Conformance">invalid</a> selector.
+ The mechanism for declaring a namespace prefix is left up to the language
+ implementing <span class="modulename">Selectors</span>.
+ In CSS, such a mechanism is defined in the General Syntax module.
+
+</p><p>CSS examples: 
+</p><div class="example">
+  <pre>@namespace foo "http://www.example.com";
+
+[foo|att=val] { color: blue }
+
+[*|att] { color: yellow }
+
+[|att] { color: green }
+
+[att] { color: green }</pre>
+  The first rule will match only elements with the attribute <code>att</code>
+  in the "http://www.example.com" namespace with the value "val".
+  <p>The second rule will match only elements with the attribute <code>att</code>
+regardless of the namespace of the attribute (including no declared namespace).
+</p><p>The last two rules are equivalent and will match only elements with the
+attribute <code>att</code> where the attribute is not declared to be in a
+namespace.</p></div>
+<h4><a name="def-values">6.3.4 Default attribute values in DTDs</a></h4>
+<p>Attribute selectors represent explicitly set attribute values in the document
+  tree. Default attribute values may be defined in a DTD or elsewhere.
+  <span class="propernoun">Selectors</span> should be designed so that they work
+  even if the default values are not included in the document tree.
+</p><div class="example">Examples:
+  <p>For example, consider an element <code>EXAMPLE</code> with an attribute
+<code>notation</code> that has a default value of "decimal". The DTD fragment
+might be </p><pre><!ATTLIST EXAMPLE notation (decimal,octal) "decimal"></pre>
+  If the selectors represent an <code>EXAMPLE</code> element when the value of
+  the attribute is explicitly set:
+  <pre>EXAMPLE[notation=decimal]
+EXAMPLE[notation=octal]</pre>
+  then to represent only the case where this attribute is set by default, and
+  not explicitly, the following selector might be used:
+  <pre>EXAMPLE:not([notation])</pre>
+</div>
+<h3><a name="class-html">6.4 Class selectors</a></h3>
+<p>Working with HTML, authors may use the period (<code>.</code>) notation as
+  an alternative to the <code>~=</code> notation when representing the <code>class</code>
+  attribute. Thus, for HTML, <code>div.value</code> and <code>div[class~=value]</code>
+  have the same meaning. The attribute value must immediately follow the "period"
+  (<code>.</code>). Note: UAs may apply selectors using the period (.) notation
+  in XML documents if the UA has namespace specific knowledge that allows it to
+  determine which attribute is the "class" attribute for the respective
+  namespace. One such example of namespace specific knowledge is the prose in
+  the specification for a particular namespace (e.g. SVG 1.0 [<a href="#SVG">SVG</a>]
+  describes the <a href="http://www.w3.org/TR/2001/PR-SVG-20010719/styling.html#ClassAttribute">SVG
+  "class" attribute</a> and how a UA should interpret it, and similarly
+  MathML 1.01 [<a href="#MATH">MATH</a>] describes the <a href="http://www.w3.org/1999/07/REC-MathML-19990707/chapter2.html#sec2.3.4">MathML
+  "class" attribute</a>.)
+</p><p>
+</p><div class="example">Examples:
+  <p>For example, we can represent an arbitrary element with
+<code>class~="pastoral"</code> as follows: </p><pre>*.pastoral</pre>or just <pre>.pastoral</pre>
+  The following selector represents an <code>h1</code> element with <code>class~="pastoral"</code>:
+  <pre>h1.pastoral</pre>
+  <p>For example, the following selector represents a <code>p</code> element whose
+<code>class</code> attribute has been assigned a list of space-separated values that
+includes "pastoral" and "marine": </p><pre>p.pastoral.marine</pre>
+<p>It is fully identical to:</p><pre>p.marine.pastoral</pre>
+  <p>This selector represents for example a <code>p</code> with <code>class="pastoral
+    blue aqua marine"</code> or <code>class="marine blue pastoral aqua" </code>but
+    not <code>class="pastoral blue"</code>.
+</p></div>
+<h3><a name="id-selectors">6.5 ID selectors</a></h3>
+<p>Document languages may contain attributes that are declared to be of type ID.
+  What makes attributes of type ID special is that no two such attributes can
+  have the same value in a document, regardless of the type of the elements that
+  carry them; whatever the document language, an ID typed attribute can be used
+  to uniquely identify its element. In HTML all ID attributes are named "id";
+  XML applications may name ID attributes differently, but the same restriction
+  applies. 
+</p><p>An ID typed attribute of a document language allows authors to assign an identifier
+  to one element instance in the document tree. W3C ID selectors represent an
+  element instance based on its identifier. An ID selector contains a "number
+  sign" (#) immediately followed by the ID value. 
+</p><div class="example">Examples:
+  <p>The following ID selector represents an <code>h1</code> element whose ID typed
+    attribute has the value "chapter1":
+  </p><pre>h1#chapter1</pre>
+  <p>The following ID selector represents any element whose ID typed attribute
+    has the value "chapter1":
+  </p><pre>#chapter1</pre>
+  The following selector represents any element whose ID typed attribute has the
+  value "z98y".
+  <pre>*#z98y</pre></div>
+<div class="note"><i><b>Note.</b> In XML 1.0 <a class="noxref" href="http://www.w3.org/TR/REC-CSS2/refs.html#ref-XML10" rel="biblioentry">[XML10]</a>, the information about which attribute contains an
+  element's IDs is contained in a DTD or a schema. When parsing XML, UAs do not
+  always read the DTD, and thus may not know what the ID of an element is
+  (though a UA may have namespace specific knowledge that allows it to determine
+  which attribute is the ID attribute for that namespace). If
+  a style sheet designer knows or suspects that a UA may not know what the ID of an
+  element is, he should use normal attribute selectors instead:
+  <code>[name=p371]</code> instead of <code>#p371</code>.
+  Elements in XML 1.0 documents without a DTD do not have IDs at all.</i></div>
+<h3><a name="pseudo-classes">6.6 Pseudo-classes</a></h3>
+<p>The pseudo-class concept is introduced to permit selection based on information
+  that lies outside of the document tree or that cannot be expressed using the
+  other simple selectors. 
+</p><p>A pseudo-class always contains a "colon" (<code>:</code>) followed
+  by the name of the pseudo-class and optionally by a value between parentheses.
+
+</p><p>Pseudo-classes are allowed in all sequences of simple selectors contained in
+  a selector. Pseudo-classes are allowed anywhere in sequences of simple selectors,
+  after the leading type selector or universal selector (possibly omitted). Pseudo-class
+  names are case-insensitive. Some pseudo-classes are mutually exclusive, while
+  others can be applied simultaneously to the same element. Pseudo-classes may
+  be dynamic, in the sense that an element may acquire or lose a pseudo-class
+  while a user interacts with the document.
+</p><h4><a name="dynamic-pseudos">6.6.1 Dynamic pseudo-classes</a></h4>
+<p>Dynamic pseudo-classes classify elements on characteristics other than their
+  name, attributes or content, in principle characteristics that cannot be deduced
+  from the document tree.
+</p><p>Dynamic pseudo-classes do not appear in the document source or document tree.
+
+</p><h5>The <a name="link">link pseudo-classes: :link and :visited</a></h5>
+<p>User agents commonly display unvisited links differently from previously
+visited ones. <span class="modulename">Selectors</span> provides the pseudo-classes <code>:link</code> and
+<code>:visited</code> to distinguish them: 
+</p><ul>
+  <li>The <code>:link</code> pseudo-class applies for links that have not yet been
+  visited.
+  </li><li>The <code>:visited</code> pseudo-class applies once the link has been visited
+  by the user. </li></ul>
+<div class="note"><i><b>Note.</b> After some amount of time, user agents may
+choose to return a visited link to the (unvisited) ':link' state.</i></div>
+<p>The two states are mutually exclusive. 
+</p><div class="example">Example:
+  <p>The following selector represents links carrying class <code>external</code> and
+already visited: </p><pre>a.external:visited</pre></div>
+<h5>The <a name="useraction-pseudos">user action pseudo-classes :hover,
+:active, and :focus</a></h5>
+<p>Interactive user agents sometimes change the rendering in response to user
+actions. <span class="modulename">Selectors</span> provides three pseudo-classes for the selection of an
+element the user is acting on. 
+</p><ul>
+  <li>The <code>:hover</code> pseudo-class applies while the user designates an
+  element (with some pointing device), but does not activate it. For example, a
+  visual user agent could apply this pseudo-class when the cursor (mouse
+  pointer) hovers over a box generated by the element. User agents not
+  supporting <a href="http://www.w3.org/TR/REC-CSS2/media.html#interactive-media-group">interactive
+  media</a> do not have to support this pseudo-class. Some conforming user
+  agents supporting <a href="http://www.w3.org/TR/REC-CSS2/media.html#interactive-media-group">interactive
+  media</a> may not be able to support this pseudo-class (e.g., a pen device).
+  </li><li>The <code>:active</code> pseudo-class applies while an element is being
+  activated by the user. For example, between the times the user presses the
+  mouse button and releases it.
+  </li><li>The <code>:focus</code> pseudo-class applies while an element has the focus
+  (accepts keyboard or mouse events, or other forms of input). </li></ul>
+<p>There may be document language or implementation specific limits on which elements can become
+<code>:active</code> or acquire <code>:focus</code>. 
+<!--
+<p>Only elements whose 'user-input' property (see <a
+href="#UI-WD">[UI]</a>) has the value of
+"enabled" can become <code>:active</code> or acquire <code>:focus</code>. -->
+</p><p>These pseudo-classes are not mutually exclusive. An element may match several
+of them at the same time. 
+</p><div class="example">Examples:
+  <pre>a:link    /* unvisited links */
+a:visited /* visited links   */
+a:hover   /* user hovers     */
+a:active  /* active links    */</pre>
+  <p>An example of combining dynamic pseudo-classes: </p><pre>a:focus
+a:focus:hover</pre>
+  <p>The last selector matches <code>a</code> elements that are in pseudo-class
+    :focus and in pseudo-class :hover. 
+</p></div>
+<div class="note"><i><b>Note.</b> An element can be both ':visited' and ':active'
+(or ':link' and ':active').</i></div>
+<h4><a name="target-pseudo">6.6.2 The target pseudo-class :target</a></h4>
+<p>Some URIs refer to a location within a resource. This kind of URI ends with
+  a "number sign" (<code>#</code>) followed by an anchor identifier
+  (called the fragment identifier). 
+</p><p>URIs with fragment identifiers link to a certain element within the document,
+known as the target element. For instance, here is a URI pointing to an anchor
+named section_2 in a HTML document: 
+</p><pre>http://example.com/html/top.html#section_2</pre>
+<p>A target element can be represented by the <code>:target</code> pseudo-class:
+
+</p><pre>p.note:target</pre>
+<p>represents a <code>p</code> of class note that is the target element of the
+  referring URI. 
+</p><div class="example">CSS example of use of the <code>:target</code> pseudo-class: <pre>*:target { color : red }
+
+*:target::before { content : url(target.png) }</pre></div>
+<h4><a name="lang-pseudo">6.6.3 The language pseudo-class :lang</a></h4>
+<p>If the document language specifies how the human language of an element is
+  determined, it is possible to write selectors that represent an element based
+  on its language. For example, in HTML <a href="#html40" rel="biblioentry">[HTML4.01]</a>, the language is determined by a combination of
+  the <code>lang</code> attribute, the <code>meta</code> element, and possibly
+  by information from the protocol (such as HTTP headers). XML uses an attribute
+  called <code>xml:lang</code>, and there may be other document language-specific
+  methods for determining the language. 
+</p><p>The pseudo-class <code>:lang(C)</code> represents an element that is in language
+  C. Here C is a language code as specified in HTML 4.01 <a href="#html40" rel="biblioentry">[HTML4.01]</a> and RFC 3066 <a href="#rfc3066" rel="biblioentry">[RFC3066]</a>.
+</p><div class="example">Examples:
+  <p>The two following selectors represent an HTML document that is in Belgian
+    French or German. The two next selectors represent <code>q</code> quotations
+    in an arbitrary element in Belgian French or German.
+  </p><pre>html:lang(fr-be)
+html:lang(de)
+:lang(fr-be) > q
+:lang(de) > q</pre>
+</div>
+<h4><a name="UIstates">6.6.4 The UI element states pseudo-classes</a></h4>
+<h5><a name="enableddisabled">The :enabled and :disabled pseudo-classes</a></h5>
+<p>The purpose of the <code>:enabled</code> pseudo-class is to allow authors to
+  customize the look of user interface elements which are enabled - which the
+  user can select/activate in some fashion (e.g. clicking on a button with a mouse).
+  There is a need for such a pseudo-class because there is no way to programmatically
+  specify the default appearance of say, an enabled <code>input</code> element
+  without also specifying what it would look like when it was disabled. 
+</p><p>Similar to <code>:enabled</code>, <code>:disabled</code> allows the author to specify
+precisely how a disabled or inactive user interface element should look. 
+</p><p>It should be noted that most elements will be neither enabled nor disabled.
+An element is enabled if the user can either activate it or transfer the focus
+to it. An element is disabled if it could be enabled, but the user cannot
+presently activate it or transfer focus to it. 
+</p><h5><a name="checked">The :checked pseudo-class</a></h5>
+<p><!--The <code>:checked</code> pseudo-class only applies to elements which are
+'user-input: enabled' or 'user-input : disabled' (see [UI] for the 'user-input'
+property). -->Radio and checkbox elements can be toggled by the user. Some menu
+items are "checked" when the user selects them. When such elements are toggled
+"on" the <code>:checked</code> pseudo-class applies. The <code>:checked</code>
+pseudo-class initially applies to such elements that have the HTML4
+<code>selected</code> attribute as described in <a href="http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.2.1">Section
+17.2.1 of HTML4</a>, but of course the user can toggle "off" such elements in
+which case the <code>:checked</code> pseudo-class would no longer apply. While the
+<code>:checked</code> pseudo-class is dynamic in nature, and is altered by user
+action, since it can also be based on the presence of the semantic HTML4
+<code>selected</code> attribute, it applies to all media. 
+</p><h5><a name="indeterminate">The :indeterminate pseudo-class</a></h5>
+<p><!--The <code>:indeterminate</code> pseudo-class only applies to elements which are
+'user-input: enabled' or 'user-input: disabled' (see <a
+href="#UI-WD">[UI]</a> for the 'user-input'
+property). -->Radio and checkbox elements can be toggled by the user, but are
+sometimes in an indeterminate state, neither checked nor unchecked. This can be
+due to an element attribute, or DOM manipulation. The <code>:indeterminate</code>
+pseudo-class applies to such elements. While the <code>:indeterminate</code>
+pseudo-class is dynamic in nature, and is altered by user action, since it can
+also be based on the presence of an element attribute, it applies to all media.
+
+</p><p>Components of a radio-group initialized with no pre-selected choice are an
+example of :indeterminate state.
+</p><h4><a name="structural-pseudos">6.6.5 Structural pseudo-classes</a></h4>
+<p><span class="modulename">Selectors</span> introduces the concept of <dfn>structural
+pseudo-classes</dfn> to permit selection based on extra information that lies in
+the document tree but cannot be represented by other simple selectors or
+combinators. 
+</p><p>Note that standalone PCDATA are not counted when calculating the position of
+an element in the list of children of its parent. When calculating the position
+of an element in the list of children of its parent, the index numbering starts
+at 1. 
+</p><h5><a name="root-pseudo">:root pseudo-class</a></h5>
+<p>The <code>:root</code> pseudo-class represents an element that is the root
+  of the document. In HTML 4, this is the <code>HTML</code> element. In XML, it
+  is whatever is appropriate for the DTD or schema and namespace for that XML
+  document. 
+</p><h5><a name="nth-child-pseudo">:nth-child() pseudo-class</a></h5>
+<p>The <code>:nth-child(an+b)</code> pseudo-class notation represents an element
+  that has an+b-1 siblings <strong>before</strong> it in the document tree, for
+  a given positive integer or zero value of n. In other words, this matches the
+  bth child of an element after all the children have been split into groups of
+  a elements each. For example, this allows the selectors to address every other
+  row in a table, and could be used, for example, to alternate the color of paragraph
+  text in a cycle of four. The a and b values must be zero, negative integers
+  or positive integers. The index of the first child of an element is 1.
+</p><p>In addition to this, <code>:nth-child()</code> can take 'odd' and 'even' for
+argument. 'odd' has the same signification as 2n+1, and 'even' has the same
+signification as 2n. 
+</p><div class="example">Examples:
+<pre>tr:nth-child(2n+1) /* represents every odd row of a HTML table */
+tr:nth-child(odd)  /* same */
+tr:nth-child(2n)   /* represents every even row of a HTML table */
+tr:nth-child(even) /* same */
+
+/* Alternate paragraph colours in CSS */
+p:nth-child(4n+1) { color: navy; }
+p:nth-child(4n+2) { color: green; }
+p:nth-child(4n+3) { color: maroon; }
+p:nth-child(4n+4) { color: purple; }</pre>
+</div>
+<p>When a=0, no repeating is used, so for example <code>:nth-child(0n+5)</code>
+matches only the fifth child. When a=0, the a part need not be included, so the
+syntax simplifies to <code>:nth-child(b)</code> and the last example simplifies
+to <code>:nth-child(5)</code>.
+</p><div class="example">
+<pre>foo:nth-child(0n+1)   /* represents an element foo, first child of its parent element */
+foo:nth-child(1)      /* same */</pre>
+</div>
+<p>When a=1, the number may be omitted from the rule,
+so the following examples are equivalent: 
+</p><div class="example">
+<pre>bar:nth-child(1n+0)   /* represents all bar elements, specificity (0,1,1) */
+bar:nth-child(n+0)    /* same */
+bar:nth-child(n)      /* same */
+bar                   /* same but lower specificity (0,0,1) */</pre>
+</div>
+<p>If b=0, then every a-th element is picked:
+</p><div class="example">
+<pre>tr:nth-child(2n) /* represents every even row of a HTML table */</pre>
+</div>
+<p>If both a and b are equal to zero, the pseudo-class represents no element in
+the document tree.
+</p><p>The value a can be negative, but only the positive values of an+b, for n>=
+  0, may represent an element in the document tree, of course:
+</p><div class="example">
+<pre>html|tr:nth-child(-n+6)  /* represents the 6 first rows of XHTML tables */</pre>
+</div>
+<h5><a name="nth-last-child-pseudo">:nth-last-child() pseudo-class</a></h5>
+<p>The <code>:nth-last-child(an+b)</code> pseudo-class notation represents an
+element that has an+b-1 siblings <strong>after</strong> it in the document tree,
+for a given positive integer or zero value of n. See <code>:nth-child()</code>
+pseudo-class for the syntax of its argument. It also accepts the 'even' and
+'odd' values for argument.
+</p><div class="example">Examples: <pre>tr:nth-last-child(-n+2)    /* represents the two last rows of a HTML table */
+
+foo:nth-last-child(odd)    /* represents all odd foo elements in their parent element,
+                              counting from the last one */</pre></div>
+<h5><a name="nth-of-type-pseudo">:nth-of-type() pseudo-class</a></h5>
+<p>The <code>:nth-of-type(an+b)</code> pseudo-class notation represents an element
+that has an+b-1 siblings with the same element name <strong>before</strong> it
+in the document tree, for a given zero or positive integer value of n. In other
+words, this matches the bth child of that type after all the children of that
+type have been split into groups of a elements each. See
+<code>:nth-child()</code> pseudo-class for the syntax of its argument. It also
+accepts the 'even' and 'odd' values for argument.
+</p><div class="example">For example, this allows in CSS to alternate the position of
+floated images: <pre>img:nth-of-type(2n+1) { float: right; }
+img:nth-of-type(2n) { float: left; }
+</pre></div>
+<h5><a name="nth-last-of-type-pseudo">:nth-last-of-type() pseudo-class</a></h5>
+<p>The <code>:nth-last-of-type(an+b)</code> pseudo-class notation represents an
+element that has an+b-1 siblings with the same element name
+<strong>after</strong> it in the document tree, for a given zero or positive
+integer value of n. See <code>:nth-child()</code> pseudo-class for the syntax of
+its argument. It also accepts the 'even' and 'odd' values for argument. 
+</p><div class="example">For example, to represent all <code>h2</code> children of a
+XHTML <code>body</code> except the first and last, one would use the following
+selector: <pre>body > h2:nth-of-type(n+2):nth-last-of-type(n+2)</pre>
+<p>In this case, one could also use <code>:not()</code>, although the selector
+ends up being just as long:</p><pre>body > h2:not(:first-of-type):not(:last-of-type) </pre></div>
+<h5><a name="first-child-pseudo">:first-child pseudo-class</a></h5>
+<p>Same as <code>:nth-child(1)</code>. The <code>:first-child</code> pseudo-class
+represents an element that is the first child of some other element.
+</p><div class="example">Examples:
+  <p>In the following example, the selector represents a <code>p</code> element that
+is the first child of a <code>div</code> element: </p><pre>div > p:first-child</pre>This selector can represent the <code>p</code>
+inside the <code>div</code> of the following fragment: <pre><p> The last P before the note.</p>
+<div class="note">
+   <p> The first P inside the note.</p>
+</div></pre>but cannot represent the second <code>p</code> in the following
+fragment: <pre><p> The last P before the note.</p>
+<div class="note">
+   <h2>Note</h2>
+   <p> The first P inside the note.</p>
+</div></pre>The following two selectors are equivalent: <pre>* > a:first-child   /* a is first child of any element */
+a:first-child       /* Same */</pre></div>
+<h5><a name="last-child-pseudo">:last-child pseudo-class</a></h5>
+<p>Same as <code>:nth-last-child(1)</code>.The <code>:last-child</code> pseudo-class
+represents an element that is the last child of some other element. 
+</p><p>The following selector represents a list item <code>li</code> that is the last
+child of an ordered list <code>ol</code>. 
+</p><div class="example">Example:
+<pre>ol > li:last-child</pre></div>
+<h5><a name="first-of-type-pseudo">:first-of-type pseudo-class</a></h5>
+<p>Same as <code>:nth-of-type(1)</code>.The <code>:first-of-type</code> pseudo-class
+represents an element that is the first sibling of its type in the list of
+children of its parent element. 
+</p><div class="example">Example:
+<p>The following selector represents a definition title <code>dt</code> inside a
+definition list <code>dl</code>, this <code>dt</code> being the first of its type in
+the list of children of its parent element. </p><pre>dl dt:first-of-type</pre>It is a valid description for the first two
+<code>dt</code> in the following example but not for the third one: <pre><dl><dt>gigogne</dt>
+        <dd><dl><dt>fus&eacute;e</dt>
+                    <dd>multistage rocket</dd>
+                <dt>table</dt>
+                    <dd>nest of tables</dd>
+            </dl></dd>
+</dl></pre></div>
+<h5><a name="last-of-type-pseudo">:last-of-type pseudo-class</a></h5>
+<p>Same as <code>:nth-last-of-type(1)</code>.The <code>:last-of-type</code>
+pseudo-class represents an element that is the last sibling of its type in the
+list of children of its parent element. 
+</p><div class="example">Example:
+<p>The following selector represents the last data cell <code>td</code> of a table
+row. </p><pre>tr > td:last-of-type</pre></div>
+<h5><a name="only-child-pseudo">:only-child pseudo-class</a></h5>
+<p>Represents an element that has no siblings. Same as
+<code>:first-child:last-child</code> or
+<code>:nth-child(1):nth-last-child(1)</code>, but with a lower specificity.
+</p><h5><a name="only-of-type-pseudo">:only-of-type pseudo-class</a></h5>
+<p>Represents an element that has no siblings with the same element name. Same
+as <code>:first-of-type:last-of-type</code> or
+<code>:nth-of-type(1):nth-last-of-type(1)</code>, but with a lower specificity.
+
+</p><h5><a name="empty-pseudo"></a>:empty pseudo-class</h5>
+<p>The <code>:empty</code> pseudo-class represents an element that has no children
+  at all, including possibly empty text nodes, from a DOM point of view.
+</p><div class="example">Examples:
+<p><code>p:empty</code> is a valid representation of the following fragment:</p><pre><p></p></pre>
+<p><code>foo:empty</code> is not a valid representation for the following
+fragments:</p><pre><foo>bar</foo></pre><pre><foo><bar>bla</bar></foo></pre><pre><foo>this is not <bar>:empty</bar></foo></pre></div>
+<h4><a name="content-selectors">6.6.6 Content pseudo-class</a></h4>
+<p>The <code>:contains("foo")</code> pseudo-class notation represents an element
+whose textual contents contain the given substring. The argument of this
+pseudo-class can be a string (surrounded by double quotes) or a keyword.
+</p><p>Usage of the content pseudo-class is restricted to static media types (see
+  <a href="#CSS2">[CSS2]</a>).
+</p><p>The textual contents of a given element is determined by the concatenation of
+all PCDATA contained in the element and sub-elements. 
+</p><div class="example">Example: <pre>p:contains("Markup")</pre>is a correct and valid, but partial, description
+of: <pre><p><strong>H</strong>yper<strong>t</strong>ext
+   <strong>M</strong><em>arkup</em>
+   <strong>L</strong>anguage</p></pre></div>
+<p>Special characters can be inserted in the argument of a content pseudo-class
+  using the escape mechanism for Unicode characters and carriage returns.
+</p><p><strong>Warning</strong>: the selector <code>ul:contains("chief")</code>
+  will match the list <code><ul><li>... the greek letter chi</li><li>effective</li></ul></code>
+</p><div><i><b>Note</b>: <code>:contains()</code> is a pseudo-class, not a pseudo-element.
+  The following CSS rule applied to the HTML fragment above will not add a red
+  background only to the word "Markup" but will add such a background to the whole
+  paragraph.</i></div>
+<pre>P:contains("Markup") { background-color : red }</pre>
+<h4><a name="negation"></a>6.6.7 The negation pseudo-class</h4>
+<p>The negation pseudo-class is a functional notation taking a <a href="#simple-selectors-dfn">simple selector</a>
+(excluding the negation pseudo-class itself and pseudo-elements) as an argument. It
+represents an element that is not represented by the argument.
+</p><div class="example">
+  <p>Examples:
+</p><p>The following CSS selector matches all <code>button</code> elements in a HTML
+document that are not disabled.</p><pre>button:not([DISABLED])</pre>
+<p>The following selector represents all but <code>FOO</code> elements.</p><pre>*:not(FOO)</pre>
+<p>The following group of selectors represents all elements but HTML links.</p><pre>html|*:not(:link):not(:visited)
+</pre></div>
+<p><strong>Note</strong>: the :not() pseudo allows useless selectors to be written.
+  For instance <code>:not(*|*)</code>, which represents no element at all, or <code>foo:not(bar)</code>,
+  which is equivalent to <code>foo</code> but with a higher specificity.
+</p><h3><a name="pseudo-elements">7. Pseudo-elements</a></h3>
+<p>Pseudo-elements create abstractions about the document tree beyond those
+specified by the document language. For instance, document languages do not
+offer mechanisms to access the first letter or first line of an element's
+content. Pseudo-elements allow designers to refer to this otherwise inaccessible
+information. Pseudo-elements may also provide designers a way to refer to
+content that does not exist in the source document (e.g., the
+<code>::before</code> and <code>::after</code> pseudo-elements give access to
+generated content). 
+</p><p>A pseudo-element is made of two colons (<code>::</code>) followed by the name of
+the pseudo-element.
+</p><p><strong>Note</strong>: this <code>::</code> notation is introduced by the current
+  document in order to establish a discrimination between pseudo-classes and pseudo-elements.
+  For compatibility with existing style sheets, user agents must also accept the
+  previous one-colon notation for pseudo-elements introduced in CSS levels 1 and
+  2. This compatibility is not allowed for the new pseudo-elements introduced
+  in CSS level 3.
+</p><p>Pseudo-elements may only appear once in the sequence of simple selectors that
+represents the <a href="#subject">subjects</a> of the
+selector. 
+</p><h4><a name="first-line">7.1 The ::first-line pseudo-element</a></h4>
+<p>The <code>::first-line</code> pseudo-element describes the first formatted line
+of an element.
+</p><p>For instance in CSS:</p><pre class="example">p::first-line { text-transform: uppercase }
+</pre>
+<p>The above rule means "change the letters of the first line of every paragraph
+to uppercase". However, the selector <code>p::first-line</code> does not match
+any real HTML element. It does match a pseudo-element that conforming user
+agents will insert at the beginning of every paragraph.
+</p><p>Note that the length of the first line depends on a number of factors,
+including the width of the page, the font size, etc. Thus, an ordinary HTML
+paragraph such as:</p><pre class="html-example"><p>This is a somewhat long HTML
+paragraph that will be broken into several
+lines. The first line will be identified
+by a fictional tag sequence. The other lines
+will be treated as ordinary lines in the
+paragraph.</p>
+</pre>
+<p>the lines of which happen to be rendered as follows if the style rule above applies:
+</p><pre class="html-example">THIS IS A SOMEWHAT LONG HTML PARAGRAPH THAT
+will be broken into several lines. The first
+line will be identified by a fictional tag
+sequence. The other lines will be treated as
+ordinary lines in the paragraph.
+</pre>
+<p>might be "rewritten" by user agents to include the <em>fictional tag sequence</em>
+for <code>::first-line</code>. This fictional tag sequence helps to show how properties
+are inherited.
+</p><pre><p><b><p::first-line></b> This is a somewhat long HTML
+paragraph that<b></p::first-line></b> will be broken into several
+lines. The first line will be identified
+by a fictional tag sequence. The other lines
+will be treated as ordinary lines in the
+paragraph.</p>
+</pre>
+<p>If a pseudo-element breaks up a real element, the desired effect can be
+described by closing and then re-opening the fictional tag sequence.
+Thus, if we mark up the previous paragraph with a <code>span</code> element:</p><pre><p><b><span class="test"></b> This is a somewhat<b></span></b> long HTML
+paragraph that will be broken into several
+lines. The first line will be identified
+by a fictional tag sequence. The other lines
+will be treated as ordinary lines in the
+paragraph.</p>
+</pre>
+<p>the user agent could generate the appropriate start and end tags for the fictional tag sequence for <code>::first-line</code>.
+
+</p><pre><p><b><span class="test"></b><p::first-line> This is a
+somewhat</p::first-line><b></span></b><p::first-line>
+long HTML paragraph that</p::first-line> will be broken into
+several lines. The first line will be identified
+by a fictional tag sequence. The other lines
+will be treated as ordinary lines in the
+paragraph.</p>
+</pre>
+<p>The <code>::first-line</code> pseudo-element can only be attached to a
+block-level element.
+</p><p>The <code>::first-line</code> pseudo-element is similar to an inline-level
+element, but with certain restrictions, depending on usage. Only the following
+properties apply to a <code>::first-line</code> pseudo-element: font properties,
+color properties, background properties, <span class="propinst-word-spacing">'word-spacing',</span> <span class="propinst-letter-spacing">'letter-spacing',</span> <span class="propinst-text-decoration">'text-decoration',</span> <span class="propinst-vertical-align">'vertical-align',</span> <span class="propinst-text-transform">'text-transform',</span> <span class="propinst-line-height">'line-height',</span> <span class="propinst-text-shadow">'text-shadow'</span>, and <span class="propins [...]
+</p><h4><a name="first-letter">7.2 The ::first-letter pseudo-element</a></h4>
+<p>The <code>::first-letter</code> pseudo-element describes the first formatted
+  letter of an element.
+</p><p>The <code>::first-letter</code> pseudo-element can be attached to all elements.
+
+</p><p>The <code>::first-letter</code> pseudo-element may be used for "initial caps" and
+"drop caps", which are common typographical effects. This type of initial letter
+is similar to an inline-level element if its CSS 'float' property is 'none', but
+with certain restrictions, depending on usage. Otherwise it is similar to a
+floated element.
+</p><p>These are the CSS properties that apply to <code>::first-letter</code>
+pseudo-elements: font properties, color properties, background properties,
+'text-decoration', 'vertical-align' (only if 'float' is 'none'),
+'text-transform', 'line-height', margin properties, padding properties, border
+properties, 'float', 'text-shadow', and 'clear'.
+</p><div class="html-example">
+<p>
+</p><p>The following CSS2 will make a drop cap initial letter span two lines:
+  </p><pre><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
+<HTML>
+ <HEAD>
+  <TITLE>Drop cap initial letter</TITLE>
+  <STYLE type="text/css">
+   P               { font-size: 12pt; line-height: 12pt }
+   P::first-letter { font-size: 200%; font-style: italic;
+                     font-weight: bold; float: left }
+   SPAN            { text-transform: uppercase }
+  </STYLE>
+ </HEAD>
+ <BODY>
+  <P><SPAN>The first</SPAN> few words of an article
+    in The Economist.</P>
+ </BODY>
+</HTML>
+</pre>
+<p>This example might be formatted as follows:
+</p><div class="figure">
+<p><img height="54" alt="Image illustrating the combined effect of the :first-letter and :first-line pseudo-elements" src="first-letter.gif" width="105"> </p></div>
+<p>The fictional tag sequence is:</p><pre><P>
+<SPAN>
+<P::first-letter>
+T
+</P::first-letter>he first
+</SPAN>
+few words of an article in the Economist.
+</P>
+</pre>
+<p>Note that the <code>::first-letter</code> pseudo-element tags abut the content
+(e.g., the initial character). When both the <code>::first-line</code> and the
+<code>::first-letter</code> pseudo-elements are used, the <code>::first-letter</code>
+fictional tag sequence is inserted inside the <code>::first-line</code>
+fictional tag sequence.</p></div>
+<p>In order to achieve traditional drop caps formatting, user agents may
+approximate font sizes, for example to align baselines. Also, the glyph outline
+may be taken into account when formatting.
+</p><p>Punctuation (i.e, characters defined in Unicode <a class="noxref" href="#UNICODE" rel="biblioentry"><span class="normref">[UNICODE]</span></a> in the "open" (Ps), "close" (Pe), and "other"
+(Po) punctuation classes), that precedes the first letter should be included, as
+in:
+</p><div class="figure">
+<p><img height="72" alt="Quotes that precede the
+first letter should be included." src="first-letter2.gif" width="114"></p></div>
+<p>The <code>::first-letter</code> pseudo-element matches parts of elements
+only.
+</p><p>Some languages may have specific rules about how to treat certain letter combinations.
+  In Dutch, for example, if the letter combination "ij" appears at the beginning
+  of a word, both letters should be considered within the <code>::first-letter</code>
+  pseudo-element. The <code>::first-letter</code> pseudo-element should select
+  select from beginning of element up to the first non-opening-punctuation character
+  cluster.
+</p><p>
+</p><div class="example">
+<p><a name="overlapping-example">The following example</a> illustrates how
+overlapping pseudo-elements may interact. The first letter of each
+<code>P</code> element will be green with a font size of '24pt'. The rest of the
+first formatted line will be 'blue' while the rest of the paragraph will be
+'red'.</p><pre>P { color: red; font-size: 12pt }
+P::first-letter { color: green; font-size: 200% }
+P::first-line { color: blue }
+
+<P>Some text that ends up on two lines</P>
+</pre>
+<p>Assuming that a line break will occur before the word "ends", the fictional
+tag sequence for this fragment might be:</p><pre><P>
+<P::first-line>
+<P::first-letter>
+S
+</P::first-letter>ome text that
+</P::first-line>
+ends up on two lines
+</P>
+</pre>
+<p>Note that the<code>::first-letter</code> element is inside the
+<code>::first-line</code> element. Properties set on <code>::first-line</code> are
+inherited by <code>::first-letter</code>, but are overridden if the same property is
+set on <code>::first-letter</code>.</p></div>
+<h4><a name="UIfragments">7.3 The UI element fragments pseudo-elements</a></h4>
+<h5><a name="selection">The ::selection pseudo-element</a></h5>
+<p>The <code>::selection</code> pseudo-element applies to the portion of a document
+that has been highlighted by the user. This also applies, for example, to
+selected text within an editable text field. This
+pseudo-element should not be confused with the <code><a href="#checked">:checked</a></code>
+pseudo-class (which used to be named <code>:selected</code>)
+</p><p>Although the <code>::selection</code> pseudo-element is dynamic in nature,
+  and is altered by user action, it is reasonable to expect that when a UA rerenders
+  to a static medium (such as a printed page, see <a href="#CSS2">[CSS2]</a>)
+  which was originally rendered to a dynamic medium (like screen), the UA may
+  wish to transfer the current <code>::selection</code> state to that other medium,
+  and have all the appropriate formatting and rendering take effect as well. This
+  is not required - UAs may omit the <code>::selection</code> pseudo-element for
+  static media.
+</p><p>These are the CSS properties that apply to <code>::selection</code>
+pseudo-elements: color, cursor, background, outline. The computed value of the 'background-image' property on
+<code>::selection</code> may be ignored.
+</p><h4><a name="gen-content">7.4 The ::before and ::after pseudo-elements</a></h4>
+<p>The <code>::before</code> and <code>::after</code> pseudo-elements can be used to
+describe generated content before or after an element's content. They are
+explained in the Generated Content/Markers CSS3 Module.
+</p><p>When the <code>::first-letter</code> and <code>::first-line</code> pseudo-elements
+are combined with <code>::before</code> and <code>::after</code>, they apply to the
+first letter or line of the element including the inserted text. 
+</p><h2><a name="combinators">8. Combinators</a></h2>
+<h3><a name="descendant-combinators">8.1 Descendant combinator</a></h3>
+<p>At times, authors may want selectors to describe an element that is the descendant
+  of another element in the document tree (e.g., "an <code>EM</code> element that
+  is contained within an <code>H1</code> element"). Descendant combinators express
+  such a relationship. A descendant combinator is a <a href="#whitespace">white space</a> that separates two sequences of simple selectors.
+  A selector of the form "<code>A B</code>" represents an element <code>B</code>
+  that is an arbitrary descendant of some ancestor element <code>A</code>. 
+</p><div class="example">Examples:
+  <p>For example, consider the following selector: </p><pre>h1 em</pre>
+  It represents an <code>em</code> element being the descendant of an <code>h1</code>
+  element. It is a correct and valid, but partial, description of the following
+  fragment:
+  <pre><h1>This <span class="myclass">headline 
+is <em>very</em> important</span></h1></pre>The
+following selector: <pre>div * p</pre>represents a <code>p</code> element that is a grandchild or later
+descendant of a <code>div</code> element. Note the white space on either side of the
+"*".
+<p>The following selector, which combines descendant combinators and <a href="#attribute-selectors">attribute
+selectors</a>, represents an element that (1) has the <code>href</code> attribute
+set and (2) is inside a <code>p</code> that is itself inside a <code>div</code>: </p><pre>div p *[href]</pre></div>
+<h3><a name="child-combinators">8.2 Child combinators</a></h3>
+<p>A <dfn>child combinator</dfn> describes a childhood relationship between
+  two elements. A child combinator is made of the "greater-than sign"
+  (<code>></code>) character and separates two sequences of simple selectors.
+
+</p><div class="example">Examples:
+  <p>The following selector represents a <code>p</code> element that is child of
+<code>body</code>: </p><pre>body > p</pre>
+  <p>The following example combines descendant combinators and child combinators.
+</p><pre>div ol>li p</pre>
+<p>It represents a <code>p</code> element that is a descendant of an <code>li</code>;
+the <code>li</code> element must be the child of an <code>ol</code> element; the
+<code>ol</code> element must be a descendant of a <code>div</code>. Notice that the
+optional white space around the ">" combinator has been left out. 
+</p><p>For information on selecting the first child of an element, please see the
+section on the <code><a href="#structural-pseudos">:first-child</a></code>
+pseudo-class above. </p></div>
+<h3><a name="adjacent-combinators">8.3 Adjacent sibling combinators</a></h3>
+<p>There are two different adjacent sibling combinators: direct adjacent
+combinator and indirect adjacent combinator. 
+</p><h4><a name="adjacent-d-combinators">8.3.1 Direct adjacent combinators</a></h4>
+<p>Direct adjacent combinators are made of the "plus sign" (<code>+</code>)
+  character that separates two sequences of simple selectors. The elements represented
+  by the two sequences share the same parent in the document tree and the element
+  represented by the first sequence immediately precedes the element represented
+  by the second one. 
+</p><div class="example">Examples:
+  <p>Thus, the following selector represents a <code>p</code> element immediately
+following a <code>math</code> element: </p><pre>math + p</pre>
+  <p>The following selector is conceptually similar to the one in the previous
+example, except that it adds an attribute selector. Thus, it adds a constraint
+to the <code>h1</code> element that must have <code>class="opener"</code>: </p><pre>h1.opener + h2</pre></div>
+<h4><a name="adjacent-i-combinators">8.3.2 Indirect adjacent combinator</a></h4>
+<p>Indirect adjacent combinators are made of the "tilde" (<code>~</code>)
+  character that separates two sequences of simple selectors. The elements represented
+  by the two sequences share the same parent in the document tree and the element
+  represented by the first sequence precedes (not necessarily immediately) the
+  element represented by the second one. 
+</p><div class="example">Example:
+  <pre>h1 ~ pre</pre>represents a <code>pre</code> element following an <code>h1</code>. It
+is a correct and valid, but partial, description of: <pre><h1>Definition of the function a</h1>
+<p>Function a(x) has to be applied to all figures in the table.</p>
+<pre>function a(x) = 12x/13.5</pre></pre></div>
+<h2><a name="specificity">9. Calculating a selector's specificity</a></h2>
+<p>A selector's specificity is calculated as follows:
+</p><ul>
+  <li>negative selectors are counted like their simple selectors argument
+  </li><li>count the number of ID attributes in the selector (= a)
+  </li><li>count the number of other attributes and pseudo-classes in the selector (=
+  b)
+  </li><li>count the number of element names in the selector (= c)
+  </li><li>ignore pseudo-elements. </li></ul>
+<p>Concatenating the three numbers a-b-c (in a number system with a large base)
+gives the specificity.
+</p><div class="example">
+  <p>Some examples: </p><pre>*               /* a=0 b=0 c=0 -> specificity =   0 */
+LI              /* a=0 b=0 c=1 -> specificity =   1 */
+UL LI           /* a=0 b=0 c=2 -> specificity =   2 */
+UL OL+LI        /* a=0 b=0 c=3 -> specificity =   3 */
+H1 + *[REL=up]  /* a=0 b=1 c=1 -> specificity =  11 */
+UL OL LI.red    /* a=0 b=1 c=3 -> specificity =  13 */
+LI.red.level    /* a=0 b=2 c=1 -> specificity =  21 */
+#x34y           /* a=1 b=0 c=0 -> specificity = 100 */
+#s12:not(FOO)   /* a=1 b=0 c=1 -> specificity = 101 */
+</pre>
+<p><b>Note</b>: the specificity of the styles specified in a HTML
+<code>style</code> attribute is described in another CSS3 Module "Cascade and
+Inheritance".</p></div>
+<div class="html-example"></div>
+<h2><a name="w3cselgrammar">10. The grammar of <span class="modulename">Selectors</span></a></h2>
+<h3><a name="grammar">10.1 Grammar</a></h3>
+<p>The grammar below defines the syntax of <span class="modulename">Selectors</span>.
+  It is globally LL(1)  and can be locally LL(2) (but note that most UA's should not use it directly,
+  since it doesn't express the parsing conventions). The format of the productions
+  is optimized for human consumption and some shorthand notations beyond Yacc
+  (see <span class="normref"><a class="noxref" href="#yacc" rel="biblioentry">[YACC]</a></span>) are used: 
+</p><ul>
+  <li><b>*</b>: 0 or more
+  </li><li><b>+</b>: 1 or more
+  </li><li><b>?</b>: 0 or 1
+  </li><li><b>|</b>: separates alternatives
+  </li><li><b>[ ]</b>: grouping </li></ul>
+<p>The productions are: 
+</p><pre>selectors_group
+  : selector [ ',' S* selector ]*
+  ;
+
+selector
+  /* there is at least one sequence of simple selectors in a */
+  /* selector and the pseudo-elements occur only in the last */
+  /* sequence ; only pseudo-element may occur */
+  : [ simple_selector_sequence combinator ]*
+       simple_selector_sequence [ pseudo_element ]?
+  ;
+
+combinator
+  /* combinators can be surrounded by white space */
+  : S* [ '+' | '>' | '~' | /* empty */ ] S*
+  ;
+
+simple_selector_sequence
+  /* the universal selector is optional */
+  : [ type_selector | universal ]?
+        [ HASH | class | attrib | pseudo_class | negation ]+ |
+    type_selector | universal
+  ;
+
+type_selector
+  : [ namespace_prefix ]? element_name
+  ;
+
+namespace_prefix
+  : [ IDENT | '*' ]? '|'
+  ;
+
+element_name
+  : IDENT
+  ;
+
+universal
+  : [ namespace_prefix ]? '*'
+  ;
+
+class
+  : '.' IDENT
+  ;
+
+attrib
+  : '[' S* [ namespace_prefix ]? IDENT S*
+        [ [ PREFIXMATCH |
+            SUFFIXMATCH |
+            SUBSTRINGMATCH |
+            '=' |
+            INCLUDES |
+            DASHMATCH ] S* [ IDENT | STRING ] S*
+        ]? ']'
+  ;
+
+pseudo_class
+  /* a pseudo-class is an ident, or a function taking an */
+  /* ident or a string or a number or a simple selector  */
+  /* (excluding negation and pseudo-elements) */
+  /* or a an+b expression for argument */
+  : ':' [ IDENT | functional_pseudo ]
+  ;
+
+functional_pseudo
+  : FUNCTION S* [ IDENT | STRING | NUMBER |
+                  expression | negation_arg ] S* ')'
+  ;
+
+expression
+  :  [ [ '-' | INTEGER ]? 'n' [ SIGNED_INTEGER ]? ] | INTEGER
+  ;
+
+negation_arg
+  : type_selector | universal | HASH | class | attrib | pseudo_class
+  ;
+
+pseudo_element
+  : [ ':' ]? ':' IDENT
+  ;
+</pre>
+<h3><a name="lex">10.2 Lexical scanner</a></h3>
+<p>The following is the <a name="x3"></a><span class="index-def" title="tokenizer">tokenizer</span>, written in Flex (see <span class="normref"><a class="noxref" href="#flex" rel="biblioentry">[FLEX]</a></span>) notation. The tokenizer is case-insensitive.
+
+</p><p>The two occurrences of "\377" represent the highest character number that
+current versions of Flex can deal with (decimal 255). They should be read as
+"\4177777" (decimal 1114111), which is the highest possible code point
+in <a name="x4"></a><span class="index-inst" title="unicode">Unicode</span>/<a name="x5"></a><span class="index-inst" title="iso-10646">ISO-10646</span>. </p><pre>%option case-insensitive
+
+h                       [0-9a-f]
+nonascii                [\200-\377]
+unicode                 \\{h}{1,6}[ \t\r\n\f]?
+escape                  {unicode}|\\[ -~\200-\377]
+nmstart                 [a-z_]|{nonascii}|{escape}
+nmchar                  [a-z0-9-_]|{nonascii}|{escape}
+string1                 \"([\t !#$%&(-~]|\\{nl}|\'|{nonascii}|{escape})*\"
+string2                 \'([\t !#$%&(-~]|\\{nl}|\"|{nonascii}|{escape})*\'
+
+ident                   {nmstart}{nmchar}*
+name                    {nmchar}+
+integer                 [-]?[0-9]+
+signed_integer          [-+][0-9]+
+num                     {integer}|[0-9]*"."[0-9]+
+string                  {string1}|{string2}
+nl                      \n|\r\n|\r|\f
+%%
+
+[ \t\r\n\f]+    {return S;}
+
+\/\*[^*]*\*+([^/][^*]*\*+)*\/   /* ignore comments */
+
+"~="                    {return INCLUDES;}
+"|="                    {return DASHMATCH;}
+"^="                    (return PREFIXMATCH;)
+"$="                    (return SUFFIXMATCH;)
+"*="                    (return SUBSTRINGMATCH;)
+{string}                {return STRING;}
+{ident}                 {return IDENT;}
+{ident}"("              {return FUNCTION;}
+{num}                   {return NUMBER;}
+{signed_integer}        {return SIGNED_INTEGER;}
+{integer]               {return INTEGER;}
+"#"{name}               {return HASH;}
+
+.                       {return *yytext;}</pre>
+<h2><a name="downlevel">11. Namespaces and Down-Level Clients</a></h2>
+<p>An important issue is the interaction of CSS selectors with XML documents in
+web clients that were produced prior to this document. Unfortunately, due to the
+fact that namespaces must be matched based on the URI which identifies the
+namespace, not the namespace prefix, some mechanism is required to identify
+namespaces in CSS by their URI as well. Without such a mechanism, it is
+impossible to construct a CSS style sheet which will properly match selectors in
+all cases against a random set of XML documents. However, given complete
+knowledge of the XML document to which a style sheet is to be applied, and a
+limited use of namespaces within the XML document, it is possible to construct a
+style sheet in which selectors would match elements and attributes correctly.
+
+</p><p>It should be noted that a down-level CSS client will (if it properly conforms
+to CSS forward compatible parsing rules) ignore all <code>@namespace</code>
+at-rules, as well as all style rules that make use of namespace qualified
+element type or attribute selectors. The syntax of delimiting namespace prefixes
+in CSS was deliberately chosen so that down-level CSS clients would ignore the
+style rules rather than possibly match them incorrectly. 
+</p><p>The use of default namespaces in CSS makes it possible to write element type
+selectors that will function in both namespace aware CSS clients as well as
+down-level clients. It should be noted that down-level clients may incorrectly
+match selectors against XML elements in other namespaces. 
+</p><p>The following are scenarios and examples in which it is possible to construct
+style sheets which would function properly in web clients that do not implement
+this proposal. 
+</p><ol>
+  <li>The XML document does not use namespaces.
+  <ul>
+    <li>In this case, it is obviously not necessary to declare or use namespaces
+    in the style sheet. Standard CSS element type and attribute selectors will
+    function adequately in a down-level client.
+    </li><li>In a CSS namespace aware client, the default behavior of element
+    selectors matching without regard to namespace will function properly
+    against all elements, since no namespaces are present. However, the use of
+    specific element type selectors that match only elements that have no
+    namespace ("<code>|name</code>") will guarantee that selectors will match only
+    XML elements that do not have a declared namespace. </li></ul>
+  </li><li>The XML document defines a single, default namespace used throughout the
+  document. No namespace prefixes are used in element names.
+  <ul>
+    <li>In this case, a down-level client will function as if namespaces were
+    not used in the XML document at all. Standard CSS element type and attribute
+    selectors will match against all elements. </li></ul>
+  </li><li>The XML document does <b>not</b> use a default namespace, all namespace
+    prefixes used are known to the style sheet author and there is a direct mapping
+    between namespace prefixes and namespace URIs. (A given prefix may only be
+    mapped to one namespace URI throughout the XML document, there may be multiple
+    prefixes mapped to the same URI).
+    <ul>
+    <li>In this case, the down-level client will view and match element type and
+    attribute selectors based on their fully qualified name, not the local part
+    as outlined in the <a href="#typenmsp">Type selectors and
+    Namespaces</a> section. CSS selectors may be declared using an escaped colon
+    "<code>\:</code>" to describe the fully qualified names, e.g.
+    "<code>html\:h1</code>" will match <code><html:h1></code>. Selectors using the
+    qualified name will only match XML elements that use the same prefix. Other
+    namespace prefixes used in the XML that are mapped to the same URI will not
+    match as expected unless additional CSS style rules are declared for them.
+    </li><li>Note that selectors declared in this fashion will <i>only</i> match in
+    down-level clients. A CSS namespace aware client will match element type and
+    attribute selectors based on the name's local part. So selectors declared
+    with the fully qualified name will not match (unless there is no namespace
+    prefix in the fully qualified name). </li></ul></li></ol>
+<p>In other scenarios: when the namespace prefixes used in the XML are not known
+in advance by the style sheet author; or a combination of elements with no
+namespace are used in conjunction with elements using a default namespace; or
+the same namespace prefix is mapped to <i>different</i> namespace URIs within
+the same document, or in different documents; it is impossible to construct a
+CSS style sheet that will function properly against all elements in those
+documents, unless, the style sheet is written using a namespace URI syntax (as
+outlined in this document or similar) and the document is processed by a CSS and
+XML namespace aware client. 
+</p><h2><a name="profiling">12. Profiles</a></h2>
+<p>Each specification using <span class="modulename">Selectors</span> must define the subset of W3C
+Selectors it allows and excludes, and describe the local meaning of all the
+components of that subset.
+</p><p>Non normative examples:
+</p><div class="profile">
+<table class="tprofile" width="75%" border="1">
+  <tbody>
+  <tr>
+    <th class="title" colspan="2"><span class="modulename">Selectors</span> profile</th></tr>
+  <tr>
+    <th>Specification</th>
+    <td>CSS level 1</td></tr>
+  <tr>
+    <th>Accepts</th>
+    <td>type selectors <br>class selectors <br>ID selectors <br>:link,
+      :visited and :active pseudo-classes <br>descendant combinator
+      <br>:first-line and :first-letter pseudo-elements </td></tr>
+  <tr>
+    <th>Excludes</th>
+    <td>
+      <p>universal selector<br>attribute selectors<br>:hover and :focus
+      pseudo-classes<br>:target pseudo-class<br>:lang() pseudo-class<br>all UI
+      element states pseudo-classes<br>all structural
+      pseudo-classes<br>:contains() pseudo-class<br>negation pseudo-class<br>all
+      UI element fragments pseudo-elements<br>::before and ::after
+      pseudo-elements<br>child combinators<br>adjacent sibling combinators
+      </p><p>namespaces</p></td></tr>
+  <tr>
+    <th>Extra constraints</th>
+    <td>only one class selector allowed per sequence of simple
+  selectors</td></tr></tbody></table><br>  <br> 
+<table class="tprofile" width="75%" border="1">
+  <tbody>
+  <tr>
+    <th class="title" colspan="2"><span class="modulename">Selectors</span> profile</th></tr>
+  <tr>
+    <th>Specification</th>
+    <td>CSS level 2</td></tr>
+  <tr>
+    <th>Accepts</th>
+    <td>type selectors <br>universal selector <br>attribute presence and
+      values selectors<br>class selectors <br>ID selectors <br>:link, :visited,
+      :active, :hover, :focus, :lang() and :first-child pseudo-classes
+      <br>descendant combinator <br>child combinator <br>adjacent direct
+      combinator <br>::first-line and ::first-letter pseudo-elements<br>::before
+      and ::after pseudo-elements</td></tr>
+  <tr>
+    <th>Excludes</th>
+    <td>
+      <p>content selectors <br>substring matching attribute selectors<br>:target
+      pseudo-classes  <br>all UI element states pseudo-classes<br>all
+      structural pseudo-classes other than :first-child<br>:contains()
+      pseudo-class<br>negation pseudo-class<br>all UI element fragments
+      pseudo-elements<br>adjacent indirect combinators 
+      </p><p>namespaces</p></td></tr>
+  <tr>
+    <th>Extra constraints</th>
+    <td>more than one class selector per sequence of simple selectors (CSS1
+      constraint) allowed</td></tr></tbody></table>
+<p>In CSS, selectors express pattern matching rules that determine which style
+rules apply to elements in the document tree. 
+</p><p>The following selector (CSS level 2) will <b>match</b> all anchors <code>a</code>
+with attribute <code>name</code> set inside a section 1 header <code>h1</code>: </p><pre>h1 a[name]</pre>
+<p>All CSS declarations attached to such a selector are applied to elements
+matching it. </p></div>
+<div class="profile">
+<table class="tprofile" width="75%" border="1">
+  <tbody>
+  <tr>
+    <th class="title" colspan="2"><span class="modulename">Selectors</span> profile</th></tr>
+  <tr>
+    <th>Specification</th>
+      <td>STTS 3</td>
+    </tr>
+  <tr>
+    <th>Accepts</th>
+    <td>
+      <p>type selectors<br>universal selectors<br>attribute selectors<br>class
+      selectors<br>ID selectors<br>all structural pseudo-classes<br>
+          :contains() pseudo-class<br>
+          all combinators
+      </p><p>namespaces</p></td></tr>
+  <tr>
+    <th>Excludes</th>
+    <td>non accepted pseudo-classes<br>pseudo-elements<br></td></tr>
+  <tr>
+    <th>Extra constraints</th>
+    <td>some selectors and combinators are not allowed in fragment
+      descriptions on the right side of STTS declarations.</td></tr></tbody></table>
+  <p><span class="modulename">Selectors</span> can be used in STTS 3 in two different
+    manners: 
+</p><ol>
+  <li>a selection mechanism equivalent to CSS selection mechanism: declarations
+  attached to a given selector are applied to elements matching that selector,
+  </li><li>fragment descriptions that appear on the right side of declarations.
+</li></ol></div>
+<h2><a name="Conformance"></a>13. Conformance and Requirements</h2>
+<p>This section defines conformance with the present specification only.
+</p><p>The inability of a user agent to implement part of this specification due to
+the limitations of a particular device (e.g., non interactive user agents will
+probably not implement dynamic pseudo-classes because they make no sense without
+interactivity) does not imply non-conformance.
+</p><p>All specifications reusing <span class="modulename">Selectors</span> must contain a <a href="#profiling">Profile</a> listing the
+subset of <span class="modulename">Selectors</span> it accepts or excludes, and describing the constraints
+it adds to the current specification. 
+</p><p>Invalidity is caused by a parsing error, e.g. an unrecognized token or a token
+which is not allowed at the current parsing point.
+</p><p>User agents must observe the rules for handling parsing errors:
+</p><ul>
+  <li>a simple selector containing an undeclared namespace prefix is invalid</li>
+  <li>a selector containing an invalid simple selector, an invalid combinator
+    or an invalid token is invalid. </li>
+  <li>a group of selectors containing an invalid selector is invalid.</li>
+</ul>
+<p>Implementations of this specification must behave as "recipients
+of text data" as defined by
+<a class="noxref" href="#CWWW" rel="biblioentry"><span class="normref">[CWWW]</span></a>
+when parsing selectors and attempting matches. (In particular, implementations must assume
+the data is normalized and must not normalize it.) Normative rules
+for matching strings are defined in
+<a class="noxref" href="#CWWW" rel="biblioentry"><span class="normref">[CWWW]</span></a>
+and <a class="noxref" href="#UNICODE" rel="biblioentry"><span class="normref">[UNICODE]</span></a>
+and apply to implementations of this specification.
+
+</p><h2><a name="Tests"></a>14. Tests</h2>
+<p>This specification contains a test suite allowing user agents to verify their
+basic conformance to the specification. This test suite does not pretend to be
+exhaustive and does not cover all possible combined cases of <span class="propernoun">Selectors</span>.
+</p><p>These tests are available [link forthcoming].
+</p><h2><a name="ACKS"></a>15. Acknowledgements</h2>
+<p>This specification is the product of the W3C Working Group on Cascading Style
+Sheets and Formatting Properties. In addition to the editors of this
+specification, the members of the Working Group are:
+</p><ul>
+  <li>Marc Attinasi (Netscape/AOL)
+  </li><li>Bert Bos (W3C)
+  </li><li>Tantek Çelik (Microsoft Corp.)
+  </li><li>Don Day (IBM)
+  </li><li>Martin Dürst (W3C)
+  </li><li>Angel Diaz (IBM)
+  </li><li>Daniel Glazman (Netscape/AOL from November 2000, and Electricité de France
+    until February 2000)
+  </li><li>Håkon W. Lie (Opera Software from April 1999, and W3C until April 1999)
+  </li><li>Chris Lilley (W3C)
+  </li><li>Dave Raggett (W3C/Openwave Systems Inc.)
+  </li><li>Pierre Saslawsky (Netscape/AOL)
+  </li><li>Robert Stevahn (Hewlett-Packard)
+  </li><li>Michel Suignard (Microsoft Corp.)
+  </li><li>Ted Wugofski (Openwave Systems Inc.)
+  </li><li>Steve Zilles (Adobe) </li></ul>
+<p>A number of invited experts to the Working Group have significantly contributed
+  to CSS3: L. David Baron, Tim Boland (NIST), Todd Fahrner, Daniel Glazman, Ian
+  Hickson, Eric Meyer (The OPAL Group), Jeff Veen.
+</p><p>Former members of the Working Group:
+</p><ul>
+  <li>Chris Brichford (Adobe)
+  </li><li>Troy Chevalier (Netscape/AOL)
+  </li><li>Dwayne Dicks (SoftQuad)
+  </li><li>Ian Jacobs (W3C)
+  </li><li>Lorin Jurow (Quark)
+  </li><li>Sho Kuwamoto (Macromedia)
+  </li><li>Peter Linss (Netscape/AOL)
+  </li><li>Steven Pemberton (CWI)
+  </li><li>Robert Pernett (Lotus)
+  </li><li>Douglas Rand (SGI)
+  </li><li>Nisheeth Ranjan (Netscape/AOL)
+  </li><li>Ed Tecot (Microsoft Corp.)
+  </li><li>Jared Sorensen (Novell)
+  </li><li>Mike Wexler (Adobe)
+  </li><li>John Williams (Quark)
+  </li><li>Chris Wilson (Microsoft Corp.) </li></ul>
+<p>We thank all of them (members, invited experts and former members) for their
+efforts.
+</p><p>Of course, this document derives from the CSS Level 1 and CSS level 2
+Recommendations. We thank all CSS1 and CSS2 authors, editors and
+contributors.
+</p><p>Dr. Hasan Ali Çelik suggested the simple and powerful syntax of the argument
+for :nth-child() while the Working Group was considering much more complex
+solutions.
+</p><p>The discussions on www-style at w3.org have been influential in many key issues.
+  Especially, we would like to thank Ian Graham, David Baron, Björn Höhrmann,
+  <i>fantasai</i>, Jelks Cabanis and Matthew Brealey for their active and useful
+  participation.
+</p><h2><a name="references">16. References</a></h2>
+<ol class="refs">
+  <li>[CSS1] <a name="CSS1"></a>Bert Bos, Håkon Wium Lie; "<i>Cascading Style
+    Sheets, level 1</i>", W3C Recommendation, 17 Dec 1996, revised 11 Jan 1999<br>
+    (<code><a href="http://www.w3.org/TR/REC-CSS1">http://www.w3.org/TR/REC-CSS1</a></code>)
+  </li><li>[CSS2]<a name="CSS2"></a> Bert Bos, Håkon Wium Lie, Chris Lilley, Ian
+    Jacobs, editors; "<i>Cascading Style Sheets, level 2</i>", W3C Recommendation,
+    12 May 1998 <br>
+    (<code><a href="http://www.w3.org/TR/REC-CSS2/">http://www.w3.org/TR/REC-CSS2/</a></code>)
+  </li><li id="CWWW">[CWWW] Martin J. Dürst, François Yergeau, Misha Wolf,
+    Asmus Freytag, Tex Texin, editors; "<i>Character Model for the World Wide
+    Web</i>", W3C Working Draft, 26 January 2001<br>
+    (<code><a href="http://www.w3.org/TR/2001/WD-charmod-20010126">http://www.w3.org/TR/2001/WD-charmod-20010126</a></code>)
+  </li><li>[FLEX] <a name="flex"></a>"Flex: The Lexical Scanner Generator",
+    Version 2.3.7, ISBN 1882114213</li>
+  <li>[HTML4.01] <a name="html40"></a>Dave Ragget, Arnaud Le Hors, Ian Jacobs,
+    editors; "HTML 4.01 Specification", W3C Recommendation, 24 December
+    1999<br>
+    (<a href="http://www.w3.org/TR/html401/"><code>http://www.w3.org/TR/html401/</code></a>)</li>
+  <li>[MATH] <a name="MATH"></a>Patrick Ion, Robert Miner; "<i>Mathematical
+    Markup Language (MathML) 1.01</i>", W3C Recommendation, revision of 7
+    July 1999<br>
+    (<code><a href="http://www.w3.org/1999/07/REC-MathML-19990707">http://www.w3.org/1999/07/REC-MathML-19990707</a></code>)<br>
+  </li>
+  <li>[NMSP] <a name="nmsp19990625"></a>Peter Linss, editor; "<i>CSS Namespace
+    Enhancements (Proposal)</i>", W3C Working Draft, 25 June 1999 <br>
+    (<code><a href="http://www.w3.org/1999/06/25/WD-css3-namespace-19990625/">http://www.w3.org/1999/06/25/WD-css3-namespace-19990625/</a></code>)
+  </li>
+  <li>[RFC3066] <a name="rfc3066"></a>H. Alvestrand; "Tags for the Identification
+    of Languages", Request for Comments 3066, January 2001<br>
+    (<a href="http://www.ietf.org/rfc/rfc3066.txt"><code>http://www.ietf.org/rfc/rfc3066.txt</code></a>)
+  </li>
+  <li>[STTS3]<a name="STTS"></a> Daniel Glazman ; "<i>Simple Tree Transformation
+    Sheets 3</i>", Electricité de France, submission to the W3C, 11 Nov
+    1998 <br>
+    (<code><a href="http://www.w3.org/TR/NOTE-STTS3">http://www.w3.org/TR/NOTE-STTS3</a></code>)
+  </li><li>[SVG] <a name="SVG"></a>Jon Ferraiolo ed.; "<i>Scalable Vector Graphics
+    (SVG) 1.0 Specification</i>", W3C Proposed Recommendation, 19 July 2001<br>
+    (<code><a href="http://www.w3.org/TR/2001/PR-SVG-20010719">http://www.w3.org/TR/2001/PR-SVG-20010719</a></code>)<br>
+  </li><li>[UI] <a name="UI-WD"></a>Tantek Çelik, editor; "<i>User Interface
+    for CSS3</i>", W3C Working Draft, 16 February 2000 <br>
+    (<code><a href="http://www.w3.org/TR/2000/WD-css3-userint-20000216">http://www.w3.org/TR/2000/WD-css3-userint-20000216</a></code>)
+  </li><li>[UNICODE] <a name="UNICODE"></a>"<i>The Unicode Standard: Version 3.0.1</i>",
+    The Unicode Consortium, Addison Wesley Longman, 2000, ISBN 0-201-61633-5.<br>
+    URL: <a href="http://www.unicode.org/unicode/standard/versions/Unicode3.0.1.html">http://www.unicode.org/unicode/standard/versions/Unicode3.0.1.html</a>.<br>
+    The latest version of Unicode. For more information, consult the Unicode Consortium's
+    home page at <code><a href="http://www.unicode.org/">http://www.unicode.org/</a></code>.
+  </li><li>[XML-NAMES] <a name="XMLNAMES"></a>Tim Bray, Dave Hollander, Andrew Layman,
+    editors; "Namespaces in XML", W3C Recommendation, 14 January 1999<br>
+    (<a href="http://www.w3.org/TR/REC-xml-names/"><code>http://www.w3.org/TR/REC-xml-names/</code></a>)</li>
+  <li>[YACC] <a name="yacc"></a>"YACC - Yet another compiler compiler",
+    S. C. Johnson, Technical Report, Murray Hill, 1975</li>
+</ol>
+<!-- Keep this comment at the end of the file
+Local variables:
+mode: sgml
+sgml-declaration:"~/SGML/HTML4.decl"
+sgml-default-doctype-name:"html"
+sgml-minimize-attributes:t
+sgml-nofill-elements:("pre" "style" "br")
+End:
+-->
+
+</body></html>
\ No newline at end of file
diff --git a/test/tools/helper.js b/test/tools/helper.js
new file mode 100644
index 0000000..0b08e98
--- /dev/null
+++ b/test/tools/helper.js
@@ -0,0 +1,51 @@
+var fs = require("fs"),
+    path = require("path"),
+    htmlparser2 = require("htmlparser2"),
+    DomUtils = htmlparser2.DomUtils,
+    CSSselect = require("../../");
+
+function getDOMFromPath(path, options){
+	return htmlparser2.parseDOM(fs.readFileSync(path).toString(), options);
+}
+
+module.exports = {
+	CSSselect: CSSselect,
+	getFile: function(name, options){
+		return getDOMFromPath(path.join(__dirname, "docs", name), options);
+	},
+	getDOMFromPath: getDOMFromPath,
+	getDOM: htmlparser2.parseDOM,
+	getDefaultDom: function(){
+		return htmlparser2.parseDOM(
+			"<elem id=foo><elem class='bar baz'><tag class='boom'> This is some simple text </tag></elem></elem>"
+		);
+	},
+	getDocument: function(path){
+		var document = getDOMFromPath(path);
+
+		document.getElementsByTagName = function(name){
+			return DomUtils.getElementsByTagName("*", document);
+		};
+		document.getElementById = function(id){
+			return DomUtils.getElementById(id, document);
+		};
+		document.createTextNode = function(content){
+			return {
+				type: "text",
+				data: "content"
+			};
+		};
+		document.createElement = function(name){
+			return {
+				type: "tag",
+				name: name,
+				children: [],
+				attribs: {}
+			};
+		};
+		document.body = DomUtils.getElementsByTagName("body", document, true, 1)[0];
+		document.documentElement = document.filter(DomUtils.isTag)[0];
+
+		return document;
+	}
+};
\ No newline at end of file
diff --git a/test/tools/slickspeed.js b/test/tools/slickspeed.js
new file mode 100644
index 0000000..8602775
--- /dev/null
+++ b/test/tools/slickspeed.js
@@ -0,0 +1,76 @@
+var helper = require("./helper.js"),
+	doc = helper.getFile("W3C_Selectors.html"),
+	CSSselect = helper.CSSselect,
+	soupselect = require("cheerio-soupselect"),
+	selectors = ["body", "div", "body div", "div p", "div > p", "div + p", "div ~ p", "div[class^=exa][class$=mple]", "div p a", "div, p, a", ".note", "div.example", "ul .tocline2", "div.example, div.note", "#title", "h1#title", "div #title", "ul.toc li.tocline2", "ul.toc > li.tocline2", "h1#title + div > p", "h1[id]:contains(Selectors)", "a[href][lang][class]", "div[class]", "div[class=example]", "div[class^=exa]", "div[class$=mple]", "div[class*=e]", "div[class|=dialog]", "div[class!=made [...]
+
+var engines = [function(a,b){return CSSselect(b,a);}, soupselect.select];
+
+//returns true when an error occurs
+function testResult(rule, index){
+	var results = engines
+		.map(function(func){ return func(doc, rule); });
+
+	//check if both had the same result
+	for(var i = 1; i < results.length; i++){
+		//TODO: might be hard to debug with more engines
+		if(results[i-1].length !== results[i].length){
+			//console.log(rule, results[i-1].length, results[i].length);
+			return true;
+		}
+		for(var j = 0; j < results[i].length; j++){
+			if(results[i-1][j] !== results[i][j]){
+				if(results[i-1].indexOf(results[i][j]) === -1){
+					return true;
+				}
+			}
+		}
+		//require("assert").deepEqual(results[i-1], results[i], rule + ": not the same elements");
+	}
+	
+	return false;
+}
+
+selectors.filter(testResult).forEach(function(rule){ print(rule, "failed!\n"); });
+
+process.exit(0); //don't run speed tests
+
+print("-----\n\nChecking performance\n\n");
+
+//test the speed
+var ben = require("ben");
+
+function testSpeed(rule){
+	print(rule, Array(28-rule.length).join(" "));
+
+	var results = engines
+		.map(function(func){ return function(){ return func(doc, rule); }});
+	
+	//also add a precompiled CSSselect test
+	var compiled = CSSselect(rule);
+	results.unshift(function(){ return CSSselect.iterate(compiled, doc); });
+	
+	results = results.map(ben);
+	
+	var min = Math.min.apply(null, results);
+	var max = Math.max.apply(null, results);
+
+	results.forEach(function(result){
+		if(result === min) return print(" +", result, "+");
+		if(result === max) return print(" !", result, "!");
+		if(Math.abs(result-min) > Math.abs(result-max)){
+			return print(" =", result, "=");
+		}
+		print(" ~", result, "~");
+	});
+	
+	print("\n");
+}
+
+print("RULE                    ", "CSSselect (pc)", "CSSselect", "soupselect\n");
+
+selectors.forEach(testSpeed);
+
+function print(){
+	process.stdout.write(Array.prototype.join.call(arguments, " "));
+}
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-css-select.git



More information about the Pkg-javascript-commits mailing list