PageRenderTime 28ms CodeModel.GetById 12ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 1ms

/testing/selenium-core/lib/cssQuery/src/cssQuery-level2.js

http://datanucleus-appengine.googlecode.com/
JavaScript | 142 lines | 93 code | 18 blank | 31 comment | 17 complexity | 8c1ea18740916e41a4a3b799f0600661 MD5 | raw file
  1/*
  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/
  5*/
  6
  7cssQuery.addModule("css-level2", function() {
  8
  9// -----------------------------------------------------------------------
 10// selectors
 11// -----------------------------------------------------------------------
 12
 13// child selector
 14selectors[">"] = function($results, $from, $tagName, $namespace) {
 15	var $element, i, j;
 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);
 21	}
 22};
 23
 24// sibling selector
 25selectors["+"] = 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);
 30	}
 31};
 32
 33// attribute selector
 34selectors["@"] = function($results, $from, $attributeSelectorID) {
 35	var $test = attributeSelectors[$attributeSelectorID].test;
 36	var $element, i;
 37	for (i = 0; ($element = $from[i]); i++)
 38		if ($test($element)) $results.push($element);
 39};
 40
 41// -----------------------------------------------------------------------
 42// pseudo-classes
 43// -----------------------------------------------------------------------
 44
 45pseudoClasses["first-child"] = function($element) {
 46	return !previousElementSibling($element);
 47};
 48
 49pseudoClasses["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"));
 53};
 54
 55// -----------------------------------------------------------------------
 56//  attribute selectors
 57// -----------------------------------------------------------------------
 58
 59// constants
 60AttributeSelector.NS_IE = /\\:/g;
 61AttributeSelector.PREFIX = "@";
 62// properties
 63AttributeSelector.tests = {};
 64// methods
 65AttributeSelector.replace = function($match, $attribute, $namespace, $compare, $value) {
 66	var $key = this.PREFIX + $match;
 67	if (!attributeSelectors[$key]) {
 68		$attribute = this.create($attribute, $compare || "", $value || "");
 69		// store the selector
 70		attributeSelectors[$key] = $attribute;
 71		attributeSelectors.push($attribute);
 72	}
 73	return attributeSelectors[$key].id;
 74};
 75AttributeSelector.parse = function($selector) {
 76	$selector = $selector.replace(this.NS_IE, "|");
 77	var $match;
 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);
 81	}
 82	return $selector;
 83};
 84AttributeSelector.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;
 92};
 93AttributeSelector.getAttribute = function($name) {
 94	switch ($name.toLowerCase()) {
 95		case "id":
 96			return "e.id";
 97		case "class":
 98			return "e.className";
 99		case "for":
100			return "e.htmlFor";
101		case "href":
102			if (isMSIE) {
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]||'')";
107			}
108	}
109	return "e.getAttribute('" + $name.replace($NAMESPACE, ":") + "')";
110};
111
112// -----------------------------------------------------------------------
113//  attribute selector tests
114// -----------------------------------------------------------------------
115
116AttributeSelector.tests[""] = function($attribute) {
117	return $attribute;
118};
119
120AttributeSelector.tests["="] = function($attribute, $value) {
121	return $attribute + "==" + Quote.add($value);
122};
123
124AttributeSelector.tests["~="] = function($attribute, $value) {
125	return "/(^| )" + regEscape($value) + "( |$)/.test(" + $attribute + ")";
126};
127
128AttributeSelector.tests["|="] = function($attribute, $value) {
129	return "/^" + regEscape($value) + "(-|$)/.test(" + $attribute + ")";
130};
131
132// -----------------------------------------------------------------------
133//  parsing
134// -----------------------------------------------------------------------
135
136// override parseSelector to parse out attribute selectors
137var _parseSelector = parseSelector;
138parseSelector = function($selector) {
139	return _parseSelector(AttributeSelector.parse($selector));
140};
141
142}); // addModule