2 X.Dom._useBuilder = true;
\r
4 /* --------------------------------------
\r
5 * 通常のwebページに対して使用する場合、dom ready で dom tree を写し取るために使う.
\r
6 * 完全にワンページアプリで<body/>が空な場合、このコードはビルドに含める必要はない
\r
8 X.Dom.listenOnce( X.Dom.Event.DOM_PRE_INIT,
\r
9 document.getElementById ?
\r
16 (function( elm, skip, head ){
\r
17 var me = arguments.callee,
\r
18 moveToHead = 'style,bgsound,area,base,meta'.split( ',' ),
\r
19 remove = 'script,noscript,noframes,comment,!,noembed,nolayer'.split( ',' ),
\r
20 noncleanup = 'pre,textarea,code,kbd,samp,xmp,plaintext,listing'.split( ',' ),
\r
21 nodes = X.copyArray( elm.childNodes ),
\r
24 node, tag, textNode, content;
\r
25 for( ; i < l; ++i ){
\r
27 switch( node.nodeType ){
\r
29 tag = node.tagName.toLowerCase();
\r
30 if( moveToHead.indexOf( tag ) !== -1 ){
\r
31 head = head || document.getElementsByTagName( 'head' )[ 0 ];
\r
32 head.appendChild( node );
\r
35 if( remove.indexOf( tag ) !== -1 ){
\r
36 elm.removeChild( node );
\r
39 // pre タグ以下はスペースの置換は行わない
\r
40 node.childNodes && node.childNodes.length && me( node, skip || noncleanup.indexOf( tag ) !== -1, head );
\r
45 content = skip ? node.data : X.Dom.cleanupWhiteSpace( node.data );
\r
46 //console.log( 'Delete space ' + node.data.length + ' => ' + content.length );
\r
47 if( !textNode && content !== ' ' && content.length ){
\r
48 node.data = content;
\r
53 textNode.data += content; // 直前が TextNode の場合 一本化して削除
\r
55 // ブロック要素直下のスペースだけは削除??
\r
57 //console.log( 'Remove type: ' + node.nodeType + ' value: ' + node.nodeValue );
\r
58 elm.removeChild( node );
\r
64 function createTree( xnode, childNodes, skipCleanup, textarea ){
\r
67 l = childNodes.length,
\r
68 child, _xnode, f, tag, text, _xtext, doc;
\r
69 childNodes = X.copyArray( childNodes );
\r
72 xnode.attr( 'value', xnode.html() ).empty();
\r
76 for( ; i < l; ++i ){
\r
77 child = childNodes[ i ];
\r
78 tag = child.tagName;
\r
79 if( ( child.nodeType !== 1 && child.nodeType !== 3 ) || tag === '!' || ( tag && tag.charAt( 0 ) === '/' ) ){
\r
80 child.parentNode.removeChild( child );
\r
84 while( xnode._xnodes && j < xnode._xnodes.length ){
\r
85 _xnode = xnode._xnodes[ j ];
\r
86 _xnode.parent = xnode;
\r
88 if( _xnode._xnodeType === 1 ){
\r
89 if( child.nodeType !== 1 ){
\r
90 if( !( text = child.data ) || ( text = X.Dom.cleanupWhiteSpace( text ) ) === ' ' ){
\r
91 child.parentNode.removeChild( child );
\r
94 alert( '[' +xnode._tag + '>' +_xnode._tag + '] !== ' + child.nodeType + '\n' + child.data );
\r
98 if( _xnode._tag.toUpperCase() !== tag ){
\r
99 alert( '[' +xnode._tag + '>' +_xnode._tag + ' ' + (_xnode._xnodes ? _xnode._xnodes.length : '' ) + '] !== ' + tag + ' ' + (child.childNodes ? child.childNodes.length : '' ) + '\n' + child.outerHTML );
\r
103 _xnode._rawNode = child;
\r
104 //if( ( doc = child.ownerDocument || child.document ) && ( doc.createElement( 'p' ).tagName === doc.createElement( 'P' ).tagName ) ){
\r
105 if( tag.charAt( 0 ) === '/' ) tag = tag.slice( 1 );
\r
106 _xnode._tag = tag; // .toUpperCase()
\r
108 _xnode._root = xnode._root;
\r
109 child.UID = _xnode._uid;
\r
110 if( 0 <= X.Dom.cleanupTagNames.indexOf( tag.toLowerCase() ) || tag === 'SCRIPT' ){ // ie で body 内の script が2度よばれるのに対処
\r
111 //alert( '[' +xnode._tag + '>' + _xnode._tag + '] remove ... ' );
\r
116 //alert( '[' +xnode._tag + '>' + _xnode._tag + ' ' + (_xnode._xnodes ? _xnode._xnodes.length : '' ) + '] === ' + tag + ' ' + (child.childNodes ? child.childNodes.length : '' ) + ' Hit\n' + child.outerHTML );
\r
117 child.childNodes && child.childNodes.length && createTree( _xnode, child.childNodes, skipCleanup || 0 <= X.Dom.skipCleanupTagNames.indexOf( tag.toLowerCase() ), tag === 'TEXTAREA' );
\r
125 if( _xnode._xnodeType === 3 ){
\r
127 if( child.nodeType !== 3 ){
\r
128 if( !( text = _xnode._text ) || ( text = X.Dom.cleanupWhiteSpace( text ) ) === ' ' ){
\r
129 console.log( '[' +xnode._tag + '>' + _xnode._uid + '] destroy ... ' );
\r
134 alert( xnode._tag + '>' + '"' + _xnode._text + '" !== ' + tag + '\n' + child.outerHTML );
\r
139 _xnode._rawNode = child;
\r
140 _xnode._root = xnode._root;
\r
141 if( !skipCleanup ){
\r
142 if( !( text = _xnode._text ) || ( text = X.Dom.cleanupWhiteSpace( text ) ) === ' ' ){
\r
143 console.log( '[' +xnode._tag + '>' + _xnode._uid + '] destroy ... ' );
\r
149 _xtext.text( _xtext._text + text );
\r
150 console.log( '[' +xnode._tag + '>' + _xnode._uid + '] xtext,destroy ... ' );
\r
155 //alert( xnode._tag + '>' + '"' + text + '"\n' + child.data );
\r
156 _xnode.text( text );
\r
160 _xtext.text( _xtext._text + _xnode._text );
\r
161 console.log( '[' +xnode._tag + '>' + _xnode._uid + '] xtext,destroy ... ' );
\r
166 _xtext = _xtext || _xnode;
\r
170 alert( 'no hit!' );
\r
175 //if( !f ) alert( '**** ' + child.outerHTML );
\r
177 while( xnode._xnodes && j < xnode._xnodes.length ){
\r
178 _xnode = xnode._xnodes[ j ];
\r
179 _xnode.parent = xnode;
\r
188 body.appendChild( elmProgress = document.createElement( 'div' ) );
\r
189 elmProgress.style.cssText = 'position:absolute;top:0;left:0;z-index:9999;width:0;height:0.5em;background:#00f;overflow:hidden;';
\r
191 X.Dom.asyncParse( body.innerHTML, true )
\r
192 //.listenOnce( X.Event.SUCCESS, X.Dom._onAsyncParseComplete, [ createTree ] )
\r
193 .listen( X.Event.PROGRESS,
\r
195 elmProgress.style.width = ( e.progress * 100 | 0 ) + '%';
\r
198 .listenOnce( X.Event.SUCCESS, function( e ){
\r
199 var xnodes = Node.root._xnodes = [], t;
\r
200 xnodes.push.apply( xnodes, e.xnodes );
\r
202 asyncCreateTree( Node.root, body.childNodes, elmProgress );
\r
210 i, n = 0, elmProgress = '_xdom_builder_progress';
\r
212 function createTree( xnode, children, skipCleanup, textarea ){
\r
213 var parent = xnode,
\r
214 xnodes = X.copyArray( parent._xnodes ),
\r
215 l = xnodes && xnodes.length,
\r
216 m = children.length,
\r
217 i = 0, j = 0, flag = 0,
\r
218 elm, tag, xtext, text;
\r
219 //children = X.copyArray( children );
\r
222 xnode.attr( 'value', xnode.html() ).empty();
\r
226 for( ; i < xnodes.length; ++i ){
\r
227 xnode = xnodes[ i ];
\r
228 xnode.parent = parent;
\r
230 if( xnode._xnodeType === 3 ){
\r
231 //alert( X.Dom.cleanupWhiteSpace( xnode._text ) );
\r
232 if( !skipCleanup ){
\r
233 if( !( text = xnode._text ) || ( text = X.Dom.cleanupWhiteSpace( text ) ) === ' ' ){
\r
237 //alert( 'xtext ' + text.charCodeAt( 0 ) + ' : ' + text.length );
\r
238 xtext.text( xtext._text + text );
\r
241 //alert( 'xnode ' + text.charCodeAt( 0 ) + ' : ' + text.length );
\r
242 xnode.text( text );
\r
246 //alert( 'skip ' + text.charCodeAt( 0 ) + ' : ' + text.length );
\r
247 xtext.text( xtext._text + xnode._text );
\r
251 xtext = xtext || xnode;
\r
255 if( xnode._xnodeType !== 1 ){
\r
256 //alert( xnode._xnodeType )
\r
260 for( ; j < m; ++j ){
\r
261 elm = children[ j ];
\r
264 * 未知のタグについては、閉じタグも含めてタグ名扱いになる
\r
266 if( tag === '!' || tag.charAt( 0 ) === '/' ){
\r
267 //alert( '## ' + tag );
\r
270 if( xnode._tag !== tag ){
\r
271 alert( xnode._tag + ' ' + ' !== ' + tag + '\nxnode.html():' + xnode.attr('cite') + '\nelm.outerHTML:' + elm.outerHTML );
\r
273 xnode._rawNode = elm;
\r
274 xnode._root = parent._root;
\r
275 //xnode._tag = X.Dom.DTD.TAG_FIX[ tag ] || tag;
\r
276 if( 0 <= X.Dom.cleanupTagNames.indexOf( tag.toLowerCase() ) || tag === 'SCRIPT' ){
\r
280 xnode._xnodes && xnode._xnodes.length && createTree( xnode, elm.children, skipCleanup || 0 <= X.Dom.skipCleanupTagNames.indexOf( tag.toLowerCase() ), tag === 'TEXTAREA' );
\r
282 !xnode._id && elm.setAttribute( 'id', 'ie4uid' + xnode._uid );
\r
283 elm.setAttribute( 'UID', xnode._uid );
\r
285 tag === 'INPUT' && (
\r
287 ( xnode._attrs = { type : 'text' } ) :
\r
288 !xnode._attrs.type || ( xnode._attrs.type = 'text' )
\r
297 if( !xnode._rawNode ){
\r
298 alert( xnode._tag + ' ' + xnode._id + ' !== none...' );
\r
304 ( flag & 6 ) && ( parent._dirty |= X.Dom.Dirty.IE4_TEXTNODE_FIX );
\r
307 * http://support.microsoft.com/kb/812417/ja
\r
308 * PRB: outerHTML の HTML 要素のプロパティは、既定の属性は表示されません。
\r
310 * body.innerHTML でなく、 body.outerHTML にはできなかった、、、
\r
312 body.insertAdjacentHTML( 'BeforeEnd', '<div id="' + elmProgress + '" style="position:absolute;top:0;left:0;z-index:9999;width:0;height:0.5em;background:#00f;overflow:hidden;"></div>' );
\r
313 elmProgress = document.all[ elmProgress ];
\r
315 X.Dom.asyncParse( body.innerHTML, true )
\r
316 //.listenOnce( X.Event.SUCCESS, X.Dom._onAsyncParseComplete, [ createTree ] )
\r
317 .listen( X.Event.PROGRESS,
\r
319 elmProgress.style.width = ( e.progress * 100 | 0 ) + '%';
\r
322 .listenOnce( X.Event.SUCCESS, function( e ){
\r
323 var xnodes = Node.root._xnodes = [], t;
\r
324 xnodes.push.apply( xnodes, e.xnodes );
\r
326 asyncCreateTree( Node.root, body.childNodes || body.children, elmProgress );
\r
333 X.Dom._onAsyncParseComplete = function( e, createTree ){
\r
334 var xnodes = Node.root._xnodes = [], t;
\r
335 xnodes.push.apply( xnodes, e.xnodes );
\r
338 createTree( Node.root, document.body.childNodes || document.body.children );
\r
339 //alert( X.getTime() - t );
\r
341 X.Dom.asyncDispatch( 0, { type : X.Dom.Event.DOM_BUILDER_COMPLETE } );
\r
343 delete X.Dom._onAsyncParseComplete;
\r
347 function asyncCreateTree( parent, elems, elmProgress, async ){
\r
348 var xnodes = async ? 0 : X.copyArray( parent._xnodes ),
\r
349 l = async ? 0 : xnodes.length,
\r
350 current = async ? async.current : {
\r
360 stack = async ? async.stack : [],
\r
361 done = async ? async.done : 0,
\r
362 startTime = X.getTime(),
\r
364 while( current || ( current = stack.pop() ) ){
\r
368 parent = current.me;
\r
369 xnodes = current.xnodes;
\r
370 while( xnode = xnodes[ i ] ){
\r
372 dive = bindElementToXnode( parent, xnode, current );
\r
378 stack[ stack.length ] = current;
\r
384 xnodes = dive.xnodes;
\r
388 if( startTime + 16 <= X.getTime() ){
\r
391 async.current = i < l && current;
\r
394 X.Timer.once( 0, asyncCreateTree, [ null, null, elmProgress, async || { stack : stack, current : i < l && current, done : done } ] );
\r
396 elmProgress.style.width = ( ( 1 - done / Node._chashe.length ) * 100 | 0 ) + '%';
\r
404 X.Dom.asyncDispatch( 0, { type : X.Dom.Event.DOM_BUILDER_COMPLETE } );
\r
407 bindElementToXnode =
\r
408 document.getElementById ?
\r
409 (function( parent, xnode, current ){
\r
410 var elems = current.elems,
\r
413 xtext = current.xtext,
\r
414 skipCleanup = current.skipCleanup,
\r
417 xnode.parent = parent;
\r
419 for( ; current.j < m; ++current.j ){
\r
420 elm = elems[ current.j ];
\r
422 if( ( elm.nodeType !== 1 && elm.nodeType !== 3 ) || tag === '!' || ( tag && tag.charAt( 0 ) === '/' ) ){
\r
423 elm.parentNode.removeChild( elm );
\r
427 if( xnode._xnodeType === 1 ){
\r
428 if( elm.nodeType === 3 ){
\r
429 if( !( text = elm.data ) || ( text = X.Dom.cleanupWhiteSpace( text ) ) === ' ' ){
\r
430 elm.parentNode.removeChild( elm );
\r
433 alert( '[' +parent._tag + '>' +xnode._tag + '] !== ' + elm.nodeType + '\n' + elm.data );
\r
435 if( xnode._tag.toUpperCase() !== tag ){
\r
436 alert( '[' +parent._tag + '>' +xnode._tag + ' ' + (xnode._xnodes ? xnode._xnodes.length : '' ) + '] !== ' + tag + ' ' + (elm.childNodes ? elm.childNodes.length : '' ) + '\n' + elm.outerHTML );
\r
438 xnode._rawNode = elm;
\r
439 //if( ( doc = elm.ownerDocument || elm.document ) && ( doc.createElement( 'p' ).tagName === doc.createElement( 'P' ).tagName ) ){
\r
440 if( tag.charAt( 0 ) === '/' ) tag = tag.slice( 1 );
\r
441 xnode._tag = tag; // .toUpperCase()
\r
443 xnode._root = parent._root;
\r
444 elm.UID = xnode._uid;
\r
446 if( tag === 'TEXTAREA' ){
\r
447 xnode.attr( 'value', xnode.html() ).empty();
\r
449 if( 0 <= X.Dom.cleanupTagNames.indexOf( tag.toLowerCase() ) || tag === 'SCRIPT' ){ // ie で body 内の script が2度よばれるのに対処
\r
450 //alert( '[' +parent._tag + '>' + xnode._tag + '] remove ... ' );
\r
453 if( elm.childNodes && elm.childNodes.length ){
\r
454 //alert( '[' +parent._tag + '>' + xnode._tag + ' ' + (xnode._xnodes ? xnode._xnodes.length : '' ) + '] === ' + tag + ' ' + (elm.childNodes ? elm.childNodes.length : '' ) + ' Hit\n' + elm.outerHTML );
\r
458 xnodes : X.copyArray( xnode._xnodes ),
\r
462 l : xnode._xnodes.length,
\r
463 elems : elm.childNodes,
\r
465 skipCleanup : skipCleanup || 0 <= X.Dom.skipCleanupTagNames.indexOf( tag.toLowerCase() )
\r
468 current.xtext = null;
\r
474 if( elm.nodeType !== 3 ){
\r
475 if( !( text = xnode._text ) || ( text = X.Dom.cleanupWhiteSpace( text ) ) === ' ' ){
\r
476 console.log( '[' +parent._tag + '>' + xnode._uid + '] destroy ... ' );
\r
480 alert( parent._tag + '>' + '"' + xnode._text + '" !== ' + tag + '\n' + elm.outerHTML );
\r
485 xnode._rawNode = elm;
\r
486 xnode._root = parent._root;
\r
487 if( !skipCleanup ){
\r
488 if( !( text = xnode._text ) || ( text = X.Dom.cleanupWhiteSpace( text ) ) === ' ' ){
\r
489 console.log( '[' +parent._tag + '>' + xnode._uid + '] destroy ... ' );
\r
493 xtext.text( xtext._text + text );
\r
494 console.log( '[' +parent._tag + '>' + xnode._uid + '] xtext,destroy ... ' );
\r
497 //alert( parent._tag + '>' + '"' + text + '"\n' + elm.data );
\r
498 xnode.text( text );
\r
502 xtext.text( xtext._text + xnode._text );
\r
503 console.log( '[' +parent._tag + '>' + xnode._uid + '] xtext,destroy ... ' );
\r
506 current.xtext = xtext || xnode;
\r
510 (function ( parent, xnode, current ){
\r
511 var elems = current.elems,
\r
514 xtext = current.xtext,
\r
515 skipCleanup = current.skipCleanup,
\r
518 xnode.parent = parent;
\r
520 if( xnode._xnodeType === 3 ){
\r
521 //alert( X.Dom.cleanupWhiteSpace( xnode._text ) );
\r
522 if( !skipCleanup ){
\r
523 if( !( text = xnode._text ) || ( text = X.Dom.cleanupWhiteSpace( text ) ) === ' ' ){
\r
527 //alert( 'xtext ' + text.charCodeAt( 0 ) + ' : ' + text.length );
\r
528 xtext.text( xtext._text + text );
\r
531 //alert( 'xnode ' + text.charCodeAt( 0 ) + ' : ' + text.length );
\r
532 xnode.text( text );
\r
536 //alert( 'skip ' + text.charCodeAt( 0 ) + ' : ' + text.length );
\r
537 xtext.text( xtext._text + xnode._text );
\r
541 current.xtext = xtext || xnode;
\r
545 if( xnode._xnodeType !== 1 ){
\r
546 //alert( xnode._xnodeType )
\r
550 for( ; j < m; ++j, ++current.j ){
\r
554 * 未知のタグについては、閉じタグも含めてタグ名扱いになる
\r
556 if( tag === '!' || tag.charAt( 0 ) === '/' ){
\r
557 //alert( '## ' + tag );
\r
560 if( xnode._tag !== tag ){
\r
561 alert( xnode._tag + ' ' + ' !== ' + tag + '\nxnode.html():' + xnode.attr('cite') + '\nelm.outerHTML:' + elm.outerHTML );
\r
565 xnode._rawNode = elm;
\r
566 xnode._root = parent._root;
\r
567 //xnode._tag = X.Dom.DTD.TAG_FIX[ tag ] || tag;
\r
568 if( 0 <= X.Dom.cleanupTagNames.indexOf( tag.toLowerCase() ) || tag === 'SCRIPT' ){
\r
573 !xnode._id && elm.setAttribute( 'id', 'ie4uid' + xnode._uid );
\r
574 elm.setAttribute( 'UID', xnode._uid );
\r
576 tag === 'INPUT' && (
\r
578 ( xnode._attrs = { type : 'text' } ) :
\r
579 !xnode._attrs.type || ( xnode._attrs.type = 'text' )
\r
582 current.xtext = null;
\r
584 if( tag === 'TEXTAREA' ){
\r
585 xnode.attr( 'value', xnode.html() ).empty();
\r
587 if( xnode._xnodes && xnode._xnodes.length ){
\r
590 xnodes : X.copyArray( xnode._xnodes ),
\r
594 l : xnode._xnodes.length,
\r
595 elems : elm.children,
\r
597 skipCleanup : skipCleanup || 0 <= X.Dom.skipCleanupTagNames.indexOf( tag.toLowerCase() )
\r
604 if( !xnode._rawNode ){
\r
605 alert( xnode._tag + ' ' + xnode._id + ' !== none...' );
\r
609 ( current.flag & 6 ) && ( parent._dirty |= X.Dom.Dirty.IE4_TEXTNODE_FIX );
\r