2 * Node( rawElement | rawTextnode | htmlString | textString )
5 * @class Node HTMLElement、TextNode をラップし jQuery 風な API で操作できます。
7 * @extends {EventDispatcher}
9 var Node = X[ 'Node' ] = X_EventDispatcher[ 'inherits' ](
17 * @alias Node.prototype._uid
25 * @alias Node.prototype._flags
27 '_flags' : X_NodeFlags_DESTROYED,
29 '_rect' : null, // TODO
32 * 最後に計測したフォントサイズを保持している。ツリーが変更されると削除される。
35 * @alias Node.prototype._fontSize
40 * NodeList と動作を一致させるためのプロパティ。常に 1。
43 * @alias Node.prototype.length
50 * @alias Node.prototype.parent
52 parent : null, // remove された枝も親子構造は維持している。
58 * @alias Node.prototype._xnodes
63 * GPU レイヤーに転送されている場合、その一番親となっている Node。未実装。
66 * @alias Node.prototype._gpuParent
71 * タグ名。テキストノードの場合は空文字列。
74 * @alias Node.prototype._tag
79 * テキストコンテンツ。テキストノードで使用。
82 * @alias Node.prototype._text
90 * @alias Node.prototype._id
95 * クラス名。複数のクラスが設定されている場合、スペース区切り。
98 * @alias Node.prototype._className
100 '_className' : '', //
106 * @alias Node.prototype._attrs
108 '_attrs' : null, // see X_Node_Attr
114 * @alias Node.prototype._newAttrs
119 * 属性を文字列にしたもの。 color="red" size="8"
122 * @alias Node.prototype._attrText
130 * @alias Node.prototype._css
138 * @alias Node.prototype._cssText
146 * @alias Node.prototype._anime
151 * TODO Node の継承ができない!
153 'Constructor' : function( v ){
154 var uid = X_Node_CHASHE.length,
155 css, xnodes, xnode, parent;
157 if( X_Node_newByTag ){
158 X_Node_newByTag = false;
159 this[ '_tag' ] = v.toUpperCase();
160 arguments[ 1 ] && this[ 'attr' ]( arguments[ 1 ] );
161 css = arguments[ 2 ];
162 css && this[ X_Type_isString( css ) ? 'cssText' : 'css' ]( css );
164 if( X_Node_newByText ){
165 X_Node_newByText = false;
168 if( 1 < arguments.length ) return new X_NodeList( arguments );
169 if( X_Type_isArray( v ) && v.length ) return new X_NodeList( v );
171 switch( X_Node_getType( v ) ){
172 case X_NodeType_XNODE :
173 case X_NodeType_XNODE_LIST :
176 case X_NodeType_RAW_HTML :
177 if( xnode = X_Node_getXNode( v ) ) return xnode;
178 // v.parentNode || v.parentElement : dom1 || dom0
179 this.parent = ( parent = v.parentNode || v.parentElement ) && parent.tagName /* ie7- */ && X_Node_getXNode( parent );
180 this[ '_rawObject' ] = v;
181 this[ '_tag' ] = v.tagName.toUpperCase();
182 this[ '_id' ] = v.id;
183 this[ '_className' ] = v.className;
185 this[ 'cssText' ]( v.style.cssText );
186 this[ '_flags' ] &= X_Node_BitMask_RESET_DIRTY; // X_NodeFlags_DIRTY_CSS を落とす
188 // TODO attr の回収は不可能、、、?
190 v.setAttribute( 'UID', '' + uid );
197 case X_NodeType_RAW_TEXT :
198 if( xnode = X_Node_getXNode( v ) ) return xnode;
199 this.parent = X_Node_getXNode( v.parentNode );
200 this[ '_rawObject' ] = v;
201 this[ '_text' ] = v.data;
205 case X_NodeType_HTML_STRING :
206 case X_NodeType_STRING :
207 if( xnodes = X_HtmlParser_parse( v, true ) && 1 < xnodes.length ) return new X_NodeList( xnodes );
208 if( xnodes.length ) return xnodes[ 0 ];
212 if( X_Node_none ) return X_Node_none;
218 if( this.parent && ( this.parent[ '_flags' ] & X_NodeFlags_IN_TREE ) ){
219 this[ '_flags' ] |= X_NodeFlags_IN_TREE;
221 this[ '_flags' ] |= X_NodeFlags_EXIST;
222 X_Node_CHASHE[ this[ '_uid' ] = uid ] = this;
225 'width' : X_Node_width,
226 'height' : X_Node_height,
227 'clientWidth' : X_Node_clientWidth,
228 'clientHeight' : X_Node_clientHeight,
229 'scrollWidth' : X_Node_scrollWidth,
230 'scrollHeight' : X_Node_scrollHeight,
231 'scrollLeft' : X_Node_scrollLeft,
232 'scrollTop' : X_Node_scrollTop,
235 'offset' : X_Node_offset,
237 'attr' : X_Node_attr,
239 'cssText' : X_Node_cssText,
241 'find' : X_Node_find,
243 'animate' : X_Node_animate,
244 'stop' : X_Node_stop,
247 'create' : X_Node_create,
249 'createAt' : X_Node_createAt,
251 'createText' : X_Node_createText,
253 'createTextAt' : X_Node_createTextAt,
255 'clone' : X_Node_clone,
257 'append' : X_Node_append,
259 'appendAt' : X_Node_appendAt,
261 'appendTo' : X_Node_appendTo,
263 'prev' : X_Node_prev,
265 'next' : X_Node_next,
267 'swap' : X_Node_swap,
269 'remove' : X_Node_remove,
271 'empty' : X_Node_empty,
273 'contains' : X_Node_contains,
275 'getChildAt' : X_Node_getChildAt,
277 'numChildren' : X_Node_numChildren,
279 'firstChild' : X_Node_firstChild,
281 'lastChild' : X_Node_lastChild,
283 'getOrder' : X_Node_getOrder,
285 'className' : X_Node_className,
286 'addClass' : X_Node_addClass,
287 'removeClass' : X_Node_removeClass,
288 'toggleClass' : X_Node_toggleClass,
289 'hasClass' : X_Node_hasClass,
291 'html' : X_Node_html,
292 'text' : X_Node_text,
293 'call' : X_Node_call,
299 var X_NodeType_XNODE = 1,
300 X_NodeType_RAW_HTML = 2,
301 X_NodeType_RAW_TEXT = 3,
302 X_NodeType_HTML_STRING = 4,
303 X_NodeType_STRING = 5,
305 X_NodeType_XNODE_LIST = 7,
306 X_NodeType_WINDOW = 8,
307 X_NodeType_DOCUMENT = 9,
308 X_NodeType_IMAGE = 10,
310 X_Node_strictElmCreation = !X_UA[ 'MacIE' ] && X_UA[ 'IE' ] <= 8,
312 X_Node_documentFragment = document.createDocumentFragment && ( !X_UA[ 'IE' ] || 5.5 <= X_UA[ 'IE' ] ) && document.createDocumentFragment(),
314 // 子の生成後に リアル文書 tree に追加する
315 X_Node_addTreeAfterChildren = !( X_UA[ 'IE' ] < 9 ),
317 X_Node_displayNoneFixForIE5 = !!X_NodeFlags_IE5_DISPLAY_NONE_FIX,
319 X_Node_newByTag = false,
321 X_Node_newByText = false,
323 X_Node_outerXNode = null,
325 X_Node_updateTimerID = 0,
328 X_Node_isXmlDocument =
332 if( X_Type_isBoolean( root.isXML ) ) return root.isXML;
333 return root.isXML = root[ '_rawObject' ].createElement( 'p' ).tagName !== root[ '_rawObject' ].createElement( 'P' ).tagName;
336 X_Node_none = X_Node_CHASHE[ 0 ] = Node(),
337 X_Node_html, // = <html>
338 X_Node_head, // = <head>
339 X_Node_body, // = <body>
340 X_Node_systemNode, // = X_Node_CHASHE[ ? ]
344 * X_Node_reserveRemoval = [] に追加。commitUpdate で remove
346 * X_Node_reserveRemoval にいたら消す, new_parent[ '_xnodes' ] に挿入
348 X_Node_reserveRemoval = [];
350 function X_Node_getType( v ){
351 if( v === '' ) return X_NodeType_STRING;
353 if( v === window ) return X_NodeType_WINDOW;
354 if( v === document ) return X_NodeType_DOCUMENT;
355 if( v.constructor === Node ) return X_NodeType_XNODE;
356 if( v.constructor === X_NodeList ) return X_NodeType_XNODE_LIST;
357 if( X_Type_isHTMLElement( v ) ) return X_NodeType_RAW_HTML;
358 if( v.nodeType === 3 ) return X_NodeType_RAW_TEXT;
359 if( X_Type_isString( v ) ){
360 return '<' === v.charAt( 0 ) && v.charAt( v.length - 1 ) === '>' ? X_NodeType_HTML_STRING : X_NodeType_STRING;
362 if( v[ 'instanceOf' ] && v[ 'instanceOf' ]( Node ) ) return X_NodeType_XNODE;
365 function X_Node_getXNode( v ){
366 var uid, i, chashe, xnode;
367 switch( X_Node_getType( v ) ){
368 case X_NodeType_XNODE :
369 case X_NodeType_XNODE_LIST :
371 case X_NodeType_RAW_HTML :
372 // fake TextNode too.
374 uid = v.getAttribute( 'UID' );
375 return uid && X_Node_CHASHE[ uid ];
377 return v[ 'UID' ] && X_Node_CHASHE[ v[ 'UID' ] ];
378 case X_NodeType_WINDOW :
380 case X_NodeType_DOCUMENT :
381 return X_ViewPort_document;
382 case X_NodeType_RAW_TEXT :
383 if( v[ 'UID' ] ) return X_Node_CHASHE[ v[ 'UID' ] ];
384 for( chashe = X_Node_CHASHE, i = chashe.length; i; ){
385 if( ( xnode = X_Node_CHASHE[ --i ] ) && ( xnode[ '_rawObject' ] === v ) ) return xnode;
390 function X_Node_getRoot( xnode ){
391 return X_ViewPort_document;
392 //return X_Node_body[ '_rawObject' ].documentElement ? node : node.ownerDocument || node.document;
397 var X_Node__ie4getRawNode = X_UA_DOM.IE4 && function ( that ){
398 return that[ '_rawObject' ] ||
399 ( that[ '_rawObject' ] = document.all[ 'ie4uid' + that[ '_uid' ] ] ) ||
400 ( that[ '_id' ] && ( that[ '_rawObject' ] = document.all[ that[ '_id' ] ] ) );
404 function X_Node_toggleInTreeFlag( xnodes, flag ){
405 var i = xnodes.length, xnode;
407 xnode = xnodes[ --i ];
408 flag ? ( xnode[ '_flags' ] |= X_NodeFlags_IN_TREE | X_NodeFlags_DIRTY_POSITION ) : ( xnode[ '_flags' ] &= ~X_NodeFlags_IN_TREE & ~X_NodeFlags_IE5_DISPLAY_NONE_FIX );
409 xnode[ '_xnodes' ] && X_Node_toggleInTreeFlag( xnode[ '_xnodes' ], flag );
413 function X_Node_toggleInGPUFlag( gpuRoot, xnodes, flag ){
414 var i = xnodes.length, xnode;
418 xnode = xnodes[ --i ];
419 if( !xnode[ '_gpuParent' ] ){
420 xnode[ '_flags' ] |= X_NodeFlags_GPU_CHILD;
421 xnode[ '_gpuParent' ] = gpuRoot;
422 xnode[ '_xnodes' ] && X_Node_toggleInTreeFlag( gpuRoot, xnode[ '_xnodes' ], flag );
427 xnode = xnodes[ --i ];
428 if( xnode[ '_gpuParent' ] === gpuRoot ){
429 xnode[ '_flags' ] &= ~X_NodeFlags_GPU_CHILD;
430 delete xnode[ '_gpuParent' ];
431 xnode[ '_xnodes' ] && X_Node_toggleInTreeFlag( gpuRoot, xnode[ '_xnodes' ], flag );
438 * タグ名等を指定して新規に子ノードを作成し、現在のノードに追加する。
439 * @alias Node.prototype.create
440 * @param {string} [tag] タグ名
441 * @param {object} [opt_attrs=] 属性
442 * @param {object|string} [opt_css=] css
443 * @return {Node} 新規作成されたノード
444 * @example var child = parent.create( 'div' );
446 function X_Node_create( tag, opt_attrs, opt_css ){
448 if( !this[ '_tag' ] ) return;
449 this[ 'append' ]( xnode = X_Doc_create( tag, opt_attrs, opt_css ) );
453 * 挿入位置とタグ名等を指定して新規に子ノードを作成し、現在のノードに挿入する。
454 * @alias Node.prototype.createAt
455 * @param {number} [index] 挿入位置
456 * @param {string} [tag] タグ名
457 * @param {object} [opt_attrs=] 属性
458 * @param {object|string} [opt_css=] css
459 * @return {Node} 新規作成されたノード
460 * @example var child = parent.create( 2, 'div' );
462 function X_Node_createAt( index, tag, opt_attrs, opt_css ){
464 if( !this[ '_tag' ] ) return;
465 this[ 'appendAt' ]( index, xnode = X_Doc_create( tag, opt_attrs, opt_css ) );
470 * テキストを指定して新規にテキストノードを作成し、現在のノードに挿入する。
471 * @alias Node.prototype.createText
472 * @param {string} [tag] テキスト
473 * @return {Node} 新規作成されたノード
475 function X_Node_createText( text ){
477 if( !this[ '_tag' ] ) return;
478 this[ 'append' ]( xnode = X_Doc_createText( text ) );
482 * 挿入位置とテキストを指定して新規に子ノードを作成し、現在のノードに挿入する。
483 * @alias Node.prototype.createTextAt
484 * @param {number} [index] 挿入位置
485 * @param {string} [tag] テキスト
486 * @return {Node} 新規作成されたノード
488 function X_Node_createTextAt( index, text ){
490 if( !this[ '_tag' ] ) return;
491 this[ 'appendAt' ]( index, xnode = X_Doc_createText( text ) );
496 * Node のクローンを作成し返す。id もクローンされる点に注意。イベントリスナはクローンされない。
497 * http://d.hatena.ne.jp/think49/20110724/1311472811
498 * http://d.hatena.ne.jp/uupaa/20100508/1273299874
499 * @alias Node.prototype.clone
500 * @param {boolean} [opt_clone_children] 子要素のクローンを行うか?
503 function X_Node_clone( opt_clone_children ){
504 var xnode, xnodes, i, l;
506 if( this[ '_tag' ] ){
507 X_Node_newByTag = true;
508 xnode = Node( this[ '_tag' ], X_Object_copy( this[ '_attrs' ] ), X_Object_copy( this[ '_css' ] ) )
509 [ 'attr' ]( { 'id' : this[ '_id' ] } )
510 [ 'className' ]( this[ '_className' ] );
512 if( this[ '_flags' ] & X_NodeFlags_IS_SVG ){
513 xnode[ '_flags' ] |= X_NodeFlags_IS_SVG;
515 if( this[ '_flags' ] & X_NodeFlags_IS_VML ){
516 xnode[ '_flags' ] |= X_NodeFlags_IS_VML;
519 if( opt_clone_children && ( xnodes = this[ '_xnodes' ] ) && ( l = xnodes.length ) ){
520 for( i = 0; i < l; ++i ){
521 xnode[ 'append' ]( xnodes[ i ][ 'clone' ]( true ) );
526 X_Node_newByText = true;
527 return Node( this[ '_text' ] );
531 * ノードを子配列の最後に追加する。文字列が渡された場合、HTMLパーサーによって Node ツリーを作成して追加する。HtmlElement, TextNode の場合は内部使用専用。
532 * @alias Node.prototype.append
533 * @param {Node|string|HTMLElement|TextNode} [v] HTMLElement と TextNode は内部のみ。
534 * @return {Node} 自身。チェインメソッド
536 * myNode.append( node );
537 * myNode.append( node, '<span>Hello,</span>', 'world.' );
539 function X_Node_append( v ){
540 var i, l, xnodes, frg;
541 if( !this[ '_tag' ] ) return;
543 if( 1 < ( l = arguments.length ) ){
544 for( i = 0; i < l; ++i ){
545 this[ 'append' ]( arguments[ i ] );
550 if( !( xnodes = this[ '_xnodes' ] ) ) this[ '_xnodes' ] = xnodes = [];
552 switch( X_Node_getType( v ) ){
553 case X_NodeType_RAW_HTML :
554 case X_NodeType_RAW_TEXT :
557 case X_NodeType_HTML_STRING :
558 case X_NodeType_STRING :
559 return X_Node_append.apply( this, X_HtmlParser_parse( v, true ) );
560 case X_NodeType_XNODE :
561 // 親の xnodes から v を消す
562 v.parent && v[ 'remove' ]();
563 // IE4 でテキストノードの追加、FIXED 済でない場合、親に要素の追加を通知
564 if( X_UA[ 'IE4' ] && !v[ '_tag' ] && ( ( this[ '_flags' ] & X_NodeFlags_IE4_FIXED ) === 0 ) ) this[ '_flags' ] |= X_NodeFlags_IE4_DIRTY_CHILDREN;
571 xnodes[ xnodes.length ] = v;
572 if( this[ '_flags' ] & X_NodeFlags_IN_TREE ){
573 v[ '_flags' ] |= X_NodeFlags_IN_TREE;
574 v[ '_xnodes' ] && X_Node_toggleInTreeFlag( v[ '_xnodes' ], true );
575 X_Node_reserveUpdate();
577 if( this[ '_flags' ] & X_NodeFlags_IS_SVG ){
578 v[ '_flags' ] |= X_NodeFlags_IS_SVG;
580 if( this[ '_flags' ] & X_NodeFlags_IS_VML ){
581 v[ '_flags' ] |= X_NodeFlags_IS_VML;
588 * @alias Node.prototype.appendAt
589 * @param {number} index 挿入位置 0以上
590 * @param {Node|string|HTMLElement|TextNode} [v] HTMLElement と TextNode は内部のみ。
591 * @return {Node} 自身。チェインメソッド
592 * @example myNode.appendAt( 1, node );
594 function X_Node_appendAt( start, v ){
597 if( !this[ '_tag' ] ) return this;
599 l = arguments.length;
600 if( !( xnodes = this[ '_xnodes' ] ) ) xnodes = this[ '_xnodes' ] = [];
602 if( xnodes.length <= start ){
603 if( l === 2 ) return this[ 'append' ]( v );
604 for( i = 1; i < l; ++i ){
605 this[ 'append' ]( arguments[ i ] );
609 if( start < 0 ) start = 0;
612 this[ 'appendAt' ]( start, arguments[ --l ] );
617 switch( X_Node_getType( v ) ){
618 case X_NodeType_RAW_HTML :
619 case X_NodeType_RAW_TEXT :
622 case X_NodeType_HTML_STRING :
623 case X_NodeType_STRING :
624 v = X_HtmlParser_parse( v, true );
625 for( i = v.length; i; ){
626 this[ 'appendAt' ]( start, v[ --i ] );
629 case X_NodeType_XNODE :
630 // 親の xnodes から v を消す
632 if( v.parent === this ){
633 i = v[ 'getOrder' ]();
634 if( i === start ) return this;
635 if( i < start ) --start;
639 // IE4 でテキストノードの追加、FIXED 済でない場合、親に要素の追加を通知
640 if( X_UA[ 'IE4' ] && !v[ '_tag' ] && ( ( this[ '_flags' ] & X_NodeFlags_IE4_FIXED ) === 0 ) ) this[ '_flags' ] |= X_NodeFlags_IE4_DIRTY_CHILDREN;
647 this[ '_xnodes' ].splice( start, 0, v );
648 if( this[ '_flags' ] & X_NodeFlags_IN_TREE ){
649 v[ '_flags' ] |= X_NodeFlags_IN_TREE;
650 v[ '_xnodes' ] && X_Node_toggleInTreeFlag( v[ '_xnodes' ], true );
651 X_Node_reserveUpdate();
653 if( this[ '_flags' ] & X_NodeFlags_IS_SVG ){
654 v[ '_flags' ] |= X_NodeFlags_IS_SVG;
656 if( this[ '_flags' ] & X_NodeFlags_IS_VML ){
657 v[ '_flags' ] |= X_NodeFlags_IS_VML;
663 * ノードを親に追加する。戻り値は子ノードなので、続けて操作が出来る。
664 * @alias Node.prototype.appendTo
665 * @param {Node|string|HTMLElement} [parent] HTMLElement は内部のみ。
666 * @param {number} [opt_index=-1] 挿入位置。省略した場合は最後に追加する。
667 * @return {Node} 自身。チェインメソッド
668 * @example childNode.appendTo( parentNode, 1 );
670 function X_Node_appendTo( parent, opt_index ){
671 switch( X_Node_getType( parent ) ){
672 case X_NodeType_RAW_HTML :
673 parent = Node( parent );
675 case X_NodeType_HTML_STRING :
676 parent = X_HtmlParser_parse( parent, true );
677 parent = parent[ 0 ] || parent;
678 case X_NodeType_XNODE :
683 X_Type_isFinite( opt_index ) ? parent[ 'appendAt' ]( opt_index, this ) : parent[ 'append' ]( this );
689 * ノードの直前の要素を取得。または直前に挿入。挿入する要素が先にいる兄弟でも正しく動作する。
690 * @alias Node.prototype.prev
691 * @param {Node|string|HTMLElement|TextNode} [...v] HTMLElement と TextNode は内部のみ。
692 * @return {Node} 自身。チェインメソッド
693 * @example childNode.prev( prevNode );
695 function X_Node_prev( v ){
696 var parent = this.parent, xnodes, i, l;
699 if( v === undefined ){
700 if( !parent ) return;
701 xnodes = parent[ '_xnodes' ];
702 i = xnodes.indexOf( this );
703 return 0 < i ? xnodes[ i - 1 ] : v;
706 if( !parent ) return this;
708 l = arguments.length;
710 for( i = 0; l; ++i ){
711 parent[ 'appendAt' ]( this[ 'getOrder' ]() - i, arguments[ --l ] );
715 parent[ 'appendAt' ]( this[ 'getOrder' ](), v );
720 * ノードの直後の要素を取得。または直後に挿入。挿入する要素が先にいる兄弟でも正しく動作する。
721 * @alias Node.prototype.next
722 * @param {Node|string|HTMLElement|TextNode} [v] HTMLElement と TextNode は内部のみ。
723 * @return {Node} 自身。チェインメソッド
724 * @example childNode.next( prevNode );
726 function X_Node_next( v ){
727 var parent = this.parent, xnodes, i, l, start;
730 if( v === undefined ){
731 if( !parent ) return;
732 xnodes = parent[ '_xnodes' ];
733 i = xnodes.indexOf( this );
734 return ++i < xnodes.length ? xnodes[ i ] : v;
737 if( !parent ) return this;
739 l = arguments.length;
740 start = this[ 'getOrder' ]() + 1;
742 if( parent[ '_xnodes' ].length <= start ){
743 for( i = 0; i < l; ++i ){
744 parent[ 'append' ]( arguments[ i ] );
749 parent[ 'appendAt' ]( this[ 'getOrder' ]() + 1, arguments[ --l ] );
752 parent[ 'appendAt' ]( start, v );
758 * 要素の入れ替え。自身は remove() される。
759 * @alias Node.prototype.swap
760 * @param {Node|string|HTMLElement|TextNode} [v] HTMLElement と TextNode は内部のみ。
761 * @return {Node} 自身。チェインメソッド
762 * @example node.swap( newNode );
764 function X_Node_swap( v ){
765 if( !this.parent ) return this;
766 return arguments.length === 1 ? this[ 'prev' ]( v )[ 'remove' ]() : X_Node_prev.apply( this, arguments )[ 'remove' ]();
770 * 要素を親要素から抜く。jQuery の remove と異なり、インスタンスは破壊(kill)されず、再び別の親に挿入等できる
771 * @alias Node.prototype.remove
772 * @return {Node} 自身。チェインメソッド
773 * @example node.remove();
774 * parent.append( node ); 新しい親に追加できる
776 function X_Node_remove(){
777 var parent = this.parent,
780 if( !parent ) return this;
783 parent[ '_xnodes' ].splice( parent[ '_xnodes' ].indexOf( this ), 1 );
785 if( this[ '_flags' ] & X_NodeFlags_IN_TREE ){
786 this[ '_flags' ] &= ~X_NodeFlags_IN_TREE & ~X_NodeFlags_IE5_DISPLAY_NONE_FIX;
787 this[ '_xnodes' ] && X_Node_toggleInTreeFlag( this[ '_xnodes' ], false );
790 if( elm = this[ '_rawObject' ] || X_Node__ie4getRawNode( this ) ){
791 X_Node_reserveRemoval[ X_Node_reserveRemoval.length ] = this;
792 X_Node_reserveUpdate();
794 if( !this[ '_tag' ] && ( ( parent[ '_flags' ] & X_NodeFlags_IE4_FIXED ) === 0 ) ){
795 parent[ '_flags' ] |= X_NodeFlags_IE4_DIRTY_CHILDREN;
798 elm = this[ '_rawObject' ];
799 if( elm && elm.parentNode && elm.parentNode.tagName ){
800 X_Node_reserveRemoval[ X_Node_reserveRemoval.length ] = this;
801 X_Node_reserveUpdate();
806 elm = this[ '_rawObject' ];
807 if( elm && elm.parentNode && elm.parentNode.tagName ){
808 X_Node_reserveRemoval[ X_Node_reserveRemoval.length ] = this;
809 X_Node_reserveUpdate();
817 * 子要素を破棄する。子要素は kill() されます。
818 * @alias Node.prototype.empty
819 * @return {Node} 自身。チェインメソッド
820 * @example node.empty();
822 function X_Node_empty(){
823 var xnodes = this[ '_xnodes' ], i;
825 if( xnodes && ( i = xnodes.length ) ){
826 delete this[ '_xnodes' ];
828 xnodes[ --i ][ 'kill' ]();
835 function X_Node_onKill( that ){
836 var parent = that.parent,
837 xnodes = that[ '_xnodes' ], i, elm;
839 if( ( that[ '_flags' ] & X_NodeFlags_EXIST ) === 0 ) return;
841 parent && parent[ '_xnodes' ] && parent[ '_xnodes' ].splice( parent[ '_xnodes' ].indexOf( that ), 1 );
843 elm = that[ '_rawObject' ] || X_UA_DOM.IE4 && X_Node__ie4getRawNode( that );
845 if( xnodes && ( i = xnodes.length ) ){
846 delete that[ '_xnodes' ];
848 xnodes[ --i ][ 'kill' ]();
853 X_Node_CHASHE[ that[ '_uid' ] ] = null; // array に対して delete X_Node_CHASHE[ uid ] はまずい!
858 X_Node_reserveRemoval[ X_Node_reserveRemoval.length ] = elm;
859 X_Node_reserveUpdate();
861 if( !that[ '_tag' ] && ( ( parent[ '_flags' ] & X_NodeFlags_IE4_FIXED ) === 0 ) ){
862 parent[ '_flags' ] |= X_NodeFlags_IE4_DIRTY_CHILDREN;
865 if( elm && elm.parentNode && elm.parentNode.tagName ){
866 X_Node_reserveRemoval[ X_Node_reserveRemoval.length ] = elm;
867 X_Node_reserveUpdate();
877 * @alias Node.prototype.contains
878 * @param {Node|string|HTMLElement|TextNode} [v] HTMLElement と TextNode は内部のみ。
880 * @example node.contains( testNode );
882 function X_Node_contains( v ){
883 var elm, type, xnodes, i;
884 if( !v || !this[ '_tag' ] || this === v ) return false;
886 if( ( elm = this[ '_rawObject' ] || X_UA_DOM.IE4 && X_Node__ie4getRawNode( this ) ) && document.contains && ( type = X_Node_getType( v ) ) && ( type === X_NodeType_RAW_HTML || type === X_NodeType_RAW_TEXT ) ){
887 return elm.contains( v );
890 //if( document.compareDocumentPosition ){
893 xnodes = this[ '_xnodes' ];
894 if( !xnodes || !xnodes.length ) return false;
895 if( xnodes.indexOf( v ) !== -1 ) return true; // fast
896 for( i = xnodes.length; i; ){
897 if( xnodes[ --i ][ 'contains' ]( v ) ) return true;
904 * @alias Node.prototype.getChildAt
905 * @param {number} index 取得する子ノードの位置。0~
907 * @example child1 = parent.getChildAt(1);
909 function X_Node_getChildAt( i ){
910 var xnodes = this[ '_xnodes' ];
911 return xnodes && 0 <= i && i < xnodes.length && xnodes[ i ];
916 * @alias Node.prototype.numChildren
917 * @return {number} 子要素の数。
918 * @example n = parent.numChildren();
920 function X_Node_numChildren(){
921 var xnodes = this[ '_xnodes' ];
922 return xnodes ? xnodes.length : 0;
927 * @alias Node.prototype.firstChild
928 * @return {Node} 最初の子要素
929 * @example child0 = parent.firstChild();
931 function X_Node_firstChild(){
932 return this[ '_xnodes' ] && this[ '_xnodes' ][ 0 ];
937 * @alias Node.prototype.lastChild
938 * @return {Node} 最後の子要素
939 * @example lastChild = parent.lastChild();
941 function X_Node_lastChild(){
942 var xnodes = this[ '_xnodes' ];
943 return xnodes && xnodes[ xnodes.length - 1 ];
948 * @alias Node.prototype.getOrder
949 * @return {number} index -1 の場合、親を持たない。
950 * @example index = node.getOrder();
952 function X_Node_getOrder(){
953 var parent = this.parent;
954 return this === X_Node_html ?
957 parent[ '_xnodes' ].indexOf( this ) :
963 * @alias Node.prototype.className
964 * @return {string|Node} getter の場合 class 文字列、setter の場合自身。
966 * className = node.className();
968 * node.className( 'myClass myClass_new' );
970 function X_Node_className( v ){
973 if( v === undefined ) return this[ '_className' ];
976 if( this[ '_className' ] === v ) return this;
977 if( !v || !X_Type_isString( v ) ){
978 delete this[ '_className' ];
983 while( v.indexOf( __ ) !== -1 ){ v = v.split( __ ).join( _ ); };
984 v.charAt( 0 ) === _ && ( v = v.substr( 1 ) );
985 v.lastIndexOf( _ ) === 0 && ( v = v.substr( 0, v.length - 1 ) );
987 if( this[ '_className' ] === v ) return this;
988 v ? ( this[ '_className' ] = v ) : delete this[ '_className' ];
990 this[ '_flags' ] |= X_NodeFlags_DIRTY_CLASSNAME;
991 this[ '_flags' ] & X_NodeFlags_IN_TREE && X_Node_reserveUpdate();
997 * @alias Node.prototype.addClass
998 * @param {string} className スペース区切りで複数のクラスを追加できる。
1000 * @example node.addClass( 'myClass myClass_new' );
1002 function X_Node_addClass( v ){
1003 var names = v.split( ' ' ),
1005 _class = this[ '_className' ],
1009 name = names[ --i ];
1010 if( !name ) continue;
1011 !this[ 'hasClass' ]( name ) && ( v += ( v ? ' ' : '' ) + name );
1013 return v ? this[ 'className' ]( ( _class ? _class + ' ' : '' ) + v ) : this;
1018 * @alias Node.prototype.removeClass
1019 * @param {string} className スペース区切りで複数のクラスを削除できる。
1020 * @return {Node} 自身。
1021 * @example node.removeClass( 'myClass myClass_new' );
1023 function X_Node_removeClass( v ){
1025 _class = this[ '_className' ],
1026 names = v.split( _ ),
1027 classNames, i, f, j;
1029 if( !_class ) return this;
1030 for( classNames = _class.split( _ ), i = classNames.length; i; ){
1031 _class = classNames[ --i ];
1032 for( j = names.length; j; ){
1033 if( _class === names[ --j ] ){
1034 classNames.splice( i, 1 );
1035 names.splice( j, 1 );
1041 return f ? this[ 'className' ]( classNames.join( _ ) ) : this;
1046 * @alias Node.prototype.toggleClass
1047 * @param {string} className スペース区切りで複数のクラスを削除できる。
1048 * @param {boolean} [opt_toggle=] true はクラスの追加。false はクラスの削除。undefined はクラスのトグル。
1049 * @return {Node} 自身。
1050 * @example node.toggleClass( 'myClass myClass_new', !!n );
1052 function X_Node_toggleClass( v, opt_toggle ){
1054 if( opt_toggle !== undefined ){
1055 return !opt_toggle ? this[ 'removeClass' ]( v ) : this[ 'addClass' ]( v );
1057 names = v.split( ' ' );
1058 for( i = names.length; i; ){
1059 name = names[ --i ];
1060 this[ 'hasClass' ]( name ) ? this[ 'removeClass' ]( name ) : this[ 'addClass' ]( name );
1067 * @alias Node.prototype.hasClass
1068 * @param {string} className スペース区切りで複数のクラスを削除できる。
1070 * @example node.hasClass( 'myClass myClass_new' );
1072 function X_Node_hasClass( v ){
1074 _class = this[ '_className' ],
1076 if( _class === v ) return true;
1077 if( !_class ) return false;
1079 _class = _ + _class + _;
1080 if( _class.indexOf( _ + v + _ ) !== -1 ) return true; // lucky hit
1082 for( v = v.split( _ ), i = v.length; i; ){
1084 if( name === '' ) continue;
1085 if( _class.indexOf( _ + name + _ ) === -1 ) return false;
1091 * innerHTML 取得・設定。outerHTML が欲しい場合は、xnode.call('outerHTML') とできる。
1092 * @alias Node.prototype.html
1093 * @param {string} [html=] html文字列
1094 * @return {string|Node}
1095 * @example node.html( '<img>' );
1097 function X_Node_html( html ){
1098 var _ = '', q = '"', xnodes, n, i, l;
1100 if( html !== undefined ){ // String 以外に Number や false null なども許可
1101 if( !this[ '_tag' ] ) return this[ 'text' ]( html );
1105 X_Node_append.apply( this, X_HtmlParser_parse( html, true ) );
1111 if( !this[ '_tag' ] ){
1112 return this[ '_text' ];
1115 this[ '_flags' ] & X_NodeFlags_OLD_CSSTEXT && X_Node_CSS_objToCssText( this );
1117 html = !X_Node_outerXNode ? [] : [
1118 '<', this[ '_tag' ],
1119 this[ '_id' ] ? ' id="' + this[ '_id' ] + q : _,
1120 this[ '_className' ] ? ' class="' + this[ '_className' ] + q : _,
1121 this[ '_flags' ] & X_NodeFlags_OLD_ATTRTEXT ? X_Node_Attr_objToAttrText( this ) : this[ '_attrText' ],
1122 this[ '_cssText' ] ? ' style="' + this[ '_cssText' ] + q : _,
1126 if( ( xnodes = this[ '_xnodes' ] ) && ( l = xnodes.length ) ){
1127 if( !X_Node_outerXNode ) X_Node_outerXNode = this;
1128 for( i = 0; i < l; ++i ){
1129 html[ n ] = xnodes[ i ][ 'html' ]();
1132 if( X_Node_outerXNode === this ) X_Node_outerXNode = null;
1134 !X_Node_outerXNode || X_Dom_DTD_EMPTY[ this[ '_tag' ] ] || ( html[ n ] = '<\/' + this[ '_tag' ] + '>' );
1135 return html.join( _ );
1139 * null が来たら '', 数値等が来たら文字列化
1142 * textContent 取得・設定。null が来たら '', 数値等が来たら文字列化
1143 * @alias Node.prototype.text
1144 * @param {string} [text=]
1145 * @return {string|Node}
1146 * @example node.text( 'Hello, world!' );
1148 function X_Node_text( text ){
1149 var xnodes, texts, i, l;
1151 if( text !== undefined ){
1152 if( text === null ) text = '';
1155 if( !this[ '_tag' ] ){
1156 if( this[ '_text' ] !== text ){
1157 text ? ( this[ '_text' ] = text ) : delete this[ '_text' ];
1158 this[ '_flags' ] |= X_NodeFlags_DIRTY_CONTENT;
1159 this[ '_flags' ] & X_NodeFlags_IN_TREE && X_Node_reserveUpdate();
1163 if( ( xnodes = this[ '_xnodes' ] ) && xnodes.length === 1 && !xnodes[ 0 ][ '_tag' ] ){
1164 xnodes[ 0 ][ 'text' ]( text );
1167 // TODO 一つのtextnode を残すケース 完全に削除したい場合は empty()を使う
1168 if( !text ) return this[ 'empty' ]();
1169 this[ 'empty' ]()[ 'createText' ]( text );
1173 if( this[ '_tag' ] ){
1174 if( ( xnodes = this[ '_xnodes' ] ) && ( l = xnodes.length ) ){
1175 for( texts = [], i = 0; i < l; ++i ){
1176 texts[ i ] = xnodes[ i ][ 'text' ]();
1178 return texts.join( '' );
1182 return this[ '_text' ];
1186 * HTML要素に対して name の関数を実行しその戻り値を返す。関数に渡す引数も任意に設定できる。
1187 * @alias Node.prototype.call
1188 * @param {string} [name] 要素の関数名
1190 * @example node.call( 'focus' );
1192 function X_Node_call( name /*, opt_args... */ ){
1193 var args = arguments,
1194 l = args.length - 1,
1195 v, raw, parent, body,
1196 childX, childY, childW, childH,
1198 parentSX, parentSY, parentSW, parentSH,
1199 visibleX, visibleY, visibleW, visibleH,
1200 visiblePartX, visiblePartY, func, args, params, i;
1204 return !!( this[ '_flags' ] & X_NodeFlags_IS_SVG );
1206 return !!( this[ '_flags' ] & X_NodeFlags_IS_VML );
1208 return this[ '_tag' ] ? 1 : 3;
1210 X_Node_outerXNode = X_Node_body; // == true ならなんでもよい。型を合わすために xbody にしている
1211 v = this[ 'html' ]();
1212 X_Node_outerXNode = null;
1214 case 'treeIsDirty' :
1215 return !!X_Node_updateTimerID;
1217 return X_Node_CSS_getCharSize( this );
1219 return !!( this[ '_flags' ] & ( X_NodeFlags_GPU_NOW | X_NodeFlags_GPU_RELEASE_RESERVED ) );
1222 X_Node_updateTimerID && X_Node_startUpdate();
1224 raw = this[ '_rawObject' ] || X_UA_DOM.IE4 && X_Node__ie4getRawNode( this );
1228 if( name === 'scrollTo' ){
1229 raw.scrollLeft = args[ 1 ] || 0;
1230 raw.scrollTop = args[ 2 ] || 0;
1234 if( name === 'inView' ){
1237 visibleX = visibleY = visibleW = visibleH = 0;
1238 while( child !== body ){
1239 parent = child.parentNode || child.parentElement;
1240 parentH = parent.clientHeight;
1241 parentW = parent.clientWidth;
1242 parentSW = parent.scrollHeight;
1243 parentSH = parent.scrollWidth;
1245 if( parentH < parentSH || parentW < parentSW ){
1246 childX = child.offsetLeft + visibleX;
1247 childY = child.offsetTop + visibleY;
1248 childW = visibleW || child.offsetWidth;
1249 childH = visibleH || child.offsetHeight;
1250 parentSX = parent.scrollLeft;
1251 parentSY = parent.scrollTop;
1253 if( parentSY < childY + childH &&
1254 childY < parentSY + parentH &&
1255 parentSX < childX + childW &&
1256 childX < parentSX + parentW ){
1258 // right:子の左側が見えている left:子の左側が見えている both:完全に見えている
1260 childX < parentSX ? 'right' :
1261 ( parentSX + parentW ) < ( childX + childW ) ? 'left' : 'both';
1263 childY < parentSY ? 'bottom' :
1264 ( parentSY + parentH ) < ( childY + childH ) ? 'top' : 'both';
1267 visibleX = visiblePartX === 'right' ? 0 : childX - parentSX;
1268 visibleY = visiblePartX === 'bottom' ? 0 : childY - parentSY;
1270 visiblePartX === 'both' ? childW :
1271 visiblePartX === 'right' ? ( parentSX + parentW - childX ) : ( childX + childW - parentSX );
1273 visiblePartY === 'both' ? childH :
1274 visiblePartY === 'bottom' ? ( parentSY + parentH - childY ) : ( childY + childH - parentSY );
1276 return { 'isInView' : false };
1281 return { 'isInView' : true };
1285 if( X_Type_isFunction( func ) ){
1287 args = X_Array_copy( args );
1289 return func.apply( raw, args );
1291 return raw[ name ]();
1293 if( X_UA[ 'IE' ] < 9 && ( X_Type_isUnknown( func ) || X_Type_isObject( func ) ) ){
1294 // typeof func === unknown に対策
1295 // http://la.ma.la/blog/diary_200509031529.htm
1297 args = X_Array_copy( args );
1301 for( i = 0; i < l; ++i ){
1302 params[ i ] = '_' + i;
1304 params = params.join( ',' );
1307 [ 'return this.', name, '(', params, ')' ].join( '' )
1308 ).apply( raw, args );
1310 return raw[ name ]();
1315 * xnode を this として関数を実行する。 NodeList.each と動作を合わせてあるため関数の戻り値は破棄される。
1318 function X_Node_each( func /*, opt_args */ ){
1320 if( 1 < arguments.length ){
1321 args = X_Array_copy( arguments );
1323 func.apply( this, args );
1325 func.call( this, 0 );
1331 /* --------------------------------------
1332 * Async commit update
1334 * TODO Timer や DOM イベントの呼び出しの最後に、まだ一度も commitUpdate していないなら commitUpdate してしまう。
1337 function X_Node_reserveUpdate(){
1338 if( !X_Node_updateTimerID ) X_Node_updateTimerID = X_Timer_requestFrame( X_Node_startUpdate );
1341 var X_Node_updateReservedByReleaseGPU = false;
1343 function X_Node_startUpdate( time ){
1344 var removal, i, xnodeOrElm;
1346 if( !X_Node_updateTimerID || X_ViewPort_readyState < X_EVENT_INIT ){
1350 X_Timer_cancelFrame( X_Node_updateTimerID );
1351 X_Node_updateTimerID = 0;
1354 // X.Timer 経由でないと発火しない このイベントでサイズを取ると無限ループに
1355 X_System[ '_listeners' ] && X_System[ '_listeners' ][ X_EVENT_BEFORE_UPDATE ] && X_System[ 'dispatch' ]( X_EVENT_BEFORE_UPDATE );
1358 removal = X_Node_reserveRemoval;
1360 if( i = removal.length ){
1362 xnodeOrElm = removal[ --i ];
1363 // TODO GPU レイヤーの子の場合、remove をスキップする。 非GPU レイヤーへ apppend される場合、clone する?
1364 if( !xnodeOrElm[ 'instanceOf' ] ){
1366 xnodeOrElm.removeAttribute( 'id' ); // ?
1367 xnodeOrElm.outerHTML = ''; // xnodeOrElm.remove() ?
1369 if( !X_UA[ 'MacIE' ] ){
1370 // elm.parentNode.tagName for ie7
1371 xnodeOrElm.parentNode && xnodeOrElm.parentNode.tagName && xnodeOrElm.parentNode.removeChild( xnodeOrElm );
1373 xnodeOrElm.parentNode && xnodeOrElm.parentNode.tagName && X_TEMP._fixed_remove( xnodeOrElm, that );
1377 X_Node__actualRemove( xnodeOrElm );
1383 if( X_Node_html[ '_flags' ] & X_Node_BitMask_IS_DIRTY ){
1384 X_Node__commitUpdate( X_Node_html, X_Node_html[ '_rawObject' ].parentNode, null, X_Node_html[ '_flags' ] );
1386 X_Node__commitUpdate( X_Node_head, X_Node_head[ '_rawObject' ].parentNode, null, X_Node_head[ '_flags' ] );
1387 X_Node__commitUpdate( X_Node_body, X_Node_body[ '_rawObject' ].parentNode, null, X_Node_body[ '_flags' ] );
1390 if( X_Node_updateReservedByReleaseGPU ){
1391 X_Node_reserveUpdate();
1392 X_Node_updateReservedByReleaseGPU = false;
1396 // X.Timer 経由でないと発火しない このイベントでサイズを取ると無限ループに
1397 X_System[ '_listeners' ] && X_System[ '_listeners' ][ X_EVENT_UPDATED ] && X_System[ 'dispatch' ]( X_EVENT_UPDATED );
1399 X_System[ '_listeners' ] && X_System[ '_listeners' ][ X_EVENT_UPDATED ] && X_System[ 'asyncDispatch' ]( X_EVENT_UPDATED );
1402 X_ViewPort[ '_listeners' ] && X_ViewPort[ '_listeners' ][ X_EVENT_AFTER_UPDATE ] && X_ViewPort[ 'asyncDispatch' ]( X_EVENT_AFTER_UPDATE );
1406 * 1. GPU_NOW の場合、子の変更は行わない
1407 * 2. GPU解放予約 の場合//、この要素のみ変更を行う。rAF 後にさらに更新するためフラグを立てる。
1409 * 4. style="display:none" の場合、これ以下の変更を行わない。
1410 * 5. ie5 非表示フラグが立っていて、親と自身の class・id によって非表示になっていて、親と自身に変更がない。accumulatedFlags を使用。
1411 * -> TODO これ TREE の変更を検出できない。 remove したときに 子まで X_NodeFlags_IE5_DISPLAY_NONE_FIXを落とす。
1415 * 9. ie5 親及び自身へのクラス・id指定で display:none になるケースがありそれを検出。
1416 * このままでは、生成と破棄が繰り返されてしまうので親と自身のクラス・idが変わった場合、ツリー位置の変化があった場合に再生する。
1418 var X_Node__commitUpdate =
1420 ( function( that, parentElement, nextElement, accumulatedFlags ){
1421 var elm = that[ '_rawObject' ],
1422 created, xnodes, l, next;
1424 // 1. GPU 一切の更新をスキップ
1425 if( that[ '_flags' ] & X_NodeFlags_GPU_NOW ){
1426 console.log( '更新のskip ' + !!( that[ '_flags' ] & X_Node_BitMask_IS_DIRTY ) );
1427 that[ '_flags' ] & X_Node_BitMask_IS_DIRTY && X_Node__updateRawNode( that, elm );
1432 if( that[ '_flags' ] & X_NodeFlags_GPU_RELEASE_RESERVED ){
1433 // console.log( 'GPU 解放 ' );
1434 //X_Node_updateReservedByReleaseGPU = true;
1435 //X_Node__updateRawNode( that, elm );
1436 that[ '_flags' ] &= X_Node_BitMask_RESET_GPU;
1437 //return elm;// TODO もしかしたらこのタイミングで更新できるかも。
1441 if( that[ '_flags' ] & X_NodeFlags_GPU_RESERVED ){
1442 that[ '_flags' ] &= X_Node_BitMask_RESET_GPU;
1443 that[ '_flags' ] |= X_NodeFlags_GPU_NOW;
1446 // 4. style="display:none" の場合
1447 if( that[ '_flags' ] & X_NodeFlags_STYLE_IS_DISPLAY_NONE ){
1448 if( X_Node_displayNoneFixForIE5 ){
1449 // filter の効いている要素を含む要素は display:none が無視される。
1450 // filter = '' で削除はできるが、再表示時に filter が消える。 -> filter な要素を削除してしまう。
1451 if( elm && elm.parentNode ){
1452 X_Node__actualRemove( that );
1456 elm && ( elm.style.display = 'none' );
1457 return ( elm && elm.nextSibling === nextElement ) ? elm : nextElement;
1461 accumulatedFlags |= that[ '_flags' ];
1463 if( that[ '_flags' ] & X_NodeFlags_IE5_DISPLAY_NONE_FIX ){
1464 if( ( accumulatedFlags & ( X_NodeFlags_DIRTY_POSITION | X_NodeFlags_DIRTY_ID | X_NodeFlags_DIRTY_CLASSNAME ) ) === 0 ){
1471 if( !that[ '_tag' ] ){
1472 that[ '_flags' ] &= X_Node_BitMask_RESET_DIRTY;
1473 elm = document.createTextNode( X_String_chrReferanceTo( that[ '_text' ] ) );
1474 if( !X_UA[ 'IE' ] ){
1475 elm[ 'UID' ] = that[ '_uid' ];
1478 if( X_Node_strictElmCreation ){
1479 that[ '_flags' ] & X_NodeFlags_OLD_CSSTEXT && X_Node_CSS_objToCssText( that, true ); // OLD_CSSTEXT ??
1482 document.createElement( [
1483 '<', that[ '_tag' ],
1484 ' UID="', that[ '_uid' ], '"',
1485 that[ '_id' ] ? ' id="' + that[ '_id' ] + '"' : '',
1486 that[ '_className' ] ? ' class="' + that[ '_className' ] + '"' : '',
1487 X_Node_Attr_objToAttrText( that, true ),
1488 that[ '_cssText' ] ? ' style="' + that[ '_cssText' ] + '"' : '',
1491 if( that[ '_flags' ] & X_NodeFlags_IS_SVG ){
1492 elm = document.createElementNS( 'http://www.w3.org/2000/svg', that[ '_tag' ].toLowerCase() );
1494 // math http://www.w3.org/1998/Math/MathML
1496 elm = document.createElement( that[ '_tag' ] );
1499 that[ '_rawObject' ] = elm;
1501 // IE には要素追加のタイミングで起こるメモリリークがありここで追加
1502 if( !X_Node_addTreeAfterChildren ){
1504 parentElement.insertBefore( elm, nextElement ) :
1505 parentElement.appendChild( elm );
1508 if( that[ '_tag' ] ){
1509 X_EventDispatcher_toggleAllEvents( that, true );// イベントの復帰
1510 that[ '_flags' ] |= X_NodeFlags_ACTUAL_LISTENING;
1512 //if( X_Node_documentFragment ){
1513 //( frg = X_Node_documentFragment ).appendChild( elm );
1514 // 連続する要素の差し替えの場合に有効
1517 if( X_Node_strictElmCreation ){
1518 that[ '_flags' ] &= X_Node_BitMask_RESET_DIRTY;
1519 // ie の string から要素を作る場合、ネットワーク系属性は onload イベントなどを拾うために、要素生成->イベント復帰後に適用する
1520 that[ '_newAttrs' ] && ( that[ '_flags' ] |= X_NodeFlags_DIRTY_ATTR ); // _newAttrs には ネットワーク系属性が入っている。Network 系の属性は遅らせて設定
1521 that[ '_flags' ] |= X_NodeFlags_DIRTY_IE_FILTER;// doc 追加後に filter を指定しないと有効にならない。
1523 elm[ 'UID' ] = that[ '_uid' ];
1524 that[ '_newAttrs' ] = that[ '_attrs' ];
1525 that[ '_flags' ] &= X_Node_BitMask_RESET_DIRTY;
1526 that[ '_flags' ] |= X_NodeFlags_DIRTY_ID | X_NodeFlags_DIRTY_CLASSNAME | X_NodeFlags_DIRTY_ATTR | X_NodeFlags_DIRTY_CSS | X_NodeFlags_DIRTY_IE_FILTER;
1528 // http://outcloud.blogspot.jp/2010/09/iframe.html
1529 // この問題は firefox3.6 で確認
1530 if( X_UA[ 'Gecko' ] ){
1531 if( that[ '_tag' ] === 'IFRAME' && ( !that[ '_attrs' ] || !that[ '_attrs' ][ 'src' ] ) ){
1532 //elm.contentWindow.location.replace = elm.src = 'about:blank';
1533 that[ 'attr' ]( 'src', 'about:blank' );
1542 if( elm.parentNode !== parentElement || ( nextElement && elm.nextSibling !== nextElement ) ){
1544 parentElement.insertBefore( elm, nextElement ) :
1545 parentElement.appendChild( elm );
1548 if( that[ '_listeners' ] && ( ( that[ '_flags' ] & X_NodeFlags_ACTUAL_LISTENING ) === 0 ) ){
1549 X_EventDispatcher_toggleAllEvents( that, true );// イベントの退避
1550 that[ '_flags' ] |= X_NodeFlags_ACTUAL_LISTENING;
1554 if( accumulatedFlags & X_Node_BitMask_IS_DIRTY ){
1555 delete that[ '_fontSize' ];
1556 X_Node__updateRawNode( that, elm );
1560 // 親及び自身へのクラス・id指定で display : none になるケースがありそれを検出
1561 // 生成と破棄が繰り返されてしまう、親と自身の id, class が変わった場合だけ再生成。 accumulatedFlags & ( ID | CLASSNAME )
1562 // currentStyle を観ていたときはエラーで停止する、alert と挟むと正常に動いて支離滅裂
1563 if( X_Node_displayNoneFixForIE5 && that[ '_tag' ] ){
1564 if( elm.runtimeStyle.display === 'none' ){
1565 X_Node__actualRemove( that );
1566 that[ '_flags' ] |= X_NodeFlags_IE5_DISPLAY_NONE_FIX;
1569 that[ '_flags' ] &= ~X_NodeFlags_IE5_DISPLAY_NONE_FIX;
1574 if( ( xnodes = that[ '_xnodes' ] ) && ( l = xnodes.length ) ) {
1576 next = X_Node__commitUpdate( xnodes[ --l ], elm, next, accumulatedFlags );
1580 if( created && X_Node_addTreeAfterChildren ){
1582 parentElement.insertBefore( elm, nextElement ) :
1583 parentElement.appendChild( elm );
1585 if( X_UA[ 'Gecko' ] && that[ '_tag' ] === 'IFRAME' && elm.contentWindow ){
1586 // tree に追加されるまで contentWindow は存在しない。
1587 elm.contentWindow.location.replace = elm.src;
1594 ( function( that, parentElement, prevElement, accumulatedFlags ){
1595 var elm = that[ '_rawObject' ] || X_Node__ie4getRawNode( that ),
1596 xnodes, l, i, dirty, mix, html, text, prev;
1598 if( !that[ '_tag' ] ){
1599 that[ '_flags' ] & X_NodeFlags_DIRTY_CONTENT && X_Node__updateRawNode( that, elm );
1603 // 4. style="display:none" の場合
1604 if( that[ '_flags' ] & X_NodeFlags_STYLE_IS_DISPLAY_NONE ){
1606 elm.style.display = 'none';
1607 if( elm.style.display !== 'none' ){ // ie4 の style は currentStyle 相当らしい、、、? div 以外への display:none が効かないので remove する。
1608 X_Node__actualRemove( that );
1612 return elm || prevElement;
1617 prevElement.insertAdjacentHTML( 'AfterEnd', X_Node__actualCreate( that, false ) ) :
1618 parentElement.insertAdjacentHTML( 'AfterBegin', X_Node__actualCreate( that, false ) );
1619 X_Node__afterActualCreate( that );
1620 return that[ '_rawObject' ] || X_Node__ie4getRawNode( that );
1623 accumulatedFlags |= that[ '_flags' ];
1625 xnodes = that[ '_xnodes' ];
1626 l = xnodes ? xnodes.length : 0;
1627 dirty = !!( that[ '_flags' ] & X_NodeFlags_IE4_DIRTY_CHILDREN );
1630 * HTML の下に TextNode だけ 。MIX_FIXED でない場合、削除、追加 を親に通知
1632 * HTML の下は MIX -> TextNode, html の削除、変更、追加
1633 * HTML の下は MIX_FIXED -> TextNode を <font> に置き換えてあるのでW3C DON 的に触ることができる
1636 that[ '_flags' ] &= ~X_NodeFlags_IE4_DIRTY_CHILDREN;
1637 for( i = 0; i < l; ++i ){
1638 if( xnodes[ i ][ '_tag' ] ){
1639 that[ '_flags' ] |= X_NodeFlags_IE4_HAS_ELEMENT;
1641 that[ '_flags' ] |= X_NodeFlags_IE4_HAS_TEXTNODE;
1643 if( that[ '_flags' ] & X_Node_BitMask_IE4_IS_MIX === X_Node_BitMask_IE4_IS_MIX ){
1650 if( that[ '_flags' ] & X_NodeFlags_IE4_FIXED || that[ '_flags' ] & X_Node_BitMask_IE4_IS_MIX === X_NodeFlags_IE4_HAS_ELEMENT ){
1651 for( i = 0; i < l; ++i ){
1652 prev = X_Node__commitUpdate( xnodes[ i ], elm, prev, accumulatedFlags );
1657 for( i = 0; i < l; ++i ){
1658 html[ i ] = X_Node__actualCreate( xnodes[ i ], false );
1660 elm.innerHTML = html.join( '' );
1661 for( i = 0; i < l; ++i ){
1662 X_Node__afterActualCreate( xnodes[ i ] );
1664 that[ '_flags' ] |= X_NodeFlags_IE4_FIXED;
1666 if( that[ '_flags' ] & X_NodeFlags_IE4_HAS_TEXTNODE ){
1667 dirty = dirty || false;
1668 for( i = 0; i < l; ++i ){
1670 if( text[ '_flags' ] & X_Node_BitMask_IS_DIRTY ){
1671 text[ '_flags' ] &= X_Node_BitMask_RESET_DIRTY;
1675 if( dirty ) elm.innerHTML = that[ 'text' ]();
1678 if( accumulatedFlags & X_Node_BitMask_IS_DIRTY ) delete that[ '_fontSize' ];
1680 that[ '_flags' ] &= ~X_NodeFlags_DIRTY_POSITION;
1681 that[ '_flags' ] & X_Node_BitMask_IS_DIRTY && X_Node__updateRawNode( that, elm );
1687 * GPU レイヤーするブラウザは、子要素から変更を当てていく? <- とりあえず、親要素から。
1689 var X_Node__updateRawNode =
1691 ( function( that, elm ){
1692 var attrs, rename, k, v;
1695 if( !that[ '_tag' ] ){
1696 elm.data = X_String_chrReferanceTo( that[ '_text' ] );
1697 that[ '_flags' ] &= X_Node_BitMask_RESET_DIRTY;
1701 if( that[ '_flags' ] & X_NodeFlags_DIRTY_ID ){
1703 ( ( that[ '_flags' ] & X_NodeFlags_IS_SVG ) ?
1704 elm.setAttribute( 'id', that[ '_id' ] ) :
1705 ( elm.id = that[ '_id' ] )
1707 ( elm.id && elm.removeAttribute( 'id' ) );
1710 if( that[ '_flags' ] & X_NodeFlags_DIRTY_CLASSNAME ){
1711 that[ '_className' ] ?
1712 ( ( that[ '_flags' ] & X_NodeFlags_IS_SVG ) ?
1713 elm.setAttribute( 'class', that[ '_className' ] ) :
1714 ( elm.className = that[ '_className' ] )
1716 ( elm.className && elm.removeAttribute( X_UA[ 'IE' ] < 8 ? 'className' : 'class' ) ); // className は ie7-
1720 if( that[ '_flags' ] & X_NodeFlags_DIRTY_ATTR && ( attrs = that[ '_newAttrs' ] || that[ '_attrs' ] ) ){
1721 rename = X_Node_Attr_renameForDOM;
1726 switch( that[ '_tag' ] + k ){
1727 case 'TEXTAREAvalue' :
1728 // IETester 5.5 ではエラーが出なかった.MultipulIE5.5 ではエラーが出たので
1729 // MultipleIE6 でもここを通す。X_UA[ 'ieExeComError' ] の場合 MultipleIE6
1730 if( ( !X_UA[ 'MacIE' ] && X_UA[ 'IE5x' ] ) || ( X_UA[ 'ieExeComError' ] && X_UA[ 'IE6' ] ) ){
1732 ( elm.firstChild.data = v || '' ) :
1733 elm.appendChild( document.createTextNode( v || '' ) );
1739 // http://outcloud.blogspot.jp/2010/09/iframe.html
1740 // この問題は firefox3.6 で確認
1741 if( X_UA[ 'Gecko' ] && elm.contentWindow ){
1742 elm.contentWindow.location.replace = elm.src = v || '';
1748 // http://d.hatena.ne.jp/NeoCat/20080921/1221940658
1749 // こちらに名前をsetしないとtargetが動作しない
1750 // これってあとから name を変更できないバグでは? itozyun
1751 // if( X_UA[ 'IE' ] ) elm.name = elm.contentWindow.name = v || '';
1754 //if( X_EMPTY_OBJECT[ k ] ) continue;
1755 // TODO IE では input, なぜか button, object も type, name の変更が出来ない、同値で置き換えようとしても不可
1757 elm.removeAttribute( rename[ k ] || k ) :
1758 ( ( that[ '_flags' ] & X_NodeFlags_IS_SVG ) ?
1759 elm.setAttribute( k, v ) :
1760 ( elm[ rename[ k ] || k ] = X_Node_Attr_noValue[ k ] ? k : v )
1763 delete that[ '_newAttrs' ];
1767 if( that[ '_flags' ] & X_NodeFlags_DIRTY_CSS ){
1768 if( that[ '_flags' ] & X_NodeFlags_OLD_CSSTEXT ? X_Node_CSS_objToCssText( that ) : that[ '_cssText' ] ){
1769 X_UA[ 'Opera78' ] || X_UA[ 'NN6' ] ?
1770 elm.setAttribute( 'style', that[ '_cssText' ] ) : // opera8用
1771 ( elm.style.cssText = that[ '_cssText' ] );
1773 elm.style.cssText = ''; // IE5.5以下 Safari3.2 で必要
1774 elm.removeAttribute( 'style' );
1777 if( that[ '_flags' ] & X_NodeFlags_DIRTY_IE_FILTER ){
1778 v = X_Node_CSS_objToIEFilterText( that );
1780 elm.style.filter = v;
1781 that[ '_flags' ] |= X_NodeFlags_IE_FILTER_NOW;
1783 elm.style.removeAttribute( 'filter' );
1784 that[ '_flags' ] &= ~X_NodeFlags_IE_FILTER_NOW;
1788 that[ '_flags' ] &= X_Node_BitMask_RESET_DIRTY;
1791 ( function( that, elm ){
1792 var attrs, rename, k, v;
1795 if( !that[ '_tag' ] ){
1796 elm.innerText = that[ '_text' ];
1797 that[ '_flags' ] &= X_Node_BitMask_RESET_DIRTY;
1802 * http://www.tohoho-web.com/js/element.htm
1803 * title、className、id、lang、language には setAttribute でなく、element.id で直接読み書きできる
1806 if( that[ '_flags' ] & X_NodeFlags_DIRTY_ID ) elm.setAttribute( 'id', that[ '_id' ] || ( 'ie4uid' + that[ '_uid' ] ) );
1809 if( that[ '_flags' ] & X_NodeFlags_DIRTY_CLASSNAME ){
1810 that[ '_className' ] ? ( elm.className = that[ '_className' ] ) : elm.removeAttribute( 'class' );
1813 if( that[ '_flags' ] & X_NodeFlags_DIRTY_CSS ){
1814 if( that[ '_flags' ] & X_NodeFlags_OLD_CSSTEXT ? X_Node_CSS_objToCssText( that ) : that[ '_cssText' ] ){
1815 elm.style.cssText = that[ '_cssText' ];
1817 elm.style.cssText = '';
1818 elm.removeAttribute( 'style' );
1821 if( that[ '_flags' ] & X_NodeFlags_DIRTY_IE_FILTER ){
1822 v = X_Node_CSS_objToIEFilterText( that );
1824 elm.style.filter = v;
1825 that[ '_flags' ] |= X_NodeFlags_IE_FILTER_NOW;
1827 elm.style.removeAttribute( 'filter' );
1828 that[ '_flags' ] &= ~X_NodeFlags_IE_FILTER_NOW;
1833 if( that[ '_flags' ] & X_NodeFlags_DIRTY_ATTR && ( attrs = that[ '_newAttrs' ] || that[ '_attrs' ] ) ){
1834 rename = X_Node_Attr_renameForDOM;
1836 //if( X_EMPTY_OBJECT[ k ] ) continue;
1837 ( v = attrs[ k ] ) === undefined ?
1838 elm.removeAttribute( rename[ k ] || k ) :
1839 elm.setAttribute( rename[ k ] || k, X_Node_Attr_noValue[ k ] ? k : v );
1841 delete that[ '_newAttrs' ];
1844 that[ '_flags' ] &= X_Node_BitMask_RESET_DIRTY;
1848 /* --------------------------------------
1851 * http://d.hatena.ne.jp/uupaa/20080718/1216362040
1852 * DOM Rangeが使える環境(Firefox2+,Opera9+,Safari3+)なら、innerHTMLいらずで、ガーって書けます。
1853 * return document.createRange().createContextualFragment("<div><select><option></option></select></div>");
1854 * insertAdjacentHTML
1856 * ie7 以下では iframe の frameborder や、input name は、createElement 後に setAttribute しても無視される
1858 * fragument がある場合 children も足して
1859 * Mozilla: 1.0+, IE: 5.5+, Netscape: 2.0+, Safari: 1.0+, Opera: 7.0+
1860 * ie6 大丈夫?fragment の場合リークしないか?チェックが必要
1861 * http://msdn.microsoft.com/ja-jp/library/bb250448%28v=vs.85%29.aspx
1863 * document.createElement of ie4 is only for OPTION & IMAGE.
1865 var X_Node__actualCreate =
1866 X_UA_DOM.IE4 && (function( that, isChild ){
1867 var uid = that[ '_uid' ],
1868 html, xnodes, n, i, l;
1870 if( !that[ '_tag' ] ){
1871 html = [ '<FONT id=ie4uid', uid, ' UID="', uid, '">', that[ '_text' ], '</FONT>' ];// fake textNode
1872 delete that[ '_rawObject' ];
1874 if( !isChild ) X_Node__actualRemove( that, /* true */ false );
1876 that[ '_flags' ] & X_NodeFlags_OLD_CSSTEXT && X_Node_CSS_objToCssText( that, true );
1879 '<', that[ '_tag' ], ' id=', ( that[ '_id' ] || ( 'ie4uid' + uid ) ), ' UID="', uid, '"',
1880 that[ '_className' ] ? ' class="' + that[ '_className' ] + '"' : '',
1881 X_Node_Attr_objToAttrText( that, true ),
1882 that[ '_cssText' ] ? ' style="' + that[ '_cssText' ] + '"' : '',
1886 if( ( xnodes = that[ '_xnodes' ] ) && ( l = xnodes.length ) ){
1888 that[ '_flags' ] &= ~X_NodeFlags_IE4_DIRTY_CHILDREN;
1889 for( i = 0; i < l; ++i ){
1890 if( xnodes[ i ][ '_tag' ] ){
1891 that[ '_flags' ] |= X_NodeFlags_IE4_HAS_ELEMENT;
1893 that[ '_flags' ] |= X_NodeFlags_IE4_HAS_TEXTNODE;
1895 if( that[ '_flags' ] & X_Node_BitMask_IE4_IS_MIX === X_Node_BitMask_IE4_IS_MIX ){
1900 if( that[ '_flags' ] & X_Node_BitMask_IE4_IS_MIX === X_NodeFlags_IE4_HAS_TEXTNODE ){
1902 html[ n ] = that[ 'text' ]();
1905 for( i = 0; i < l; ++i ){
1906 html[ n ] = X_Node__actualCreate( xnodes[ i ], true );
1909 that[ '_flags' ] |= X_NodeFlags_IE4_FIXED;
1912 X_Dom_DTD_EMPTY[ that[ '_tag' ] ] || ( html[ n ] = '<\/' + that[ '_tag' ] + '>' );
1914 that[ '_newAttrs' ] && ( that[ '_flags' ] |= X_NodeFlags_DIRTY_ATTR );
1917 return html.join( '' );
1920 var X_Node__afterActualCreate =
1921 X_UA_DOM.IE4 && (function( that ){
1924 if( !that[ '_tag' ] ) return that;
1926 if( ( xnodes = that[ '_xnodes' ] ) && ( i = xnodes.length ) ){
1928 X_Node__afterActualCreate( xnodes[ --i ] );
1931 // ネットワーク系属性と filter は要素生成後に適用
1932 if( that[ '_flags' ] & ( X_NodeFlags_DIRTY_ATTR | X_NodeFlags_DIRTY_IE_FILTER ) ){
1933 X_Node__updateRawNode( that, that[ '_rawObject' ] || X_Node__ie4getRawNode( that ) );
1935 that[ '_flags' ] &= X_Node_BitMask_RESET_DIRTY;
1937 X_EventDispatcher_toggleAllEvents( that, true );// イベントの復帰
1940 var X_Node__actualRemove =
1942 // GPUレイヤーにいるうちは remove しない。-> GPU解除してから remove する
1943 // Firefox34 では遭遇せず、Safari で何度かアニメーションしているうちに発生
1944 ( function( that, isChild ){
1945 var xnodes = that[ '_xnodes' ],
1946 elm = that[ '_rawObject' ],
1949 if( xnodes && ( l = xnodes.length ) ){
1950 for( i = 0; i < l; ++i ){
1951 child = xnodes[ i ];
1952 child[ '_tag' ] && X_Node__actualRemove( child, true );
1958 if( that[ '_flags' ] & X_NodeFlags_ACTUAL_LISTENING ){
1959 that[ '_listeners' ] && X_EventDispatcher_toggleAllEvents( that, false );// イベントの退避
1960 that[ '_flags' ] &= ~X_NodeFlags_ACTUAL_LISTENING;
1963 // ie5では filter の効いている要素をremove時に破棄して、再度append 時に新規生成する
1964 // ちなみに elm.filters に触ると ie8 でなぜかカラム落ちが発生、、、
1965 if( X_Node_displayNoneFixForIE5 ){
1966 if( elm.filters && elm.filters.length ){
1968 delete that[ '_rawObject' ];
1969 // 破棄前にインタラクティブな属性値を控える
1970 if( X_Node_Attr_HAS_VALUE[ that[ '_tag' ] ] && ( !that[ '_newAttrs' ] || !X_Object_inObject( 'value', that[ '_newAttrs' ] ) ) ){
1971 if( !that[ '_attrs' ] ) that[ '_attrs' ] = {};
1972 that[ '_attrs' ].value = elm.value;
1974 if( that[ '_tag' ] === 'OPTION' && ( !that[ '_newAttrs' ] || !X_Object_inObject( 'selected', that[ '_newAttrs' ] ) ) ){
1975 if( !that[ '_attrs' ] ) that[ '_attrs' ] = {};
1976 that[ '_attrs' ].selected = elm.selected;
1978 if( that[ '_tag' ] === 'SELECT' && ( !that[ '_newAttrs' ] || !X_Object_inObject( 'selectedIndex', that[ '_newAttrs' ] ) ) ){
1979 if( !that[ '_attrs' ] ) that[ '_attrs' ] = {};
1980 that[ '_attrs' ].selectedIndex = elm.selectedIndex;
1982 if( that[ '_tag' ] === 'INPUT' && that._attr && ( that._attr.type === 'checkbox' || that._attr.type === 'radio' ) && ( !that[ '_newAttrs' ] || !X_Object_inObject( 'checked', that[ '_newAttrs' ] ) ) ){
1983 if( !that[ '_attrs' ] ) that[ '_attrs' ] = {};
1984 that[ '_attrs' ].checked = elm.checked;
1991 if( !X_UA[ 'MacIE' ] ){
1992 // elm.parentNode.tagName for ie7
1993 !isChild && elm.parentNode && elm.parentNode.tagName && elm.parentNode.removeChild( elm );
1995 !isChild && elm.parentNode && elm.parentNode.tagName && X_TEMP._fixed_remove( elm, that );
1999 ( function( that, isChild ){
2000 var xnodes = that[ '_xnodes' ],
2001 elm = that[ '_rawObject' ] || X_Node__ie4getRawNode( that ),
2003 if( xnodes && ( l = xnodes.length ) ){
2004 for( i = 0; i < l; ++i ){
2005 X_Node__actualRemove( xnodes[ i ], true );
2010 that[ '_listeners' ] && X_EventDispatcher_toggleAllEvents( that, false );// イベントの退避
2012 // 破棄前にインタラクティブな属性値を控える
2013 if( X_Node_Attr_HAS_VALUE[ that[ '_tag' ] ] && ( !that[ '_newAttrs' ] || !X_Object_inObject( 'value', that[ '_newAttrs' ] ) ) ){
2014 if( !that[ '_attrs' ] ) that[ '_attrs' ] = {};
2015 that[ '_attrs' ].value = elm.value;
2017 if( that[ '_tag' ] === 'OPTION' && ( !that[ '_newAttrs' ] || !X_Object_inObject( 'selected', that[ '_newAttrs' ] ) ) ){
2018 if( !that[ '_attrs' ] ) that[ '_attrs' ] = {};
2019 that[ '_attrs' ].selected = elm.selected;
2021 if( that[ '_tag' ] === 'SELECT' && ( !that[ '_newAttrs' ] || !X_Object_inObject( 'selectedIndex', that[ '_newAttrs' ] ) ) ){
2022 if( !that[ '_attrs' ] ) that[ '_attrs' ] = {};
2023 that[ '_attrs' ].selectedIndex = elm.selectedIndex;
2025 if( that[ '_tag' ] === 'INPUT' && that._attr && ( that._attr.type === 'checkbox' || that._attr.type === 'radio' ) && ( !that[ '_newAttrs' ] || !X_Object_inObject( 'checked', that[ '_newAttrs' ] ) ) ){
2026 if( !that[ '_attrs' ] ) that[ '_attrs' ] = {};
2027 that[ '_attrs' ].checked = elm.checked;
2030 elm.removeAttribute( 'id' ); // ?
2031 //document.all[ that[ '_id' ] || ( 'ie4uid' + that[ '_uid' ] ) ] = null; // MacIE5 でエラー
2032 if( !isChild ) elm.outerHTML = ''; // elm.remove() ?
2033 delete that[ '_rawObject' ];
2037 X_ViewPort[ 'listenOnce' ]( X_EVENT_UNLOAD, X_Node__actualRemove, [ X_Node_html, true ] );