2
cssQuery, version 2.0.2 (2005-08-19)
3
Copyright: 2004-2005, Dean Edwards (http://dean.edwards.name/)
4
License: http://creativecommons.org/licenses/LGPL/2.1/
7
cssQuery.addModule("css-level2", function() {
9
// -----------------------------------------------------------------------
11
// -----------------------------------------------------------------------
14
selectors[">"] = function($results, $from, $tagName, $namespace) {
16
for (i = 0; i < $from.length; i++) {
17
var $subset = childElements($from[i]);
18
for (j = 0; ($element = $subset[j]); j++)
19
if (compareTagName($element, $tagName, $namespace))
20
$results.push($element);
25
selectors["+"] = function($results, $from, $tagName, $namespace) {
26
for (var i = 0; i < $from.length; i++) {
27
var $element = nextElementSibling($from[i]);
28
if ($element && compareTagName($element, $tagName, $namespace))
29
$results.push($element);
34
selectors["@"] = function($results, $from, $attributeSelectorID) {
35
var $test = attributeSelectors[$attributeSelectorID].test;
37
for (i = 0; ($element = $from[i]); i++)
38
if ($test($element)) $results.push($element);
41
// -----------------------------------------------------------------------
43
// -----------------------------------------------------------------------
45
pseudoClasses["first-child"] = function($element) {
46
return !previousElementSibling($element);
49
pseudoClasses["lang"] = function($element, $code) {
50
$code = new RegExp("^" + $code, "i");
51
while ($element && !$element.getAttribute("lang")) $element = $element.parentNode;
52
return $element && $code.test($element.getAttribute("lang"));
55
// -----------------------------------------------------------------------
56
// attribute selectors
57
// -----------------------------------------------------------------------
60
AttributeSelector.NS_IE = /\\:/g;
61
AttributeSelector.PREFIX = "@";
63
AttributeSelector.tests = {};
65
AttributeSelector.replace = function($match, $attribute, $namespace, $compare, $value) {
66
var $key = this.PREFIX + $match;
67
if (!attributeSelectors[$key]) {
68
$attribute = this.create($attribute, $compare || "", $value || "");
70
attributeSelectors[$key] = $attribute;
71
attributeSelectors.push($attribute);
73
return attributeSelectors[$key].id;
75
AttributeSelector.parse = function($selector) {
76
$selector = $selector.replace(this.NS_IE, "|");
78
while ($match = $selector.match(this.match)) {
79
var $replace = this.replace($match[0], $match[1], $match[2], $match[3], $match[4]);
80
$selector = $selector.replace(this.match, $replace);
84
AttributeSelector.create = function($propertyName, $test, $value) {
85
var $attributeSelector = {};
86
$attributeSelector.id = this.PREFIX + attributeSelectors.length;
87
$attributeSelector.name = $propertyName;
88
$test = this.tests[$test];
89
$test = $test ? $test(this.getAttribute($propertyName), getText($value)) : false;
90
$attributeSelector.test = new Function("e", "return " + $test);
91
return $attributeSelector;
93
AttributeSelector.getAttribute = function($name) {
94
switch ($name.toLowerCase()) {
103
// IE always returns the full path not the fragment in the href attribute
104
// so we RegExp it out of outerHTML. Opera does the same thing but there
105
// is no way to get the original attribute.
106
return "String((e.outerHTML.match(/href=\\x22?([^\\s\\x22]*)\\x22?/)||[])[1]||'')";
109
return "e.getAttribute('" + $name.replace($NAMESPACE, ":") + "')";
112
// -----------------------------------------------------------------------
113
// attribute selector tests
114
// -----------------------------------------------------------------------
116
AttributeSelector.tests[""] = function($attribute) {
120
AttributeSelector.tests["="] = function($attribute, $value) {
121
return $attribute + "==" + Quote.add($value);
124
AttributeSelector.tests["~="] = function($attribute, $value) {
125
return "/(^| )" + regEscape($value) + "( |$)/.test(" + $attribute + ")";
128
AttributeSelector.tests["|="] = function($attribute, $value) {
129
return "/^" + regEscape($value) + "(-|$)/.test(" + $attribute + ")";
132
// -----------------------------------------------------------------------
134
// -----------------------------------------------------------------------
136
// override parseSelector to parse out attribute selectors
137
var _parseSelector = parseSelector;
138
parseSelector = function($selector) {
139
return _parseSelector(AttributeSelector.parse($selector));