1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/Selector.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,431 @@ 1.4 +/*** 1.5 + 1.6 +MochiKit.Selector 1.4 1.7 + 1.8 +See <http://mochikit.com/> for documentation, downloads, license, etc. 1.9 + 1.10 +(c) 2005 Bob Ippolito and others. All rights Reserved. 1.11 + 1.12 +***/ 1.13 + 1.14 +if (typeof(dojo) != 'undefined') { 1.15 + dojo.provide('MochiKit.Selector'); 1.16 + dojo.require('MochiKit.Base'); 1.17 + dojo.require('MochiKit.DOM'); 1.18 + dojo.require('MochiKit.Iter'); 1.19 +} 1.20 + 1.21 +if (typeof(JSAN) != 'undefined') { 1.22 + JSAN.use("MochiKit.Base", []); 1.23 + JSAN.use("MochiKit.DOM", []); 1.24 + JSAN.use("MochiKit.Iter", []); 1.25 +} 1.26 + 1.27 +try { 1.28 + if (typeof(MochiKit.Base) === 'undefined' || 1.29 + typeof(MochiKit.DOM) === 'undefined' || 1.30 + typeof(MochiKit.Iter) === 'undefined') { 1.31 + throw ""; 1.32 + } 1.33 +} catch (e) { 1.34 + throw "MochiKit.Selector depends on MochiKit.Base, MochiKit.DOM and MochiKit.Iter!"; 1.35 +} 1.36 + 1.37 +if (typeof(MochiKit.Selector) == 'undefined') { 1.38 + MochiKit.Selector = {}; 1.39 +} 1.40 + 1.41 +MochiKit.Selector.NAME = "MochiKit.Selector"; 1.42 + 1.43 +MochiKit.Selector.VERSION = "1.4"; 1.44 + 1.45 +MochiKit.Selector.__repr__ = function () { 1.46 + return "[" + this.NAME + " " + this.VERSION + "]"; 1.47 +}; 1.48 + 1.49 +MochiKit.Selector.toString = function () { 1.50 + return this.__repr__(); 1.51 +}; 1.52 + 1.53 +MochiKit.Selector.EXPORT = [ 1.54 + "Selector", 1.55 + "findChildElements", 1.56 + "findDocElements", 1.57 + "$$" 1.58 +]; 1.59 + 1.60 +MochiKit.Selector.EXPORT_OK = [ 1.61 +]; 1.62 + 1.63 +MochiKit.Selector.Selector = function (expression) { 1.64 + this.params = {classNames: [], pseudoClassNames: []}; 1.65 + this.expression = expression.toString().replace(/(^\s+|\s+$)/g, ''); 1.66 + this.parseExpression(); 1.67 + this.compileMatcher(); 1.68 +}; 1.69 + 1.70 +MochiKit.Selector.Selector.prototype = { 1.71 + /*** 1.72 + 1.73 + Selector class: convenient object to make CSS selections. 1.74 + 1.75 + ***/ 1.76 + __class__: MochiKit.Selector.Selector, 1.77 + 1.78 + /** @id MochiKit.Selector.Selector.prototype.parseExpression */ 1.79 + parseExpression: function () { 1.80 + function abort(message) { 1.81 + throw 'Parse error in selector: ' + message; 1.82 + } 1.83 + 1.84 + if (this.expression == '') { 1.85 + abort('empty expression'); 1.86 + } 1.87 + 1.88 + var repr = MochiKit.Base.repr; 1.89 + var params = this.params; 1.90 + var expr = this.expression; 1.91 + var match, modifier, clause, rest; 1.92 + while (match = expr.match(/^(.*)\[([a-z0-9_:-]+?)(?:([~\|!^$*]?=)(?:"([^"]*)"|([^\]\s]*)))?\]$/i)) { 1.93 + params.attributes = params.attributes || []; 1.94 + params.attributes.push({name: match[2], operator: match[3], value: match[4] || match[5] || ''}); 1.95 + expr = match[1]; 1.96 + } 1.97 + 1.98 + if (expr == '*') { 1.99 + return this.params.wildcard = true; 1.100 + } 1.101 + 1.102 + while (match = expr.match(/^([^a-z0-9_-])?([a-z0-9_-]+(?:\([^)]*\))?)(.*)/i)) { 1.103 + modifier = match[1]; 1.104 + clause = match[2]; 1.105 + rest = match[3]; 1.106 + switch (modifier) { 1.107 + case '#': 1.108 + params.id = clause; 1.109 + break; 1.110 + case '.': 1.111 + params.classNames.push(clause); 1.112 + break; 1.113 + case ':': 1.114 + params.pseudoClassNames.push(clause); 1.115 + break; 1.116 + case '': 1.117 + case undefined: 1.118 + params.tagName = clause.toUpperCase(); 1.119 + break; 1.120 + default: 1.121 + abort(repr(expr)); 1.122 + } 1.123 + expr = rest; 1.124 + } 1.125 + 1.126 + if (expr.length > 0) { 1.127 + abort(repr(expr)); 1.128 + } 1.129 + }, 1.130 + 1.131 + /** @id MochiKit.Selector.Selector.prototype.buildMatchExpression */ 1.132 + buildMatchExpression: function () { 1.133 + var repr = MochiKit.Base.repr; 1.134 + var params = this.params; 1.135 + var conditions = []; 1.136 + var clause, i; 1.137 + 1.138 + function childElements(element) { 1.139 + return "MochiKit.Base.filter(function (node) { return node.nodeType == 1; }, " + element + ".childNodes)"; 1.140 + } 1.141 + 1.142 + if (params.wildcard) { 1.143 + conditions.push('true'); 1.144 + } 1.145 + if (clause = params.id) { 1.146 + conditions.push('element.id == ' + repr(clause)); 1.147 + } 1.148 + if (clause = params.tagName) { 1.149 + conditions.push('element.tagName.toUpperCase() == ' + repr(clause)); 1.150 + } 1.151 + if ((clause = params.classNames).length > 0) { 1.152 + for (i = 0; i < clause.length; i++) { 1.153 + conditions.push('MochiKit.DOM.hasElementClass(element, ' + repr(clause[i]) + ')'); 1.154 + } 1.155 + } 1.156 + if ((clause = params.pseudoClassNames).length > 0) { 1.157 + for (i = 0; i < clause.length; i++) { 1.158 + var match = clause[i].match(/^([^(]+)(?:\((.*)\))?$/); 1.159 + var pseudoClass = match[1]; 1.160 + var pseudoClassArgument = match[2]; 1.161 + switch (pseudoClass) { 1.162 + case 'root': 1.163 + conditions.push('element.nodeType == 9 || element === element.ownerDocument.documentElement'); break; 1.164 + case 'nth-child': 1.165 + case 'nth-last-child': 1.166 + case 'nth-of-type': 1.167 + case 'nth-last-of-type': 1.168 + match = pseudoClassArgument.match(/^((?:(\d+)n\+)?(\d+)|odd|even)$/); 1.169 + if (!match) { 1.170 + throw "Invalid argument to pseudo element nth-child: " + pseudoClassArgument; 1.171 + } 1.172 + var a, b; 1.173 + if (match[0] == 'odd') { 1.174 + a = 2; 1.175 + b = 1; 1.176 + } else if (match[0] == 'even') { 1.177 + a = 2; 1.178 + b = 0; 1.179 + } else { 1.180 + a = match[2] && parseInt(match) || null; 1.181 + b = parseInt(match[3]); 1.182 + } 1.183 + conditions.push('this.nthChild(element,' + a + ',' + b 1.184 + + ',' + !!pseudoClass.match('^nth-last') // Reverse 1.185 + + ',' + !!pseudoClass.match('of-type$') // Restrict to same tagName 1.186 + + ')'); 1.187 + break; 1.188 + case 'first-child': 1.189 + conditions.push('this.nthChild(element, null, 1)'); 1.190 + break; 1.191 + case 'last-child': 1.192 + conditions.push('this.nthChild(element, null, 1, true)'); 1.193 + break; 1.194 + case 'first-of-type': 1.195 + conditions.push('this.nthChild(element, null, 1, false, true)'); 1.196 + break; 1.197 + case 'last-of-type': 1.198 + conditions.push('this.nthChild(element, null, 1, true, true)'); 1.199 + break; 1.200 + case 'only-child': 1.201 + conditions.push(childElements('element.parentNode') + '.length == 1'); 1.202 + break; 1.203 + case 'only-of-type': 1.204 + conditions.push('MochiKit.Base.filter(function (node) { return node.tagName == element.tagName; }, ' + childElements('element.parentNode') + ').length == 1'); 1.205 + break; 1.206 + case 'empty': 1.207 + conditions.push('element.childNodes.length == 0'); 1.208 + break; 1.209 + case 'enabled': 1.210 + conditions.push('(this.isUIElement(element) && element.disabled === false)'); 1.211 + break; 1.212 + case 'disabled': 1.213 + conditions.push('(this.isUIElement(element) && element.disabled === true)'); 1.214 + break; 1.215 + case 'checked': 1.216 + conditions.push('(this.isUIElement(element) && element.checked === true)'); 1.217 + break; 1.218 + case 'not': 1.219 + var subselector = new MochiKit.Selector.Selector(pseudoClassArgument); 1.220 + conditions.push('!( ' + subselector.buildMatchExpression() + ')') 1.221 + break; 1.222 + } 1.223 + } 1.224 + } 1.225 + if (clause = params.attributes) { 1.226 + MochiKit.Base.map(function (attribute) { 1.227 + var value = 'MochiKit.DOM.getNodeAttribute(element, ' + repr(attribute.name) + ')'; 1.228 + var splitValueBy = function (delimiter) { 1.229 + return value + '.split(' + repr(delimiter) + ')'; 1.230 + } 1.231 + 1.232 + switch (attribute.operator) { 1.233 + case '=': 1.234 + conditions.push(value + ' == ' + repr(attribute.value)); 1.235 + break; 1.236 + case '~=': 1.237 + conditions.push(value + ' && MochiKit.Base.findValue(' + splitValueBy(' ') + ', ' + repr(attribute.value) + ') > -1'); 1.238 + break; 1.239 + case '^=': 1.240 + conditions.push(value + '.substring(0, ' + attribute.value.length + ') == ' + repr(attribute.value)); 1.241 + break; 1.242 + case '$=': 1.243 + conditions.push(value + '.substring(' + value + '.length - ' + attribute.value.length + ') == ' + repr(attribute.value)); 1.244 + break; 1.245 + case '*=': 1.246 + conditions.push(value + '.match(' + repr(attribute.value) + ')'); 1.247 + break; 1.248 + case '|=': 1.249 + conditions.push( 1.250 + value + ' && ' + splitValueBy('-') + '[0].toUpperCase() == ' + repr(attribute.value.toUpperCase()) 1.251 + ); 1.252 + break; 1.253 + case '!=': 1.254 + conditions.push(value + ' != ' + repr(attribute.value)); 1.255 + break; 1.256 + case '': 1.257 + case undefined: 1.258 + conditions.push(value + ' != null'); 1.259 + break; 1.260 + default: 1.261 + throw 'Unknown operator ' + attribute.operator + ' in selector'; 1.262 + } 1.263 + }, clause); 1.264 + } 1.265 + 1.266 + return conditions.join(' && '); 1.267 + }, 1.268 + 1.269 + /** @id MochiKit.Selector.Selector.prototype.compileMatcher */ 1.270 + compileMatcher: function () { 1.271 + this.match = new Function('element', 'if (!element.tagName) return false; \ 1.272 + return ' + this.buildMatchExpression()); 1.273 + }, 1.274 + 1.275 + /** @id MochiKit.Selector.Selector.prototype.nthChild */ 1.276 + nthChild: function (element, a, b, reverse, sametag){ 1.277 + var siblings = MochiKit.Base.filter(function (node) { 1.278 + return node.nodeType == 1; 1.279 + }, element.parentNode.childNodes); 1.280 + if (sametag) { 1.281 + siblings = MochiKit.Base.filter(function (node) { 1.282 + return node.tagName == element.tagName; 1.283 + }, siblings); 1.284 + } 1.285 + if (reverse) { 1.286 + siblings = MochiKit.Iter.reversed(siblings); 1.287 + } 1.288 + if (a) { 1.289 + var actualIndex = MochiKit.Base.findIdentical(siblings, element); 1.290 + return ((actualIndex + 1 - b) / a) % 1 == 0; 1.291 + } else { 1.292 + return b == MochiKit.Base.findIdentical(siblings, element) + 1; 1.293 + } 1.294 + }, 1.295 + 1.296 + /** @id MochiKit.Selector.Selector.prototype.isUIElement */ 1.297 + isUIElement: function (element) { 1.298 + return MochiKit.Base.findValue(['input', 'button', 'select', 'option', 'textarea', 'object'], 1.299 + element.tagName.toLowerCase()) > -1; 1.300 + }, 1.301 + 1.302 + /** @id MochiKit.Selector.Selector.prototype.findElements */ 1.303 + findElements: function (scope, axis) { 1.304 + var element; 1.305 + 1.306 + if (axis == undefined) { 1.307 + axis = ""; 1.308 + } 1.309 + 1.310 + function inScope(element, scope) { 1.311 + if (axis == "") { 1.312 + return MochiKit.DOM.isChildNode(element, scope); 1.313 + } else if (axis == ">") { 1.314 + return element.parentNode == scope; 1.315 + } else if (axis == "+") { 1.316 + return element == nextSiblingElement(scope); 1.317 + } else if (axis == "~") { 1.318 + var sibling = scope; 1.319 + while (sibling = nextSiblingElement(sibling)) { 1.320 + if (element == sibling) { 1.321 + return true; 1.322 + } 1.323 + } 1.324 + return false; 1.325 + } else { 1.326 + throw "Invalid axis: " + axis; 1.327 + } 1.328 + } 1.329 + 1.330 + if (element = MochiKit.DOM.getElement(this.params.id)) { 1.331 + if (this.match(element)) { 1.332 + if (!scope || inScope(element, scope)) { 1.333 + return [element]; 1.334 + } 1.335 + } 1.336 + } 1.337 + 1.338 + function nextSiblingElement(node) { 1.339 + node = node.nextSibling; 1.340 + while (node && node.nodeType != 1) { 1.341 + node = node.nextSibling; 1.342 + } 1.343 + return node; 1.344 + } 1.345 + 1.346 + if (axis == "") { 1.347 + scope = (scope || MochiKit.DOM.currentDocument()).getElementsByTagName(this.params.tagName || '*'); 1.348 + } else if (axis == ">") { 1.349 + if (!scope) { 1.350 + throw "> combinator not allowed without preceeding expression"; 1.351 + } 1.352 + scope = MochiKit.Base.filter(function (node) { 1.353 + return node.nodeType == 1; 1.354 + }, scope.childNodes); 1.355 + } else if (axis == "+") { 1.356 + if (!scope) { 1.357 + throw "+ combinator not allowed without preceeding expression"; 1.358 + } 1.359 + scope = nextSiblingElement(scope) && [nextSiblingElement(scope)]; 1.360 + } else if (axis == "~") { 1.361 + if (!scope) { 1.362 + throw "~ combinator not allowed without preceeding expression"; 1.363 + } 1.364 + var newscope = []; 1.365 + while (nextSiblingElement(scope)) { 1.366 + scope = nextSiblingElement(scope); 1.367 + newscope.push(scope); 1.368 + } 1.369 + scope = newscope; 1.370 + } 1.371 + 1.372 + if (!scope) { 1.373 + return []; 1.374 + } 1.375 + 1.376 + var results = MochiKit.Base.filter(MochiKit.Base.bind(function (scopeElt) { 1.377 + return this.match(scopeElt); 1.378 + }, this), scope); 1.379 + 1.380 + return results; 1.381 + }, 1.382 + 1.383 + /** @id MochiKit.Selector.Selector.prototype.repr */ 1.384 + repr: function () { 1.385 + return 'Selector(' + this.expression + ')'; 1.386 + }, 1.387 + 1.388 + toString: MochiKit.Base.forwardCall("repr") 1.389 +}; 1.390 + 1.391 +MochiKit.Base.update(MochiKit.Selector, { 1.392 + 1.393 + /** @id MochiKit.Selector.findChildElements */ 1.394 + findChildElements: function (element, expressions) { 1.395 + return MochiKit.Base.flattenArray(MochiKit.Base.map(function (expression) { 1.396 + var nextScope = ""; 1.397 + return MochiKit.Iter.reduce(function (results, expr) { 1.398 + if (match = expr.match(/^[>+~]$/)) { 1.399 + nextScope = match[0]; 1.400 + return results; 1.401 + } else { 1.402 + var selector = new MochiKit.Selector.Selector(expr); 1.403 + var elements = MochiKit.Iter.reduce(function (elements, result) { 1.404 + return MochiKit.Base.extend(elements, selector.findElements(result || element, nextScope)); 1.405 + }, results, []); 1.406 + nextScope = ""; 1.407 + return elements; 1.408 + } 1.409 + }, expression.replace(/(^\s+|\s+$)/g, '').split(/\s+/), [null]); 1.410 + }, expressions)); 1.411 + }, 1.412 + 1.413 + findDocElements: function () { 1.414 + return MochiKit.Selector.findChildElements(MochiKit.DOM.currentDocument(), arguments); 1.415 + }, 1.416 + 1.417 + __new__: function () { 1.418 + var m = MochiKit.Base; 1.419 + 1.420 + this.$$ = this.findDocElements; 1.421 + 1.422 + this.EXPORT_TAGS = { 1.423 + ":common": this.EXPORT, 1.424 + ":all": m.concat(this.EXPORT, this.EXPORT_OK) 1.425 + }; 1.426 + 1.427 + m.nameFunctions(this); 1.428 + } 1.429 +}); 1.430 + 1.431 +MochiKit.Selector.__new__(); 1.432 + 1.433 +MochiKit.Base._exportSymbols(this, MochiKit.Selector); 1.434 +