OSDN Git Service

Version 0.6.195, add opacity-fix for IE8.
[pettanr/clientJs.git] / 0.6.x / js / 02_dom / 20_XNode.js
1 /**
2  * Node( rawElement | rawTextnode | htmlString | textString )
3  *      
4  * @alias X.Node
5  * @class Node HTMLElement、TextNode をラップし jQuery 風な API で操作できます。
6  * @constructs Node
7  * @extends {EventDispatcher}
8  */
9 var     Node = X[ 'Node' ] = X_EventDispatcher[ 'inherits' ](
10         'X.Node',
11         X_Class.POOL_OBJECT,
12         {
13                 /**
14                  * 要素に振られるユニークID
15                  * @type {number}
16                  * @private
17                  * @alias Node.prototype._uid
18                  */
19                 '_uid'       : 0,
20                 
21                 /**
22                  * Node の状態を表すフラグ。
23                  * @type {number}
24                  * @private
25                  * @alias Node.prototype._flags
26                  */
27                 '_flags'     : X_NodeFlags_DESTROYED,
28
29                 '_rect'      : null, // TODO
30                 
31                 /**
32                  * 最後に計測したフォントサイズを保持している。ツリーが変更されると削除される。
33                  * @type {number}
34                  * @private
35                  * @alias Node.prototype._fontSize
36                  */
37                 '_fontSize'  : 0,
38                 
39                 /**
40                  * NodeList と動作を一致させるためのプロパティ。常に 1。
41                  * @type {number}
42                  * @const
43                  * @alias Node.prototype.length
44                  */
45                 length       : 1,
46                 
47                 /**
48                  * 親 Node。
49                  * @type {Node}
50                  * @alias Node.prototype.parent
51                  */
52                 parent       : null, // remove された枝も親子構造は維持している。
53                 
54                 /**
55                  * 子 Node リスト
56                  * @type {Array}
57                  * @private
58                  * @alias Node.prototype._xnodes
59                  */
60                 '_xnodes'    : null,
61                 
62                 /**
63                  * GPU レイヤーに転送されている場合、その一番親となっている Node。未実装。
64                  * @type {Node}
65                  * @private
66                  * @alias Node.prototype._gpuParent
67                  */
68                 '_gpuParent' : null,
69
70                 /**
71                  * タグ名。テキストノードの場合は空文字列。
72                  * @type {string}
73                  * @private
74                  * @alias Node.prototype._tag
75                  */
76                 '_tag'       : '',
77                 
78                 /**
79                  * テキストコンテンツ。テキストノードで使用。
80                  * @type {string}
81                  * @private
82                  * @alias Node.prototype._text
83                  */
84                 '_text'      : '',
85                 
86                 /**
87                  * id
88                  * @type {string}
89                  * @private
90                  * @alias Node.prototype._id
91                  */
92                 '_id'        : '',
93                 
94                 /**
95                  * クラス名。複数のクラスが設定されている場合、スペース区切り。
96                  * @type {string}
97                  * @private
98                  * @alias Node.prototype._className
99                  */
100                 '_className' : '', //
101
102                 /**
103                  * 属性。
104                  * @type {object}
105                  * @private
106                  * @alias Node.prototype._attrs
107                  */
108                 '_attrs'     : null, // see X_Node_Attr
109                 
110                 /**
111                  * まだコミットされていない属性。
112                  * @type {object}
113                  * @private
114                  * @alias Node.prototype._newAttrs
115                  */
116                 '_newAttrs'  : null,
117                 
118                 /**
119                  * 属性を文字列にしたもの。 color="red" size="8"
120                  * @type {object}
121                  * @private
122                  * @alias Node.prototype._attrText
123                  */
124                 '_attrText'  : '',
125                 
126                 /**
127                  * スタイル。
128                  * @type {object}
129                  * @private
130                  * @alias Node.prototype._css
131                  */
132                 '_css'       : null,
133                 
134                 /**
135                  * cssText
136                  * @type {string}
137                  * @private
138                  * @alias Node.prototype._cssText
139                  */
140                 '_cssText'   : '',
141                 
142                 /**
143                  * アニメーション用オブジェクト。
144                  * @type {object}
145                  * @private
146                  * @alias Node.prototype._anime
147                  */
148                 '_anime'     : null,
149                 
150         /*
151          * TODO Node の継承ができない!
152          */
153                 'Constructor' : function( v ){
154                         // TODO uid = X_Node_CHASHE.indexOf( null ), uid === -1 ? X_Node_CHASHE.length : uid;
155                         var uid = X_Node_CHASHE.length,
156                                 css, xnodes, xnode, parent;
157                         
158                         if( X_Node_newByTag ){
159                                 X_Node_newByTag = false;
160                                 this[ '_tag' ]  = v.toUpperCase();
161                                 arguments[ 1 ] && this[ 'attr' ]( arguments[ 1 ] );
162                                 css = arguments[ 2 ];
163                                 css && this[ X_Type_isString( css ) ? 'cssText' : 'css' ]( css );
164                         } else
165                         if( X_Node_newByText ){
166                                 X_Node_newByText = false;
167                                 this[ '_text' ]  = v;
168                         } else {
169                                 if( 1 < arguments.length ) return new X_NodeList( arguments );
170                                 if( X_Type_isArray( v ) && v.length ) return new X_NodeList( v );
171
172                                 switch( X_Node_getType( v ) ){
173                                         case X_NodeType_XNODE :
174                                         case X_NodeType_XNODE_LIST :
175                                                 return v;
176
177                                         case X_NodeType_RAW_HTML :
178                                                 if( xnode = X_Node_getXNode( v ) ) return xnode;
179                                                 // v.parentNode || v.parentElement : dom1 || dom0
180                                                 this.parent     = ( parent = v.parentNode || v.parentElement ) && parent.tagName /* ie7- */ && X_Node_getXNode( parent );
181                                                 this[ '_rawObject' ] = v;
182                                                 this[ '_tag' ]       = v.tagName.toUpperCase();
183                                                 this[ '_id' ]        = v.id;
184                                                 this[ '_className' ] = v.className;
185                                                 
186                                                 this[ 'cssText' ]( v.style.cssText );
187                                                 this[ '_flags' ] &= X_Node_BitMask_RESET_DIRTY; // X_NodeFlags_DIRTY_CSS を落とす
188                                                 
189                                                 // TODO attr の回収は不可能、、、?
190                                                 if( X_UA_DOM.IE4 ){
191                                                         v.setAttribute( 'UID', '' + uid );
192                                                 } else {
193                                                         v[ 'UID' ] = uid;
194                                                 };
195                                                 // childNodes...
196                                                 break;
197
198                                         case X_NodeType_RAW_TEXT :
199                                                 if( xnode = X_Node_getXNode( v ) ) return xnode;
200                                                 this.parent     = X_Node_getXNode( v.parentNode );
201                                                 this[ '_rawObject' ] = v;
202                                                 this[ '_text' ]      = v.data;
203                                                 v[ 'UID' ] = uid;
204                                                 break;
205
206                                         case X_NodeType_HTML_STRING :
207                                         case X_NodeType_STRING :
208                                                 if( xnodes = X_HtmlParser_parse( v, true ) && 1 < xnodes.length ) return new X_NodeList( xnodes );
209                                                 if( xnodes.length ) return xnodes[ 0 ];
210                                                 return X_Node_none;
211
212                                         default :
213                                                 if( X_Node_none ) return X_Node_none;
214                                                 this.length = 0;
215                                                 return;
216                                 };
217                         };
218                         
219                         if( this.parent && ( this.parent[ '_flags' ] & X_NodeFlags_IN_TREE ) ){
220                                 this[ '_flags' ] |= X_NodeFlags_IN_TREE;
221                         };
222                         this[ '_flags' ] |= X_NodeFlags_EXIST;
223                         X_Node_CHASHE[ this[ '_uid' ] = uid ] = this;
224                 },
225                 
226                 'width'          : X_Node_width,
227                 'height'         : X_Node_height,
228                 'clientWidth'    : X_Node_clientWidth,
229                 'clientHeight'   : X_Node_clientHeight,
230                 'scrollWidth'    : X_Node_scrollWidth,
231                 'scrollHeight'   : X_Node_scrollHeight,
232                 'scrollLeft'     : X_Node_scrollLeft,
233                 'scrollTop'      : X_Node_scrollTop,
234                 'x'              : X_Node_x,
235                 'y'              : X_Node_y,
236                 'offset'         : X_Node_offset,
237
238                 'attr'           : X_Node_attr,
239                 'css'            : X_Node_css,
240                 'cssText'        : X_Node_cssText,
241
242                 'find'           : X_Node_find,
243                 
244                 'animate'        : X_Node_animate,
245                 'stop'           : X_Node_stop,
246                 
247                 
248                 'create'         : X_Node_create,
249                 
250                 'createAt'       : X_Node_createAt,
251                 
252                 'createText'     : X_Node_createText,
253                 
254                 'createTextAt'   : X_Node_createTextAt,
255                 
256                 'clone'          : X_Node_clone,
257                 
258                 'append'         : X_Node_append,
259                 
260                 'appendAt'       : X_Node_appendAt,
261                 
262                 'appendTo'       : X_Node_appendTo,
263                 
264                 'prev'           : X_Node_prev,
265                 
266                 'next'           : X_Node_next,
267                 
268                 'swap'           : X_Node_swap,
269                 
270                 'remove'         : X_Node_remove,
271                 
272                 'empty'          : X_Node_empty,
273                 
274                 'contains'       : X_Node_contains,
275                 
276                 'getChildAt'     : X_Node_getChildAt,
277                 
278                 'numChildren'    : X_Node_numChildren,
279                 
280                 'firstChild'     : X_Node_firstChild,
281                 
282                 'lastChild'      : X_Node_lastChild,
283                 
284                 'getOrder'       : X_Node_getOrder,
285                 
286                 'className'      : X_Node_className,
287                 'addClass'       : X_Node_addClass,
288                 'removeClass'    : X_Node_removeClass,
289                 'toggleClass'    : X_Node_toggleClass,
290                 'hasClass'       : X_Node_hasClass,
291                 
292                 'html'           : X_Node_html,
293                 'text'           : X_Node_text,
294                 'call'           : X_Node_call,
295                 'each'           : X_Node_each
296                 
297         }
298 );
299
300 var X_NodeType_XNODE       = 1,
301         X_NodeType_RAW_HTML    = 2,
302         X_NodeType_RAW_TEXT    = 3,
303         X_NodeType_HTML_STRING = 4,
304         X_NodeType_STRING      = 5,
305                 //DOC_FRAG    = 6,
306         X_NodeType_XNODE_LIST  = 7,
307         X_NodeType_WINDOW      = 8,
308         X_NodeType_DOCUMENT    = 9,
309         X_NodeType_IMAGE       = 10,
310         
311         X_Node_strictElmCreation    = !X_UA[ 'MacIE' ] && X_UA[ 'IE' ] <= 8,
312         
313         X_Node_documentFragment     = document.createDocumentFragment && ( !X_UA[ 'IE' ] || 5.5 <= X_UA[ 'IE' ] ) && document.createDocumentFragment(),
314         
315         // 子の生成後に リアル文書 tree に追加する
316         X_Node_addTreeAfterChildren = !( X_UA[ 'IE' ] < 9 ),
317         
318         X_Node_displayNoneFixForIE5 = !!X_NodeFlags_IE5_DISPLAY_NONE_FIX,
319         
320         X_Node_newByTag      = false,
321         
322         X_Node_newByText     = false,
323         
324         X_Node_outerXNode    = null,
325         
326         X_Node_updateTimerID = 0,
327
328         // XMLかどうかを判別する
329         X_Node_isXmlDocument =
330                 X_UA_DOM.IE4 ?
331                         X_emptyFunction :
332                         (function( root ){
333                                 if( X_Type_isBoolean( root.isXML ) ) return root.isXML;
334                                 return root.isXML = root[ '_rawObject' ].createElement( 'p' ).tagName !== root[ '_rawObject' ].createElement( 'P' ).tagName;
335                         }),
336         X_Node_CHASHE     = [],
337         X_Node_none       = X_Node_CHASHE[ 0 ] = Node(),
338         X_Node_html, // = <html>
339         X_Node_head, // = <head>
340         X_Node_body, // = <body>
341         X_Node_systemNode, // = X_Node_CHASHE[ ? ]
342         X_Node_fontSizeNode,
343 /*
344  * remove :
345  *  X_Node_reserveRemoval = [] に追加。commitUpdate で remove
346  * add :
347  *  X_Node_reserveRemoval にいたら消す, new_parent[ '_xnodes' ] に挿入
348  */
349         X_Node_reserveRemoval = [];
350
351 function X_Node_getType( v ){
352         if( v === '' ) return X_NodeType_STRING;
353         if( !v ) return 0;
354         if( v === window ) return X_NodeType_WINDOW;
355         if( v === document ) return X_NodeType_DOCUMENT;
356         if( v.constructor === Node ) return X_NodeType_XNODE;
357         if( v.constructor === X_NodeList ) return X_NodeType_XNODE_LIST;
358         if( X_Type_isHTMLElement( v ) ) return X_NodeType_RAW_HTML;
359         if( v.nodeType === 3 ) return X_NodeType_RAW_TEXT;
360         if( X_Type_isString( v ) ){
361                 return '<' === v.charAt( 0 ) && v.charAt( v.length - 1 ) === '>' ? X_NodeType_HTML_STRING : X_NodeType_STRING;
362         };
363         if( v[ 'instanceOf' ] && v[ 'instanceOf' ]( Node ) ) return X_NodeType_XNODE;
364         return 0;
365 };
366 function X_Node_getXNode( v ){
367         var uid, i, chashe, xnode;
368         switch( X_Node_getType( v ) ){
369                 case X_NodeType_XNODE :
370                 case X_NodeType_XNODE_LIST :
371                         return v;
372                 case X_NodeType_RAW_HTML :
373                         // fake TextNode too.
374                         if( X_UA_DOM.IE4 ){
375                                 uid = v.getAttribute( 'UID' );
376                                 return uid && X_Node_CHASHE[ uid ];
377                         };
378                         return v[ 'UID' ] && X_Node_CHASHE[ v[ 'UID' ] ];
379                 case X_NodeType_WINDOW :
380                         return X_ViewPort;
381                 case X_NodeType_DOCUMENT :
382                         return X_ViewPort_document;
383                 case X_NodeType_RAW_TEXT :
384                         if( v[ 'UID' ] ) return X_Node_CHASHE[ v[ 'UID' ] ];
385                         for( chashe = X_Node_CHASHE, i = chashe.length; i; ){
386                                 if( ( xnode = chashe[ --i ] ) && ( xnode[ '_rawObject' ] === v ) ) return xnode;
387                         };
388         };
389 };
390
391 function X_Node_getRoot( xnode ){
392         return X_ViewPort_document;
393         //return X_Node_body[ '_rawObject' ].documentElement ? node : node.ownerDocument || node.document;
394 };
395
396
397 // TODO document.all[ uid ] -> document[ uid ] 
398 var X_Node__ie4getRawNode = X_UA_DOM.IE4 && function ( that ){
399                 return that[ '_rawObject' ] ||
400                         ( that[ '_rawObject' ] = document.all[ 'ie4uid' + that[ '_uid' ] ] || that[ '_id' ] && document.all[ that[ '_id' ] ] );
401         };
402
403
404 function X_Node_toggleInTreeFlag( xnodes, flag ){
405         var i = xnodes.length, xnode;
406         for( ; i; ){
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 );
410         };
411 };
412
413 function X_Node_toggleInGPUFlag( gpuRoot, xnodes, flag ){
414         var i = xnodes.length, xnode;
415
416         if( flag ){
417                 for( ; i; ){
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 );     
423                         };
424                 };
425         } else {
426                 for( ; i; ){
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 );
432                         };
433                 };
434         };
435 };
436
437 /**
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' );
445  */
446 function X_Node_create( tag, opt_attrs, opt_css ){
447         var xnode;
448         if( !this[ '_tag' ] ) return;
449         this[ 'append' ]( xnode = X_Doc_create( tag, opt_attrs, opt_css ) );
450         return xnode;
451 };
452 /**
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' );
461  */
462 function X_Node_createAt( index, tag, opt_attrs, opt_css ){
463         var xnode;
464         if( !this[ '_tag' ] ) return;
465         this[ 'appendAt' ]( index, xnode = X_Doc_create( tag, opt_attrs, opt_css ) );
466         return xnode;
467 };
468
469 /**
470  * テキストを指定して新規にテキストノードを作成し、現在のノードに挿入する。
471  * @alias Node.prototype.createText
472  * @param {string} [tag] テキスト
473  * @return {Node} 新規作成されたノード
474  */
475 function X_Node_createText( text ){
476         var xnode;
477         if( !this[ '_tag' ] ) return;
478         this[ 'append' ]( xnode = X_Doc_createText( text ) );
479         return xnode;
480 };
481 /**
482  * 挿入位置とテキストを指定して新規に子ノードを作成し、現在のノードに挿入する。
483  * @alias Node.prototype.createTextAt
484  * @param {number} [index] 挿入位置
485  * @param {string} [tag] テキスト
486  * @return {Node} 新規作成されたノード
487  */
488 function X_Node_createTextAt( index, text ){
489         var xnode;
490         if( !this[ '_tag' ] ) return;
491         this[ 'appendAt' ]( index, xnode = X_Doc_createText( text ) );
492         return xnode;
493 };
494
495 /**
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] 子要素のクローンを行うか?
501  * @return {Node}
502  */
503 function X_Node_clone( opt_clone_children ){
504         var xnode, xnodes, i, l;
505         
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' ] );
511                 
512                 if( this[ '_flags' ] & X_NodeFlags_IS_SVG ){
513                         xnode[ '_flags' ] |= X_NodeFlags_IS_SVG;
514                 };
515                 if( this[ '_flags' ] & X_NodeFlags_IS_VML ){
516                         xnode[ '_flags' ] |= X_NodeFlags_IS_VML;
517                 };
518                 
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 ) );
522                         };
523                 };
524                 return xnode;
525         };
526         X_Node_newByText = true;
527         return Node( this[ '_text' ] );
528 };
529
530 /**
531  * ノードを子配列の最後に追加する。文字列が渡された場合、HTMLパーサーによって Node ツリーを作成して追加する。HtmlElement, TextNode の場合は内部使用専用。
532  * @alias Node.prototype.append
533  * @param {Node|string|HTMLElement|TextNode} [v] HTMLElement と TextNode は内部のみ。
534  * @return {Node} 自身。チェインメソッド
535  * @example //
536  * myNode.append( node );
537  * myNode.append( node, '&lt;span&gt;Hello,&lt;/span&gt;', 'world.' );
538  */
539 function X_Node_append( v ){
540         var i, l, xnodes, frg;
541         if( !this[ '_tag' ] ) return;
542         
543         if( 1 < ( l = arguments.length ) ){
544                 for( i = 0; i < l; ++i ){
545                         this[ 'append' ]( arguments[ i ] );
546                 };
547                 return this;
548         };
549         
550         if( !( xnodes = this[ '_xnodes' ] ) ) this[ '_xnodes' ] = xnodes = [];
551         
552         switch( X_Node_getType( v ) ){
553                 case X_NodeType_RAW_HTML :
554                 case X_NodeType_RAW_TEXT :
555                         v = Node( v );
556                         break;
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;
565                         break;
566                 default :
567                         return this;
568         };
569
570         v.parent = this;
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();
576         };
577         if( this[ '_flags' ] & X_NodeFlags_IS_SVG ){
578                 v[ '_flags' ] |= X_NodeFlags_IS_SVG;
579         };
580         if( this[ '_flags' ] & X_NodeFlags_IS_VML ){
581                 v[ '_flags' ] |= X_NodeFlags_IS_VML;
582         };
583         return this;
584 };
585
586 /**
587  * ノードを挿入位置に追加する。
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 );
593  */
594 function X_Node_appendAt( start, v ){
595         var xnodes, l, i;
596         
597         if( !this[ '_tag' ] ) return this;
598         
599         l = arguments.length;
600         if( !( xnodes = this[ '_xnodes' ] ) ) xnodes = this[ '_xnodes' ] = [];
601         
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 ] );
606                 };
607                 return this;
608         };
609         if( start < 0 ) start = 0;
610         if( 2 < l ){
611                 for( ; l; ){
612                         this[ 'appendAt' ]( start, arguments[ --l ] );
613                 };
614                 return this;
615         };
616
617         switch( X_Node_getType( v ) ){
618                 case X_NodeType_RAW_HTML :
619                 case X_NodeType_RAW_TEXT :
620                         v = Node( v );
621                         break;
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 ] );
627                         };
628                         return this;
629                 case X_NodeType_XNODE :
630                         // 親の xnodes から v を消す
631                         if( v.parent ){
632                                 if( v.parent === this ){
633                                         i = v[ 'getOrder' ]();
634                                         if( i === start ) return this;
635                                         if( i < start ) --start;
636                                 };
637                                 v[ 'remove' ]();
638                         };
639                         // IE4 でテキストノードの追加、FIXED 済でない場合、親に要素の追加を通知
640                         if( X_UA[ 'IE4' ] && !v[ '_tag' ] && ( ( this[ '_flags' ] & X_NodeFlags_IE4_FIXED ) === 0 ) ) this[ '_flags' ] |= X_NodeFlags_IE4_DIRTY_CHILDREN;
641                         break;
642                 default :
643                         return this;
644         };
645
646         v.parent = this;
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();
652         };
653         if( this[ '_flags' ] & X_NodeFlags_IS_SVG ){
654                 v[ '_flags' ] |= X_NodeFlags_IS_SVG;
655         };
656         if( this[ '_flags' ] & X_NodeFlags_IS_VML ){
657                 v[ '_flags' ] |= X_NodeFlags_IS_VML;
658         };
659         return this;
660 };
661
662 /**
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 );
669  */
670 function X_Node_appendTo( parent, opt_index ){
671         switch( X_Node_getType( parent ) ){
672                 case X_NodeType_RAW_HTML :
673                         parent = Node( parent );
674                         break;
675                 case X_NodeType_HTML_STRING :
676                         parent = X_HtmlParser_parse( parent, true );
677                         parent = parent[ 0 ] || parent;
678                 case X_NodeType_XNODE :
679                         break;
680                 default :
681                         return this;
682         };
683         X_Type_isFinite( opt_index ) ? parent[ 'appendAt' ]( opt_index, this ) : parent[ 'append' ]( this );
684         return this;
685 };
686
687
688 /**
689  * ノードの直前の要素を取得。または直前に挿入。挿入する要素が先にいる兄弟でも正しく動作する。
690  * @alias Node.prototype.prev
691  * @param {Node|string|HTMLElement|TextNode} [...v] HTMLElement と TextNode は内部のみ。
692  * @return {Node} 自身。チェインメソッド
693  * @example childNode.prev( prevNode );
694  */
695 function X_Node_prev( v ){
696         var parent = this.parent, xnodes, i, l;
697         
698         // getter
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;
704         };
705         
706         if( !parent ) return this;
707         
708         l = arguments.length;
709         if( 1 < l ){
710                 for( i = 0; l; ++i ){
711                         parent[ 'appendAt' ]( this[ 'getOrder' ]() - i, arguments[ --l ] );
712                 };
713                 return this;
714         };
715         parent[ 'appendAt' ]( this[ 'getOrder' ](), v );
716         return this;
717 };
718
719 /**
720  * ノードの直後の要素を取得。または直後に挿入。挿入する要素が先にいる兄弟でも正しく動作する。
721  * @alias Node.prototype.next
722  * @param {Node|string|HTMLElement|TextNode} [v] HTMLElement と TextNode は内部のみ。
723  * @return {Node} 自身。チェインメソッド
724  * @example childNode.next( prevNode );
725  */
726 function X_Node_next( v ){
727         var parent = this.parent, xnodes, i, l, start;
728         
729         // getter
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;
735         };
736         
737         if( !parent ) return this;
738         
739         l = arguments.length;
740         start = this[ 'getOrder' ]() + 1;
741         
742         if( parent[ '_xnodes' ].length <= start ){
743                 for( i = 0; i < l; ++i ){
744                         parent[ 'append' ]( arguments[ i ] );
745                 };
746         } else
747         if( 1 < l ){
748                 for( ; l; ){
749                         parent[ 'appendAt' ]( this[ 'getOrder' ]() + 1, arguments[ --l ] );
750                 };
751         } else {
752                 parent[ 'appendAt' ]( start, v );
753         };
754         return this;
755 };
756
757 /**
758  * 要素の入れ替え。自身は remove() される。
759  * @alias Node.prototype.swap
760  * @param {Node|string|HTMLElement|TextNode} [v] HTMLElement と TextNode は内部のみ。
761  * @return {Node} 自身。チェインメソッド
762  * @example node.swap( newNode );
763  */
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' ]();
767 };
768
769 /**
770  * 要素を親要素から抜く。jQuery の remove と異なり、インスタンスは破壊(kill)されず、再び別の親に挿入等できる
771  * @alias Node.prototype.remove
772  * @return {Node} 自身。チェインメソッド
773  * @example node.remove();
774  * parent.append( node ); 新しい親に追加できる
775  */
776 function X_Node_remove(){
777         var parent = this.parent,
778                 elm;
779         
780         if( !parent ) return this;
781
782         delete this.parent;
783         parent[ '_xnodes' ].splice( parent[ '_xnodes' ].indexOf( this ), 1 );
784         
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 );
788
789                 if( X_UA_DOM.IE4 ){
790                         if( elm = this[ '_rawObject' ] || X_Node__ie4getRawNode( this ) ){
791                                 X_Node_reserveRemoval[ X_Node_reserveRemoval.length ] = this;
792                                 X_Node_reserveUpdate();                 
793                         } else
794                         if( !this[ '_tag' ] && ( ( parent[ '_flags' ] & X_NodeFlags_IE4_FIXED ) === 0 ) ){
795                                 parent[ '_flags' ] |= X_NodeFlags_IE4_DIRTY_CHILDREN;
796                         };
797                 } else {
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();                 
802                         };
803                 };
804         } else {
805                 if( !X_UA_DOM.IE4 ){
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();                 
810                         };
811                 };
812         };
813         return this;
814 };
815
816 /**
817  * 子要素を破棄する。子要素は kill() されます。
818  * @alias Node.prototype.empty
819  * @return {Node} 自身。チェインメソッド
820  * @example node.empty();
821  */
822 function X_Node_empty(){
823         var xnodes = this[ '_xnodes' ], i;
824         
825         if( xnodes && ( i = xnodes.length ) ){
826                 delete this[ '_xnodes' ];
827                 for( ; i; ){
828                         xnodes[ --i ][ 'kill' ]();
829                 };
830                 xnodes.length = 0;
831         };
832         return this;
833 };
834
835 function X_Node_onKill( that ){
836         var parent = that.parent,
837                 xnodes = that[ '_xnodes' ], i, elm;
838         
839         if( ( that[ '_flags' ] & X_NodeFlags_EXIST ) === 0 ) return;
840         
841         parent && parent[ '_xnodes' ] && parent[ '_xnodes' ].splice( parent[ '_xnodes' ].indexOf( that ), 1 );
842
843         elm = that[ '_rawObject' ] || X_UA_DOM.IE4 && X_Node__ie4getRawNode( that );
844
845         if( xnodes && ( i = xnodes.length ) ){
846                 delete that[ '_xnodes' ];
847                 for( ; i; ){
848                         xnodes[ --i ][ 'kill' ]();
849                 };
850                 xnodes.length = 0;
851         };
852
853         X_Node_CHASHE[ that[ '_uid' ] ] = null; // array に対して delete X_Node_CHASHE[ uid ] はまずい!
854         
855 // remove _xnodes
856         if( X_UA_DOM.IE4 ){
857                 if( elm ){
858                         X_Node_reserveRemoval[ X_Node_reserveRemoval.length ] = elm;
859                         X_Node_reserveUpdate();                 
860                 } else
861                 if( !that[ '_tag' ] && ( ( parent[ '_flags' ] & X_NodeFlags_IE4_FIXED ) === 0 ) ){
862                         parent[ '_flags' ] |= X_NodeFlags_IE4_DIRTY_CHILDREN;
863                 };
864         } else {
865                 if( elm && elm.parentNode && elm.parentNode.tagName ){
866                         X_Node_reserveRemoval[ X_Node_reserveRemoval.length ] = elm;
867                         X_Node_reserveUpdate();
868                 };
869         };
870 };
871
872
873
874
875 /**
876  * 要素を子以下に持つか?調べる。
877  * @alias Node.prototype.contains
878  * @param {Node|string|HTMLElement|TextNode} [v] HTMLElement と TextNode は内部のみ。
879  * @return {boolean} 
880  * @example node.contains( testNode );
881  */
882 function X_Node_contains( v ){
883         var elm, type, xnodes, i;
884         if( !v || !this[ '_tag' ] || this === v ) return false;
885         // contains ie4+
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 );
888         };
889
890         //if( document.compareDocumentPosition ){
891         //      
892         //};
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;
898         };
899         return false;
900 };
901
902 /**
903  * index の子要素を取得する。
904  * @alias Node.prototype.getChildAt
905  * @param {number} index 取得する子ノードの位置。0~
906  * @return {Node} 子要素
907  * @example child1 = parent.getChildAt(1);
908  */
909 function X_Node_getChildAt( i ){
910         var xnodes = this[ '_xnodes' ];
911         return xnodes && 0 <= i && i < xnodes.length && xnodes[ i ];
912 };
913
914 /**
915  * 子要素の数を取得する。
916  * @alias Node.prototype.numChildren
917  * @return {number} 子要素の数。
918  * @example n = parent.numChildren();
919  */
920 function X_Node_numChildren(){
921         var xnodes = this[ '_xnodes' ];
922         return xnodes ? xnodes.length : 0;
923 };
924
925 /**
926  * 最初の子要素を取得する。
927  * @alias Node.prototype.firstChild
928  * @return {Node} 最初の子要素
929  * @example child0 = parent.firstChild();
930  */
931 function X_Node_firstChild(){
932         return this[ '_xnodes' ] && this[ '_xnodes' ][ 0 ];
933 };
934
935 /**
936  * 最後の子要素を取得する。
937  * @alias Node.prototype.lastChild
938  * @return {Node} 最後の子要素
939  * @example lastChild = parent.lastChild();
940  */
941 function X_Node_lastChild(){
942         var xnodes = this[ '_xnodes' ];
943         return xnodes && xnodes[ xnodes.length - 1 ];
944 };
945
946 /**
947  * 要素の index 位置を取得する。
948  * @alias Node.prototype.getOrder
949  * @return {number} index -1 の場合、親を持たない。
950  * @example index = node.getOrder();
951  */
952 function X_Node_getOrder(){
953         var parent = this.parent;
954         return this === X_Node_html ?
955                                 0 :
956                    parent ?
957                                 parent[ '_xnodes' ].indexOf( this ) :
958                                 -1;
959 };
960
961 /**
962  * className の取得と設定。
963  * @alias Node.prototype.className
964  * @return {string|Node} getter の場合 class 文字列、setter の場合自身。
965  * @example // getter
966  * className = node.className();
967  * // setter
968  * node.className( 'myClass myClass_new' );
969  */
970 function X_Node_className( v ){
971         var node, _, __;
972         // getter
973         if( v === undefined ) return this[ '_className' ];
974         
975         // setter
976         if( this[ '_className' ] === v ) return this;
977         if( !v || !X_Type_isString( v ) ){
978                 delete this[ '_className' ];
979         } else {
980                 // cleanup
981                 _  = ' ';
982                 __ = '  ';
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 ) );
986                 
987                 if( this[ '_className' ] === v ) return this;
988                 v ? ( this[ '_className' ] = v ) : delete this[ '_className' ];
989         };
990         this[ '_flags' ] |= X_NodeFlags_DIRTY_CLASSNAME;
991         this[ '_flags' ] & X_NodeFlags_IN_TREE && X_Node_reserveUpdate();
992         return this;
993 };
994
995 /**
996  * className の追加。
997  * @alias Node.prototype.addClass
998  * @param {string} className スペース区切りで複数のクラスを追加できる。
999  * @return {Node} 自身。
1000  * @example node.addClass( 'myClass myClass_new' );
1001  */
1002 function X_Node_addClass( v ){
1003         var names  = v.split( ' ' ),
1004                 i      = names.length,
1005                 _class = this[ '_className' ],
1006                 name;
1007         v = '';
1008         for( ; i; ){
1009                 name = names[ --i ];
1010                 if( !name ) continue;
1011                 !this[ 'hasClass' ]( name ) && ( v += ( v ? ' ' : '' ) + name );
1012         };
1013         return v ? this[ 'className' ]( ( _class ? _class + ' ' : '' ) + v ) : this;
1014 };
1015
1016 /**
1017  * className の削除。
1018  * @alias Node.prototype.removeClass
1019  * @param {string} className スペース区切りで複数のクラスを削除できる。
1020  * @return {Node} 自身。
1021  * @example node.removeClass( 'myClass myClass_new' );
1022  */
1023 function X_Node_removeClass( v ){
1024         var _      = ' ',
1025                 _class = this[ '_className' ],
1026                 names  = v.split( _ ),
1027                 classNames, i, f, j;
1028
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 );
1036                                 f = true;
1037                                 break;
1038                         };
1039                 };
1040         };
1041         return f ? this[ 'className' ]( classNames.join( _ ) ) : this;
1042 };
1043
1044 /**
1045  * className の更新。
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 );
1051  */
1052 function X_Node_toggleClass( v, opt_toggle ){
1053         var names, i, name;
1054         if( opt_toggle !== undefined ){
1055                 return !opt_toggle ? this[ 'removeClass' ]( v ) : this[ 'addClass' ]( v );      
1056         };
1057         names = v.split( ' ' );
1058         for( i = names.length; i; ){
1059                 name = names[ --i ];
1060                 this[ 'hasClass' ]( name ) ? this[ 'removeClass' ]( name ) : this[ 'addClass' ]( name );
1061         };
1062         return this;
1063 };
1064
1065 /**
1066  * className を持つか。
1067  * @alias Node.prototype.hasClass
1068  * @param {string} className スペース区切りで複数のクラスを削除できる。
1069  * @return {boolean} 
1070  * @example node.hasClass( 'myClass myClass_new' );
1071  */
1072 function X_Node_hasClass( v ){
1073         var _ = ' ',
1074                 _class = this[ '_className' ],
1075                 i, name;
1076         if( _class === v ) return true;
1077         if( !_class ) return false;
1078         
1079         _class = _ + _class + _;
1080         if( _class.indexOf( _ + v + _ ) !== -1 ) return true; // lucky hit
1081         
1082         for( v = v.split( _ ), i = v.length; i; ){
1083                 name = v[ --i ];
1084                 if( name === '' ) continue;
1085                 if( _class.indexOf( _ + name + _ ) === -1 ) return false;
1086         };
1087         return true;
1088 };
1089
1090 /**
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>' );
1096  */
1097 function X_Node_html( html ){
1098         var _ = '', q = '"', xnodes, n, i, l;
1099         // setter
1100         if( html !== undefined ){ // String 以外に Number や false null なども許可
1101                 if( !this[ '_tag' ] ) return this[ 'text' ]( html );
1102                 
1103                 this[ 'empty' ]();
1104                 if( html ){
1105                         X_Node_append.apply( this, X_HtmlParser_parse( html, true ) );
1106                 };
1107                 return this;
1108         };
1109         
1110         // getter
1111         if( !this[ '_tag' ] ){
1112                 return this[ '_text' ];
1113         };
1114         
1115         this[ '_flags' ] & X_NodeFlags_OLD_CSSTEXT && X_Node_CSS_objToCssText( this );
1116
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 : _,
1123         '>' ];
1124         
1125         n = html.length;
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' ]();
1130                         ++n;
1131                 };
1132                 if( X_Node_outerXNode === this ) X_Node_outerXNode = null;
1133         };
1134         !X_Node_outerXNode || X_Dom_DTD_EMPTY[ this[ '_tag' ] ] || ( html[ n ] = '<\/' + this[ '_tag' ] + '>' );
1135         return html.join( _ );
1136 };
1137
1138 /*
1139  * null が来たら '', 数値等が来たら文字列化
1140  */
1141 /**
1142  * textContent 取得・設定。null が来たら '', 数値等が来たら文字列化
1143  * @alias Node.prototype.text
1144  * @param {string} [text=]
1145  * @return {string|Node} 
1146  * @example node.text( 'Hello, world!' );
1147  */
1148 function X_Node_text( text ){
1149         var xnodes, texts, i, l;
1150         // setter
1151         if( text !== undefined ){
1152                 if( text === null ) text = '';
1153                 text += '';
1154                 
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();
1160                         };
1161                         return this;
1162                 };
1163                 if( ( xnodes = this[ '_xnodes' ] ) && xnodes.length === 1 && !xnodes[ 0 ][ '_tag' ] ){
1164                         xnodes[ 0 ][ 'text' ]( text );
1165                         return this;
1166                 };
1167                 // TODO 一つのtextnode を残すケース 完全に削除したい場合は empty()を使う
1168                 if( !text ) return this[ 'empty' ]();           
1169                 this[ 'empty' ]()[ 'createText' ]( text );
1170                 return this;
1171         };
1172         // getter
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' ]();
1177                         };
1178                         return texts.join( '' );
1179                 };
1180                 return '';
1181         };
1182         return this[ '_text' ];
1183 };
1184
1185 /**
1186  * HTML要素に対して name の関数を実行しその戻り値を返す。関数に渡す引数も任意に設定できる。
1187  * @alias Node.prototype.call
1188  * @param {string} [name] 要素の関数名
1189  * @return {*} 
1190  * @example node.call( 'focus' );
1191  */
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,
1197                 parentW, parentH,
1198                 parentSX, parentSY, parentSW, parentSH,
1199                 visibleX, visibleY, visibleW, visibleH,
1200                 visiblePartX, visiblePartY, func, args, params, i;
1201
1202         switch( name ){
1203                 case 'isSVG' :
1204                         return !!( this[ '_flags' ] & X_NodeFlags_IS_SVG );
1205                 case 'isVML' :
1206                         return !!( this[ '_flags' ] & X_NodeFlags_IS_VML );
1207                 case 'nodeType' :
1208                         return this[ '_tag' ] ? 1 : 3;
1209                 case 'outerHTML' :
1210                         X_Node_outerXNode = X_Node_body; // == true ならなんでもよい。型を合わすために xbody にしている
1211                         v = this[ 'html' ]();
1212                         X_Node_outerXNode = null;
1213                         return v;
1214                 case 'treeIsDirty' :
1215                         return !!X_Node_updateTimerID;
1216                 case 'fontSize' :
1217                         return this.parent ? X_Node_CSS_getCharSize( this ) : 0;
1218                 case 'inGPU' :
1219                         return !!( this[ '_flags' ] & ( X_NodeFlags_GPU_NOW | X_NodeFlags_GPU_RELEASE_RESERVED ) );
1220         };
1221         
1222         X_Node_updateTimerID && X_Node_startUpdate();
1223         
1224         raw  = this[ '_rawObject' ] || X_UA_DOM.IE4 && X_Node__ie4getRawNode( this );
1225         
1226         if( !raw ) return;
1227         
1228         if( name === 'scrollTo' ){
1229                 raw.scrollLeft = args[ 1 ] || 0;
1230                 raw.scrollTop  = args[ 2 ] || 0;
1231                 return;
1232         };
1233         
1234         if( name === 'inView' ){
1235                 body   = X_elmBody;
1236                 child  = raw;
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;
1244                         // 親がスクロール領域を持つ
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;
1252                                 // 子が表示領域内
1253                                 if( parentSY < childY   + childH &&
1254                             childY   < parentSY + parentH &&
1255                             parentSX < childX   + childW  &&
1256                             childX   < parentSX + parentW ){
1257                                 
1258                             // right:子の左側が見えている left:子の左側が見えている both:完全に見えている
1259                                         visiblePartX =
1260                                                 childX < parentSX ? 'right' :
1261                                                 ( parentSX + parentW ) < ( childX + childW ) ? 'left' : 'both';
1262                                         visiblePartY = 
1263                                                 childY < parentSY ? 'bottom' :
1264                                                 ( parentSY + parentH ) < ( childY + childH ) ? 'top' : 'both';
1265
1266                                         // 子が見える領域
1267                                         visibleX = visiblePartX === 'right'  ? 0 : childX - parentSX;
1268                                         visibleY = visiblePartX === 'bottom' ? 0 : childY - parentSY;
1269                                         visibleW =
1270                                                 visiblePartX === 'both'   ? childW :
1271                                                 visiblePartX === 'right'  ? ( parentSX + parentW - childX ) : ( childX + childW - parentSX );
1272                                         visibleH =
1273                                                 visiblePartY === 'both'   ? childH :
1274                                                 visiblePartY === 'bottom' ? ( parentSY + parentH - childY ) : ( childY + childH - parentSY );           
1275                     } else {
1276                         return { 'isInView' : false };
1277                     };
1278                         };
1279                         child = parent;         
1280                 };
1281                 return { 'isInView' : true };
1282         };
1283         
1284         func = raw[ name ];
1285         if( X_Type_isFunction( func ) ){
1286                 if( l ){
1287                         args = X_Array_copy( args );
1288                         args.shift();
1289                         return func.apply( raw, args );
1290                 };
1291                 return raw[ name ]();           
1292         } else
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          
1296                 if( l ){
1297                         args = X_Array_copy( args );
1298                         args.shift();
1299                         
1300                 params = [];
1301                 for( i = 0; i < l; ++i ){
1302                         params[ i ] = '_' + i;
1303                 };
1304                 params = params.join( ',' );
1305                 return Function(
1306                         params,
1307                     [ 'return this.', name, '(', params, ')' ].join( '' )
1308                 ).apply( raw, args );
1309                 };
1310                 return raw[ name ]();
1311         };
1312 };
1313
1314 /*
1315  * xnode を this として関数を実行する。 NodeList.each と動作を合わせてあるため関数の戻り値は破棄される。
1316  * 関数に渡す引数も任意に設定できる。
1317  */
1318 function X_Node_each( func /*, opt_args */ ){
1319         var args;
1320         if( 1 < arguments.length ){
1321                 args = X_Array_copy( arguments );
1322                 args[ 0 ] = 0;          
1323                 func.apply( this, args );
1324         } else {
1325                 func.call( this, 0 );
1326         };
1327         return this;
1328 };
1329
1330
1331 /* --------------------------------------
1332  *  Async commit update
1333  * 
1334  * TODO Timer や DOM イベントの呼び出しの最後に、まだ一度も commitUpdate していないなら commitUpdate してしまう。
1335  */
1336         
1337 function X_Node_reserveUpdate(){
1338         if( !X_Node_updateTimerID ) X_Node_updateTimerID = X_Timer_requestFrame( X_Node_startUpdate );
1339 };
1340
1341 var X_Node_updateReservedByReleaseGPU = false;
1342
1343 function X_Node_startUpdate( time ){
1344         var removal, i, xnodeOrElm;
1345         
1346         if( !X_Node_updateTimerID || X_ViewPort_readyState < X_EVENT_INIT ){
1347                 return;
1348         };
1349
1350         X_Timer_cancelFrame( X_Node_updateTimerID );
1351         X_Node_updateTimerID = 0;
1352
1353         if( time ){
1354                 // X.Timer 経由でないと発火しない このイベントでサイズを取ると無限ループに
1355                 X_System[ '_listeners' ] && X_System[ '_listeners' ][ X_EVENT_BEFORE_UPDATE ] && X_System[ 'dispatch' ]( X_EVENT_BEFORE_UPDATE );
1356         };
1357
1358         removal = X_Node_reserveRemoval;
1359
1360         if( i = removal.length ){
1361                 for( ; i; ){
1362                         xnodeOrElm = removal[ --i ];
1363                         // TODO GPU レイヤーの子の場合、remove をスキップする。 非GPU レイヤーへ apppend される場合、clone する?
1364                         if( !xnodeOrElm[ 'instanceOf' ] ){
1365                                 if( X_UA_DOM.IE4 ){
1366                                         xnodeOrElm.removeAttribute( 'id' ); // ?
1367                                         xnodeOrElm.outerHTML = ''; // xnodeOrElm.remove() ?
1368                                 } else {
1369                                         if( !X_UA[ 'MacIE' ] ){
1370                                                 // elm.parentNode.tagName for ie7
1371                                                 xnodeOrElm.parentNode && xnodeOrElm.parentNode.tagName && xnodeOrElm.parentNode.removeChild( xnodeOrElm );
1372                                         } else {
1373                                                 xnodeOrElm.parentNode && xnodeOrElm.parentNode.tagName && X_TEMP._fixed_remove( xnodeOrElm, that );
1374                                         };
1375                                 };
1376                         } else {
1377                                 X_Node__actualRemove( xnodeOrElm );
1378                         };
1379                 };
1380                 removal.length = 0;
1381         };
1382         
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' ], 1 );
1385         } else {
1386                 X_Node__commitUpdate( X_Node_head, X_Node_head[ '_rawObject' ].parentNode, null, X_Node_head[ '_flags' ], 1 );
1387                 X_Node__commitUpdate( X_Node_body, X_Node_body[ '_rawObject' ].parentNode, null, X_Node_body[ '_flags' ], 1 );
1388         };
1389         
1390         if( X_Node_updateReservedByReleaseGPU ){
1391                 X_Node_reserveUpdate();
1392                 X_Node_updateReservedByReleaseGPU = false;
1393         };
1394         
1395         if( time ){
1396                 // X.Timer 経由でないと発火しない このイベントでサイズを取ると無限ループに
1397                 X_System[ '_listeners' ] && X_System[ '_listeners' ][ X_EVENT_UPDATED ] && X_System[ 'dispatch' ]( X_EVENT_UPDATED );   
1398         } else {
1399                 X_System[ '_listeners' ] && X_System[ '_listeners' ][ X_EVENT_UPDATED ] && X_System[ 'asyncDispatch' ]( X_EVENT_UPDATED );
1400         };
1401         
1402         X_ViewPort[ '_listeners' ] && X_ViewPort[ '_listeners' ][ X_EVENT_AFTER_UPDATE ] && X_ViewPort[ 'asyncDispatch' ]( X_EVENT_AFTER_UPDATE );
1403 };
1404
1405 /*
1406  * 1. GPU_NOW の場合、子の変更は行わない
1407  * 2. GPU解放予約 の場合//、この要素のみ変更を行う。rAF 後にさらに更新するためフラグを立てる。
1408  * 3. GPU予約 -> GPU
1409  * 4. style="display:none" の場合、これ以下の変更を行わない。
1410  * 5. ie5 非表示フラグが立っていて、親と自身の class・id によって非表示になっていて、親と自身に変更がない。accumulatedFlags を使用。
1411  *     -> TODO これ TREE の変更を検出できない。 remove したときに 子まで X_NodeFlags_IE5_DISPLAY_NONE_FIXを落とす。
1412  * 6. 要素の生成
1413  * 7. 要素の位置のズレを補正
1414  * 8. 更新の適用
1415  * 9. ie5 親及び自身へのクラス・id指定で display:none になるケースがありそれを検出。
1416  *   このままでは、生成と破棄が繰り返されてしまうので親と自身のクラス・idが変わった場合、ツリー位置の変化があった場合に再生する。
1417  */
1418 var X_Node__commitUpdate =
1419         X_UA_DOM.W3C ?
1420                 ( function( that, parentElement, nextElement, accumulatedFlags, ie8opacity ){
1421                         var elm = that[ '_rawObject' ],
1422                                 created, xnodes, l, next;
1423
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 );
1428                                 return elm;
1429                         };
1430
1431                         // 2. GPU解放予約
1432                         if( that[ '_flags' ] & X_NodeFlags_GPU_RELEASE_RESERVED ){
1433                                 // console.log( 'GPU 解放 ' );
1434                                 //X_Node_updateReservedByReleaseGPU = true;
1435                                 that[ '_flags' ] &= X_Node_BitMask_RESET_GPU;
1436                                 //return elm;// TODO もしかしたらこのタイミングで更新できるかも。
1437                         };
1438
1439                         // 3. GPU予約 -> GPU
1440                         if( that[ '_flags' ] & X_NodeFlags_GPU_RESERVED ){
1441                                 that[ '_flags' ] &= X_Node_BitMask_RESET_GPU;
1442                                 that[ '_flags' ] |= X_NodeFlags_GPU_NOW;
1443                         };
1444
1445                         // 4. style="display:none" の場合
1446                         if( that[ '_flags' ] & X_NodeFlags_STYLE_IS_DISPLAY_NONE ){
1447                                 if( X_Node_displayNoneFixForIE5 ){
1448                                         // filter の効いている要素を含む要素は display:none が無視される。
1449                                         // filter = '' で削除はできるが、再表示時に filter が消える。 -> filter な要素を削除してしまう。                                         
1450                                         if( elm && elm.parentNode ){
1451                                                 X_Node__actualRemove( that );
1452                                         };
1453                                         return nextElement;
1454                                 };
1455                                 elm && ( elm.style.display = 'none' );
1456                                 return ( elm && elm.nextSibling === nextElement ) ? elm : nextElement;
1457                         };
1458                         
1459                         // 5. ie5 非表示fixフラグ
1460                         accumulatedFlags |= that[ '_flags' ];
1461                         
1462                         if( that[ '_flags' ] & X_NodeFlags_IE5_DISPLAY_NONE_FIX ){
1463                                 if( ( accumulatedFlags & ( X_NodeFlags_DIRTY_POSITION | X_NodeFlags_DIRTY_ID | X_NodeFlags_DIRTY_CLASSNAME ) ) === 0 ){
1464                                         return nextElement;
1465                                 };
1466                         };
1467                         
1468                         // 6. 要素の生成
1469                         if( !elm ){
1470                                 if( !that[ '_tag' ] ){
1471                                         that[ '_flags' ] &= X_Node_BitMask_RESET_DIRTY;
1472                                         elm = document.createTextNode( X_String_chrReferanceTo( that[ '_text' ] ) );
1473                                         if( !X_UA[ 'IE' ] ){
1474                                                 elm[ 'UID' ] = that[ '_uid' ];
1475                                         };
1476                                 } else
1477                                 if( X_Node_strictElmCreation ){
1478                                         that[ '_flags' ] & X_NodeFlags_OLD_CSSTEXT && X_Node_CSS_objToCssText( that, true ); // OLD_CSSTEXT ??
1479                 
1480                                         elm =
1481                                                 document.createElement( [
1482                                                         '<', that[ '_tag' ],
1483                                                                 ' UID="', that[ '_uid' ], '"',
1484                                                                 that[ '_id' ] ? ' id="' + that[ '_id' ] + '"' : '',
1485                                                                 that[ '_className' ] ? ' class="' + that[ '_className' ] + '"' : '',
1486                                                                 X_Node_Attr_objToAttrText( that, true ),
1487                                                                 that[ '_cssText' ] ? ' style="' + that[ '_cssText' ] + '"' : '',
1488                                                         '>' ].join( '' ) );
1489                                 } else
1490                                 if( that[ '_flags' ] & X_NodeFlags_IS_SVG ){
1491                                         elm = document.createElementNS( 'http://www.w3.org/2000/svg', that[ '_tag' ].toLowerCase() );
1492                                         
1493                                         // math http://www.w3.org/1998/Math/MathML
1494                                 } else {
1495                                         elm = document.createElement( that[ '_tag' ] );
1496                                 };
1497                                 
1498                                 that[ '_rawObject' ] = elm;
1499                                 
1500                                 // IE には要素追加のタイミングで起こるメモリリークがありここで追加
1501                                 if( !X_Node_addTreeAfterChildren ){
1502                                         nextElement ?
1503                                                 parentElement.insertBefore( elm, nextElement ) :
1504                                                 parentElement.appendChild( elm );
1505                                 };
1506
1507                                 if( that[ '_tag' ] ){
1508                                         X_EventDispatcher_toggleAllEvents( that, true );// イベントの復帰                                
1509                                         that[ '_flags' ] |= X_NodeFlags_ACTUAL_LISTENING;
1510                                         
1511                                         //if( X_Node_documentFragment ){
1512                                                 //( frg = X_Node_documentFragment ).appendChild( elm );
1513                                                 // 連続する要素の差し替えの場合に有効
1514                                         //};
1515
1516                                         if( X_Node_strictElmCreation ){
1517                                                 that[ '_flags' ] &= X_Node_BitMask_RESET_DIRTY;
1518                                                 // ie の string から要素を作る場合、ネットワーク系属性は onload イベントなどを拾うために、要素生成->イベント復帰後に適用する
1519                                                 that[ '_newAttrs' ] && ( that[ '_flags' ] |= X_NodeFlags_DIRTY_ATTR ); // _newAttrs には ネットワーク系属性が入っている。Network 系の属性は遅らせて設定
1520                                                 that[ '_flags' ] |= X_NodeFlags_DIRTY_IE_FILTER;// doc 追加後に filter を指定しないと有効にならない。
1521                                         } else {
1522                                                 elm[ 'UID' ] = that[ '_uid' ];
1523                                                 that[ '_newAttrs' ] = that[ '_attrs' ];
1524                                                 that[ '_flags' ] &= X_Node_BitMask_RESET_DIRTY;
1525                                                 that[ '_flags' ] |= X_NodeFlags_DIRTY_ID | X_NodeFlags_DIRTY_CLASSNAME | X_NodeFlags_DIRTY_ATTR | X_NodeFlags_DIRTY_CSS | X_NodeFlags_DIRTY_IE_FILTER;
1526                                                 
1527                                                 // http://outcloud.blogspot.jp/2010/09/iframe.html
1528                                                 // この問題は firefox3.6 で確認
1529                                                 if( X_UA[ 'Gecko' ] ){
1530                                                         if( that[ '_tag' ] === 'IFRAME' && ( !that[ '_attrs' ] || !that[ '_attrs' ][ 'src' ] ) ){
1531                                                                 //elm.contentWindow.location.replace = elm.src = 'about:blank';
1532                                                                 that[ 'attr' ]( 'src', 'about:blank' );
1533                                                         };
1534                                                 };
1535                                         };
1536                                 };
1537                                 
1538                                 created = true;
1539                         } else
1540                         // 7. 要素の位置のズレを補正
1541                         if( elm.parentNode !== parentElement || ( nextElement && elm.nextSibling !== nextElement ) ){
1542                                 nextElement ?
1543                                         parentElement.insertBefore( elm, nextElement ) :
1544                                         parentElement.appendChild( elm );
1545                         };
1546                         
1547                         if( that[ '_listeners' ] && ( ( that[ '_flags' ] & X_NodeFlags_ACTUAL_LISTENING ) === 0 ) ){
1548                                 X_EventDispatcher_toggleAllEvents( that, true );// イベントの退避
1549                                 that[ '_flags' ] |= X_NodeFlags_ACTUAL_LISTENING;
1550                         };
1551                         
1552                         ie8opacity = ie8opacity * ( that[ '_css' ] && 0 <= that[ '_css' ].opacity ? that[ '_css' ].opacity : 1 );
1553                         
1554                         // 8. 更新の適用
1555                         if( accumulatedFlags & X_Node_BitMask_IS_DIRTY ){
1556                                 delete that[ '_fontSize' ];
1557                                 X_Node__updateRawNode( that, elm, ie8opacity, accumulatedFlags );
1558                         };
1559
1560                         // 9. ie5 only
1561                         // 親及び自身へのクラス・id指定で display : none になるケースがありそれを検出
1562                         // 生成と破棄が繰り返されてしまう、親と自身の id, class が変わった場合だけ再生成。 accumulatedFlags & ( ID | CLASSNAME )
1563                         // currentStyle を観ていたときはエラーで停止する、alert と挟むと正常に動いて支離滅裂
1564                         if( X_Node_displayNoneFixForIE5 && that[ '_tag' ] ){
1565                                 if( elm.runtimeStyle.display === 'none' ){
1566                                         X_Node__actualRemove( that );
1567                                         that[ '_flags' ] |= X_NodeFlags_IE5_DISPLAY_NONE_FIX;
1568                                         return nextElement;                                     
1569                                 } else {
1570                                         that[ '_flags' ] &= ~X_NodeFlags_IE5_DISPLAY_NONE_FIX;
1571                                 };
1572                         };
1573                         
1574                         // 10. 子要素の更新。
1575                         if( ( xnodes = that[ '_xnodes' ] ) && ( l = xnodes.length ) ) {
1576                                 for( ; l; ){
1577                                         next = X_Node__commitUpdate( xnodes[ --l ], elm, next, accumulatedFlags, ie8opacity );
1578                                 };
1579                         };
1580
1581                         if( created && X_Node_addTreeAfterChildren ){
1582                                 nextElement ?
1583                                         parentElement.insertBefore( elm, nextElement ) :
1584                                         parentElement.appendChild( elm );
1585                                 
1586                                 if( X_UA[ 'Gecko' ] && that[ '_tag' ] === 'IFRAME' && elm.contentWindow ){
1587                                         // tree に追加されるまで contentWindow は存在しない。
1588                                         elm.contentWindow.location.replace = elm.src;
1589                                 };
1590                         };
1591
1592                         return elm;
1593                 }) :
1594         X_UA_DOM.IE4 ? 
1595                 ( function( that, parentElement, prevElement, accumulatedFlags ){
1596                         var elm = that[ '_rawObject' ] || X_Node__ie4getRawNode( that ),
1597                                 xnodes, l, i, dirty, mix, html, text, prev;
1598
1599                         if( !that[ '_tag' ] ){
1600                                 that[ '_flags' ] & X_NodeFlags_DIRTY_CONTENT && X_Node__updateRawNode( that, elm );
1601                                 return elm;
1602                         };
1603                         
1604                         // 4. style="display:none" の場合
1605                         if( that[ '_flags' ] & X_NodeFlags_STYLE_IS_DISPLAY_NONE ){
1606                                 if( elm ){
1607                                         elm.style.display = 'none';
1608                                         if( elm.style.display !== 'none' ){ // ie4 の style は currentStyle 相当らしい、、、? div 以外への display:none が効かないので remove する。
1609                                                 X_Node__actualRemove( that );
1610                                                 return prevElement;
1611                                         };
1612                                 };
1613                                 return elm || prevElement;
1614                         };
1615                         
1616                         if( !elm ){
1617                                 prevElement ?
1618                                         prevElement.insertAdjacentHTML( 'AfterEnd', X_Node__actualCreate( that, false ) ) :
1619                                         parentElement.insertAdjacentHTML( 'AfterBegin', X_Node__actualCreate( that, false ) );
1620                                 X_Node__afterActualCreate( that );
1621                                 return that[ '_rawObject' ] || X_Node__ie4getRawNode( that );
1622                         };
1623                         
1624                         accumulatedFlags |= that[ '_flags' ];
1625                         
1626                         xnodes = that[ '_xnodes' ];
1627                         l      = xnodes ? xnodes.length : 0;
1628                         dirty  = !!( that[ '_flags' ] & X_NodeFlags_IE4_DIRTY_CHILDREN );
1629                         
1630                         /*
1631                          * HTML の下に TextNode だけ 。MIX_FIXED でない場合、削除、追加 を親に通知
1632                          * HTML の下に HTML だけ
1633                          * HTML の下は MIX -> TextNode, html の削除、変更、追加
1634                          * HTML の下は MIX_FIXED -> TextNode を <font> に置き換えてあるのでW3C DON 的に触ることができる
1635                          */
1636                         if( dirty ){
1637                                 that[ '_flags' ] &= ~X_NodeFlags_IE4_DIRTY_CHILDREN;
1638                                 for( i = 0; i < l; ++i ){
1639                                         if( xnodes[ i ][ '_tag' ] ){
1640                                                 that[ '_flags' ] |= X_NodeFlags_IE4_HAS_ELEMENT;
1641                                         } else {
1642                                                 that[ '_flags' ] |= X_NodeFlags_IE4_HAS_TEXTNODE;
1643                                         };
1644                                         if( that[ '_flags' ] & X_Node_BitMask_IE4_IS_MIX === X_Node_BitMask_IE4_IS_MIX ){
1645                                                 mix = true;
1646                                                 break;
1647                                         };
1648                                 };
1649                         };
1650                         
1651                         if( that[ '_flags' ] & X_NodeFlags_IE4_FIXED || that[ '_flags' ] & X_Node_BitMask_IE4_IS_MIX === X_NodeFlags_IE4_HAS_ELEMENT ){
1652                                 for( i = 0; i < l; ++i ){
1653                                         prev = X_Node__commitUpdate( xnodes[ i ], elm, prev, accumulatedFlags );
1654                                 };
1655                         } else
1656                         if( mix ){
1657                                 html = [];
1658                                 for( i = 0; i < l; ++i ){
1659                                         html[ i ] = X_Node__actualCreate( xnodes[ i ], false );
1660                                 };
1661                                 elm.innerHTML = html.join( '' );
1662                                 for( i = 0; i < l; ++i ){
1663                                         X_Node__afterActualCreate( xnodes[ i ] );
1664                                 };
1665                                 that[ '_flags' ] |= X_NodeFlags_IE4_FIXED;
1666                         } else
1667                         if( that[ '_flags' ] & X_NodeFlags_IE4_HAS_TEXTNODE ){
1668                                 dirty = dirty || false;
1669                                 for( i = 0; i < l; ++i ){
1670                                         text = xnodes[ i ];
1671                                         if( text[ '_flags' ] & X_Node_BitMask_IS_DIRTY ){
1672                                                 text[ '_flags' ] &= X_Node_BitMask_RESET_DIRTY;
1673                                                 dirty = true;
1674                                         };
1675                                 };
1676                                 if( dirty ) elm.innerHTML = that[ 'text' ]();
1677                         };
1678                         
1679                         if( accumulatedFlags & X_Node_BitMask_IS_DIRTY ) delete that[ '_fontSize' ];
1680                         
1681                         that[ '_flags' ] &= ~X_NodeFlags_DIRTY_POSITION;
1682                         that[ '_flags' ] & X_Node_BitMask_IS_DIRTY && X_Node__updateRawNode( that, elm );
1683                         return elm;
1684                 }) :
1685                 (function(){});
1686
1687 /*
1688  * GPU レイヤーするブラウザは、子要素から変更を当てていく? <- とりあえず、親要素から。
1689  */
1690 var X_Node__updateRawNode =
1691         X_UA_DOM.W3C ?
1692                 ( function( that, elm, ie8opacity, accumulatedFlags ){
1693                         var attrs, rename, k, v, memory, f;
1694
1695                         // textNode
1696                         if( !that[ '_tag' ] ){
1697                                 elm.data = X_String_chrReferanceTo( that[ '_text' ] );
1698                                 that[ '_flags' ] &= X_Node_BitMask_RESET_DIRTY;
1699                                 return;
1700                         };
1701                         // id
1702                         if( that[ '_flags' ] & X_NodeFlags_DIRTY_ID ){
1703                                 that[ '_id' ] ?
1704                                         ( ( that[ '_flags' ] & X_NodeFlags_IS_SVG ) ?
1705                                                 elm.setAttribute( 'id', that[ '_id' ] ) :
1706                                                 ( elm.id = that[ '_id' ] )
1707                                         ) :
1708                                         ( elm.id && elm.removeAttribute( 'id' ) );              
1709                         };
1710                         // className
1711                         if( that[ '_flags' ] & X_NodeFlags_DIRTY_CLASSNAME ){
1712                                 that[ '_className' ] ?
1713                                         ( ( that[ '_flags' ] & X_NodeFlags_IS_SVG ) ?
1714                                                 elm.setAttribute( 'class', that[ '_className' ] ) :
1715                                                 ( elm.className = that[ '_className' ] )
1716                                         ) :
1717                                         ( elm.className && elm.removeAttribute( X_UA[ 'IE' ] < 8 ? 'className' : 'class' ) ); // className は ie7-                     
1718                         };
1719                         
1720                         // attr
1721                         if( that[ '_flags' ] & X_NodeFlags_DIRTY_ATTR && ( attrs = that[ '_newAttrs' ] || that[ '_attrs' ] ) ){
1722                                 rename = X_Node_Attr_renameForDOM;
1723                                                 
1724                                 for( k in attrs ){
1725                                         v = attrs[ k ];
1726                                         
1727                                         switch( that[ '_tag' ] + k ){
1728                                                 case 'TEXTAREAvalue' :
1729                                                         // IETester 5.5 ではエラーが出なかった.MultipulIE5.5 ではエラーが出たので
1730                                                         // MultipleIE6 でもここを通す。X_UA[ 'ieExeComError' ] の場合 MultipleIE6
1731                                                         if( ( !X_UA[ 'MacIE' ] && X_UA[ 'IE5x' ] ) || ( X_UA[ 'ieExeComError' ] && X_UA[ 'IE6' ] ) ){
1732                                                                 elm.firstChild ?
1733                                                                         ( elm.firstChild.data = v || '' ) :
1734                                                                         elm.appendChild( document.createTextNode( v || '' ) );
1735                                                                 continue;
1736                                                         };
1737                                                         break;
1738                                                 
1739                                                 case 'IFRAMEsrc' :
1740                                                         // http://outcloud.blogspot.jp/2010/09/iframe.html
1741                                                         // この問題は firefox3.6 で確認
1742                                                         if( X_UA[ 'Gecko' ] && elm.contentWindow ){
1743                                                                 elm.contentWindow.location.replace = elm.src = v || '';
1744                                                                 continue;
1745                                                         };
1746                                                         break;
1747                                                 
1748                                                 case 'IFRAMEname' :
1749                                                         // http://d.hatena.ne.jp/NeoCat/20080921/1221940658
1750                                                         // こちらに名前をsetしないとtargetが動作しない
1751                                                         // これってあとから name を変更できないバグでは? itozyun
1752                                                         // if( X_UA[ 'IE' ] ) elm.name = elm.contentWindow.name = v || '';
1753                                         };
1754                                         
1755                                         //if( X_EMPTY_OBJECT[ k ] ) continue;
1756                                         // TODO IE では input, なぜか button, object も type, name の変更が出来ない、同値で置き換えようとしても不可
1757                                         v === undefined ?
1758                                                 elm.removeAttribute( rename[ k ] || k ) :
1759                                         ( ( that[ '_flags' ] & X_NodeFlags_IS_SVG ) ?
1760                                                 elm.setAttribute( k, v ) :
1761                                                 ( elm[ rename[ k ] || k ] = X_Node_Attr_noValue[ k ] ? k : v )
1762                                         );
1763                                 };
1764                                 delete that[ '_newAttrs' ];
1765                         };
1766                         
1767                         if( accumulatedFlags & X_NodeFlags_IE8_OPACITY_FIX ){
1768                                 memory = that[ '_css' ] && that[ '_css' ].opacity;
1769                                 if( f = true ){
1770                                         if( !that[ '_css' ] ) that[ '_css' ] = {};
1771                                         that[ '_css' ].opacity = ie8opacity;
1772                                         if( that[ '_flags' ] & X_NodeFlags_DIRTY_CSS ){
1773                                                 that[ '_flags' ] |= X_NodeFlags_OLD_CSSTEXT;
1774                                         } else
1775                                         if( !( that[ '_flags' ] & X_NodeFlags_DIRTY_IE_FILTER ) ){
1776                                                 that[ '_flags' ] |= X_NodeFlags_DIRTY_IE_FILTER;
1777                                         };
1778                                 };
1779                         };
1780                         
1781                         // style
1782                         if( that[ '_flags' ] & X_NodeFlags_DIRTY_CSS ){
1783                                 if( that[ '_flags' ] & X_NodeFlags_OLD_CSSTEXT ? X_Node_CSS_objToCssText( that ) : that[ '_cssText' ] ){
1784                                         X_UA[ 'Opera78' ] || X_UA[ 'NN6' ] ?
1785                                                 elm.setAttribute( 'style', that[ '_cssText' ] ) : // opera8用
1786                                                 ( elm.style.cssText = that[ '_cssText' ] );
1787                                 } else {
1788                                         elm.style.cssText = ''; // IE5.5以下 Safari3.2 で必要
1789                                         elm.removeAttribute( 'style' );
1790                                 };
1791                         } else
1792                         if( that[ '_flags' ] & X_NodeFlags_DIRTY_IE_FILTER ){
1793                                 v = X_Node_CSS_objToIEFilterText( that );
1794                                 if( v ){
1795                                         elm.style.filter = v;
1796                                         that[ '_flags' ] |= X_NodeFlags_IE_FILTER_NOW;
1797                                 } else {
1798                                         elm.style.removeAttribute( 'filter' );
1799                                         that[ '_flags' ] &= ~X_NodeFlags_IE_FILTER_NOW;
1800                                 };
1801                         };
1802                         
1803                         if( f ){
1804                                 if( 0 <= memory ){
1805                                         that[ '_css' ].opacity = memory;
1806                                 } else {
1807                                         delete that[ '_css' ].opacity;
1808                                         if( X_Object_isEmpty( that[ '_css' ] ) ) delete that[ '_css' ];
1809                                 };
1810                         };
1811                         
1812                         that[ '_flags' ] &= X_Node_BitMask_RESET_DIRTY;
1813                 }) :
1814         X_UA_DOM.IE4 ? 
1815                 ( function( that, elm ){
1816                         var attrs, rename, k, v;
1817
1818                         // fake textNode
1819                         if( !that[ '_tag' ] ){
1820                                 elm.innerText = that[ '_text' ];
1821                                 that[ '_flags' ] &= X_Node_BitMask_RESET_DIRTY;
1822                                 return;
1823                         };
1824                         
1825                 /*
1826                  * http://www.tohoho-web.com/js/element.htm
1827                  * title、className、id、lang、language には setAttribute でなく、element.id で直接読み書きできる
1828                  */     
1829                         // id
1830                         if( that[ '_flags' ] & X_NodeFlags_DIRTY_ID ) elm.setAttribute( 'id', that[ '_id' ] || ( 'ie4uid' + that[ '_uid' ] ) );
1831
1832                         // className
1833                         if( that[ '_flags' ] & X_NodeFlags_DIRTY_CLASSNAME ){
1834                                 that[ '_className' ] ? ( elm.className = that[ '_className' ] ) : elm.removeAttribute( 'class' );
1835                         };
1836                         // style
1837                         if( that[ '_flags' ] & X_NodeFlags_DIRTY_CSS ){
1838                                 if( that[ '_flags' ] & X_NodeFlags_OLD_CSSTEXT ? X_Node_CSS_objToCssText( that ) : that[ '_cssText' ] ){
1839                                         elm.style.cssText = that[ '_cssText' ];
1840                                 } else {
1841                                         elm.style.cssText = '';
1842                                         elm.removeAttribute( 'style' );
1843                                 };
1844                         } else
1845                         if( that[ '_flags' ] & X_NodeFlags_DIRTY_IE_FILTER ){
1846                                 v = X_Node_CSS_objToIEFilterText( that );
1847                                 if( v ){
1848                                         elm.style.filter = v;
1849                                         that[ '_flags' ] |= X_NodeFlags_IE_FILTER_NOW;
1850                                 } else {
1851                                         elm.style.removeAttribute( 'filter' );
1852                                         that[ '_flags' ] &= ~X_NodeFlags_IE_FILTER_NOW;
1853                                 };
1854                         };
1855                         
1856                         // attr
1857                         if( that[ '_flags' ] & X_NodeFlags_DIRTY_ATTR && ( attrs = that[ '_newAttrs' ] || that[ '_attrs' ] ) ){
1858                                 rename = X_Node_Attr_renameForDOM;
1859                                 for( k in attrs ){
1860                                         //if( X_EMPTY_OBJECT[ k ] ) continue;
1861                                         ( v = attrs[ k ] ) === undefined ?
1862                                                 elm.removeAttribute( rename[ k ] || k ) :
1863                                                 elm.setAttribute( rename[ k ] || k, X_Node_Attr_noValue[ k ] ? k : v );
1864                                 };
1865                                 delete that[ '_newAttrs' ];
1866                         };
1867
1868                         that[ '_flags' ] &= X_Node_BitMask_RESET_DIRTY;
1869                 }) :
1870                 (function(){});
1871
1872 /* --------------------------------------
1873  *  Create
1874  * 
1875  * http://d.hatena.ne.jp/uupaa/20080718/1216362040
1876  * DOM Rangeが使える環境(Firefox2+,Opera9+,Safari3+)なら、innerHTMLいらずで、ガーって書けます。
1877  * return document.createRange().createContextualFragment("<div><select><option></option></select></div>");
1878  * insertAdjacentHTML
1879  * 
1880  * ie7 以下では iframe の frameborder や、input name は、createElement 後に setAttribute しても無視される
1881  * 
1882  * fragument がある場合 children も足して
1883  * Mozilla: 1.0+, IE: 5.5+, Netscape: 2.0+, Safari: 1.0+, Opera: 7.0+
1884  * ie6 大丈夫?fragment の場合リークしないか?チェックが必要
1885  * http://msdn.microsoft.com/ja-jp/library/bb250448%28v=vs.85%29.aspx
1886  * 
1887  * document.createElement of ie4 is only for OPTION & IMAGE.
1888  */
1889 var X_Node__actualCreate =
1890         X_UA_DOM.IE4 && (function( that, isChild ){
1891                 var uid = that[ '_uid' ],
1892                         html, xnodes, n, i, l;
1893                 
1894                 if( !that[ '_tag' ] ){
1895                         html = [ '<FONT id=ie4uid', uid, ' UID="', uid, '">', that[ '_text' ], '</FONT>' ];// fake textNode
1896                         delete that[ '_rawObject' ];
1897                 } else {
1898                         if( !isChild ) X_Node__actualRemove( that, /* true */ false );
1899                         
1900                         that[ '_flags' ] & X_NodeFlags_OLD_CSSTEXT && X_Node_CSS_objToCssText( that, true );
1901                         
1902                         html = [
1903                                 '<', that[ '_tag' ], ' id=', ( that[ '_id' ] || ( 'ie4uid' + uid ) ), ' UID="', uid, '"',
1904                                 that[ '_className' ] ? ' class="' + that[ '_className' ] + '"' : '',
1905                                 X_Node_Attr_objToAttrText( that, true ),
1906                                 that[ '_cssText' ] ? ' style="' + that[ '_cssText' ] + '"' : '',
1907                         '>' ];
1908                         
1909                         n = html.length;
1910                         if( ( xnodes = that[ '_xnodes' ] ) && ( l = xnodes.length ) ){
1911                                 
1912                                 that[ '_flags' ] &= ~X_NodeFlags_IE4_DIRTY_CHILDREN;
1913                                 for( i = 0; i < l; ++i ){
1914                                         if( xnodes[ i ][ '_tag' ] ){
1915                                                 that[ '_flags' ] |= X_NodeFlags_IE4_HAS_ELEMENT;
1916                                         } else {
1917                                                 that[ '_flags' ] |= X_NodeFlags_IE4_HAS_TEXTNODE;
1918                                         };
1919                                         if( that[ '_flags' ] & X_Node_BitMask_IE4_IS_MIX === X_Node_BitMask_IE4_IS_MIX ){
1920                                                 break;
1921                                         };
1922                                 };
1923                                 
1924                                 if( that[ '_flags' ] & X_Node_BitMask_IE4_IS_MIX === X_NodeFlags_IE4_HAS_TEXTNODE ){
1925                                         // only textnode
1926                                         html[ n ] = that[ 'text' ]();
1927                                         ++n;
1928                                 } else {
1929                                         for( i = 0; i < l; ++i ){
1930                                                 html[ n ] = X_Node__actualCreate( xnodes[ i ], true );
1931                                                 ++n;
1932                                         };
1933                                         that[ '_flags' ] |= X_NodeFlags_IE4_FIXED;
1934                                 };
1935                         };
1936                         X_Dom_DTD_EMPTY[ that[ '_tag' ] ] || ( html[ n ] = '<\/' + that[ '_tag' ] + '>' );
1937                         
1938                         that[ '_newAttrs' ] && ( that[ '_flags' ] |= X_NodeFlags_DIRTY_ATTR );
1939                 };
1940                 
1941                 return html.join( '' );
1942         });
1943
1944 var X_Node__afterActualCreate =
1945         X_UA_DOM.IE4 && (function( that ){
1946                 var xnodes, i, v;
1947                 
1948                 if( !that[ '_tag' ] ) return that;
1949                 
1950                 if( ( xnodes = that[ '_xnodes' ] ) && ( i = xnodes.length ) ){
1951                         for( ; i; ){
1952                                 X_Node__afterActualCreate( xnodes[ --i ] );
1953                         };
1954                 };
1955                 // ネットワーク系属性と filter は要素生成後に適用
1956                 if( that[ '_flags' ] & ( X_NodeFlags_DIRTY_ATTR | X_NodeFlags_DIRTY_IE_FILTER ) ){
1957                         X_Node__updateRawNode( that, that[ '_rawObject' ] || X_Node__ie4getRawNode( that ) );
1958                 } else {
1959                         that[ '_flags' ] &= X_Node_BitMask_RESET_DIRTY;
1960                 };
1961                 X_EventDispatcher_toggleAllEvents( that, true );// イベントの復帰
1962         });
1963
1964 var X_Node__actualRemove =
1965         X_UA_DOM.W3C ?
1966                 // GPUレイヤーにいるうちは remove しない。-> GPU解除してから remove する
1967                 // Firefox34 では遭遇せず、Safari で何度かアニメーションしているうちに発生
1968                 ( function( that, isChild ){
1969                         var xnodes = that[ '_xnodes' ],
1970                                 elm    = that[ '_rawObject' ],
1971                                 child, i, l;
1972
1973                         if( xnodes && ( l = xnodes.length ) ){
1974                                 for( i = 0; i < l; ++i ){
1975                                         child = xnodes[ i ];
1976                                         child[ '_tag' ] && X_Node__actualRemove( child, true );
1977                                 };
1978                         };
1979
1980                         if( !elm ) return;
1981                         
1982                         if( that[ '_flags' ] & X_NodeFlags_ACTUAL_LISTENING ){
1983                                 that[ '_listeners' ] && X_EventDispatcher_toggleAllEvents( that, false );// イベントの退避
1984                                 that[ '_flags' ] &= ~X_NodeFlags_ACTUAL_LISTENING;
1985                         };
1986
1987                         // ie5では filter の効いている要素をremove時に破棄して、再度append 時に新規生成する
1988                         // ちなみに elm.filters に触ると ie8 でなぜかカラム落ちが発生、、、
1989                         if( X_Node_displayNoneFixForIE5 ){
1990                                 if( elm.filters && elm.filters.length ){
1991                                         isChild = false;
1992                                         delete that[ '_rawObject' ];
1993                                         // 破棄前にインタラクティブな属性値を控える
1994                                         if( X_Node_Attr_HAS_VALUE[ that[ '_tag' ] ] && ( !that[ '_newAttrs' ] || !X_Object_inObject( 'value', that[ '_newAttrs' ] ) ) ){
1995                                                 if( !that[ '_attrs' ] ) that[ '_attrs' ] = {};
1996                                                 that[ '_attrs' ].value = elm.value;
1997                                         };
1998                                         if( that[ '_tag' ] === 'OPTION' && ( !that[ '_newAttrs' ] || !X_Object_inObject( 'selected', that[ '_newAttrs' ] ) ) ){
1999                                                 if( !that[ '_attrs' ] ) that[ '_attrs' ] = {};
2000                                                 that[ '_attrs' ].selected = elm.selected;
2001                                         };
2002                                         if( that[ '_tag' ] === 'SELECT' && ( !that[ '_newAttrs' ] || !X_Object_inObject( 'selectedIndex', that[ '_newAttrs' ] ) ) ){
2003                                                 if( !that[ '_attrs' ] ) that[ '_attrs' ] = {};
2004                                                 that[ '_attrs' ].selectedIndex = elm.selectedIndex;
2005                                         };
2006                                         if( that[ '_tag' ] === 'INPUT' && that[ '_attrs' ] && ( that[ '_attrs' ].type === 'checkbox' || that[ '_attrs' ].type === 'radio' ) && ( !that[ '_newAttrs' ] || !X_Object_inObject( 'checked', that[ '_newAttrs' ] ) ) ){
2007                                                 if( !that[ '_attrs' ] ) that[ '_attrs' ] = {};
2008                                                 that[ '_attrs' ].checked = elm.checked;
2009                                         };
2010                                         // 子要素への参照を外す
2011                                         elm.innerHTML = '';
2012                                 };
2013                         };
2014                         
2015                         if( !X_UA[ 'MacIE' ] ){
2016                                 // elm.parentNode.tagName for ie7
2017                                 !isChild && elm.parentNode && elm.parentNode.tagName && elm.parentNode.removeChild( elm );
2018                         } else {
2019                                 !isChild && elm.parentNode && elm.parentNode.tagName && X_TEMP._fixed_remove( elm, that );
2020                         };
2021                 }) :
2022         X_UA_DOM.IE4 ?
2023                 ( function( that, isChild ){
2024                         var xnodes = that[ '_xnodes' ],
2025                                 elm    = that[ '_rawObject' ] || X_Node__ie4getRawNode( that ),
2026                                 i, l, xnode;
2027                         if( xnodes && ( l = xnodes.length ) ){
2028                                 for( i = 0; i < l; ++i ){
2029                                         X_Node__actualRemove( xnodes[ i ], true );
2030                                 };
2031                         };
2032
2033                         if( !elm ) return;
2034                         that[ '_listeners' ] && X_EventDispatcher_toggleAllEvents( that, false );// イベントの退避
2035                         
2036                         // 破棄前にインタラクティブな属性値を控える
2037                         if( X_Node_Attr_HAS_VALUE[ that[ '_tag' ] ] && ( !that[ '_newAttrs' ] || !X_Object_inObject( 'value', that[ '_newAttrs' ] ) ) ){
2038                                 if( !that[ '_attrs' ] ) that[ '_attrs' ] = {};
2039                                 that[ '_attrs' ].value = elm.value;
2040                         };
2041                         if( that[ '_tag' ] === 'OPTION' && ( !that[ '_newAttrs' ] || !X_Object_inObject( 'selected', that[ '_newAttrs' ] ) ) ){
2042                                 if( !that[ '_attrs' ] ) that[ '_attrs' ] = {};
2043                                 that[ '_attrs' ].selected = elm.selected;
2044                         };
2045                         if( that[ '_tag' ] === 'SELECT' && ( !that[ '_newAttrs' ] || !X_Object_inObject( 'selectedIndex', that[ '_newAttrs' ] ) ) ){
2046                                 if( !that[ '_attrs' ] ) that[ '_attrs' ] = {};
2047                                 that[ '_attrs' ].selectedIndex = elm.selectedIndex;
2048                         };
2049                         if( that[ '_tag' ] === 'INPUT' && that[ '_attrs' ] && ( that[ '_attrs' ].type === 'checkbox' || that[ '_attrs' ].type === 'radio' ) && ( !that[ '_newAttrs' ] || !X_Object_inObject( 'checked', that[ '_newAttrs' ] ) ) ){
2050                                 if( !that[ '_attrs' ] ) that[ '_attrs' ] = {};
2051                                 that[ '_attrs' ].checked = elm.checked;
2052                         };
2053
2054                         elm.removeAttribute( 'id' ); // ?
2055                         //document.all[ that[ '_id' ] || ( 'ie4uid' + that[ '_uid' ] ) ] = null; // MacIE5 でエラー
2056                         if( !isChild ) elm.outerHTML = ''; // elm.remove() ?
2057                         delete that[ '_rawObject' ];
2058                 }) :
2059                 (function(){});
2060
2061 X_ViewPort[ 'listenOnce' ]( X_EVENT_UNLOAD, X_Node__actualRemove, [ X_Node_html, true ] );
2062