OSDN Git Service

e1740fd141530164d2472396ce0edd585178444f
[pettanr/clientJs.git] / 0.6.x / js / dom / 18_XDomQuery.js
1 /**\r
2  * Original code by ofk ( kQuery, ksk )\r
3  * http://d.hatena.ne.jp/ofk/comment/20090106/1231258010\r
4  * http://d.hatena.ne.jp/ofk/20090111/1231668170 \r
5  *\r
6  * セレクタ Level 3\r
7  * http://standards.mitsue.co.jp/resources/w3c/TR/css3-selectors/#nth-child-pseudo\r
8  */\r
9 \r
10 X.Dom.Query = {\r
11         _PSEUDO    : {\r
12                 'nth-child'        : 9,\r
13                 'nth-last-child'   : 14,\r
14                 'nth-of-type'      : 11,\r
15                 'nth-last-of-type' : 16,\r
16                 root               : 4,\r
17                 link               : 4,\r
18                 lang               : 4,\r
19                 empty              : 5,\r
20                 target             : 6,\r
21                 invalid            : 7,\r
22                 enabled            : 7,\r
23                 checked            : 7,\r
24                 disabled           : 8,\r
25                 contains           : 8,\r
26                 'last-child'       : 10,\r
27                 'only-child'       : 10,                        \r
28                 'first-child'      : 11,\r
29                 'last-of-type'     : 12,\r
30                 'only-of-type'     : 12,                \r
31                 'first-of-type'    : 13\r
32         },\r
33         \r
34         _COMBINATOR : {\r
35                 ''    : 0, // none\r
36                 ' '   : 1, // 子孫セレクタ\r
37                 '>'   : 2, // 子セレクタ\r
38                 '+'   : 3, // 兄弟セレクタ,共通の親を持つ、1番目要素が2番目要素の1つ前にある\r
39                 '~'   : 4, // 一般兄弟セレクタ,共通の親を持つ、1番目要素が2番目要素の前 (直前でなくともよい) にある\r
40                 ','   : 5\r
41         },\r
42         _SELECTOR : {\r
43                 ''    : 0, // none\r
44                 tag   : 1,\r
45                 '#'   : 2,\r
46                 '.'   : 3,\r
47                 ':'   : 4,\r
48                 '['   : 5,\r
49                 not   : 6,\r
50                 scope : 7,\r
51                 root  : 8,\r
52                 link  : 9\r
53         },\r
54         _OPERATORS : { '==' : 1, '!=': 2, '~=': 3, '^=': 4, '$=': 5, '*=': 6, '|=': 7 },\r
55         _ALPHABET  : 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-0123456789\\',\r
56         _NUMBER    : '+-0123456789'\r
57 };\r
58                         \r
59 /*\r
60  * セレクタ文字の解析\r
61  * 結合子 + 単体セレクタ( タグ,*,#,.,[],: )\r
62  * ' ' 子孫セレクタ, '>' 直下,'+','~' の場合、tagName|* を返す\r
63  * return [ pointer, [ selector, ... ] ] error: return pointer\r
64  */\r
65 X.Dom.Query._parse = function( query, last ){\r
66         var COMBINATOR = X.Dom.Query._COMBINATOR,\r
67                 SELECTOR   = X.Dom.Query._SELECTOR,\r
68                 OPERATORS  = X.Dom.Query._OPERATORS,\r
69                 ALPHABET   = X.Dom.Query._ALPHABET,\r
70                 NUMBER     = X.Dom.Query._NUMBER,\r
71                 result     = [],\r
72                 i          = -1,\r
73                 l          = query.length,\r
74                 phase      = 0x0,\r
75                 combinator = 0,\r
76                 selector   = 0,\r
77                 chr, chrCode, nameChr, name1st,\r
78                 tmp, escape, quot, start,\r
79                 name, key, value, operator, a, b, not;\r
80         query += ' ';\r
81         while( i < l ){\r
82                 chr     = query.charAt( ++i );\r
83                 chrCode = ALPHABET.indexOf( chr );\r
84                 nameChr = chrCode !== -1;\r
85                 name1st = nameChr && chrCode < 52;\r
86                 switch( phase ){\r
87                         case 0x0 :\r
88                                 name1st ? // tagName\r
89                                         ( ( selector = 1 ) && ( phase = 0x2 ) && ( start = i ) ) :\r
90                                 !not && ( tmp = COMBINATOR[ chr ] ) ? (\r
91                                         ( 1 < tmp && 1 < combinator ) ?\r
92                                                 ( phase = 0xf ) :\r
93                                                 ( phase = tmp === 5 ? 0xe : 0x0 ) & ( ( 1 < tmp || combinator < 1 ) && ( combinator = tmp ) ) & ( tmp === 5 && ++i ) ) : // ' ' でない結合子の上書きはエラー\r
94                                 ( tmp = SELECTOR[ chr ] ) ? // [\r
95                                         ( selector = tmp ) && ( phase = selector === 5 ? 0x4 : 0x1 ) : // 7:[, 0<:\r
96                                 chr === '*' ?\r
97                                         ( ( selector = 1 ) && ( name = chr ) && ( phase = 0xe ) && ++i ) :\r
98                                         chr !== ' ' && ( phase = 0xf );\r
99                                 //console.log( '0x0: ' + name1st + ' ' + chrCode + ' ' + chr + ' ' + phase + ' tmp:' + tmp + ' comb:' + combinator );\r
100                                 break;\r
101                         case 0x1 :\r
102                                 name1st ?\r
103                                         ( ( start = i ) && ( phase = 0x2 ) ) :\r
104                                         chr !== ' ' && ( phase = 0xf );\r
105                                 break;\r
106                         case 0x2 :\r
107                                 !nameChr && !( escape && ( selector === 2 || selector === 3 ) && ( chr === ':' || chr === '.' ) ) ? // id or class の場合 : . を直前にエスケープした場合に限り使える\r
108                                         ( name = query.substring( start, i ) ) && ( phase = selector === 4 && name !== 'not' && chr === '(' ? 0x8 : 0xe ) :\r
109                                 SELECTOR[ chr ] < 4 && ( phase = 0xe );\r
110                                 break;\r
111                         \r
112                         case 0x3 : //:nth-\r
113                                 break;\r
114                         \r
115                         case 0x4 : // start attr filter\r
116                                 name1st ?\r
117                                         ( ( phase = 0x5 ) && ( start = i ) ) :\r
118                                         ( phase = 0xf );\r
119                                 break;\r
120                         case 0x5 : // attr filter key\r
121                                 chr === '=' ?\r
122                                         ( ( operator = 1 ) && ( phase = 0x6 ) && ( key || ( key = query.substring( start, i ) ) ) && ( start = i + 1 ) ) :\r
123                                 chr === ']' ?\r
124                                         ( !( operator = 0 ) && ( phase = 0xe ) && ( key || ( key = query.substring( start, i ) ) ) && ++i ) :\r
125                                 chr === ' ' ?\r
126                                         ( key || ( key = query.substring( start, i ) ) ) :\r
127                                 ( operator = OPERATORS[ query.substr( i, 2 ) ] ) ?\r
128                                         ( ( phase = 0x6 ) && ( key || ( key = query.substring( start, i ) ) ) && ( start = ++i ) ) :\r
129                                         !nameChr && ( phase = 0xf );\r
130                                 //console.log( name1st + ' ' + chrCode + chr + phase );\r
131                                 break;\r
132                         case 0x6 :\r
133                                 ( chr === '"' || chr === "'" ) && !escape && !quot ?\r
134                                         ( quot = chr ) && ( start = i + 1 ) && ( phase = 0x7 ) :\r
135                                         chr !== ' ' && ( start = i ) && ( phase = 0x7 );\r
136                                 break;\r
137                                 \r
138                         case 0x7 : // attr filter value\r
139                                 chr === quot ?\r
140                                         !escape && !value && ( value = query.substring( start, i ) ) :\r
141                                 chr === ']' ?\r
142                                         ( ( value || ( value = query.substring( start, i ) ) ) && ( phase = 0xe ) && ++i ) :    \r
143                                 chr === ' ' && !quot && !value && ( value = query.substring( start, i ) );                      \r
144                                         //( chr === '"' || chr === "'" ) && !quot && ( quot = chr ) && ( start = i + 1 );\r
145                                 break;\r
146                                 \r
147                         case 0x8 : // 4, 2n, even, odd, -n+4,\r
148                                 NUMBER.indexOf( chr ) !== -1 ?\r
149                                         ( start = i ) && ( phase = 0x9 ) :\r
150                                 chr === 'n' ?\r
151                                         ( phase = 0xa ) && ( a = 1 ) && ( start = i + 1 ) :\r
152                                 query.substr( i, 4 ) === 'even' ?\r
153                                         ( ( a = 2 ) && !( b = 0 ) && ( i += 3 ) ) :\r
154                                 query.substr( i, 3 ) === 'odd' ?\r
155                                         ( ( a = 2 ) && ( b = 1 ) && ( i += 2 ) ) :                                      \r
156                                 chr === ')' && ( phase = a ? 0xe : 0xf ) && ++i;\r
157                                 //console.log( '0x8: ' + name1st + ' ' + chrCode + ' ' + chr + ' ' + phase );\r
158                                 break;\r
159                         case 0x9 :\r
160                                 tmp = query.substring( start, i );\r
161                                 chr === 'n' ?\r
162                                         ( phase = 0xa ) && ( start = i + 1 ) && ( a = tmp === '+' ? 1 : tmp === '-' ? -1 : parseFloat( tmp ) ) :\r
163                                 chr === ')' && ( phase = 0xe ) && ( b = parseFloat( tmp ) ) && ++i && ( a = 0 );\r
164                                 //console.log( '0x9: ' + name1st + ' ' + chrCode + ' ' + chr + ' ' + phase );\r
165                                 break;\r
166                         case 0xa :\r
167                                 chr === ')' && ( phase = 0xe ) && ++i && ( b = parseFloat( query.substring( start, i ) ) || 0 );\r
168                                 //console.log( '0xa: ' + start + ' ' + i );\r
169                                 break;\r
170                         default :\r
171                 };\r
172                 //alert( chr + ' ' + phase + ' ' + selector + ' ' + name + ' ' + name1st )\r
173                 if( phase === 0xe ){\r
174                         if( selector === 4 ){// :not\r
175                                 if( name === 'not' ){\r
176                                         if( not ) return i; // error\r
177                                         not      = true;\r
178                                         selector = 0;\r
179                                         phase    = 0x0;\r
180                                         name     = null;\r
181                                         continue;\r
182                                 } else {\r
183                                         if( a !== a || b !== b ) return i;\r
184                                         result = [ not ? 0 : combinator, selector, name, a, b ];\r
185                                         break;  \r
186                                 };\r
187                                 // lang result = [ not ? 0 : combinator, selector, name, lan ];\r
188                                 // contains result = [ not ? 0 : combinator, selector, name, text ];\r
189                         };\r
190                         result =\r
191                                 combinator === 5 ?\r
192                                         5 :\r
193                                 selector === 5 ?\r
194                                         [ not ? 0 : combinator, selector, key, operator, value ] :\r
195                                         [ not ? 0 : combinator, selector, name.split( '\\' ).join( '' ) ];\r
196                         break;\r
197                 } else\r
198                 if( phase === 0xf ) return i;\r
199 \r
200                 escape = chr === '\\' && !escape;\r
201         };\r
202         //if( phase !== 0xe ) return i;\r
203         if( not && ( tmp = query.substr( i ).indexOf( ')' ) ) === -1 ) return i;\r
204         return not ? [ i + tmp + 1, [ combinator, 6, result ] ] : [ i, result ];\r
205 };\r
206 \r
207         // セレクター\r
208         X.Dom.find = X._shortcut = Node.prototype.find = X.Dom.NodeList.prototype.find = function( queryString ){\r
209                 var HTML      = Node._html,\r
210                         scope     = this.constructor === X.Dom.NodeList && this.length ? this : [ this.constructor === Node ? this : Node._body ],\r
211                         parents   = scope, // 探索元の親要素 XNodeList の場合あり\r
212                         noLower   = 'title id name class for action archive background cite classid codebase data href longdesc profile src usemap',// + X.Dom.DTD.ATTR_VAL_IS_URI.join( ' ' ),\r
213                         ARY_PUSH  = Array.prototype.push,\r
214                         ret       = [], // 結果要素\r
215                         root      = X.Dom.Node.getRoot( scope[ 0 ] ),\r
216                         isXML     = !!X.Dom.Node.isXmlDocument( root ),\r
217                         isMulti   = 1 < scope.length,// 要素をマージする必要がある\r
218                         isStart   = true,\r
219                         _         = ' ',\r
220                         isAll, isNot, hasRoot,\r
221                         l, i, n, parsed,\r
222                         xnodes, // 一時保存用\r
223                         merge, // 要素がコメントノードで汚染されている場合使う\r
224                         combinator, selector, name, tagName,\r
225                         uid, tmp, xnode, filter, key, op, val, toLower, useName,\r
226             links, className, attr, flag;\r
227 \r
228                 /*@+debug[*/\r
229                 if( X.Dom.readyState < X.Dom.Event.XDOM_READY ){\r
230                         alert( 'not ready! use X.Dom.listenOnce( X.Dom.Event.XDOM_READY, callback )' );\r
231                         return;\r
232                 };\r
233                 /*]@+debug*/\r
234 \r
235                 // 文字列以外は空で返す\r
236                 if( typeof queryString !== 'string' ) return ret;\r
237                 \r
238                 xnodes = [];\r
239                 \r
240                 // 以下、パースと探索\r
241                 for( ; queryString.length; ){\r
242                         //console.log( 'queryString[' + queryString + ']' );\r
243                         \r
244                         // 初期化処理\r
245                         if( !parsed ){\r
246                                 parsed = X.Dom.Query._parse( queryString );\r
247                                 \r
248                                 if( typeof parsed === 'number' ){\r
249                                         // error\r
250                                         return [];\r
251                                 };\r
252                                 \r
253                                 queryString = queryString.substr( parsed[ 0 ] );\r
254                                 parsed      = parsed[ 1 ];\r
255                                 \r
256                                 //console.log( 'X.Dom.Query._parse ' + parsed );\r
257                                 \r
258                                 if( parsed === 5 ){\r
259                                         isMulti = true;\r
260                                         parents = scope;\r
261                                         xnodes && xnodes.length && ARY_PUSH.apply( ret, xnodes );\r
262                                         parsed  = null;\r
263                                         xnodes  = [];\r
264                                         isStart = true;\r
265                                         continue;\r
266                                 };\r
267                         };\r
268                         \r
269                         combinator  = parsed[ 0 ];\r
270                         selector    = parsed[ 1 ];\r
271                         name        = parsed[ 2 ];\r
272                         tagName     = selector === 1 ? ( isXML ? name : name.toUpperCase() ) : '*';\r
273                         isAll       = tagName === '*';\r
274         \r
275                         if( !isStart ){\r
276                                 if( !xnodes.length ){\r
277                                         parsed = null;\r
278                                         continue;                                       \r
279                                 } else\r
280                                 if( combinator !== 0 ){\r
281                                         parents = xnodes;\r
282                                         xnodes  = [];\r
283                                         //console.log( 'cobinator !== 0 ' + parents.length + ' : ' + xnodes.length );\r
284                                 };\r
285                         };\r
286                         \r
287                         i = 0;\r
288                         l = parents.length;\r
289                         n = -1; \r
290                         isMulti = isMulti || 1 < l;\r
291                         \r
292                         //console.log( 'combinator ' + combinator );\r
293         \r
294                         switch( combinator ){\r
295                                 // > TagName|*\r
296                                 case 2 :\r
297                                         for( ; i < l; ++i ){\r
298                                                 for( xnode = parents[ i ].firstChild(); xnode; xnode = xnode.nextNode() ){\r
299                                                         if( xnode._xnodeType === 1 && ( isAll || tagName === xnode._tag ) ) xnodes[ ++n ] = xnode;\r
300                                                 };                              \r
301                                         };\r
302                                         break;\r
303                                 // + TagName|*\r
304                                 case 3 :\r
305                                         for( ; i < l; ++i ){\r
306                                                 for( xnode = parents[ i ].nextNode(); xnode; xnode = xnode.nextNode() ){\r
307                                                         if( xnode._xnodeType === 1 ){\r
308                                                                 if( isAll || tagName === xnode._tag ) xnodes[ ++n ] = xnode;\r
309                                                                 break;\r
310                                                         };                                                                      \r
311                                                 };                                                              \r
312                                         };\r
313                                         break;\r
314                                 // ~ TagName|*\r
315                                 case 4 :\r
316                                         merge  = {};\r
317                                         for( ; i < l; ++i ){\r
318                                                 for( xnode = parents[ i ].nextNode(); xnode; xnode = xnode.nextNode() ){\r
319                                                         if( xnode._xnodeType === 1 && ( isAll || tagName === xnode._tag ) ){\r
320                                                                 uid = xnode._uid;\r
321                                                                 if( merge[ uid ] ){\r
322                                                                         break;\r
323                                                                 } else {\r
324                                                                         merge[ uid ] = true;\r
325                                                                         xnodes[ ++n ] = xnode;\r
326                                                                 };\r
327                                                         };                                                                      \r
328                                                 };                                                              \r
329                                         };\r
330                                         break;\r
331                                         \r
332                                 default :\r
333                                         if( combinator === 1 || ( isStart && selector < 7 ) ){\r
334                                                 //console.log( l + ' > ' + xnodes.length + ' tag:' + tagName );\r
335                                                 for( ; i < l; ++i ){\r
336                                                         xnode = parents[ i ];\r
337                                                         xnode._xnodes && xnode._xnodes.length && X.Dom.Query._fetchElements( xnodes, xnode, isAll ? null : tagName );\r
338                                                 };\r
339                                                 //console.log( l + ' >> ' + xnodes.length + ' tag:' + tagName );\r
340                                         };\r
341                         };\r
342                         \r
343                         isStart = false;\r
344                         \r
345                         //alert( 'pre-selector:' + ( xnodes && xnodes.length ) )\r
346                         \r
347                         switch( selector ){\r
348                                 // #, ID\r
349                                 case 2 :\r
350                                         filter = [ 'id', 1, name ]; break;\r
351                                 // ., class\r
352                                 case 3 :\r
353                                         filter = [ 'class', 3 /*'~='*/, name ]; break;\r
354                                 // :, 擬似クラス\r
355                                 case 4 :\r
356                                         if( !( filter = X.Dom.Query._filter[ name ] ) ){\r
357                                                 return [];\r
358                                         };\r
359                                         break;\r
360                                 // [] 属性\r
361                                 case 5 :\r
362                                         filter = [ name, parsed[ 3 ], parsed[ 4 ] ]; break;\r
363                                 // :not\r
364                                 case 6 :\r
365                                         isNot  = true;\r
366                                         parsed = parsed[ 2 ];\r
367                                         continue;\r
368                                 // scope\r
369                                 case 7 :\r
370                                         xnodes = scope; break;\r
371                                 // root\r
372                                 case 8 :\r
373                                         hasRoot = true;\r
374                                         xnodes = [ HTML ]; break;\r
375                                 // link\r
376                                 case 9 :\r
377                                         if( links = document.links ){\r
378                                                 for( xnodes = [], i = links.length; i; ){\r
379                                                         xnodes[ --i ] = new Node( links[ i ] );\r
380                                                 };\r
381                                         } else {\r
382                                                 // area[href],a[href]\r
383                                         }\r
384                         };\r
385                         \r
386                         if( filter && xnodes.length ){\r
387                                 // filter.mが関数の場合\r
388                                 if( filter.m ){\r
389                                         xnodes = filter.m(\r
390                                                 {\r
391                                                         not : isNot,\r
392                                                         xml : isXML\r
393                                                 },\r
394                                                 xnodes,\r
395                                                 parsed[ 3 ], parsed[ 4 ]\r
396                                         );\r
397                                 } else\r
398                                 // filterが関数の場合\r
399                                 if( typeof filter === 'function' ){\r
400                                         tmp = [];\r
401                                         for( i = 0, n = -1; xnode = xnodes[ i ]; ++i ){\r
402                                                 if( ( !!filter( xnode ) ) ^ isNot ) tmp[ ++n ] = xnode; \r
403                                         };\r
404                                         xnodes = tmp;\r
405                                 } else {\r
406                                 // 属性セレクター                        \r
407                                         tmp = [];\r
408                                         key = filter[ 0 ];\r
409                                         op  = filter[ 1 ];\r
410                                         val = filter[ 2 ];\r
411                                         \r
412                                         key = X.Dom.Attr.renameForTag[ key ] || key;\r
413                                         \r
414                                         // [class~='val']\r
415                                         if( !isXML && key === 'class' && op === 3 ){\r
416                                                 val = _ + val + _;\r
417                                                 for( i = 0, n = -1; xnode = xnodes[ i ]; ++i ){\r
418                                                         className = xnode._className;\r
419                                                         if( !!( className && ( _ + className + _ ).indexOf( val ) > -1 ) ^ isNot ) tmp[ ++n ] = xnode;\r
420                                                 };\r
421                                         } else {\r
422                                         // 通常\r
423                                                 // 諦めて、funcAttrを呼ぶ\r
424                                                 // flag_call  = ($.browser.safari && key === 'selected');\r
425                                                 // getAttributeを使わない\r
426                                                 useName = X.UA.IE && key !== 'href' && key !== 'src';\r
427                                                 toLower = !!val && !isXML && noLower.indexOf( key ) === -1; //!noLower.test(key);\r
428                                                 if( toLower ) val = val.toLowerCase();\r
429                                                 if( op === 3 ) val = _ + val + _;\r
430 \r
431                                                 for( i = 0, n = -1, l = xnodes.length; i < l; ++i ){\r
432                                                         xnode = xnodes[ i ];\r
433                                                         attr =\r
434                                                                 key === 'id' ? xnode._id :\r
435                                                                 key === 'class' ? xnode._className :\r
436                                                                 xnode._attrs && xnode._attrs[ key ];\r
437                                                                 //flag_call ?\r
438                                                                 //      funcAttr( elem, key ) :\r
439                                                                 //useName ?\r
440                                                                 //      elem[ X.Dom.Attr.renameForDOM[ key ] || key ] :\r
441                                                                 //      elem.getAttribute( key, 2 );\r
442                                                         flag = attr != null;// && ( !useName || attr !== '' );\r
443                                                         if( flag && op ){\r
444                                                                 if( toLower ) attr = attr.toLowerCase();\r
445                                                                 \r
446                                                                 switch( op ){\r
447                                                                         case 1: // =\r
448                                                                                 flag = attr === val;\r
449                                                                                 break;\r
450                                                                         case 2: // !=\r
451                                                                                 flag = attr !== val;\r
452                                                                                 break;\r
453                                                                         case 3: // ~=\r
454                                                                                 flag = ( _ + attr + _ ).indexOf( val ) !== -1;\r
455                                                                                 break;\r
456                                                                         case 4: // ^=\r
457                                                                                 flag = attr.indexOf( val ) === 0;\r
458                                                                                 break;\r
459                                                                         case 5: // $=\r
460                                                                                 flag = attr.lastIndexOf( val ) + val.length === attr.length;\r
461                                                                                 break;\r
462                                                                         case 6: // *=\r
463                                                                                 flag = attr.indexOf( val ) !== -1;\r
464                                                                                 break;\r
465                                                                         case 7: // |=\r
466                                                                                 flag = attr === val || attr.substring( 0, val.length + 1 ) === val + '-';\r
467                                                                                 break;\r
468                                                                 };\r
469                                                         };\r
470                                                         if( !!flag ^ isNot ) tmp[ ++n ] = xnode;\r
471                                                 };\r
472                                         };\r
473                                         xnodes = tmp;\r
474                                 };\r
475                         };\r
476                         filter  = null;\r
477                         isNot   = false;\r
478                         parsed  = null;\r
479                         \r
480                         //console.log( '//end :' + ( xnodes && xnodes.length ) );\r
481                 };\r
482                 //console.log( 'multi:' + ( xnodes && xnodes.length ) );\r
483                 \r
484                 // tree 順に並び替え、同一要素の排除\r
485                 if( isMulti ){\r
486                         xnodes && xnodes.length && ARY_PUSH.apply( ret, xnodes );\r
487                         l = ret.length;\r
488                         if( l < 2 ) return ret[ 0 ] || Node.none;\r
489                         \r
490                         xnodes = [];\r
491                         merge  = {};\r
492                         for( i = 0, n = -1; i < l; ++i ){\r
493                                 //alert( 'multi:' + i )\r
494                                 xnode = ret[ i ];\r
495                                 if( !merge[ uid = xnode._uid ] ){\r
496                                         merge[ uid ] = true;\r
497                                         xnodes[ ++n ] = xnode;\r
498                                 };\r
499                         };\r
500                         X.Dom.Query._sortElementOrder( ret = [], xnodes, hasRoot ? [ HTML ] : HTML._xnodes );\r
501                         xnodes = ret;\r
502                 };\r
503 \r
504                 return xnodes.length === 1 ? xnodes[ 0 ] : new X.Dom.NodeList( xnodes );\r
505         };\r
506         \r
507         X.Dom.Query._sortElementOrder = function( newList, list, xnodes ){\r
508                 var l = xnodes.length,\r
509                         i = 0,\r
510                         j, child, _xnodes;\r
511                 for( ; i < l; ++i ){\r
512                         child = xnodes[ i ];\r
513                         if( child._xnodeType !== 1 ) continue;\r
514                         //console.log( child._tag );\r
515                         if( ( j = list.indexOf( child ) ) !== -1 ){\r
516                                 newList[ newList.length ] = child;\r
517                                 list.splice( j, 1 );\r
518                                 if( list.length === 1 ){\r
519                                         newList[ newList.length ] = list[ 0 ];\r
520                                         list.length = 0;\r
521                                         return true;\r
522                                 };\r
523                                 if( list.length === 0 ) return true;\r
524                         };\r
525                         if( ( _xnodes = child._xnodes ) && X.Dom.Query._sortElementOrder( newList, list, _xnodes ) ){\r
526                                 return true;\r
527                         };\r
528                 };\r
529         };\r
530         \r
531         X.Dom.Query._fetchElements = function( list, parent, tag ){\r
532                 var xnodes = parent._xnodes,\r
533                         l      = xnodes.length,\r
534                         i      = 0,\r
535                         child;\r
536                 for( ; i < l; ++i ){\r
537                         child = xnodes[ i ];\r
538                         if( child._xnodeType === 1 ){\r
539                                 ( !tag || child._tag === tag ) && ( list[ list.length ] = child );\r
540                                 //console.log( parent._tag + ' > ' + child._tag + ' == ' + tag+ ' l:' + list.length );\r
541                                 child._xnodes && child._xnodes.length && X.Dom.Query._fetchElements( list, child, tag );\r
542                         };\r
543                 };\r
544         };\r
545 \r
546         X.Dom.Query._funcSelectorChild = function( type, flag_all, flags, xnodes ){\r
547                 var res      = [],\r
548                         flag_not = flags.not,\r
549                         i = 0, n = -1, xnode, node,\r
550                         tagName, tmp;\r
551                 for( ; xnode = xnodes[ i ]; ++i ){\r
552                         tagName = flag_all || xnode._tag;\r
553                         tmp     = null;\r
554                         if( /* tmp === null && */ type <= 0 ){\r
555                                 for( node = xnode.prevNode(); node; node = node.prevNode() ){\r
556                                         if( node._xnodeType === 1 && ( flag_all || tagName === node._tag ) ){\r
557                                                 tmp = false;\r
558                                                 break;\r
559                                         };\r
560                                 };\r
561                         };\r
562                         if( tmp === null && 0 <= type ){\r
563                                 for( node = xnode.nextNode(); node; node = node.nextNode() ){\r
564                                         if( node._xnodeType === 1 && ( flag_all || tagName === node._tag ) ){\r
565                                                 tmp = false;\r
566                                                 break;\r
567                                         };              \r
568                                 };                                              \r
569                         };\r
570                         if( tmp === null ) tmp = true;\r
571                         if( tmp ^ flag_not ) res[ ++n ] = xnode;\r
572                 };\r
573                 return res;\r
574         };\r
575         X.Dom.Query._funcSelectorNth = function( pointer, sibling, flag_all, flags, xnodes, a, b ){\r
576                 var _data    = funcData,\r
577                         res      = [],\r
578                         checked  = {},\r
579                         flag_not = flags.not,\r
580                         i = 0, n = -1, uid,\r
581                         c, xnode, tmp, node, tagName;\r
582                 for( ; xnode = xnodes[ i ]; ++i ){\r
583                         uid = xnode._uid;\r
584                         tmp = checked[ uid ];\r
585                         if( tmp === void 0 ){\r
586                                 for( c = 0, node = xnode.parent[ pointer ](), tagName = flag_all || xnode._tag; node; node = node[ sibling ]() ){\r
587                                         if( node._xnodeType === 1 && ( flag_all || tagName === node._tag ) ){\r
588                                                 ++c;\r
589                                                 checked[ node._uid ] = a === 0 ? c === b : (c - b) % a === 0 && (c - b) / a >= 0;\r
590                                         };                                                      \r
591                                 };\r
592                                 tmp = checked[ uid ];\r
593                         };\r
594                         if( tmp ^ flag_not ) res[ ++n ] = xnode;\r
595                 };\r
596                 return res;\r
597         };\r
598         X.Dom.Query._funcSelectorProp = function( prop, flag, flags, xnodes ){\r
599                 var res = [],\r
600                         flag_not = flag ? flags.not : !flags.not,\r
601                         i = 0, n = -1, xnode;\r
602                 for( ; xnode = xnodes[ i ]; ++i ){\r
603                         if( xnode._attrs && xnode._attrs[ prop ] ^ flag_not ) res[ ++n ] = xnode;\r
604                 };\r
605                 return res;\r
606         };\r
607 \r
608 X.Dom.Query._filter = {\r
609         root : function( elem ){\r
610                 return elem === ( elem.ownerDocument || elem.document ).documentElement;\r
611         },\r
612         target : {\r
613                 m : function( flags, xnodes ){\r
614                         var res  = [],\r
615                                 hash = location.hash.slice( 1 ),\r
616                                 flag_not = flags.not,\r
617                                 i = 0, n = -1, xnode;\r
618                         for ( ; xnode = xnodes[ i ]; ++i ){\r
619                                 if( ( ( xnode._id || xnode._attrs && xnode._attrs.name ) === hash ) ^ flag_not ) res[ ++n ] = xnode;                                            \r
620                         };\r
621                         return res;\r
622                 }\r
623         },\r
624         'first-child' : {\r
625                 m : function( flags, xnodes ){ return X.Dom.Query._funcSelectorChild( -1, true, flags, xnodes ); }\r
626         },\r
627         'last-child' : {\r
628                 m : function( flags, xnodes ){ return X.Dom.Query._funcSelectorChild( 1, true, flags, xnodes ); }\r
629         },\r
630         'only-child' : {\r
631                 m : function( flags, xnodes ){ return X.Dom.Query._funcSelectorChild( 0, true, flags, xnodes ); }\r
632         },\r
633         'first-of-type' : {\r
634                 m : function( flags, xnodes ){ return X.Dom.Query._funcSelectorChild( -1, false, flags, xnodes ); }\r
635         },\r
636         'last-of-type' : {\r
637                 m : function( flags, xnodes ){ return X.Dom.Query._funcSelectorChild( 1, false, flags, xnodes ); }\r
638         },\r
639         'only-of-type' : {\r
640                 m : function( flags, xnodes ){ return X.Dom.Query._funcSelectorChild( 0, false, flags, xnodes ); }\r
641         },\r
642         'nth-child' : {\r
643                 m : function( flags, xnodes, a, b ){ return X.Dom.Query._funcSelectorNth( 'firstChild', 'nextNode', true, flags, xnodes, a, b ); }\r
644         },\r
645         'nth-last-child' : {\r
646                 m : function( flags, xnodes, a, b ){ return X.Dom.Query._funcSelectorNth( 'lastChild', 'prevNode', true, flags, xnodes, a, b ); }\r
647         },\r
648         'nth-of-type' : {\r
649                 m : function( flags, xnodes, a, b ){ return X.Dom.Query._funcSelectorNth( 'firstChild', 'nextNode', false, flags, xnodes, a, b ); }\r
650         },\r
651         'nth-last-of-type' : {\r
652                 m : function( flags, xnodes, a, b ){ return X.Dom.Query._funcSelectorNth( 'lastChild', 'prevNode', false, flags, xnodes, a, b ); }\r
653         },\r
654         empty : {\r
655                 m : function( flags, xnodes ){\r
656                         var res = [],\r
657                                 flag_not = flags.not,\r
658                                 i = 0, n = -1, xnode, tmp, node;\r
659                         for( ; xnode = xnodes[i]; ++i ){\r
660                                 tmp = true;\r
661                                 for( node = xnode.firstChild(); node; node = node.nextSibling() ){\r
662                                         if( node._xnodeType === 1 || ( node._xnodeType === 3 && node._text ) ){\r
663                                                 tmp = false;\r
664                                                 break;\r
665                                         };                              \r
666                                 };\r
667                                 if( tmp ^ flag_not ) res[ ++n ] = xnode;\r
668                         };\r
669                         return res;\r
670                 }\r
671         },\r
672         link : {\r
673                 m : function( flags, xnodes ){\r
674                         var links = ( xnodes[ 0 ].ownerDocument || xnodes[ 0 ].document ).links,\r
675                                 _data = funcData,\r
676                                 res   = [],\r
677                                 checked, flag_not, i, link, xnode, n;\r
678                         if( !links ) return res;\r
679                         checked = {};\r
680                         flag_not = flags.not;\r
681                         for( i = 0; link = links[ i ]; ++i ){\r
682                                 checked[ ( new Node( link ) )._uid ] = true;\r
683                         };\r
684                         for( i = 0, n = -1; xnode = xnodes[ i ]; ++i ){\r
685                                 if( checked[ xnode._uid ] ^ flag_not ) res[ ++n ] = xnode;\r
686                         };\r
687                         return res;\r
688                 }\r
689         },\r
690         lang : {\r
691                 m : function( flags, xnodes, arg ){\r
692                         var res = [],\r
693                                 //reg = new RegExp('^' + arg, 'i'),\r
694                                 flag_not = flags.not,\r
695                                 i = 0, n = -1, xnode, tmp, lang;\r
696                         arg = arg.toLowerCase();\r
697                         for( ; tmp = xnode = xnodes[ i ]; ++i ){\r
698                                 while( tmp && !( lang = tmp._attrs && tmp._attrs[ 'lang' ] ) ){\r
699                                         tmp = tmp.parent;\r
700                                 };\r
701                                 //tmp = !!(tmp && reg.test(tmp.getAttribute('lang')));\r
702                                 if( ( !!lang && lang.toLowerCase().indexOf( arg ) === 0 ) ^ flag_not ) res[ ++n ] = xnode;\r
703                         };\r
704                         return res;\r
705                 }\r
706         },\r
707         enabled : {\r
708                 m : function( flags, xnodes ){ return X.Dom.Query._funcSelectorProp( 'disabled', false, flags, xnodes ); }\r
709         },\r
710         disabled : {\r
711                 m : function( flags, xnodes ){ return X.Dom.Query._funcSelectorProp( 'disabled', true, flags, xnodes ); }\r
712         },\r
713         checked : {\r
714                 m : function( flags, xnodes ){ return X.Dom.Query._funcSelectorProp( 'checked', true, flags, xnodes ); }\r
715         },\r
716         contains : {\r
717                 m : function( flags, xnodes, arg ){\r
718                         var res = [],\r
719                                 flag_not = flags.not,\r
720                                 i = 0, n = -1, xnode;\r
721                         for( ; xnode = xnodes[ i ]; ++i ){\r
722                                 if ( ( -1 < ( xnode.text() ).indexOf( arg ) ) ^ flag_not ) res[ ++n ] = xnode;                                          \r
723                         };\r
724                         return res;\r
725                 }\r
726         }\r
727 };\r
728 \r
729 \r
730 \r