X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=0.6.x%2Fjs%2F02_dom%2F08_XNodeSelector.js;h=1208076d2f473a3ee8cdfc6e4078d32cfb8d733b;hb=d93e0f2faddc687d56a9528402f74ba2415a0407;hp=578d9b2f8ff443e0c343787d4fb7ab076780ad9e;hpb=0231a4fe0d679b2959968193b0fbeebc44531aaf;p=pettanr%2FclientJs.git diff --git a/0.6.x/js/02_dom/08_XNodeSelector.js b/0.6.x/js/02_dom/08_XNodeSelector.js index 578d9b2..1208076 100644 --- a/0.6.x/js/02_dom/08_XNodeSelector.js +++ b/0.6.x/js/02_dom/08_XNodeSelector.js @@ -1,4 +1,4 @@ -/** +/* * Original code by ofk ( kQuery, ksk ) * http://d.hatena.ne.jp/ofk/comment/20090106/1231258010 * http://d.hatena.ne.jp/ofk/20090111/1231668170 @@ -56,9 +56,6 @@ var // TODO { a : 1, A : 2, _ : 3,,, } X_Node_Selector__ALPHABET = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-0123456789\\', X_Node_Selector__NUMBER = '+-0123456789'; - -// XMLWrapper のために今だけ外部に公開 -X_NodeSelector_parse = X_Node_Selector__parse; /* * セレクタ文字列の解析、但し一挙に行わず、ひと塊づつ @@ -81,10 +78,11 @@ function X_Node_Selector__parse( query, last ){ chr, chrCode, nameChr, name1st, tmp, escape, quot, start, name, key, value, operator, a, b, not; + query += ' '; while( i < l ){ chr = query.charAt( ++i ); - chrCode = ALPHABET.indexOf( chr ); + chrCode = ALPHABET.indexOf( chr ); // TODO この関数無くす! nameChr = chrCode !== -1; name1st = nameChr && chrCode < 52; switch( phase ){ @@ -198,7 +196,7 @@ function X_Node_Selector__parse( query, last ){ not = true; selector = 0; phase = 0x0; - name = null; + name = null; }; //continue; } else @@ -228,8 +226,32 @@ function X_Node_Selector__parse( query, last ){ return not ? [ i + tmp + 1, [ combinator, 6, result ] ] : [ i, result ]; }; - // セレクター - X[ 'Doc' ][ 'find' ] = X_shortcutFunction = Node.prototype[ 'find' ] = X_NodeList.prototype[ 'find' ] = function ( queryString ){ + /** + * selector を使って Node, NodeList を取得する + * @alias X.Doc.find + * @function + * @param {string} セレクター文字列 + * @return {Node|NodeList} + */ + X[ 'Doc' ][ 'find' ] = X_shortcutFunction = + + /** + * selector を使って Node, NodeList を取得する + * @alias NodeList.prototype.find + * @function + * @param {string} セレクター文字列 + * @return {Node|NodeList} + */ + X_NodeList.prototype[ 'find' ] = X_Node_find; + + /** + * selector を使って Node, NodeList を取得する + * @alias Node.prototype.find + * @function + * @param {string} セレクター文字列 + * @return {Node|NodeList} + */ + function X_Node_find( queryString ){ var HTML = X_Node_html, scope = this.constructor === X_NodeList && this.length ? this : [ this.constructor === Node ? this : X_Node_body ], parents = scope, // 探索元の親要素 XNodeList の場合あり @@ -259,7 +281,7 @@ function X_Node_Selector__parse( query, last ){ /*]@+debug*/ // 文字列以外は空で返す - if( typeof queryString !== 'string' ) return ret; + if( !X_Type_isString( queryString ) ) return ret; xnodes = []; @@ -271,7 +293,7 @@ function X_Node_Selector__parse( query, last ){ if( !parsed ){ parsed = X_Node_Selector__parse( queryString ); - if( typeof parsed === 'number' ){ + if( X_Type_isNumber( parsed ) ){ // error return []; }; @@ -396,10 +418,7 @@ function X_Node_Selector__parse( query, last ){ }; //console.log( l + ' > ' + xnodes.length + ' tag:' + tagName ); merge = {}; - for( ; i < l; ++i ){ - xnode = parents[ i ]; - xnode[ '_xnodes' ] && xnode[ '_xnodes' ].length && X_Node_Selector__fetchElements( xnodes, xnode, isAll ? '' : tagName, merge ); - }; + X_Node_Selector__fetchElements( xnodes, parents, isAll ? '' : tagName, merge ); //console.log( l + ' >> ' + xnodes.length + ' tag:' + tagName ); }; }; @@ -480,7 +499,7 @@ function X_Node_Selector__parse( query, last ){ ); } else // filterが関数の場合 - if( typeof filter === 'function' ){ + if( X_Type_isFunction( filter ) ){ tmp = []; for( i = 0, n = -1; xnode = xnodes[ i ]; ++i ){ if( ( !!filter( xnode ) ) ^ isNot ) tmp[ ++n ] = xnode; @@ -611,23 +630,69 @@ function X_Node_Selector__parse( query, last ){ }; }; }; - - function X_Node_Selector__fetchElements( list, parent, tag, merge ){ - var xnodes = parent[ '_xnodes' ], - l = xnodes.length, + + function X_Node_Selector__fetchElements( list, xnodes, tag, merge ){ + var l = xnodes.length, i = 0, - child, uid; + child, uid, _tag, _xnodes; for( ; i < l; ++i ){ child = xnodes[ i ]; uid = child[ '_uid' ]; - if( !merge[ uid ] && child[ '_tag' ] ){ + _tag = child[ '_tag' ]; + if( !merge[ uid ] && _tag ){ merge[ uid ] = true; - ( !tag || child[ '_tag' ] === tag ) && ( list[ list.length ] = child ); - child[ '_xnodes' ] && X_Node_Selector__fetchElements( list, child, tag, merge ); + ( !tag || tag === _tag ) && ( list[ list.length ] = child ); + if( ( _xnodes = child[ '_xnodes' ] ) && ( 1 < _xnodes.length || ( _xnodes[ 0 ] && _xnodes[ 0 ][ '_tag' ] ) ) ){ + X_Node_Selector__fetchElements( list, _xnodes, tag, merge ); + }; }; }; }; - +/* + function X_Node_Selector__fetchElements( list, parent, tag, merge ){ + + var xnodes = parent[ '_xnodes' ], + memory = { + i : 0, + l : xnodes.length, + xnodes : xnodes + }, + memories = [ memory ], + i, l, xnode, + uid, _xnodes; + + while( memories.length ){ + memory = memories.pop(); + xnodes = memory.xnodes; + i = memory.i; + l = memory.l; + for( ; i < l; ++i ){ + xnode = xnodes[ i ]; + uid = xnode[ '_uid' ]; + if( !merge[ uid ] && xnode[ '_tag' ] ){ + if( !tag || xnode[ '_tag' ] === tag ) list[ list.length ] = xnode; + + if( _xnodes = xnode[ '_xnodes' ] ){ + if( 1 < _xnodes.length || ( _xnodes[ 0 ] && _xnodes[ 0 ][ '_tag' ] ) ){ + memory.i = i + 1; + memory.l = l; + memory.xnodes = xnodes; + memories[ memories.length ] = memory; + memories[ memories.length ] = { + i : 0, + l : _xnodes.length, + xnodes : _xnodes + }; + merge[ uid ] = true; + break; + }; + }; + }; + merge[ uid ] = true; + }; + }; + }; + */ function X_Node_Selector__funcSelectorChild( type, flag_all, flags, xnodes ){ var res = [], flag_not = flags.not, @@ -658,8 +723,7 @@ function X_Node_Selector__parse( query, last ){ return res; }; function X_Node_Selector__funcSelectorNth( pointer, sibling, flag_all, flags, xnodes, a, b ){ - var _data = funcData, - res = [], + var res = [], checked = {}, flag_not = flags.not, i = 0, n = -1, uid,