2 * XMLWrapper_find 周りの オリジナルコードに関する情報
\r
3 * Original code by pettanR team
\r
4 * - https://osdn.jp/projects/pettanr/scm/git/clientJs/blobs/master/0.6.x/js/02_dom/08_XNodeSelector.js
\r
6 * Original code by ofk ( kQuery, ksk )
\r
7 * - http://d.hatena.ne.jp/ofk/comment/20090106/1231258010
\r
8 * - http://d.hatena.ne.jp/ofk/20090111/1231668170
\r
10 * TODO X.Class で作り、kill を強要する
\r
13 X[ 'XML' ] = XMLWrapper;
\r
18 * @class XML 探索用のラッパークラスです
\r
22 function XMLWrapper( xml ){
\r
26 XMLWrapper.prototype.length = 1;
\r
27 XMLWrapper.prototype[ 'parent' ] = XMLWrapper_parent;
\r
28 XMLWrapper.prototype[ 'has' ] = XMLWrapper_has;
\r
29 XMLWrapper.prototype[ 'get' ] = XMLWrapper_get;
\r
30 XMLWrapper.prototype[ 'val' ] = XMLWrapper_val;
\r
31 XMLWrapper.prototype[ 'find' ] = XMLWrapper_find;
\r
33 function XMLWrapper_parent(){
\r
34 if( this.length === 1 ) return this._rawXML && this._rawXML.parentNode ? new XMLWrapper( this._rawXML.parentNode ) : null;
\r
35 if( this.length === 0 ) return null;
\r
37 return this[ 0 ].parentNode ? ( new XMLWrapper( this[ 0 ].parentNode ) ) : null;
\r
40 function XMLWrapper_has( queryString ){
\r
41 return !!this.find( queryString ).length;
\r
44 function XMLWrapper_get( index ){
\r
45 if( this.length === 1 ) return this;
\r
46 if( this.length === 0 ) return null;
\r
47 // 一度発行した XMLWrapper は控えて置いて再発行する。
\r
48 if( this._wraps && this._wraps[ index ] ) return this._wraps[ index ];
\r
49 if( !this._wraps ) this._wraps = [];
\r
50 return this[ index ] ?
\r
51 ( this._wraps[ index ] = new XMLWrapper( this[ index ] ) ) :
\r
55 function XMLWrapper_val( queryString, type ){
\r
56 var //attr_textContent = X_UA[ 'IE' ] < 9 || X_UA[ 'Opera' ] ? 'innerText' : X_UA[ 'IE9' ] ? 'text' : 'textContent',
\r
59 switch( queryString ){
\r
69 wrapper = queryString ? this.find( queryString ) : this;
\r
70 xml = wrapper.length === 1 ? wrapper._rawXML : wrapper[ 0 ];
\r
86 v = xml.nodeType === 1 ? xml.innerText || xml.text || xml.textContent : xml.nodeValue;
\r
90 return parseFloat( v );
\r
92 return parseFloat( v ) | 0;
\r
94 return !!X_String_parse( v );
\r
101 function XMLWrapper_find( queryString ){
\r
103 var scope = this.constructor === XMLListWrapper ? this : [ this._rawXML ],
\r
104 parents = scope, // 探索元の親要素 xmlList の場合あり
\r
105 ARY_PUSH = Array.prototype.push,
\r
108 isMulti = 1 < scope.length,// 要素をマージする必要がある
\r
111 isAll, isNot, hasRoot,
\r
114 merge, // 要素がコメントノードで汚染されている場合使う
\r
115 combinator, selector, name, tagName,
\r
116 uid, tmp, xml, filter, key, op, val, toLower, useName,
\r
117 links, className, attr, flag;
\r
120 if( !X_Type_isString( queryString ) ) return XMLListWrapper_0;
\r
125 for( ; queryString.length; ){
\r
126 //console.log( 'queryString[' + queryString + ']' );
\r
130 parsed = X_Node_Selector__parse( queryString );
\r
132 if( X_Type_isNumber( parsed ) ){
\r
134 return XMLListWrapper_0;
\r
137 queryString = queryString.substr( parsed[ 0 ] );
\r
138 parsed = parsed[ 1 ];
\r
140 if( parsed === 5 ){
\r
143 xmlList && xmlList.length && ARY_PUSH.apply( ret, xmlList );
\r
151 combinator = parsed[ 0 ];
\r
152 selector = parsed[ 1 ];
\r
153 name = parsed[ 2 ];
\r
154 tagName = selector === 1 ? name : '*';
\r
155 isAll = tagName === '*';
\r
158 if( !xmlList.length ){
\r
162 if( combinator !== 0 ){
\r
165 //console.log( 'cobinator !== 0 ' + parents.length + ' : ' + xmlList.length );
\r
170 l = parents.length;
\r
172 isMulti = isMulti || 1 < l;
\r
174 console.log( 'combinator ' + combinator );
\r
176 switch( combinator ){
\r
179 for( ; i < l; ++i ){
\r
180 for( xml = parents[ i ].firstChild; xml; xml = xml.nextSibling ){
\r
181 if( xml.nodeType === 1 && ( isAll || tagName === xml.tagName ) ) xmlList[ ++n ] = xml;
\r
187 for( ; i < l; ++i ){
\r
188 for( xml = parents[ i ].nextSibling; xml; xml = xml.nextSibling ){
\r
189 if( xml.nodeType === 1 ){
\r
190 if( isAll || tagName === xml.tagName ) xmlList[ ++n ] = xml;
\r
199 for( ; i < l; ++i ){
\r
200 for( xml = parents[ i ].nextSibling; xml; xml = xml.nextSibling ){
\r
201 if( xml.nodeType === 1 && ( isAll || tagName === xml.tagName ) ){
\r
202 if( merge.indexOf( xml ) !== -1 ){
\r
205 merge[ merge.length ] = xml;
\r
206 xmlList[ ++n ] = xml;
\r
217 for( ; i < l; ++i ){
\r
218 if( xml = parents[ i ].getAttributeNode( name ) ){
\r
219 xmlList[ ++n ] = xml;
\r
224 if( combinator === 1 || ( isStart && selector < 7 ) ){
\r
225 console.log( l + ' > ' + xmlList.length + ' tag:' + tagName );
\r
226 for( ; i < l; ++i ){
\r
227 xml = parents[ i ];
\r
228 xml.childNodes && xml.childNodes.length && XMLWrapper_fetchElements( xmlList, xml, isAll ? null : tagName );
\r
230 console.log( l + ' >> ' + xmlList.length + ' tag:' + tagName );
\r
236 //alert( 'pre-selector:' + ( xmlList && xmlList.length ) )
\r
238 switch( selector ){
\r
241 filter = [ 'id', 1, name ]; break;
\r
244 filter = [ 'class', 3 /*'~='*/, name ]; break;
\r
247 if( !( filter = XMLWrapper_filter[ name ] ) ){
\r
248 return XMLListWrapper_0;;
\r
253 filter = [ name, parsed[ 3 ], parsed[ 4 ] ]; break;
\r
257 parsed = parsed[ 2 ];
\r
258 name = parsed[ 2 ];
\r
259 switch( parsed[ 1 ] ) {
\r
261 filter = [ 'tag', 1, name ]; break;
\r
264 filter = [ 'id', 1, name ]; break;
\r
267 filter = [ 'class', 3, name ]; break;
\r
270 if( !( filter = X_Node_Selector__filter[ name ] ) ){
\r
276 filter = [ name, parsed[ 3 ], parsed[ 4 ] ]; break;
\r
281 xmlList = scope; break;
\r
285 xmlList = [ HTML ]; break;
\r
288 if( links = document.links ){
\r
289 for( xmlList = [], i = links.length; i; ){
\r
290 xmlList[ --i ] = new Node( links[ i ] );
\r
293 // area[href],a[href]
\r
297 if( filter && xmlList.length ){
\r
300 xmlList = filter.m(
\r
306 parsed[ 3 ], parsed[ 4 ]
\r
310 if( X_Type_isFunction( filter ) ){
\r
312 for( i = 0, n = -1; xml = xmlList[ i ]; ++i ){
\r
313 if( ( !!filter( xml ) ) ^ isNot ) tmp[ ++n ] = xml;
\r
324 if( op === 3 ) val = _ + val + _;
\r
326 for( i = 0, n = -1, l = xmlList.length; i < l; ++i ){
\r
327 xml = xmlList[ i ];
\r
328 attr = xml.getAttribute( key, 2 );
\r
329 flag = attr != null;// && ( !useName || attr !== '' );
\r
331 //if( toLower ) attr = attr.toLowerCase();
\r
335 flag = attr === val;
\r
338 flag = attr !== val;
\r
341 flag = ( _ + attr + _ ).indexOf( val ) !== -1;
\r
344 flag = attr.indexOf( val ) === 0;
\r
347 flag = attr.lastIndexOf( val ) + val.length === attr.length;
\r
350 flag = attr.indexOf( val ) !== -1;
\r
353 flag = attr === val || attr.substring( 0, val.length + 1 ) === val + '-';
\r
357 if( !!flag ^ isNot ) tmp[ ++n ] = xml;
\r
367 //console.log( '//end :' + ( xmlList && xmlList.length ) );
\r
369 //console.log( 'multi:' + ( xmlList && xmlList.length ) );
\r
371 // tree 順に並び替え、同一要素の排除
\r
373 xmlList && xmlList.length && ARY_PUSH.apply( ret, xmlList );
\r
375 if( l === 0 ) return XMLListWrapper_0;
\r
376 if( l === 1 ) return new XMLWrapper( ret[ 0 ] );
\r
380 for( i = 0, n = -1; i < l; ++i ){
\r
381 //alert( 'multi:' + i )
\r
383 if( xmlList.indexOf( xml ) === -1 ){
\r
384 //merge[ merge.length ] = xml;
\r
385 xmlList[ ++n ] = xml;
\r
388 XMLWrapper_sortElementOrder( ret = [], xmlList, this._rawXML.childNodes );
\r
391 for( i = 0, l = xmlList.length; i < l; ++i ){
\r
392 if( ret.indexOf( xml = xmlList[ i ] ) === -1 ){
\r
393 ret[ ret.length ] = xml;
\r
400 return xmlList.length === 1 ? new XMLWrapper( xmlList[ 0 ] ) : new XMLListWrapper( xmlList );
\r
403 function XMLWrapper_sortElementOrder( newList, list, xmlList ){
\r
404 var l = xmlList.length,
\r
406 j, child, _xmlList;
\r
407 for( ; i < l; ++i ){
\r
408 child = xmlList[ i ];
\r
409 //if( child.nodeType !== 1 ) continue;
\r
410 //console.log( child.tagName );
\r
411 if( ( j = list.indexOf( child ) ) !== -1 ){
\r
412 newList[ newList.length ] = child;
\r
413 list.splice( j, 1 );
\r
414 if( list.length === 1 ){
\r
415 newList[ newList.length ] = list[ 0 ];
\r
419 if( list.length === 0 ) return true;
\r
421 if( ( _xmlList = child.childNodes ) && XMLWrapper_sortElementOrder( newList, list, _xmlList ) ){
\r
427 function XMLWrapper_fetchElements( list, parent, tag ){
\r
428 var xmlList = parent.childNodes,
\r
429 l = xmlList.length,
\r
432 for( ; i < l; ++i ){
\r
433 child = xmlList[ i ];
\r
434 if( child.nodeType === 1 ){
\r
435 ( !tag || child.tagName === tag ) && ( list[ list.length ] = child );
\r
436 //console.log( parent.tagName + ' > ' + child.tagName + ' == ' + tag+ ' l:' + list.length );
\r
437 child.childNodes && child.childNodes.length && XMLWrapper_fetchElements( list, child, tag );
\r
442 function XMLWrapper_funcSelectorChild( type, flag_all, flags, xmlList ){
\r
444 flag_not = flags.not,
\r
445 i = 0, n = -1, xnode, node,
\r
447 for( ; xnode = xmlList[ i ]; ++i ){
\r
448 tagName = flag_all || xnode.tagName;
\r
450 if( /* tmp === null && */ type <= 0 ){
\r
451 for( node = xnode.previousSibling; node; node = node.previousSibling ){
\r
452 if( node.nodeType === 1 && ( flag_all || tagName === node.tagName ) ){
\r
458 if( tmp === null && 0 <= type ){
\r
459 for( node = xnode.nextSibling; node; node = node.nextSibling ){
\r
460 if( node.nodeType === 1 && ( flag_all || tagName === node.tagName ) ){
\r
466 if( tmp === null ) tmp = true;
\r
467 if( tmp ^ flag_not ) res[ ++n ] = xnode;
\r
471 function XMLWrapper_funcSelectorNth( pointer, sibling, flag_all, flags, xmlList, a, b ){
\r
474 flag_not = flags.not,
\r
475 i = 0, n = -1, uid,
\r
476 c, xnode, tmp, node, tagName;
\r
477 for( ; xnode = xmlList[ i ]; ++i ){
\r
479 tmp = checked[ uid ];
\r
480 if( tmp === undefined ){
\r
481 for( c = 0, node = xnode.parentNode[ pointer ], tagName = flag_all || xnode.tagName; node; node = node[ sibling ] ){
\r
482 if( node.nodeType === 1 && ( flag_all || tagName === node.tagName ) ){
\r
484 checked[ node._uid ] = a === 0 ? c === b : (c - b) % a === 0 && (c - b) / a >= 0;
\r
487 tmp = checked[ uid ];
\r
489 if( tmp ^ flag_not ) res[ ++n ] = xnode;
\r
494 function XMLWrapper_funcSelectorProp( prop, flag, flags, xmlList ){
\r
496 flag_not = flag ? flags.not : !flags.not,
\r
497 i = 0, n = -1, xnode;
\r
498 for( ; xnode = xmlList[ i ]; ++i ){
\r
499 if( xnode.getAttributeNode( prop ) ^ flag_not ) res[ ++n ] = xnode;
\r
504 var XMLWrapper_filter = {
\r
506 m : function( flags, xmlList ){ return XMLWrapper_funcSelectorChild( -1, true, flags, xmlList ); }
\r
509 m : function( flags, xmlList ){ return XMLWrapper_funcSelectorChild( 1, true, flags, xmlList ); }
\r
512 m : function( flags, xmlList ){ return XMLWrapper_funcSelectorChild( 0, true, flags, xmlList ); }
\r
514 'first-of-type' : {
\r
515 m : function( flags, xmlList ){ return XMLWrapper_funcSelectorChild( -1, false, flags, xmlList ); }
\r
518 m : function( flags, xmlList ){ return XMLWrapper_funcSelectorChild( 1, false, flags, xmlList ); }
\r
521 m : function( flags, xmlList ){ return XMLWrapper_funcSelectorChild( 0, false, flags, xmlList ); }
\r
524 m : function( flags, xmlList, a, b ){ return XMLWrapper_funcSelectorNth( 'firstChild', 'nextSibling', true, flags, xmlList, a, b ); }
\r
526 'nth-last-child' : {
\r
527 m : function( flags, xmlList, a, b ){ return XMLWrapper_funcSelectorNth( 'lastChild', 'previousSibling', true, flags, xmlList, a, b ); }
\r
530 m : function( flags, xmlList, a, b ){ return XMLWrapper_funcSelectorNth( 'firstChild', 'nextSibling', false, flags, xmlList, a, b ); }
\r
532 'nth-last-of-type' : {
\r
533 m : function( flags, xmlList, a, b ){ return XMLWrapper_funcSelectorNth( 'lastChild', 'previousSibling', false, flags, xmlList, a, b ); }
\r
536 m : function( flags, xmlList ){
\r
538 flag_not = flags.not,
\r
539 i = 0, n = -1, xnode, tmp, node;
\r
540 for( ; xnode = xmlList[i]; ++i ){
\r
542 for( node = xnode.firstChild; node; node = node.nextSibling ){
\r
543 if( node.nodeType === 1 || ( node.nodeType === 3 && node.nodeValue ) ){
\r
548 if( tmp ^ flag_not ) res[ ++n ] = xnode;
\r
554 m : function( flags, xmlList, arg ){
\r
556 flag_not = flags.not,
\r
557 i = 0, n = -1, xnode, text = '';
\r
559 for( ; xnode = xmlList[ i ]; ++i ){
\r
560 switch( xnode.nodeType ){
\r
562 text = xml.nodeType === 1 ? xml.innerText || xml.text || xml.textContent : xml.nodeValue;
\r
566 text = xnode.nodeValue;
\r
569 console.log( text + ' ' + arg );
\r
570 if ( ( -1 < text.indexOf( arg ) ) ^ flag_not ) res[ ++n ] = xnode;
\r
577 function XMLListWrapper( xmlList ){
\r
578 var i = 0, l = xmlList ? xmlList.length : 0;
\r
579 for( ; i < l; ++i ){
\r
580 this[ i ] = xmlList[ i ];
\r
585 var XMLListWrapper_0 = new XMLListWrapper();
\r
587 XMLListWrapper.prototype.length = 0;
\r
588 XMLListWrapper.prototype._wraps = null;
\r
589 XMLListWrapper.prototype[ 'parent' ] = XMLWrapper_parent;
\r
590 XMLListWrapper.prototype[ 'has' ] = XMLWrapper_has;
\r
591 XMLListWrapper.prototype[ 'get' ] = XMLWrapper_get;
\r
592 XMLListWrapper.prototype[ 'val' ] = XMLWrapper_val;
\r
593 XMLListWrapper.prototype[ 'find' ] = XMLWrapper_find;
\r