Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /*** |
michael@0 | 2 | |
michael@0 | 3 | MochiKit.Selector 1.4.2 |
michael@0 | 4 | |
michael@0 | 5 | See <http://mochikit.com/> for documentation, downloads, license, etc. |
michael@0 | 6 | |
michael@0 | 7 | (c) 2005 Bob Ippolito and others. All rights Reserved. |
michael@0 | 8 | |
michael@0 | 9 | ***/ |
michael@0 | 10 | |
michael@0 | 11 | MochiKit.Base._deps('Selector', ['Base', 'DOM', 'Iter']); |
michael@0 | 12 | |
michael@0 | 13 | MochiKit.Selector.NAME = "MochiKit.Selector"; |
michael@0 | 14 | MochiKit.Selector.VERSION = "1.4.2"; |
michael@0 | 15 | |
michael@0 | 16 | MochiKit.Selector.__repr__ = function () { |
michael@0 | 17 | return "[" + this.NAME + " " + this.VERSION + "]"; |
michael@0 | 18 | }; |
michael@0 | 19 | |
michael@0 | 20 | MochiKit.Selector.toString = function () { |
michael@0 | 21 | return this.__repr__(); |
michael@0 | 22 | }; |
michael@0 | 23 | |
michael@0 | 24 | MochiKit.Selector.EXPORT = [ |
michael@0 | 25 | "Selector", |
michael@0 | 26 | "findChildElements", |
michael@0 | 27 | "findDocElements", |
michael@0 | 28 | "$$" |
michael@0 | 29 | ]; |
michael@0 | 30 | |
michael@0 | 31 | MochiKit.Selector.EXPORT_OK = [ |
michael@0 | 32 | ]; |
michael@0 | 33 | |
michael@0 | 34 | MochiKit.Selector.Selector = function (expression) { |
michael@0 | 35 | this.params = {classNames: [], pseudoClassNames: []}; |
michael@0 | 36 | this.expression = expression.toString().replace(/(^\s+|\s+$)/g, ''); |
michael@0 | 37 | this.parseExpression(); |
michael@0 | 38 | this.compileMatcher(); |
michael@0 | 39 | }; |
michael@0 | 40 | |
michael@0 | 41 | MochiKit.Selector.Selector.prototype = { |
michael@0 | 42 | /*** |
michael@0 | 43 | |
michael@0 | 44 | Selector class: convenient object to make CSS selections. |
michael@0 | 45 | |
michael@0 | 46 | ***/ |
michael@0 | 47 | __class__: MochiKit.Selector.Selector, |
michael@0 | 48 | |
michael@0 | 49 | /** @id MochiKit.Selector.Selector.prototype.parseExpression */ |
michael@0 | 50 | parseExpression: function () { |
michael@0 | 51 | function abort(message) { |
michael@0 | 52 | throw 'Parse error in selector: ' + message; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | if (this.expression == '') { |
michael@0 | 56 | abort('empty expression'); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | var repr = MochiKit.Base.repr; |
michael@0 | 60 | var params = this.params; |
michael@0 | 61 | var expr = this.expression; |
michael@0 | 62 | var match, modifier, clause, rest; |
michael@0 | 63 | while (match = expr.match(/^(.*)\[([a-z0-9_:-]+?)(?:([~\|!^$*]?=)(?:"([^"]*)"|([^\]\s]*)))?\]$/i)) { |
michael@0 | 64 | params.attributes = params.attributes || []; |
michael@0 | 65 | params.attributes.push({name: match[2], operator: match[3], value: match[4] || match[5] || ''}); |
michael@0 | 66 | expr = match[1]; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | if (expr == '*') { |
michael@0 | 70 | return this.params.wildcard = true; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | while (match = expr.match(/^([^a-z0-9_-])?([a-z0-9_-]+(?:\([^)]*\))?)(.*)/i)) { |
michael@0 | 74 | modifier = match[1]; |
michael@0 | 75 | clause = match[2]; |
michael@0 | 76 | rest = match[3]; |
michael@0 | 77 | switch (modifier) { |
michael@0 | 78 | case '#': |
michael@0 | 79 | params.id = clause; |
michael@0 | 80 | break; |
michael@0 | 81 | case '.': |
michael@0 | 82 | params.classNames.push(clause); |
michael@0 | 83 | break; |
michael@0 | 84 | case ':': |
michael@0 | 85 | params.pseudoClassNames.push(clause); |
michael@0 | 86 | break; |
michael@0 | 87 | case '': |
michael@0 | 88 | case undefined: |
michael@0 | 89 | params.tagName = clause.toUpperCase(); |
michael@0 | 90 | break; |
michael@0 | 91 | default: |
michael@0 | 92 | abort(repr(expr)); |
michael@0 | 93 | } |
michael@0 | 94 | expr = rest; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | if (expr.length > 0) { |
michael@0 | 98 | abort(repr(expr)); |
michael@0 | 99 | } |
michael@0 | 100 | }, |
michael@0 | 101 | |
michael@0 | 102 | /** @id MochiKit.Selector.Selector.prototype.buildMatchExpression */ |
michael@0 | 103 | buildMatchExpression: function () { |
michael@0 | 104 | var repr = MochiKit.Base.repr; |
michael@0 | 105 | var params = this.params; |
michael@0 | 106 | var conditions = []; |
michael@0 | 107 | var clause, i; |
michael@0 | 108 | |
michael@0 | 109 | function childElements(element) { |
michael@0 | 110 | return "MochiKit.Base.filter(function (node) { return node.nodeType == 1; }, " + element + ".childNodes)"; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | if (params.wildcard) { |
michael@0 | 114 | conditions.push('true'); |
michael@0 | 115 | } |
michael@0 | 116 | if (clause = params.id) { |
michael@0 | 117 | conditions.push('element.id == ' + repr(clause)); |
michael@0 | 118 | } |
michael@0 | 119 | if (clause = params.tagName) { |
michael@0 | 120 | conditions.push('element.tagName.toUpperCase() == ' + repr(clause)); |
michael@0 | 121 | } |
michael@0 | 122 | if ((clause = params.classNames).length > 0) { |
michael@0 | 123 | for (i = 0; i < clause.length; i++) { |
michael@0 | 124 | conditions.push('MochiKit.DOM.hasElementClass(element, ' + repr(clause[i]) + ')'); |
michael@0 | 125 | } |
michael@0 | 126 | } |
michael@0 | 127 | if ((clause = params.pseudoClassNames).length > 0) { |
michael@0 | 128 | for (i = 0; i < clause.length; i++) { |
michael@0 | 129 | var match = clause[i].match(/^([^(]+)(?:\((.*)\))?$/); |
michael@0 | 130 | var pseudoClass = match[1]; |
michael@0 | 131 | var pseudoClassArgument = match[2]; |
michael@0 | 132 | switch (pseudoClass) { |
michael@0 | 133 | case 'root': |
michael@0 | 134 | conditions.push('element.nodeType == 9 || element === element.ownerDocument.documentElement'); break; |
michael@0 | 135 | case 'nth-child': |
michael@0 | 136 | case 'nth-last-child': |
michael@0 | 137 | case 'nth-of-type': |
michael@0 | 138 | case 'nth-last-of-type': |
michael@0 | 139 | match = pseudoClassArgument.match(/^((?:(\d+)n\+)?(\d+)|odd|even)$/); |
michael@0 | 140 | if (!match) { |
michael@0 | 141 | throw "Invalid argument to pseudo element nth-child: " + pseudoClassArgument; |
michael@0 | 142 | } |
michael@0 | 143 | var a, b; |
michael@0 | 144 | if (match[0] == 'odd') { |
michael@0 | 145 | a = 2; |
michael@0 | 146 | b = 1; |
michael@0 | 147 | } else if (match[0] == 'even') { |
michael@0 | 148 | a = 2; |
michael@0 | 149 | b = 0; |
michael@0 | 150 | } else { |
michael@0 | 151 | a = match[2] && parseInt(match) || null; |
michael@0 | 152 | b = parseInt(match[3]); |
michael@0 | 153 | } |
michael@0 | 154 | conditions.push('this.nthChild(element,' + a + ',' + b |
michael@0 | 155 | + ',' + !!pseudoClass.match('^nth-last') // Reverse |
michael@0 | 156 | + ',' + !!pseudoClass.match('of-type$') // Restrict to same tagName |
michael@0 | 157 | + ')'); |
michael@0 | 158 | break; |
michael@0 | 159 | case 'first-child': |
michael@0 | 160 | conditions.push('this.nthChild(element, null, 1)'); |
michael@0 | 161 | break; |
michael@0 | 162 | case 'last-child': |
michael@0 | 163 | conditions.push('this.nthChild(element, null, 1, true)'); |
michael@0 | 164 | break; |
michael@0 | 165 | case 'first-of-type': |
michael@0 | 166 | conditions.push('this.nthChild(element, null, 1, false, true)'); |
michael@0 | 167 | break; |
michael@0 | 168 | case 'last-of-type': |
michael@0 | 169 | conditions.push('this.nthChild(element, null, 1, true, true)'); |
michael@0 | 170 | break; |
michael@0 | 171 | case 'only-child': |
michael@0 | 172 | conditions.push(childElements('element.parentNode') + '.length == 1'); |
michael@0 | 173 | break; |
michael@0 | 174 | case 'only-of-type': |
michael@0 | 175 | conditions.push('MochiKit.Base.filter(function (node) { return node.tagName == element.tagName; }, ' + childElements('element.parentNode') + ').length == 1'); |
michael@0 | 176 | break; |
michael@0 | 177 | case 'empty': |
michael@0 | 178 | conditions.push('element.childNodes.length == 0'); |
michael@0 | 179 | break; |
michael@0 | 180 | case 'enabled': |
michael@0 | 181 | conditions.push('(this.isUIElement(element) && element.disabled === false)'); |
michael@0 | 182 | break; |
michael@0 | 183 | case 'disabled': |
michael@0 | 184 | conditions.push('(this.isUIElement(element) && element.disabled === true)'); |
michael@0 | 185 | break; |
michael@0 | 186 | case 'checked': |
michael@0 | 187 | conditions.push('(this.isUIElement(element) && element.checked === true)'); |
michael@0 | 188 | break; |
michael@0 | 189 | case 'not': |
michael@0 | 190 | var subselector = new MochiKit.Selector.Selector(pseudoClassArgument); |
michael@0 | 191 | conditions.push('!( ' + subselector.buildMatchExpression() + ')') |
michael@0 | 192 | break; |
michael@0 | 193 | } |
michael@0 | 194 | } |
michael@0 | 195 | } |
michael@0 | 196 | if (clause = params.attributes) { |
michael@0 | 197 | MochiKit.Base.map(function (attribute) { |
michael@0 | 198 | var value = 'MochiKit.DOM.getNodeAttribute(element, ' + repr(attribute.name) + ')'; |
michael@0 | 199 | var splitValueBy = function (delimiter) { |
michael@0 | 200 | return value + '.split(' + repr(delimiter) + ')'; |
michael@0 | 201 | } |
michael@0 | 202 | conditions.push(value + ' != null'); |
michael@0 | 203 | switch (attribute.operator) { |
michael@0 | 204 | case '=': |
michael@0 | 205 | conditions.push(value + ' == ' + repr(attribute.value)); |
michael@0 | 206 | break; |
michael@0 | 207 | case '~=': |
michael@0 | 208 | conditions.push('MochiKit.Base.findValue(' + splitValueBy(' ') + ', ' + repr(attribute.value) + ') > -1'); |
michael@0 | 209 | break; |
michael@0 | 210 | case '^=': |
michael@0 | 211 | conditions.push(value + '.substring(0, ' + attribute.value.length + ') == ' + repr(attribute.value)); |
michael@0 | 212 | break; |
michael@0 | 213 | case '$=': |
michael@0 | 214 | conditions.push(value + '.substring(' + value + '.length - ' + attribute.value.length + ') == ' + repr(attribute.value)); |
michael@0 | 215 | break; |
michael@0 | 216 | case '*=': |
michael@0 | 217 | conditions.push(value + '.match(' + repr(attribute.value) + ')'); |
michael@0 | 218 | break; |
michael@0 | 219 | case '|=': |
michael@0 | 220 | conditions.push(splitValueBy('-') + '[0].toUpperCase() == ' + repr(attribute.value.toUpperCase())); |
michael@0 | 221 | break; |
michael@0 | 222 | case '!=': |
michael@0 | 223 | conditions.push(value + ' != ' + repr(attribute.value)); |
michael@0 | 224 | break; |
michael@0 | 225 | case '': |
michael@0 | 226 | case undefined: |
michael@0 | 227 | // Condition already added above |
michael@0 | 228 | break; |
michael@0 | 229 | default: |
michael@0 | 230 | throw 'Unknown operator ' + attribute.operator + ' in selector'; |
michael@0 | 231 | } |
michael@0 | 232 | }, clause); |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | return conditions.join(' && '); |
michael@0 | 236 | }, |
michael@0 | 237 | |
michael@0 | 238 | /** @id MochiKit.Selector.Selector.prototype.compileMatcher */ |
michael@0 | 239 | compileMatcher: function () { |
michael@0 | 240 | var code = 'return (!element.tagName) ? false : ' + |
michael@0 | 241 | this.buildMatchExpression() + ';'; |
michael@0 | 242 | this.match = new Function('element', code); |
michael@0 | 243 | }, |
michael@0 | 244 | |
michael@0 | 245 | /** @id MochiKit.Selector.Selector.prototype.nthChild */ |
michael@0 | 246 | nthChild: function (element, a, b, reverse, sametag){ |
michael@0 | 247 | var siblings = MochiKit.Base.filter(function (node) { |
michael@0 | 248 | return node.nodeType == 1; |
michael@0 | 249 | }, element.parentNode.childNodes); |
michael@0 | 250 | if (sametag) { |
michael@0 | 251 | siblings = MochiKit.Base.filter(function (node) { |
michael@0 | 252 | return node.tagName == element.tagName; |
michael@0 | 253 | }, siblings); |
michael@0 | 254 | } |
michael@0 | 255 | if (reverse) { |
michael@0 | 256 | siblings = MochiKit.Iter.reversed(siblings); |
michael@0 | 257 | } |
michael@0 | 258 | if (a) { |
michael@0 | 259 | var actualIndex = MochiKit.Base.findIdentical(siblings, element); |
michael@0 | 260 | return ((actualIndex + 1 - b) / a) % 1 == 0; |
michael@0 | 261 | } else { |
michael@0 | 262 | return b == MochiKit.Base.findIdentical(siblings, element) + 1; |
michael@0 | 263 | } |
michael@0 | 264 | }, |
michael@0 | 265 | |
michael@0 | 266 | /** @id MochiKit.Selector.Selector.prototype.isUIElement */ |
michael@0 | 267 | isUIElement: function (element) { |
michael@0 | 268 | return MochiKit.Base.findValue(['input', 'button', 'select', 'option', 'textarea', 'object'], |
michael@0 | 269 | element.tagName.toLowerCase()) > -1; |
michael@0 | 270 | }, |
michael@0 | 271 | |
michael@0 | 272 | /** @id MochiKit.Selector.Selector.prototype.findElements */ |
michael@0 | 273 | findElements: function (scope, axis) { |
michael@0 | 274 | var element; |
michael@0 | 275 | |
michael@0 | 276 | if (axis == undefined) { |
michael@0 | 277 | axis = ""; |
michael@0 | 278 | } |
michael@0 | 279 | |
michael@0 | 280 | function inScope(element, scope) { |
michael@0 | 281 | if (axis == "") { |
michael@0 | 282 | return MochiKit.DOM.isChildNode(element, scope); |
michael@0 | 283 | } else if (axis == ">") { |
michael@0 | 284 | return element.parentNode === scope; |
michael@0 | 285 | } else if (axis == "+") { |
michael@0 | 286 | return element === nextSiblingElement(scope); |
michael@0 | 287 | } else if (axis == "~") { |
michael@0 | 288 | var sibling = scope; |
michael@0 | 289 | while (sibling = nextSiblingElement(sibling)) { |
michael@0 | 290 | if (element === sibling) { |
michael@0 | 291 | return true; |
michael@0 | 292 | } |
michael@0 | 293 | } |
michael@0 | 294 | return false; |
michael@0 | 295 | } else { |
michael@0 | 296 | throw "Invalid axis: " + axis; |
michael@0 | 297 | } |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | if (element = MochiKit.DOM.getElement(this.params.id)) { |
michael@0 | 301 | if (this.match(element)) { |
michael@0 | 302 | if (!scope || inScope(element, scope)) { |
michael@0 | 303 | return [element]; |
michael@0 | 304 | } |
michael@0 | 305 | } |
michael@0 | 306 | } |
michael@0 | 307 | |
michael@0 | 308 | function nextSiblingElement(node) { |
michael@0 | 309 | node = node.nextSibling; |
michael@0 | 310 | while (node && node.nodeType != 1) { |
michael@0 | 311 | node = node.nextSibling; |
michael@0 | 312 | } |
michael@0 | 313 | return node; |
michael@0 | 314 | } |
michael@0 | 315 | |
michael@0 | 316 | if (axis == "") { |
michael@0 | 317 | scope = (scope || MochiKit.DOM.currentDocument()).getElementsByTagName(this.params.tagName || '*'); |
michael@0 | 318 | } else if (axis == ">") { |
michael@0 | 319 | if (!scope) { |
michael@0 | 320 | throw "> combinator not allowed without preceding expression"; |
michael@0 | 321 | } |
michael@0 | 322 | scope = MochiKit.Base.filter(function (node) { |
michael@0 | 323 | return node.nodeType == 1; |
michael@0 | 324 | }, scope.childNodes); |
michael@0 | 325 | } else if (axis == "+") { |
michael@0 | 326 | if (!scope) { |
michael@0 | 327 | throw "+ combinator not allowed without preceding expression"; |
michael@0 | 328 | } |
michael@0 | 329 | scope = nextSiblingElement(scope) && [nextSiblingElement(scope)]; |
michael@0 | 330 | } else if (axis == "~") { |
michael@0 | 331 | if (!scope) { |
michael@0 | 332 | throw "~ combinator not allowed without preceding expression"; |
michael@0 | 333 | } |
michael@0 | 334 | var newscope = []; |
michael@0 | 335 | while (nextSiblingElement(scope)) { |
michael@0 | 336 | scope = nextSiblingElement(scope); |
michael@0 | 337 | newscope.push(scope); |
michael@0 | 338 | } |
michael@0 | 339 | scope = newscope; |
michael@0 | 340 | } |
michael@0 | 341 | |
michael@0 | 342 | if (!scope) { |
michael@0 | 343 | return []; |
michael@0 | 344 | } |
michael@0 | 345 | |
michael@0 | 346 | var results = MochiKit.Base.filter(MochiKit.Base.bind(function (scopeElt) { |
michael@0 | 347 | return this.match(scopeElt); |
michael@0 | 348 | }, this), scope); |
michael@0 | 349 | |
michael@0 | 350 | return results; |
michael@0 | 351 | }, |
michael@0 | 352 | |
michael@0 | 353 | /** @id MochiKit.Selector.Selector.prototype.repr */ |
michael@0 | 354 | repr: function () { |
michael@0 | 355 | return 'Selector(' + this.expression + ')'; |
michael@0 | 356 | }, |
michael@0 | 357 | |
michael@0 | 358 | toString: MochiKit.Base.forwardCall("repr") |
michael@0 | 359 | }; |
michael@0 | 360 | |
michael@0 | 361 | MochiKit.Base.update(MochiKit.Selector, { |
michael@0 | 362 | |
michael@0 | 363 | /** @id MochiKit.Selector.findChildElements */ |
michael@0 | 364 | findChildElements: function (element, expressions) { |
michael@0 | 365 | var uniq = function(arr) { |
michael@0 | 366 | var res = []; |
michael@0 | 367 | for (var i = 0; i < arr.length; i++) { |
michael@0 | 368 | if (MochiKit.Base.findIdentical(res, arr[i]) < 0) { |
michael@0 | 369 | res.push(arr[i]); |
michael@0 | 370 | } |
michael@0 | 371 | } |
michael@0 | 372 | return res; |
michael@0 | 373 | }; |
michael@0 | 374 | return MochiKit.Base.flattenArray(MochiKit.Base.map(function (expression) { |
michael@0 | 375 | var nextScope = ""; |
michael@0 | 376 | var reducer = function (results, expr) { |
michael@0 | 377 | if (match = expr.match(/^[>+~]$/)) { |
michael@0 | 378 | nextScope = match[0]; |
michael@0 | 379 | return results; |
michael@0 | 380 | } else { |
michael@0 | 381 | var selector = new MochiKit.Selector.Selector(expr); |
michael@0 | 382 | var elements = MochiKit.Iter.reduce(function (elements, result) { |
michael@0 | 383 | return MochiKit.Base.extend(elements, selector.findElements(result || element, nextScope)); |
michael@0 | 384 | }, results, []); |
michael@0 | 385 | nextScope = ""; |
michael@0 | 386 | return elements; |
michael@0 | 387 | } |
michael@0 | 388 | }; |
michael@0 | 389 | var exprs = expression.replace(/(^\s+|\s+$)/g, '').split(/\s+/); |
michael@0 | 390 | return uniq(MochiKit.Iter.reduce(reducer, exprs, [null])); |
michael@0 | 391 | }, expressions)); |
michael@0 | 392 | }, |
michael@0 | 393 | |
michael@0 | 394 | findDocElements: function () { |
michael@0 | 395 | return MochiKit.Selector.findChildElements(MochiKit.DOM.currentDocument(), arguments); |
michael@0 | 396 | }, |
michael@0 | 397 | |
michael@0 | 398 | __new__: function () { |
michael@0 | 399 | var m = MochiKit.Base; |
michael@0 | 400 | |
michael@0 | 401 | this.$$ = this.findDocElements; |
michael@0 | 402 | |
michael@0 | 403 | this.EXPORT_TAGS = { |
michael@0 | 404 | ":common": this.EXPORT, |
michael@0 | 405 | ":all": m.concat(this.EXPORT, this.EXPORT_OK) |
michael@0 | 406 | }; |
michael@0 | 407 | |
michael@0 | 408 | m.nameFunctions(this); |
michael@0 | 409 | } |
michael@0 | 410 | }); |
michael@0 | 411 | |
michael@0 | 412 | MochiKit.Selector.__new__(); |
michael@0 | 413 | |
michael@0 | 414 | MochiKit.Base._exportSymbols(this, MochiKit.Selector); |
michael@0 | 415 |