]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/javascript/tiny_mce/classes/dom/Sizzle.js
Release 6.5.0
[Github/sugarcrm.git] / include / javascript / tiny_mce / classes / dom / Sizzle.js
1 // #ifndef jquery
2
3 /*
4  * Sizzle CSS Selector Engine - v1.0
5  *  Copyright 2009, The Dojo Foundation
6  *  Released under the MIT, BSD, and GPL Licenses.
7  *  More information: http://sizzlejs.com/
8  */
9 (function(){
10
11 var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^\[\]]*\]|['"][^'"]*['"]|[^\[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g,
12         done = 0,
13         toString = Object.prototype.toString,
14         hasDuplicate = false,
15         baseHasDuplicate = true;
16
17 // Here we check if the JavaScript engine is using some sort of
18 // optimization where it does not always call our comparision
19 // function. If that is the case, discard the hasDuplicate value.
20 //   Thus far that includes Google Chrome.
21 [0, 0].sort(function(){
22         baseHasDuplicate = false;
23         return 0;
24 });
25
26 var Sizzle = function(selector, context, results, seed) {
27         results = results || [];
28         context = context || document;
29
30         var origContext = context;
31
32         if ( context.nodeType !== 1 && context.nodeType !== 9 ) {
33                 return [];
34         }
35         
36         if ( !selector || typeof selector !== "string" ) {
37                 return results;
38         }
39
40         var parts = [], m, set, checkSet, extra, prune = true, contextXML = Sizzle.isXML(context),
41                 soFar = selector, ret, cur, pop, i;
42         
43         // Reset the position of the chunker regexp (start from head)
44         do {
45                 chunker.exec("");
46                 m = chunker.exec(soFar);
47
48                 if ( m ) {
49                         soFar = m[3];
50                 
51                         parts.push( m[1] );
52                 
53                         if ( m[2] ) {
54                                 extra = m[3];
55                                 break;
56                         }
57                 }
58         } while ( m );
59
60         if ( parts.length > 1 && origPOS.exec( selector ) ) {
61                 if ( parts.length === 2 && Expr.relative[ parts[0] ] ) {
62                         set = posProcess( parts[0] + parts[1], context );
63                 } else {
64                         set = Expr.relative[ parts[0] ] ?
65                                 [ context ] :
66                                 Sizzle( parts.shift(), context );
67
68                         while ( parts.length ) {
69                                 selector = parts.shift();
70
71                                 if ( Expr.relative[ selector ] ) {
72                                         selector += parts.shift();
73                                 }
74                                 
75                                 set = posProcess( selector, set );
76                         }
77                 }
78         } else {
79                 // Take a shortcut and set the context if the root selector is an ID
80                 // (but not if it'll be faster if the inner selector is an ID)
81                 if ( !seed && parts.length > 1 && context.nodeType === 9 && !contextXML &&
82                                 Expr.match.ID.test(parts[0]) && !Expr.match.ID.test(parts[parts.length - 1]) ) {
83                         ret = Sizzle.find( parts.shift(), context, contextXML );
84                         context = ret.expr ? Sizzle.filter( ret.expr, ret.set )[0] : ret.set[0];
85                 }
86
87                 if ( context ) {
88                         ret = seed ?
89                                 { expr: parts.pop(), set: makeArray(seed) } :
90                                 Sizzle.find( parts.pop(), parts.length === 1 && (parts[0] === "~" || parts[0] === "+") && context.parentNode ? context.parentNode : context, contextXML );
91                         set = ret.expr ? Sizzle.filter( ret.expr, ret.set ) : ret.set;
92
93                         if ( parts.length > 0 ) {
94                                 checkSet = makeArray(set);
95                         } else {
96                                 prune = false;
97                         }
98
99                         while ( parts.length ) {
100                                 cur = parts.pop();
101                                 pop = cur;
102
103                                 if ( !Expr.relative[ cur ] ) {
104                                         cur = "";
105                                 } else {
106                                         pop = parts.pop();
107                                 }
108
109                                 if ( pop == null ) {
110                                         pop = context;
111                                 }
112
113                                 Expr.relative[ cur ]( checkSet, pop, contextXML );
114                         }
115                 } else {
116                         checkSet = parts = [];
117                 }
118         }
119
120         if ( !checkSet ) {
121                 checkSet = set;
122         }
123
124         if ( !checkSet ) {
125                 Sizzle.error( cur || selector );
126         }
127
128         if ( toString.call(checkSet) === "[object Array]" ) {
129                 if ( !prune ) {
130                         results.push.apply( results, checkSet );
131                 } else if ( context && context.nodeType === 1 ) {
132                         for ( i = 0; checkSet[i] != null; i++ ) {
133                                 if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && Sizzle.contains(context, checkSet[i])) ) {
134                                         results.push( set[i] );
135                                 }
136                         }
137                 } else {
138                         for ( i = 0; checkSet[i] != null; i++ ) {
139                                 if ( checkSet[i] && checkSet[i].nodeType === 1 ) {
140                                         results.push( set[i] );
141                                 }
142                         }
143                 }
144         } else {
145                 makeArray( checkSet, results );
146         }
147
148         if ( extra ) {
149                 Sizzle( extra, origContext, results, seed );
150                 Sizzle.uniqueSort( results );
151         }
152
153         return results;
154 };
155
156 Sizzle.uniqueSort = function(results){
157         if ( sortOrder ) {
158                 hasDuplicate = baseHasDuplicate;
159                 results.sort(sortOrder);
160
161                 if ( hasDuplicate ) {
162                         for ( var i = 1; i < results.length; i++ ) {
163                                 if ( results[i] === results[i-1] ) {
164                                         results.splice(i--, 1);
165                                 }
166                         }
167                 }
168         }
169
170         return results;
171 };
172
173 Sizzle.matches = function(expr, set){
174         return Sizzle(expr, null, null, set);
175 };
176
177 Sizzle.find = function(expr, context, isXML){
178         var set;
179
180         if ( !expr ) {
181                 return [];
182         }
183
184         for ( var i = 0, l = Expr.order.length; i < l; i++ ) {
185                 var type = Expr.order[i], match;
186                 
187                 if ( (match = Expr.leftMatch[ type ].exec( expr )) ) {
188                         var left = match[1];
189                         match.splice(1,1);
190
191                         if ( left.substr( left.length - 1 ) !== "\\" ) {
192                                 match[1] = (match[1] || "").replace(/\\/g, "");
193                                 set = Expr.find[ type ]( match, context, isXML );
194                                 if ( set != null ) {
195                                         expr = expr.replace( Expr.match[ type ], "" );
196                                         break;
197                                 }
198                         }
199                 }
200         }
201
202         if ( !set ) {
203                 set = context.getElementsByTagName("*");
204         }
205
206         return {set: set, expr: expr};
207 };
208
209 Sizzle.filter = function(expr, set, inplace, not){
210         var old = expr, result = [], curLoop = set, match, anyFound,
211                 isXMLFilter = set && set[0] && Sizzle.isXML(set[0]);
212
213         while ( expr && set.length ) {
214                 for ( var type in Expr.filter ) {
215                         if ( (match = Expr.leftMatch[ type ].exec( expr )) != null && match[2] ) {
216                                 var filter = Expr.filter[ type ], found, item, left = match[1];
217                                 anyFound = false;
218
219                                 match.splice(1,1);
220
221                                 if ( left.substr( left.length - 1 ) === "\\" ) {
222                                         continue;
223                                 }
224
225                                 if ( curLoop === result ) {
226                                         result = [];
227                                 }
228
229                                 if ( Expr.preFilter[ type ] ) {
230                                         match = Expr.preFilter[ type ]( match, curLoop, inplace, result, not, isXMLFilter );
231
232                                         if ( !match ) {
233                                                 anyFound = found = true;
234                                         } else if ( match === true ) {
235                                                 continue;
236                                         }
237                                 }
238
239                                 if ( match ) {
240                                         for ( var i = 0; (item = curLoop[i]) != null; i++ ) {
241                                                 if ( item ) {
242                                                         found = filter( item, match, i, curLoop );
243                                                         var pass = not ^ !!found;
244
245                                                         if ( inplace && found != null ) {
246                                                                 if ( pass ) {
247                                                                         anyFound = true;
248                                                                 } else {
249                                                                         curLoop[i] = false;
250                                                                 }
251                                                         } else if ( pass ) {
252                                                                 result.push( item );
253                                                                 anyFound = true;
254                                                         }
255                                                 }
256                                         }
257                                 }
258
259                                 if ( found !== undefined ) {
260                                         if ( !inplace ) {
261                                                 curLoop = result;
262                                         }
263
264                                         expr = expr.replace( Expr.match[ type ], "" );
265
266                                         if ( !anyFound ) {
267                                                 return [];
268                                         }
269
270                                         break;
271                                 }
272                         }
273                 }
274
275                 // Improper expression
276                 if ( expr === old ) {
277                         if ( anyFound == null ) {
278                                 Sizzle.error( expr );
279                         } else {
280                                 break;
281                         }
282                 }
283
284                 old = expr;
285         }
286
287         return curLoop;
288 };
289
290 Sizzle.error = function( msg ) {
291         throw "Syntax error, unrecognized expression: " + msg;
292 };
293
294 var Expr = Sizzle.selectors = {
295         order: [ "ID", "NAME", "TAG" ],
296         match: {
297                 ID: /#((?:[\w\u00c0-\uFFFF\-]|\\.)+)/,
298                 CLASS: /\.((?:[\w\u00c0-\uFFFF\-]|\\.)+)/,
299                 NAME: /\[name=['"]*((?:[\w\u00c0-\uFFFF\-]|\\.)+)['"]*\]/,
300                 ATTR: /\[\s*((?:[\w\u00c0-\uFFFF\-]|\\.)+)\s*(?:(\S?=)\s*(['"]*)(.*?)\3|)\s*\]/,
301                 TAG: /^((?:[\w\u00c0-\uFFFF\*\-]|\\.)+)/,
302                 CHILD: /:(only|nth|last|first)-child(?:\((even|odd|[\dn+\-]*)\))?/,
303                 POS: /:(nth|eq|gt|lt|first|last|even|odd)(?:\((\d*)\))?(?=[^\-]|$)/,
304                 PSEUDO: /:((?:[\w\u00c0-\uFFFF\-]|\\.)+)(?:\((['"]?)((?:\([^\)]+\)|[^\(\)]*)+)\2\))?/
305         },
306         leftMatch: {},
307         attrMap: {
308                 "class": "className",
309                 "for": "htmlFor"
310         },
311         attrHandle: {
312                 href: function(elem){
313                         return elem.getAttribute("href");
314                 }
315         },
316         relative: {
317                 "+": function(checkSet, part){
318                         var isPartStr = typeof part === "string",
319                                 isTag = isPartStr && !/\W/.test(part),
320                                 isPartStrNotTag = isPartStr && !isTag;
321
322                         if ( isTag ) {
323                                 part = part.toLowerCase();
324                         }
325
326                         for ( var i = 0, l = checkSet.length, elem; i < l; i++ ) {
327                                 if ( (elem = checkSet[i]) ) {
328                                         while ( (elem = elem.previousSibling) && elem.nodeType !== 1 ) {}
329
330                                         checkSet[i] = isPartStrNotTag || elem && elem.nodeName.toLowerCase() === part ?
331                                                 elem || false :
332                                                 elem === part;
333                                 }
334                         }
335
336                         if ( isPartStrNotTag ) {
337                                 Sizzle.filter( part, checkSet, true );
338                         }
339                 },
340                 ">": function(checkSet, part){
341                         var isPartStr = typeof part === "string",
342                                 elem, i = 0, l = checkSet.length;
343
344                         if ( isPartStr && !/\W/.test(part) ) {
345                                 part = part.toLowerCase();
346
347                                 for ( ; i < l; i++ ) {
348                                         elem = checkSet[i];
349                                         if ( elem ) {
350                                                 var parent = elem.parentNode;
351                                                 checkSet[i] = parent.nodeName.toLowerCase() === part ? parent : false;
352                                         }
353                                 }
354                         } else {
355                                 for ( ; i < l; i++ ) {
356                                         elem = checkSet[i];
357                                         if ( elem ) {
358                                                 checkSet[i] = isPartStr ?
359                                                         elem.parentNode :
360                                                         elem.parentNode === part;
361                                         }
362                                 }
363
364                                 if ( isPartStr ) {
365                                         Sizzle.filter( part, checkSet, true );
366                                 }
367                         }
368                 },
369                 "": function(checkSet, part, isXML){
370                         var doneName = done++, checkFn = dirCheck, nodeCheck;
371
372                         if ( typeof part === "string" && !/\W/.test(part) ) {
373                                 part = part.toLowerCase();
374                                 nodeCheck = part;
375                                 checkFn = dirNodeCheck;
376                         }
377
378                         checkFn("parentNode", part, doneName, checkSet, nodeCheck, isXML);
379                 },
380                 "~": function(checkSet, part, isXML){
381                         var doneName = done++, checkFn = dirCheck, nodeCheck;
382
383                         if ( typeof part === "string" && !/\W/.test(part) ) {
384                                 part = part.toLowerCase();
385                                 nodeCheck = part;
386                                 checkFn = dirNodeCheck;
387                         }
388
389                         checkFn("previousSibling", part, doneName, checkSet, nodeCheck, isXML);
390                 }
391         },
392         find: {
393                 ID: function(match, context, isXML){
394                         if ( typeof context.getElementById !== "undefined" && !isXML ) {
395                                 var m = context.getElementById(match[1]);
396                                 return m ? [m] : [];
397                         }
398                 },
399                 NAME: function(match, context){
400                         if ( typeof context.getElementsByName !== "undefined" ) {
401                                 var ret = [], results = context.getElementsByName(match[1]);
402
403                                 for ( var i = 0, l = results.length; i < l; i++ ) {
404                                         if ( results[i].getAttribute("name") === match[1] ) {
405                                                 ret.push( results[i] );
406                                         }
407                                 }
408
409                                 return ret.length === 0 ? null : ret;
410                         }
411                 },
412                 TAG: function(match, context){
413                         return context.getElementsByTagName(match[1]);
414                 }
415         },
416         preFilter: {
417                 CLASS: function(match, curLoop, inplace, result, not, isXML){
418                         match = " " + match[1].replace(/\\/g, "") + " ";
419
420                         if ( isXML ) {
421                                 return match;
422                         }
423
424                         for ( var i = 0, elem; (elem = curLoop[i]) != null; i++ ) {
425                                 if ( elem ) {
426                                         if ( not ^ (elem.className && (" " + elem.className + " ").replace(/[\t\n]/g, " ").indexOf(match) >= 0) ) {
427                                                 if ( !inplace ) {
428                                                         result.push( elem );
429                                                 }
430                                         } else if ( inplace ) {
431                                                 curLoop[i] = false;
432                                         }
433                                 }
434                         }
435
436                         return false;
437                 },
438                 ID: function(match){
439                         return match[1].replace(/\\/g, "");
440                 },
441                 TAG: function(match, curLoop){
442                         return match[1].toLowerCase();
443                 },
444                 CHILD: function(match){
445                         if ( match[1] === "nth" ) {
446                                 // parse equations like 'even', 'odd', '5', '2n', '3n+2', '4n-1', '-n+6'
447                                 var test = /(-?)(\d*)n((?:\+|-)?\d*)/.exec(
448                                         match[2] === "even" && "2n" || match[2] === "odd" && "2n+1" ||
449                                         !/\D/.test( match[2] ) && "0n+" + match[2] || match[2]);
450
451                                 // calculate the numbers (first)n+(last) including if they are negative
452                                 match[2] = (test[1] + (test[2] || 1)) - 0;
453                                 match[3] = test[3] - 0;
454                         }
455
456                         // TODO: Move to normal caching system
457                         match[0] = done++;
458
459                         return match;
460                 },
461                 ATTR: function(match, curLoop, inplace, result, not, isXML){
462                         var name = match[1].replace(/\\/g, "");
463                         
464                         if ( !isXML && Expr.attrMap[name] ) {
465                                 match[1] = Expr.attrMap[name];
466                         }
467
468                         if ( match[2] === "~=" ) {
469                                 match[4] = " " + match[4] + " ";
470                         }
471
472                         return match;
473                 },
474                 PSEUDO: function(match, curLoop, inplace, result, not){
475                         if ( match[1] === "not" ) {
476                                 // If we're dealing with a complex expression, or a simple one
477                                 if ( ( chunker.exec(match[3]) || "" ).length > 1 || /^\w/.test(match[3]) ) {
478                                         match[3] = Sizzle(match[3], null, null, curLoop);
479                                 } else {
480                                         var ret = Sizzle.filter(match[3], curLoop, inplace, true ^ not);
481                                         if ( !inplace ) {
482                                                 result.push.apply( result, ret );
483                                         }
484                                         return false;
485                                 }
486                         } else if ( Expr.match.POS.test( match[0] ) || Expr.match.CHILD.test( match[0] ) ) {
487                                 return true;
488                         }
489                         
490                         return match;
491                 },
492                 POS: function(match){
493                         match.unshift( true );
494                         return match;
495                 }
496         },
497         filters: {
498                 enabled: function(elem){
499                         return elem.disabled === false && elem.type !== "hidden";
500                 },
501                 disabled: function(elem){
502                         return elem.disabled === true;
503                 },
504                 checked: function(elem){
505                         return elem.checked === true;
506                 },
507                 selected: function(elem){
508                         // Accessing this property makes selected-by-default
509                         // options in Safari work properly
510                         elem.parentNode.selectedIndex;
511                         return elem.selected === true;
512                 },
513                 parent: function(elem){
514                         return !!elem.firstChild;
515                 },
516                 empty: function(elem){
517                         return !elem.firstChild;
518                 },
519                 has: function(elem, i, match){
520                         return !!Sizzle( match[3], elem ).length;
521                 },
522                 header: function(elem){
523                         return (/h\d/i).test( elem.nodeName );
524                 },
525                 text: function(elem){
526                         return "text" === elem.type;
527                 },
528                 radio: function(elem){
529                         return "radio" === elem.type;
530                 },
531                 checkbox: function(elem){
532                         return "checkbox" === elem.type;
533                 },
534                 file: function(elem){
535                         return "file" === elem.type;
536                 },
537                 password: function(elem){
538                         return "password" === elem.type;
539                 },
540                 submit: function(elem){
541                         return "submit" === elem.type;
542                 },
543                 image: function(elem){
544                         return "image" === elem.type;
545                 },
546                 reset: function(elem){
547                         return "reset" === elem.type;
548                 },
549                 button: function(elem){
550                         return "button" === elem.type || elem.nodeName.toLowerCase() === "button";
551                 },
552                 input: function(elem){
553                         return (/input|select|textarea|button/i).test(elem.nodeName);
554                 }
555         },
556         setFilters: {
557                 first: function(elem, i){
558                         return i === 0;
559                 },
560                 last: function(elem, i, match, array){
561                         return i === array.length - 1;
562                 },
563                 even: function(elem, i){
564                         return i % 2 === 0;
565                 },
566                 odd: function(elem, i){
567                         return i % 2 === 1;
568                 },
569                 lt: function(elem, i, match){
570                         return i < match[3] - 0;
571                 },
572                 gt: function(elem, i, match){
573                         return i > match[3] - 0;
574                 },
575                 nth: function(elem, i, match){
576                         return match[3] - 0 === i;
577                 },
578                 eq: function(elem, i, match){
579                         return match[3] - 0 === i;
580                 }
581         },
582         filter: {
583                 PSEUDO: function(elem, match, i, array){
584                         var name = match[1], filter = Expr.filters[ name ];
585
586                         if ( filter ) {
587                                 return filter( elem, i, match, array );
588                         } else if ( name === "contains" ) {
589                                 return (elem.textContent || elem.innerText || Sizzle.getText([ elem ]) || "").indexOf(match[3]) >= 0;
590                         } else if ( name === "not" ) {
591                                 var not = match[3];
592
593                                 for ( var j = 0, l = not.length; j < l; j++ ) {
594                                         if ( not[j] === elem ) {
595                                                 return false;
596                                         }
597                                 }
598
599                                 return true;
600                         } else {
601                                 Sizzle.error( "Syntax error, unrecognized expression: " + name );
602                         }
603                 },
604                 CHILD: function(elem, match){
605                         var type = match[1], node = elem;
606                         switch (type) {
607                                 case 'only':
608                                 case 'first':
609                                         while ( (node = node.previousSibling) )  {
610                                                 if ( node.nodeType === 1 ) { 
611                                                         return false; 
612                                                 }
613                                         }
614                                         if ( type === "first" ) { 
615                                                 return true; 
616                                         }
617                                         node = elem;
618                                 case 'last':
619                                         while ( (node = node.nextSibling) )      {
620                                                 if ( node.nodeType === 1 ) { 
621                                                         return false; 
622                                                 }
623                                         }
624                                         return true;
625                                 case 'nth':
626                                         var first = match[2], last = match[3];
627
628                                         if ( first === 1 && last === 0 ) {
629                                                 return true;
630                                         }
631                                         
632                                         var doneName = match[0],
633                                                 parent = elem.parentNode;
634         
635                                         if ( parent && (parent.sizcache !== doneName || !elem.nodeIndex) ) {
636                                                 var count = 0;
637                                                 for ( node = parent.firstChild; node; node = node.nextSibling ) {
638                                                         if ( node.nodeType === 1 ) {
639                                                                 node.nodeIndex = ++count;
640                                                         }
641                                                 } 
642                                                 parent.sizcache = doneName;
643                                         }
644                                         
645                                         var diff = elem.nodeIndex - last;
646                                         if ( first === 0 ) {
647                                                 return diff === 0;
648                                         } else {
649                                                 return ( diff % first === 0 && diff / first >= 0 );
650                                         }
651                         }
652                 },
653                 ID: function(elem, match){
654                         return elem.nodeType === 1 && elem.getAttribute("id") === match;
655                 },
656                 TAG: function(elem, match){
657                         return (match === "*" && elem.nodeType === 1) || elem.nodeName.toLowerCase() === match;
658                 },
659                 CLASS: function(elem, match){
660                         return (" " + (elem.className || elem.getAttribute("class")) + " ")
661                                 .indexOf( match ) > -1;
662                 },
663                 ATTR: function(elem, match){
664                         var name = match[1],
665                                 result = Expr.attrHandle[ name ] ?
666                                         Expr.attrHandle[ name ]( elem ) :
667                                         elem[ name ] != null ?
668                                                 elem[ name ] :
669                                                 elem.getAttribute( name ),
670                                 value = result + "",
671                                 type = match[2],
672                                 check = match[4];
673
674                         return result == null ?
675                                 type === "!=" :
676                                 type === "=" ?
677                                 value === check :
678                                 type === "*=" ?
679                                 value.indexOf(check) >= 0 :
680                                 type === "~=" ?
681                                 (" " + value + " ").indexOf(check) >= 0 :
682                                 !check ?
683                                 value && result !== false :
684                                 type === "!=" ?
685                                 value !== check :
686                                 type === "^=" ?
687                                 value.indexOf(check) === 0 :
688                                 type === "$=" ?
689                                 value.substr(value.length - check.length) === check :
690                                 type === "|=" ?
691                                 value === check || value.substr(0, check.length + 1) === check + "-" :
692                                 false;
693                 },
694                 POS: function(elem, match, i, array){
695                         var name = match[2], filter = Expr.setFilters[ name ];
696
697                         if ( filter ) {
698                                 return filter( elem, i, match, array );
699                         }
700                 }
701         }
702 };
703
704 var origPOS = Expr.match.POS,
705         fescape = function(all, num){
706                 return "\\" + (num - 0 + 1);
707         };
708
709 for ( var type in Expr.match ) {
710         Expr.match[ type ] = new RegExp( Expr.match[ type ].source + (/(?![^\[]*\])(?![^\(]*\))/.source) );
711         Expr.leftMatch[ type ] = new RegExp( /(^(?:.|\r|\n)*?)/.source + Expr.match[ type ].source.replace(/\\(\d+)/g, fescape) );
712 }
713
714 var makeArray = function(array, results) {
715         array = Array.prototype.slice.call( array, 0 );
716
717         if ( results ) {
718                 results.push.apply( results, array );
719                 return results;
720         }
721         
722         return array;
723 };
724
725 // Perform a simple check to determine if the browser is capable of
726 // converting a NodeList to an array using builtin methods.
727 // Also verifies that the returned array holds DOM nodes
728 // (which is not the case in the Blackberry browser)
729 try {
730         Array.prototype.slice.call( document.documentElement.childNodes, 0 )[0].nodeType;
731
732 // Provide a fallback method if it does not work
733 } catch(e){
734         makeArray = function(array, results) {
735                 var ret = results || [], i = 0;
736
737                 if ( toString.call(array) === "[object Array]" ) {
738                         Array.prototype.push.apply( ret, array );
739                 } else {
740                         if ( typeof array.length === "number" ) {
741                                 for ( var l = array.length; i < l; i++ ) {
742                                         ret.push( array[i] );
743                                 }
744                         } else {
745                                 for ( ; array[i]; i++ ) {
746                                         ret.push( array[i] );
747                                 }
748                         }
749                 }
750
751                 return ret;
752         };
753 }
754
755 var sortOrder;
756
757 if ( document.documentElement.compareDocumentPosition ) {
758         sortOrder = function( a, b ) {
759                 if ( !a.compareDocumentPosition || !b.compareDocumentPosition ) {
760                         if ( a == b ) {
761                                 hasDuplicate = true;
762                         }
763                         return a.compareDocumentPosition ? -1 : 1;
764                 }
765
766                 var ret = a.compareDocumentPosition(b) & 4 ? -1 : a === b ? 0 : 1;
767                 if ( ret === 0 ) {
768                         hasDuplicate = true;
769                 }
770                 return ret;
771         };
772 } else if ( "sourceIndex" in document.documentElement ) {
773         sortOrder = function( a, b ) {
774                 if ( !a.sourceIndex || !b.sourceIndex ) {
775                         if ( a == b ) {
776                                 hasDuplicate = true;
777                         }
778                         return a.sourceIndex ? -1 : 1;
779                 }
780
781                 var ret = a.sourceIndex - b.sourceIndex;
782                 if ( ret === 0 ) {
783                         hasDuplicate = true;
784                 }
785                 return ret;
786         };
787 } else if ( document.createRange ) {
788         sortOrder = function( a, b ) {
789                 if ( !a.ownerDocument || !b.ownerDocument ) {
790                         if ( a == b ) {
791                                 hasDuplicate = true;
792                         }
793                         return a.ownerDocument ? -1 : 1;
794                 }
795
796                 var aRange = a.ownerDocument.createRange(), bRange = b.ownerDocument.createRange();
797                 aRange.setStart(a, 0);
798                 aRange.setEnd(a, 0);
799                 bRange.setStart(b, 0);
800                 bRange.setEnd(b, 0);
801                 var ret = aRange.compareBoundaryPoints(Range.START_TO_END, bRange);
802                 if ( ret === 0 ) {
803                         hasDuplicate = true;
804                 }
805                 return ret;
806         };
807 }
808
809 // Utility function for retreiving the text value of an array of DOM nodes
810 Sizzle.getText = function( elems ) {
811         var ret = "", elem;
812
813         for ( var i = 0; elems[i]; i++ ) {
814                 elem = elems[i];
815
816                 // Get the text from text nodes and CDATA nodes
817                 if ( elem.nodeType === 3 || elem.nodeType === 4 ) {
818                         ret += elem.nodeValue;
819
820                 // Traverse everything else, except comment nodes
821                 } else if ( elem.nodeType !== 8 ) {
822                         ret += Sizzle.getText( elem.childNodes );
823                 }
824         }
825
826         return ret;
827 };
828
829 // Check to see if the browser returns elements by name when
830 // querying by getElementById (and provide a workaround)
831 (function(){
832         // We're going to inject a fake input element with a specified name
833         var form = document.createElement("div"),
834                 id = "script" + (new Date()).getTime();
835         form.innerHTML = "<a name='" + id + "'/>";
836
837         // Inject it into the root element, check its status, and remove it quickly
838         var root = document.documentElement;
839         root.insertBefore( form, root.firstChild );
840
841         // The workaround has to do additional checks after a getElementById
842         // Which slows things down for other browsers (hence the branching)
843         if ( document.getElementById( id ) ) {
844                 Expr.find.ID = function(match, context, isXML){
845                         if ( typeof context.getElementById !== "undefined" && !isXML ) {
846                                 var m = context.getElementById(match[1]);
847                                 return m ? m.id === match[1] || typeof m.getAttributeNode !== "undefined" && m.getAttributeNode("id").nodeValue === match[1] ? [m] : undefined : [];
848                         }
849                 };
850
851                 Expr.filter.ID = function(elem, match){
852                         var node = typeof elem.getAttributeNode !== "undefined" && elem.getAttributeNode("id");
853                         return elem.nodeType === 1 && node && node.nodeValue === match;
854                 };
855         }
856
857         root.removeChild( form );
858         root = form = null; // release memory in IE
859 })();
860
861 (function(){
862         // Check to see if the browser returns only elements
863         // when doing getElementsByTagName("*")
864
865         // Create a fake element
866         var div = document.createElement("div");
867         div.appendChild( document.createComment("") );
868
869         // Make sure no comments are found
870         if ( div.getElementsByTagName("*").length > 0 ) {
871                 Expr.find.TAG = function(match, context){
872                         var results = context.getElementsByTagName(match[1]);
873
874                         // Filter out possible comments
875                         if ( match[1] === "*" ) {
876                                 var tmp = [];
877
878                                 for ( var i = 0; results[i]; i++ ) {
879                                         if ( results[i].nodeType === 1 ) {
880                                                 tmp.push( results[i] );
881                                         }
882                                 }
883
884                                 results = tmp;
885                         }
886
887                         return results;
888                 };
889         }
890
891         // Check to see if an attribute returns normalized href attributes
892         div.innerHTML = "<a href='#'></a>";
893         if ( div.firstChild && typeof div.firstChild.getAttribute !== "undefined" &&
894                         div.firstChild.getAttribute("href") !== "#" ) {
895                 Expr.attrHandle.href = function(elem){
896                         return elem.getAttribute("href", 2);
897                 };
898         }
899
900         div = null; // release memory in IE
901 })();
902
903 if ( document.querySelectorAll ) {
904         (function(){
905                 var oldSizzle = Sizzle, div = document.createElement("div");
906                 div.innerHTML = "<p class='TEST'></p>";
907
908                 // Safari can't handle uppercase or unicode characters when
909                 // in quirks mode.
910                 if ( div.querySelectorAll && div.querySelectorAll(".TEST").length === 0 ) {
911                         return;
912                 }
913         
914                 Sizzle = function(query, context, extra, seed){
915                         context = context || document;
916
917                         // Only use querySelectorAll on non-XML documents
918                         // (ID selectors don't work in non-HTML documents)
919                         if ( !seed && context.nodeType === 9 && !Sizzle.isXML(context) ) {
920                                 try {
921                                         return makeArray( context.querySelectorAll(query), extra );
922                                 } catch(e){}
923                         }
924                 
925                         return oldSizzle(query, context, extra, seed);
926                 };
927
928                 for ( var prop in oldSizzle ) {
929                         Sizzle[ prop ] = oldSizzle[ prop ];
930                 }
931
932                 div = null; // release memory in IE
933         })();
934 }
935
936 (function(){
937         var div = document.createElement("div");
938
939         div.innerHTML = "<div class='test e'></div><div class='test'></div>";
940
941         // Opera can't find a second classname (in 9.6)
942         // Also, make sure that getElementsByClassName actually exists
943         if ( !div.getElementsByClassName || div.getElementsByClassName("e").length === 0 ) {
944                 return;
945         }
946
947         // Safari caches class attributes, doesn't catch changes (in 3.2)
948         div.lastChild.className = "e";
949
950         if ( div.getElementsByClassName("e").length === 1 ) {
951                 return;
952         }
953         
954         Expr.order.splice(1, 0, "CLASS");
955         Expr.find.CLASS = function(match, context, isXML) {
956                 if ( typeof context.getElementsByClassName !== "undefined" && !isXML ) {
957                         return context.getElementsByClassName(match[1]);
958                 }
959         };
960
961         div = null; // release memory in IE
962 })();
963
964 function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
965         for ( var i = 0, l = checkSet.length; i < l; i++ ) {
966                 var elem = checkSet[i];
967                 if ( elem ) {
968                         elem = elem[dir];
969                         var match = false;
970
971                         while ( elem ) {
972                                 if ( elem.sizcache === doneName ) {
973                                         match = checkSet[elem.sizset];
974                                         break;
975                                 }
976
977                                 if ( elem.nodeType === 1 && !isXML ){
978                                         elem.sizcache = doneName;
979                                         elem.sizset = i;
980                                 }
981
982                                 if ( elem.nodeName.toLowerCase() === cur ) {
983                                         match = elem;
984                                         break;
985                                 }
986
987                                 elem = elem[dir];
988                         }
989
990                         checkSet[i] = match;
991                 }
992         }
993 }
994
995 function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
996         for ( var i = 0, l = checkSet.length; i < l; i++ ) {
997                 var elem = checkSet[i];
998                 if ( elem ) {
999                         elem = elem[dir];
1000                         var match = false;
1001
1002                         while ( elem ) {
1003                                 if ( elem.sizcache === doneName ) {
1004                                         match = checkSet[elem.sizset];
1005                                         break;
1006                                 }
1007
1008                                 if ( elem.nodeType === 1 ) {
1009                                         if ( !isXML ) {
1010                                                 elem.sizcache = doneName;
1011                                                 elem.sizset = i;
1012                                         }
1013                                         if ( typeof cur !== "string" ) {
1014                                                 if ( elem === cur ) {
1015                                                         match = true;
1016                                                         break;
1017                                                 }
1018
1019                                         } else if ( Sizzle.filter( cur, [elem] ).length > 0 ) {
1020                                                 match = elem;
1021                                                 break;
1022                                         }
1023                                 }
1024
1025                                 elem = elem[dir];
1026                         }
1027
1028                         checkSet[i] = match;
1029                 }
1030         }
1031 }
1032
1033 Sizzle.contains = document.compareDocumentPosition ? function(a, b){
1034         return !!(a.compareDocumentPosition(b) & 16);
1035 } : function(a, b){
1036         return a !== b && (a.contains ? a.contains(b) : true);
1037 };
1038
1039 Sizzle.isXML = function(elem){
1040         // documentElement is verified for cases where it doesn't yet exist
1041         // (such as loading iframes in IE - #4833) 
1042         var documentElement = (elem ? elem.ownerDocument || elem : 0).documentElement;
1043         return documentElement ? documentElement.nodeName !== "HTML" : false;
1044 };
1045
1046 var posProcess = function(selector, context){
1047         var tmpSet = [], later = "", match,
1048                 root = context.nodeType ? [context] : context;
1049
1050         // Position selectors must be done after the filter
1051         // And so must :not(positional) so we move all PSEUDOs to the end
1052         while ( (match = Expr.match.PSEUDO.exec( selector )) ) {
1053                 later += match[0];
1054                 selector = selector.replace( Expr.match.PSEUDO, "" );
1055         }
1056
1057         selector = Expr.relative[selector] ? selector + "*" : selector;
1058
1059         for ( var i = 0, l = root.length; i < l; i++ ) {
1060                 Sizzle( selector, root[i], tmpSet );
1061         }
1062
1063         return Sizzle.filter( later, tmpSet );
1064 };
1065
1066 // EXPOSE
1067
1068 window.tinymce.dom.Sizzle = Sizzle;
1069
1070 })();
1071
1072 // #endif