X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=0.6.x%2Fjs%2F05_util%2F04_XXML.js;h=79304335054b54279f26980d50cfcfb2b575e890;hb=bafa8683f87b2f909d1301fca80684bf9ff221ed;hp=4f7ab2aff5f9f674893917b954ca16c741d366b2;hpb=1ed2190f012cf923f7e48d3c0d122aa56c56c5f3;p=pettanr%2FclientJs.git diff --git a/0.6.x/js/05_util/04_XXML.js b/0.6.x/js/05_util/04_XXML.js index 4f7ab2a..7930433 100644 --- a/0.6.x/js/05_util/04_XXML.js +++ b/0.6.x/js/05_util/04_XXML.js @@ -1,7 +1,7 @@ /* * XMLWrapper_find 周りの オリジナルコードに関する情報 * Original code by pettanR team - * - http://sourceforge.jp/projects/pettanr/scm/git/clientJs/blobs/master/0.6.x/js/01_dom/18_XDomQuery.js + * - https://osdn.jp/projects/pettanr/scm/git/clientJs/blobs/master/0.6.x/js/02_dom/08_XNodeSelector.js * and * Original code by ofk ( kQuery, ksk ) * - http://d.hatena.ne.jp/ofk/comment/20090106/1231258010 @@ -9,30 +9,69 @@ * * TODO X.Class で作り、kill を強要する */ - X[ 'XML' ] = XMLWrapper; /** - * XML 探索用のラッパークラスです + * XML ツリーを探索して読み出す用のラッパークラスです.XML を操作する機能はありません、あしからず… * @alias X.XML * @class XML 探索用のラッパークラスです * @constructor - * @param {xml} + * @param {XMLElement} */ function XMLWrapper( xml ){ - this._rawXML = xml; + if( xml ){ + this._rawXML = xml; + this.tagName = xml.tagName; + }; }; -XMLWrapper.prototype.length = 1; -XMLWrapper.prototype[ 'has' ] = XMLWrapper_has; -XMLWrapper.prototype[ 'get' ] = XMLWrapper_get; -XMLWrapper.prototype[ 'val' ] = XMLWrapper_val; -XMLWrapper.prototype[ 'find' ] = XMLWrapper_find; +/** + * tagName または nodeName + * @alias X.XML.prototype.tagName + * @type {string} + */ +XMLWrapper.prototype.tagName = ''; +/** + * ラップした xml の数 常に1または0, XMLList の場合2以上 + * @alias X.XML.prototype.length + * @type {Number} + */ +XMLWrapper.prototype.length = 1; +XMLWrapper.prototype[ 'parent' ] = XMLWrapper_parent; +XMLWrapper.prototype[ 'has' ] = XMLWrapper_has; +XMLWrapper.prototype[ 'get' ] = XMLWrapper_get; +XMLWrapper.prototype[ 'val' ] = XMLWrapper_val; +XMLWrapper.prototype[ 'find' ] = XMLWrapper_find; + +/** + * 親要素を返す、ルート要素の場合 null を返す + * @alias X.XML.prototype.parent + * @return {X.XML} 親要素 + */ +function XMLWrapper_parent(){ + if( this.length === 1 ) return this._rawXML && this._rawXML.parentNode ? new XMLWrapper( this._rawXML.parentNode ) : null; + if( this.length === 0 ) return null; + + return this[ 0 ].parentNode ? ( new XMLWrapper( this[ 0 ].parentNode ) ) : null; +}; +/** + * セレクターにヒットした要素数を返す + * @alias X.XML.prototype.has + * @param {string} queryString XML セレクター文字列 + * @return {number} + */ function XMLWrapper_has( queryString ){ return !!this.find( queryString ).length; }; +/** + *

X.XML では常に自信を返す + *

X.XMLList ではラップした xml 群から index のものを返す + * @alias X.XML.prototype.get + * @param {number} index + * @return {X.XML} X.XML では自身、X.XMLList では index の X.XML + */ function XMLWrapper_get( index ){ if( this.length === 1 ) return this; if( this.length === 0 ) return null; @@ -44,8 +83,15 @@ function XMLWrapper_get( index ){ null; }; +/** + * セレクターにヒットした要素の内容を指定されたデータ型で返す。複数要素にヒットした場合、0番目の要素の内容を使用する。 + * @alias X.XML.prototype.val + * @param {string} queryString XML セレクター文字列 + * @param {string} type 'number','int','boolean','string' + * @return {boolean|number|string} 内容を型変換した値 + */ function XMLWrapper_val( queryString, type ){ - var attr_textContent = X_UA[ 'IE' ] < 9 || X_UA[ 'Opera' ] ? 'innerText' : X_UA[ 'IE9' ] ? 'text' : 'textContent', + var //attr_textContent = X_UA[ 'IE' ] < 9 || X_UA[ 'Opera' ] ? 'innerText' : X_UA[ 'IE9' ] ? 'text' : 'textContent', wrapper, xml, v; switch( queryString ){ @@ -55,7 +101,7 @@ function XMLWrapper_val( queryString, type ){ case 'string' : case undefined : type = queryString; - queryString = undefined; + queryString = 0; }; wrapper = queryString ? this.find( queryString ) : this; @@ -81,15 +127,21 @@ function XMLWrapper_val( queryString, type ){ case 'number' : return parseFloat( v ); case 'int' : - return parseInt( v ); + return parseFloat( v ) | 0; case 'boolean' : - return v && v !== '0' && v !== 'false' && v !== 'null' && v !== 'undefined' && v !== 'NaN'; + return !!X_String_parse( v ); //case 'string' : //default : }; return v || ''; }; +/** + * セレクターにヒットした要素を返す。0~1個の要素がヒットした場合は X.XML を、それ以上の場合は X.XMLList を返す。 + * @alias X.XML.prototype.find + * @param {string} queryString セレクター文字列 + * @return {X.XML|X.XMLList} + */ function XMLWrapper_find( queryString ){ var scope = this.constructor === XMLListWrapper ? this : [ this._rawXML ], @@ -163,7 +215,7 @@ function XMLWrapper_val( queryString, type ){ n = -1; isMulti = isMulti || 1 < l; - console.log( 'combinator ' + combinator ); + //console.log( 'combinator ' + combinator ); switch( combinator ){ // > TagName|* @@ -214,12 +266,12 @@ function XMLWrapper_val( queryString, type ){ break; default : if( combinator === 1 || ( isStart && selector < 7 ) ){ - console.log( l + ' > ' + xmlList.length + ' tag:' + tagName ); + //console.log( l + ' > ' + xmlList.length + ' tag:' + tagName ); for( ; i < l; ++i ){ xml = parents[ i ]; xml.childNodes && xml.childNodes.length && XMLWrapper_fetchElements( xmlList, xml, isAll ? null : tagName ); }; - console.log( l + ' >> ' + xmlList.length + ' tag:' + tagName ); + //console.log( l + ' >> ' + xmlList.length + ' tag:' + tagName ); }; }; @@ -237,7 +289,7 @@ function XMLWrapper_val( queryString, type ){ // :, 擬似クラス case 4 : if( !( filter = XMLWrapper_filter[ name ] ) ){ - return XMLListWrapper_0;; + return XMLListWrapper_0; }; break; // [] 属性 @@ -259,7 +311,7 @@ function XMLWrapper_val( queryString, type ){ filter = [ 'class', 3, name ]; break; // :, 擬似クラス case 4 : - if( !( filter = X_Node_Selector__filter[ name ] ) ){ + if( !( filter = XMLWrapper_filter[ name ] ) ){ return []; }; break; @@ -396,6 +448,7 @@ function XMLWrapper_val( queryString, type ){ var l = xmlList.length, i = 0, j, child, _xmlList; + for( ; i < l; ++i ){ child = xmlList[ i ]; //if( child.nodeType !== 1 ) continue; @@ -421,6 +474,7 @@ function XMLWrapper_val( queryString, type ){ l = xmlList.length, i = 0, child; + for( ; i < l; ++i ){ child = xmlList[ i ]; if( child.nodeType === 1 ){ @@ -434,13 +488,13 @@ function XMLWrapper_val( queryString, type ){ function XMLWrapper_funcSelectorChild( type, flag_all, flags, xmlList ){ var res = [], flag_not = flags.not, - i = 0, n = -1, xnode, node, + i = 0, n = -1, xml, node, tagName, tmp; - for( ; xnode = xmlList[ i ]; ++i ){ - tagName = flag_all || xnode.tagName; + for( ; xml = xmlList[ i ]; ++i ){ + tagName = flag_all || xml.tagName; tmp = null; if( /* tmp === null && */ type <= 0 ){ - for( node = xnode.previousSibling; node; node = node.previousSibling ){ + for( node = xml.previousSibling; node; node = node.previousSibling ){ if( node.nodeType === 1 && ( flag_all || tagName === node.tagName ) ){ tmp = false; break; @@ -448,7 +502,7 @@ function XMLWrapper_val( queryString, type ){ }; }; if( tmp === null && 0 <= type ){ - for( node = xnode.nextSibling; node; node = node.nextSibling ){ + for( node = xml.nextSibling; node; node = node.nextSibling ){ if( node.nodeType === 1 && ( flag_all || tagName === node.tagName ) ){ tmp = false; break; @@ -456,29 +510,32 @@ function XMLWrapper_val( queryString, type ){ }; }; if( tmp === null ) tmp = true; - if( tmp ^ flag_not ) res[ ++n ] = xnode; + if( tmp ^ flag_not ) res[ ++n ] = xml; }; return res; }; function XMLWrapper_funcSelectorNth( pointer, sibling, flag_all, flags, xmlList, a, b ){ - var res = [], + var uids = X_Array_copy( xmlList ), + res = [], checked = {}, flag_not = flags.not, - i = 0, n = -1, uid, - c, xnode, tmp, node, tagName; - for( ; xnode = xmlList[ i ]; ++i ){ - uid = xnode._uid; - tmp = checked[ uid ]; - if( tmp === void 0 ){ - for( c = 0, node = xnode.parentNode[ pointer ], tagName = flag_all || xnode.tagName; node; node = node[ sibling ] ){ + i = 0, n = -1, + c, xml, tmp, node, tagName, uid; + + for( ; xml = xmlList[ i ]; ++i ){ + tmp = checked[ i ]; + if( tmp === undefined ){ + for( c = 0, node = xml.parentNode[ pointer ], tagName = flag_all || xml.tagName; node; node = node[ sibling ] ){ if( node.nodeType === 1 && ( flag_all || tagName === node.tagName ) ){ ++c; - checked[ node._uid ] = a === 0 ? c === b : (c - b) % a === 0 && (c - b) / a >= 0; - }; + uid = uids.indexOf( node ); + if( uid === -1 ) uids[ uid = uids.length ] = node; + checked[ uid ] = a === 0 ? c === b : (c - b) % a === 0 && (c - b) / a >= 0; + }; }; - tmp = checked[ uid ]; + tmp = checked[ i ]; }; - if( tmp ^ flag_not ) res[ ++n ] = xnode; + if( tmp ^ flag_not ) res[ ++n ] = xml; }; return res; }; @@ -486,9 +543,9 @@ function XMLWrapper_val( queryString, type ){ function XMLWrapper_funcSelectorProp( prop, flag, flags, xmlList ){ var res = [], flag_not = flag ? flags.not : !flags.not, - i = 0, n = -1, xnode; - for( ; xnode = xmlList[ i ]; ++i ){ - if( xnode.getAttributeNode( prop ) ^ flag_not ) res[ ++n ] = xnode; + i = 0, n = -1, xml; + for( ; xml = xmlList[ i ]; ++i ){ + if( xml.getAttributeNode( prop ) ^ flag_not ) res[ ++n ] = xml; }; return res; }; */ @@ -528,16 +585,16 @@ var XMLWrapper_filter = { m : function( flags, xmlList ){ var res = [], flag_not = flags.not, - i = 0, n = -1, xnode, tmp, node; - for( ; xnode = xmlList[i]; ++i ){ + i = 0, n = -1, xml, tmp, node; + for( ; xml = xmlList[i]; ++i ){ tmp = true; - for( node = xnode.firstChild; node; node = node.nextSibling ){ - if( node.nodeType === 1 || ( node.nodeType === 3 && node._text ) ){ + for( node = xml.firstChild; node; node = node.nextSibling ){ + if( node.nodeType === 1 || ( node.nodeType === 3 && node.nodeValue ) ){ tmp = false; break; }; }; - if( tmp ^ flag_not ) res[ ++n ] = xnode; + if( tmp ^ flag_not ) res[ ++n ] = xml; }; return res; } @@ -546,26 +603,32 @@ var XMLWrapper_filter = { m : function( flags, xmlList, arg ){ var res = [], flag_not = flags.not, - i = 0, n = -1, xnode, text = ''; + i = 0, n = -1, xml, text = ''; - for( ; xnode = xmlList[ i ]; ++i ){ - switch( xnode.nodeType ){ + for( ; xml = xmlList[ i ]; ++i ){ + switch( xml.nodeType ){ case 1 : - text = xml.nodeType === 1 ? xml.innerText || xml.text || xml.textContent : xml.nodeValue; + text = xml.innerText || xml.text || xml.textContent; break; //case 2 : case 3 : - text = xnode.nodeValue; + text = xml.nodeValue; break; }; - console.log( text + ' ' + arg ); - if ( ( -1 < text.indexOf( arg ) ) ^ flag_not ) res[ ++n ] = xnode; + if ( ( -1 < text.indexOf( arg ) ) ^ flag_not ) res[ ++n ] = xml; }; return res; } } }; +/** + * XML配列を扱う XML 探索用のラッパークラスです + * @alias X.XMLList + * @class XMLList XML配列を扱う XML 探索用のラッパークラスです + * @constructor + * @extends {X.XML} + */ function XMLListWrapper( xmlList ){ var i = 0, l = xmlList ? xmlList.length : 0; for( ; i < l; ++i ){ @@ -576,9 +639,10 @@ function XMLListWrapper( xmlList ){ var XMLListWrapper_0 = new XMLListWrapper(); -XMLListWrapper.prototype.length = 0; -XMLListWrapper.prototype._wraps = null; -XMLListWrapper.prototype[ 'has' ] = XMLWrapper_has; -XMLListWrapper.prototype[ 'get' ] = XMLWrapper_get; -XMLListWrapper.prototype[ 'val' ] = XMLWrapper_val; -XMLListWrapper.prototype[ 'find' ] = XMLWrapper_find; +XMLListWrapper.prototype.length = 0; +XMLListWrapper.prototype._wraps = null; +XMLListWrapper.prototype[ 'parent' ] = XMLWrapper_parent; +XMLListWrapper.prototype[ 'has' ] = XMLWrapper_has; +XMLListWrapper.prototype[ 'get' ] = XMLWrapper_get; +XMLListWrapper.prototype[ 'val' ] = XMLWrapper_val; +XMLListWrapper.prototype[ 'find' ] = XMLWrapper_find;