PageRenderTime 20ms CodeModel.GetById 6ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
JavaScript | 150 lines | 93 code | 34 blank | 23 comment | 32 complexity | 19a9a12db544d7de0ee1ea9b6dfe71c9 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
  7/* Thanks to Bill Edney */
  8
  9cssQuery.addModule("css-level3", function() {
 10
 11// -----------------------------------------------------------------------
 12// selectors
 13// -----------------------------------------------------------------------
 14
 15// indirect sibling selector
 16selectors["~"] = function($results, $from, $tagName, $namespace) {
 17	var $element, i;
 18	for (i = 0; ($element = $from[i]); i++) {
 19		while ($element = nextElementSibling($element)) {
 20			if (compareTagName($element, $tagName, $namespace))
 21				$results.push($element);
 22		}
 23	}
 24};
 25
 26// -----------------------------------------------------------------------
 27// pseudo-classes
 28// -----------------------------------------------------------------------
 29
 30// I'm hoping these pseudo-classes are pretty readable. Let me know if
 31//  any need explanation.
 32
 33pseudoClasses["contains"] = function($element, $text) {
 34	$text = new RegExp(regEscape(getText($text)));
 35	return $text.test(getTextContent($element));
 36};
 37
 38pseudoClasses["root"] = function($element) {
 39	return $element == getDocument($element).documentElement;
 40};
 41
 42pseudoClasses["empty"] = function($element) {
 43	var $node, i;
 44	for (i = 0; ($node = $element.childNodes[i]); i++) {
 45		if (thisElement($node) || $node.nodeType == 3) return false;
 46	}
 47	return true;
 48};
 49
 50pseudoClasses["last-child"] = function($element) {
 51	return !nextElementSibling($element);
 52};
 53
 54pseudoClasses["only-child"] = function($element) {
 55	$element = $element.parentNode;
 56	return firstElementChild($element) == lastElementChild($element);
 57};
 58
 59pseudoClasses["not"] = function($element, $selector) {
 60	var $negated = cssQuery($selector, getDocument($element));
 61	for (var i = 0; i < $negated.length; i++) {
 62		if ($negated[i] == $element) return false;
 63	}
 64	return true;
 65};
 66
 67pseudoClasses["nth-child"] = function($element, $arguments) {
 68	return nthChild($element, $arguments, previousElementSibling);
 69};
 70
 71pseudoClasses["nth-last-child"] = function($element, $arguments) {
 72	return nthChild($element, $arguments, nextElementSibling);
 73};
 74
 75pseudoClasses["target"] = function($element) {
 76	return $element.id == location.hash.slice(1);
 77};
 78
 79// UI element states
 80
 81pseudoClasses["checked"] = function($element) {
 82	return $element.checked;
 83};
 84
 85pseudoClasses["enabled"] = function($element) {
 86	return $element.disabled === false;
 87};
 88
 89pseudoClasses["disabled"] = function($element) {
 90	return $element.disabled;
 91};
 92
 93pseudoClasses["indeterminate"] = function($element) {
 94	return $element.indeterminate;
 95};
 96
 97// -----------------------------------------------------------------------
 98//  attribute selector tests
 99// -----------------------------------------------------------------------
100
101AttributeSelector.tests["^="] = function($attribute, $value) {
102	return "/^" + regEscape($value) + "/.test(" + $attribute + ")";
103};
104
105AttributeSelector.tests["$="] = function($attribute, $value) {
106	return "/" + regEscape($value) + "$/.test(" + $attribute + ")";
107};
108
109AttributeSelector.tests["*="] = function($attribute, $value) {
110	return "/" + regEscape($value) + "/.test(" + $attribute + ")";
111};
112
113// -----------------------------------------------------------------------
114//  nth child support (Bill Edney)
115// -----------------------------------------------------------------------
116
117function nthChild($element, $arguments, $traverse) {
118	switch ($arguments) {
119		case "n": return true;
120		case "even": $arguments = "2n"; break;
121		case "odd": $arguments = "2n+1";
122	}
123
124	var $$children = childElements($element.parentNode);
125	function _checkIndex($index) {
126		var $index = ($traverse == nextElementSibling) ? $$children.length - $index : $index - 1;
127		return $$children[$index] == $element;
128	};
129
130	//	it was just a number (no "n")
131	if (!isNaN($arguments)) return _checkIndex($arguments);
132
133	$arguments = $arguments.split("n");
134	var $multiplier = parseInt($arguments[0]);
135	var $step = parseInt($arguments[1]);
136
137	if ((isNaN($multiplier) || $multiplier == 1) && $step == 0) return true;
138	if ($multiplier == 0 && !isNaN($step)) return _checkIndex($step);
139	if (isNaN($step)) $step = 0;
140
141	var $count = 1;
142	while ($element = $traverse($element)) $count++;
143
144	if (isNaN($multiplier) || $multiplier == 1)
145		return ($traverse == nextElementSibling) ? ($count <= $step) : ($step >= $count);
146
147	return ($count % $multiplier) == $step;
148};
149
150}); // addModule