Changeset 1157
- Timestamp:
- 10/09/06 07:31:57 (2 years ago)
- Files:
-
- mochikit/branches/selector/MochiKit/Selector.js (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
mochikit/branches/selector/MochiKit/Selector.js
r1156 r1157 1 1 if (typeof(dojo) != 'undefined') { 2 2 dojo.provide('MochiKit.Selector'); 3 } 4 5 if (typeof(MochiKit) == 'undefined') { 6 MochiKit = {}; 3 dojo.require('MochiKit.Base'); 4 dojo.require('MochiKit.DOM'); 5 dojo.require('MochiKit.Iter'); 6 } 7 8 if (typeof(JSAN) != 'undefined') { 9 JSAN.use("MochiKit.Base", []); 10 JSAN.use("MochiKit.DOM", []); 11 JSAN.use("MochiKit.Iter", []); 12 } 13 14 try { 15 if (typeof(MochiKit.Base) === 'undefined' || 16 typeof(MochiKit.DOM) === 'undefined' || 17 typeof(MochiKit.Iter) === 'undefined') { 18 throw ""; 19 } 20 } catch (e) { 21 throw "MochiKit.Selector depends on MochiKit.Base, MochiKit.DOM and MochiKit.Iter!"; 7 22 } 8 23 … … 40 55 } 41 56 42 var params = this.params, expr = this.expression, match, modifier, clause, rest; 57 var params = this.params; 58 var expr = this.expression; 59 var match, modifier, clause, rest; 43 60 while (match = expr.match(/^(.*)\[([a-z0-9_:-]+?)(?:([~\|!^$*]?=)(?:"([^"]*)"|([^\]\s]*)))?\]$/i)) { 44 61 params.attributes = params.attributes || []; … … 52 69 53 70 while (match = expr.match(/^([^a-z0-9_-])?([a-z0-9_-]+(?:\([^)]*\))?)(.*)/i)) { 54 modifier = match[1], clause = match[2], rest = match[3]; 71 modifier = match[1]; 72 clause = match[2]; 73 rest = match[3]; 55 74 switch (modifier) { 56 case '#': 75 case '#': 57 76 params.id = clause; 58 77 break; 59 case '.': 78 case '.': 60 79 params.classNames.push(clause); 61 80 break; … … 84 103 85 104 function childElements(element) { 86 return " filter(function (node){ return node.nodeType == 1; }, " + element + ".childNodes)";105 return "MochiKit.Base.filter(function (node) { return node.nodeType == 1; }, " + element + ".childNodes)"; 87 106 } 88 107 … … 98 117 if ((clause = params.classNames).length > 0) { 99 118 for (var i = 0; i < clause.length; i++) { 100 conditions.push(' hasElementClass(element, ' + repr(clause[i]) + ')');119 conditions.push('MochiKit.DOM.hasElementClass(element, ' + repr(clause[i]) + ')'); 101 120 } 102 121 } … … 149 168 break; 150 169 case 'only-of-type': 151 conditions.push(' filter(function (node) { return node.tagName == element.tagName; }, ' + childElements('element.parentNode') + ').length == 1');170 conditions.push('MochiKit.Base.filter(function (node) { return node.tagName == element.tagName; }, ' + childElements('element.parentNode') + ').length == 1'); 152 171 break; 153 172 case 'empty': … … 171 190 } 172 191 if (clause = params.attributes) { 173 map(function (attribute) {174 var value = ' getNodeAttribute(element, ' + repr(attribute.name) + ')';192 MochiKit.Base.map(function (attribute) { 193 var value = 'MochiKit.DOM.getNodeAttribute(element, ' + repr(attribute.name) + ')'; 175 194 var splitValueBy = function (delimiter) { 176 195 return value + ' && ' + value + '.split(' + repr(delimiter) + ')'; … … 182 201 break; 183 202 case '~=': 184 conditions.push(' findValue(' + splitValueBy(' ') + ', ' + repr(attribute.value) + ') > -1');203 conditions.push('MochiKit.Base.findValue(' + splitValueBy(' ') + ', ' + repr(attribute.value) + ') > -1'); 185 204 break; 186 205 case '^=': … … 220 239 221 240 nthChild: function (element, a, b, reverse, sametag){ 222 var siblings = filter(function (node) {241 var siblings = MochiKit.Base.filter(function (node) { 223 242 return node.nodeType == 1; 224 }, element.parentNode.childNodes);243 }, element.parentNode.childNodes); 225 244 if (sametag) { 226 siblings = filter(function (node) {245 siblings = MochiKit.Base.filter(function (node) { 227 246 return node.tagName == element.tagName; 228 247 }, siblings); … … 255 274 } else if (axis == "~") { 256 275 while (element.previousSibling) { 257 if (element.previousSibling == scope) return true; 276 if (element.previousSibling == scope) { 277 return true; 278 } 258 279 element = element.previousSibling; 259 280 } … … 286 307 throw "> combinator not allowed without preceeding expression"; 287 308 } 288 scope = filter(function (node) {309 scope = MochiKit.Base.filter(function (node) { 289 310 return node.nodeType == 1; 290 311 }, scope.childNodes); … … 298 319 throw "~ combinator not allowed without preceeding expression"; 299 320 } 300 var newscope = new Array();321 var newscope = []; 301 322 while (nextSiblingElement(scope)) { 302 323 scope = nextSiblingElement(scope); … … 329 350 330 351 findChildElements: function (element, expressions) { 331 return flattenArray(map(function (expression) {352 return MochiKit.Base.flattenArray(MochiKit.Base.map(function (expression) { 332 353 var nextScope = ""; 333 return reduce(function (results, expr) {354 return MochiKit.Iter.reduce(function (results, expr) { 334 355 if (match = expr.match(/^[>+~]$/)) { 335 356 nextScope = match[0]; … … 337 358 } else { 338 359 var selector = new MochiKit.Selector.Selector(expr); 339 var elements = reduce(function (elements, result) {340 return extend(elements, selector.findElements(result || element, nextScope));360 var elements = MochiKit.Iter.reduce(function (elements, result) { 361 return MochiKit.Base.extend(elements, selector.findElements(result || element, nextScope)); 341 362 }, results, []); 342 363 nextScope = "";
