Made it so that you can't change the type of an input element, having it throw an...
[jquery.git] / src / selector / selector.js
1
2 var chars = jQuery.browser.safari && parseInt(jQuery.browser.version) < 417 ?
3                 "(?:[\\w*_-]|\\\\.)" :
4                 "(?:[\\w\u0128-\uFFFF*_-]|\\\\.)",
5         quickChild = new RegExp("^>\\s*(" + chars + "+)"),
6         quickID = new RegExp("^(" + chars + "+)(#)(" + chars + "+)"),
7         quickClass = new RegExp("^([#.]?)(" + chars + "*)");
8
9 jQuery.extend({
10         expr: {
11                 "": "m[2]=='*'||jQuery.nodeName(a,m[2])",
12                 "#": "a.getAttribute('id')==m[2]",
13                 ":": {
14                         // Position Checks
15                         lt: "i<m[3]-0",
16                         gt: "i>m[3]-0",
17                         nth: "m[3]-0==i",
18                         eq: "m[3]-0==i",
19                         first: "i==0",
20                         last: "i==r.length-1",
21                         even: "i%2==0",
22                         odd: "i%2",
23
24                         // Child Checks
25                         "first-child": "a.parentNode.getElementsByTagName('*')[0]==a",
26                         "last-child": "jQuery.nth(a.parentNode.lastChild,1,'previousSibling')==a",
27                         "only-child": "!jQuery.nth(a.parentNode.lastChild,2,'previousSibling')",
28
29                         // Parent Checks
30                         parent: "a.firstChild",
31                         empty: "!a.firstChild",
32
33                         // Text Check
34                         contains: "(a.textContent||a.innerText||'').indexOf(m[3])>=0",
35
36                         // Visibility
37                         visible: '"hidden"!=a.type&&jQuery.css(a,"display")!="none"&&jQuery.css(a,"visibility")!="hidden"',
38                         hidden: '"hidden"==a.type||jQuery.css(a,"display")=="none"||jQuery.css(a,"visibility")=="hidden"',
39
40                         // Form attributes
41                         enabled: "!a.disabled",
42                         disabled: "a.disabled",
43                         checked: "a.checked",
44                         selected: "a.selected||jQuery.attr(a,'selected')",
45
46                         // Form elements
47                         text: "'text'==a.type",
48                         radio: "'radio'==a.type",
49                         checkbox: "'checkbox'==a.type",
50                         file: "'file'==a.type",
51                         password: "'password'==a.type",
52                         submit: "'submit'==a.type",
53                         image: "'image'==a.type",
54                         reset: "'reset'==a.type",
55                         button: '"button"==a.type||jQuery.nodeName(a,"button")',
56                         input: "/input|select|textarea|button/i.test(a.nodeName)",
57
58                         // :has()
59                         has: "jQuery.find(m[3],a).length",
60
61                         // :header
62                         header: "/h\\d/i.test(a.nodeName)"
63                 }
64         },
65         
66         // The regular expressions that power the parsing engine
67         parse: [
68                 // Match: [@value='test'], [@foo]
69                 /^(\[) *@?([\w-]+) *([!*$^~=]*) *('?"?)(.*?)\4 *\]/,
70
71                 // Match: :contains('foo')
72                 /^(:)([\w-]+)\("?'?(.*?(\(.*?\))?[^(]*?)"?'?\)/,
73
74                 // Match: :even, :last-chlid, #id, .class
75                 new RegExp("^([:.#]*)(" + chars + "+)")
76         ],
77
78         multiFilter: function( expr, elems, not ) {
79                 var old, cur = [];
80
81                 while ( expr && expr != old ) {
82                         old = expr;
83                         var f = jQuery.filter( expr, elems, not );
84                         expr = f.t.replace(/^\s*,\s*/, "" );
85                         cur = not ? elems = f.r : jQuery.merge( cur, f.r );
86                 }
87
88                 return cur;
89         },
90
91         find: function( t, context ) {
92                 // Quickly handle non-string expressions
93                 if ( typeof t != "string" )
94                         return [ t ];
95
96                 // Make sure that the context is a DOM Element
97                 if ( context && !context.nodeType )
98                         context = null;
99
100                 // Set the correct context (if none is provided)
101                 context = context || document;
102
103                 // Initialize the search
104                 var ret = [context], done = [], last;
105
106                 // Continue while a selector expression exists, and while
107                 // we're no longer looping upon ourselves
108                 while ( t && last != t ) {
109                         var r = [];
110                         last = t;
111
112                         t = jQuery.trim(t);
113
114                         var foundToken = false;
115
116                         // An attempt at speeding up child selectors that
117                         // point to a specific element tag
118                         var re = quickChild;
119                         var m = re.exec(t);
120
121                         if ( m ) {
122                                 var nodeName = m[1].toUpperCase();
123
124                                 // Perform our own iteration and filter
125                                 for ( var i = 0; ret[i]; i++ )
126                                         for ( var c = ret[i].firstChild; c; c = c.nextSibling )
127                                                 if ( c.nodeType == 1 && (nodeName == "*" || c.nodeName.toUpperCase() == nodeName.toUpperCase()) )
128                                                         r.push( c );
129
130                                 ret = r;
131                                 t = t.replace( re, "" );
132                                 if ( t.indexOf(" ") == 0 ) continue;
133                                 foundToken = true;
134                         } else {
135                                 re = /^([>+~])\s*(\w*)/i;
136
137                                 if ( (m = re.exec(t)) != null ) {
138                                         r = [];
139
140                                         var nodeName = m[2], mergeNum = jQuery.mergeNum++;
141                                         m = m[1];
142
143                                         for ( var j = 0, rl = ret.length; j < rl; j++ ) {
144                                                 var n = m == "~" || m == "+" ? ret[j].nextSibling : ret[j].firstChild;
145                                                 for ( ; n; n = n.nextSibling )
146                                                         if ( n.nodeType == 1 ) {
147                                                                 if ( m == "~" && n.mergeNum == mergeNum ) break;
148                                                                 
149                                                                 if (!nodeName || n.nodeName.toUpperCase() == nodeName.toUpperCase() ) {
150                                                                         if ( m == "~" ) n.mergeNum = mergeNum;
151                                                                         r.push( n );
152                                                                 }
153                                                                 
154                                                                 if ( m == "+" ) break;
155                                                         }
156                                         }
157
158                                         ret = r;
159
160                                         // And remove the token
161                                         t = jQuery.trim( t.replace( re, "" ) );
162                                         foundToken = true;
163                                 }
164                         }
165
166                         // See if there's still an expression, and that we haven't already
167                         // matched a token
168                         if ( t && !foundToken ) {
169                                 // Handle multiple expressions
170                                 if ( !t.indexOf(",") ) {
171                                         // Clean the result set
172                                         if ( context == ret[0] ) ret.shift();
173
174                                         // Merge the result sets
175                                         done = jQuery.merge( done, ret );
176
177                                         // Reset the context
178                                         r = ret = [context];
179
180                                         // Touch up the selector string
181                                         t = " " + t.substr(1,t.length);
182
183                                 } else {
184                                         // Optimize for the case nodeName#idName
185                                         var re2 = quickID;
186                                         var m = re2.exec(t);
187                                         
188                                         // Re-organize the results, so that they're consistent
189                                         if ( m ) {
190                                            m = [ 0, m[2], m[3], m[1] ];
191
192                                         } else {
193                                                 // Otherwise, do a traditional filter check for
194                                                 // ID, class, and element selectors
195                                                 re2 = quickClass;
196                                                 m = re2.exec(t);
197                                         }
198
199                                         m[2] = m[2].replace(/\\/g, "");
200
201                                         var elem = ret[ret.length-1];
202
203                                         // Try to do a global search by ID, where we can
204                                         if ( m[1] == "#" && elem && elem.getElementById && !jQuery.isXMLDoc(elem) ) {
205                                                 // Optimization for HTML document case
206                                                 var oid = elem.getElementById(m[2]);
207                                                 
208                                                 // Do a quick check for the existence of the actual ID attribute
209                                                 // to avoid selecting by the name attribute in IE
210                                                 // also check to insure id is a string to avoid selecting an element with the name of 'id' inside a form
211                                                 if ( (jQuery.browser.msie||jQuery.browser.opera) && oid && typeof oid.id == "string" && oid.id != m[2] )
212                                                         oid = jQuery('[@id="'+m[2]+'"]', elem)[0];
213
214                                                 // Do a quick check for node name (where applicable) so
215                                                 // that div#foo searches will be really fast
216                                                 ret = r = oid && (!m[3] || jQuery.nodeName(oid, m[3])) ? [oid] : [];
217                                         } else {
218                                                 // We need to find all descendant elements
219                                                 for ( var i = 0; ret[i]; i++ ) {
220                                                         // Grab the tag name being searched for
221                                                         var tag = m[1] == "#" && m[3] ? m[3] : m[1] != "" || m[0] == "" ? "*" : m[2];
222
223                                                         // Handle IE7 being really dumb about <object>s
224                                                         if ( tag == "*" && ret[i].nodeName.toLowerCase() == "object" )
225                                                                 tag = "param";
226
227                                                         r = jQuery.merge( r, ret[i].getElementsByTagName( tag ));
228                                                 }
229
230                                                 // It's faster to filter by class and be done with it
231                                                 if ( m[1] == "." )
232                                                         r = jQuery.classFilter( r, m[2] );
233
234                                                 // Same with ID filtering
235                                                 if ( m[1] == "#" ) {
236                                                         var tmp = [];
237
238                                                         // Try to find the element with the ID
239                                                         for ( var i = 0; r[i]; i++ )
240                                                                 if ( r[i].getAttribute("id") == m[2] ) {
241                                                                         tmp = [ r[i] ];
242                                                                         break;
243                                                                 }
244
245                                                         r = tmp;
246                                                 }
247
248                                                 ret = r;
249                                         }
250
251                                         t = t.replace( re2, "" );
252                                 }
253
254                         }
255
256                         // If a selector string still exists
257                         if ( t ) {
258                                 // Attempt to filter it
259                                 var val = jQuery.filter(t,r);
260                                 ret = r = val.r;
261                                 t = jQuery.trim(val.t);
262                         }
263                 }
264
265                 // An error occurred with the selector;
266                 // just return an empty set instead
267                 if ( t )
268                         ret = [];
269
270                 // Remove the root context
271                 if ( ret && context == ret[0] )
272                         ret.shift();
273
274                 // And combine the results
275                 done = jQuery.merge( done, ret );
276
277                 return done;
278         },
279
280         classFilter: function(r,m,not){
281                 m = " " + m + " ";
282                 var tmp = [];
283                 for ( var i = 0; r[i]; i++ ) {
284                         var pass = (" " + r[i].className + " ").indexOf( m ) >= 0;
285                         if ( !not && pass || not && !pass )
286                                 tmp.push( r[i] );
287                 }
288                 return tmp;
289         },
290
291         filter: function(t,r,not) {
292                 var last;
293
294                 // Look for common filter expressions
295                 while ( t  && t != last ) {
296                         last = t;
297
298                         var p = jQuery.parse, m;
299
300                         for ( var i = 0; p[i]; i++ ) {
301                                 m = p[i].exec( t );
302
303                                 if ( m ) {
304                                         // Remove what we just matched
305                                         t = t.substring( m[0].length );
306
307                                         m[2] = m[2].replace(/\\/g, "");
308                                         break;
309                                 }
310                         }
311
312                         if ( !m )
313                                 break;
314
315                         // :not() is a special case that can be optimized by
316                         // keeping it out of the expression list
317                         if ( m[1] == ":" && m[2] == "not" )
318                                 r = jQuery.filter(m[3], r, true).r;
319
320                         // We can get a big speed boost by filtering by class here
321                         else if ( m[1] == "." )
322                                 r = jQuery.classFilter(r, m[2], not);
323
324                         else if ( m[1] == "[" ) {
325                                 var tmp = [], type = m[3];
326                                 
327                                 for ( var i = 0, rl = r.length; i < rl; i++ ) {
328                                         var a = r[i], z = a[ jQuery.props[m[2]] || m[2] ];
329                                         
330                                         if ( z == null || /href|src|selected/.test(m[2]) )
331                                                 z = jQuery.attr(a,m[2]) || '';
332
333                                         if ( (type == "" && !!z ||
334                                                  type == "=" && z == m[5] ||
335                                                  type == "!=" && z != m[5] ||
336                                                  type == "^=" && z && !z.indexOf(m[5]) ||
337                                                  type == "$=" && z.substr(z.length - m[5].length) == m[5] ||
338                                                  (type == "*=" || type == "~=") && z.indexOf(m[5]) >= 0) ^ not )
339                                                         tmp.push( a );
340                                 }
341                                 
342                                 r = tmp;
343
344                         // We can get a speed boost by handling nth-child here
345                         } else if ( m[1] == ":" && m[2] == "nth-child" ) {
346                                 var num = jQuery.mergeNum++, tmp = [],
347                                         test = /(\d*)n\+?(\d*)/.exec(
348                                                 m[3] == "even" && "2n" || m[3] == "odd" && "2n+1" ||
349                                                 !/\D/.test(m[3]) && "n+" + m[3] || m[3]),
350                                         first = (test[1] || 1) - 0, last = test[2] - 0;
351
352                                 for ( var i = 0, rl = r.length; i < rl; i++ ) {
353                                         var node = r[i], parentNode = node.parentNode;
354
355                                         if ( num != parentNode.mergeNum ) {
356                                                 var c = 1;
357
358                                                 for ( var n = parentNode.firstChild; n; n = n.nextSibling )
359                                                         if ( n.nodeType == 1 )
360                                                                 n.nodeIndex = c++;
361
362                                                 parentNode.mergeNum = num;
363                                         }
364
365                                         var add = false;
366
367                                         if ( first == 1 ) {
368                                                 if ( last == 0 || node.nodeIndex == last )
369                                                         add = true;
370                                         } else if ( (node.nodeIndex + last) % first == 0 )
371                                                 add = true;
372
373                                         if ( add ^ not )
374                                                 tmp.push( node );
375                                 }
376
377                                 r = tmp;
378
379                         // Otherwise, find the expression to execute
380                         } else {
381                                 var f = jQuery.expr[m[1]];
382                                 if ( typeof f != "string" )
383                                         f = jQuery.expr[m[1]][m[2]];
384
385                                 // Build a custom macro to enclose it
386                                 f = eval("false||function(a,i){return " + f + "}");
387
388                                 // Execute it against the current filter
389                                 r = jQuery.grep( r, f, not );
390                         }
391                 }
392
393                 // Return an array of filtered elements (r)
394                 // and the modified expression string (t)
395                 return { r: r, t: t };
396         },
397
398         parents: function( elem ){
399                 var matched = [];
400                 var cur = elem.parentNode;
401                 while ( cur && cur != document ) {
402                         matched.push( cur );
403                         cur = cur.parentNode;
404                 }
405                 return matched;
406         },
407         
408         nth: function(cur,result,dir,elem){
409                 result = result || 1;
410                 var num = 0;
411
412                 for ( ; cur; cur = cur[dir] )
413                         if ( cur.nodeType == 1 && ++num == result )
414                                 break;
415
416                 return cur;
417         },
418         
419         sibling: function( n, elem ) {
420                 var r = [];
421
422                 for ( ; n; n = n.nextSibling ) {
423                         if ( n.nodeType == 1 && (!elem || n != elem) )
424                                 r.push( n );
425                 }
426
427                 return r;
428         }
429 });