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
16 // textarea の内容を控えて、消す。xnode tree 構築後に復帰。でないと、html パースでこける
\r
19 (function/*cleanUpTree*/( elm, skip, head ){
\r
20 var moveToHead = 'style,bgsound,area,base,meta'.split( ',' ),
\r
21 remove = 'script,noscript,noframes,comment,!,noembed,nolayer'.split( ',' ),
\r
22 noncleanup = 'pre,textarea,code,kbd,samp,xmp,plaintext,listing'.split( ',' ),
\r
23 nodes = X.copyArray( elm.childNodes ),
\r
26 node, tag, textNode, content;
\r
27 for( ; i < l; ++i ){
\r
29 switch( node.nodeType ){
\r
31 tag = node.tagName.toLowerCase();
\r
32 if( moveToHead.indexOf( tag ) !== -1 ){
\r
33 head = head || document.getElementsByTagName( 'head' )[ 0 ];
\r
34 head.appendChild( node );
\r
37 if( remove.indexOf( tag ) !== -1 ){
\r
38 elm.removeChild( node );
\r
41 // pre タグ以下はスペースの置換は行わない
\r
42 node.childNodes && node.childNodes.length && /*cleanUpTree*/arguments.callee( node, skip || noncleanup.indexOf( tag ) !== -1, head );
\r
47 content = skip ? node.data : X.Dom.cleanupWhiteSpace( node.data );
\r
48 //console.log( 'Delete space ' + node.data.length + ' => ' + content.length );
\r
49 if( !textNode && content !== ' ' && content.length ){
\r
50 node.data = content;
\r
55 textNode.data += content; // 直前が TextNode の場合 一本化して削除
\r
57 // ブロック要素直下のスペースだけは削除??
\r
59 //console.log( 'Remove type: ' + node.nodeType + ' value: ' + node.nodeValue );
\r
60 elm.removeChild( node );
\r
68 body.appendChild( elmProgress = document.createElement( 'div' ) );
\r
69 elmProgress.style.cssText = 'position:absolute;top:0;left:0;z-index:9999;width:0;height:0.5em;background:#00f;overflow:hidden;';
\r
71 X.Dom.asyncParse( body.innerHTML, true )
\r
72 .listen( X.Event.PROGRESS,
\r
74 elmProgress.style.width = ( e.progress * 100 | 0 ) + '%';
\r
77 .listenOnce( X.Event.SUCCESS, function( e ){
\r
78 var xnodes = Node.root._xnodes = [], t;
\r
79 xnodes.push.apply( xnodes, e.xnodes );
\r
81 X.Dom._asyncCreateTree( Node.root, body.childNodes, elmProgress );
\r
89 i, n = 0, elmProgress = '_xdom_builder_progress';
\r
92 * http://support.microsoft.com/kb/812417/ja
\r
93 * PRB: outerHTML の HTML 要素のプロパティは、既定の属性は表示されません。
\r
95 * body.innerHTML でなく、 body.outerHTML にはできなかった、、、
\r
97 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
98 elmProgress = document.all[ elmProgress ];
\r
100 X.Dom.asyncParse( body.innerHTML, true )
\r
101 .listen( X.Event.PROGRESS,
\r
103 elmProgress.style.width = ( e.progress * 100 | 0 ) + '%';
\r
106 .listenOnce( X.Event.SUCCESS,
\r
108 var xnodes = Node.root._xnodes = [], t;
\r
109 xnodes.push.apply( xnodes, e.xnodes );
\r
111 X.Dom._asyncCreateTree( Node.root, body.childNodes || body.children, elmProgress );
\r
119 X.Dom._asyncCreateTree = function ( parent, elems, elmProgress, async ){
\r
120 var xnodes = async ? 0 : X.copyArray( parent._xnodes ),
\r
121 l = async ? 0 : xnodes.length,
\r
122 stack = async ? async.stack : [],
\r
123 done = async ? async.done : 0,
\r
124 startTime = X.getTime(),
\r
125 current = async ? async.current : {
\r
130 elems : X.copyArray( elems ),
\r
136 while( current || ( current = stack.pop() ) ){
\r
140 parent = current.me;
\r
141 xnodes = current.xnodes;
\r
142 while( xnode = xnodes[ i ] ){
\r
144 dive = X.Dom._bindElementToXnode( parent, xnode, current );
\r
150 stack[ stack.length ] = current;
\r
156 xnodes = dive.xnodes;
\r
160 if( startTime + 16 <= X.getTime() ){
\r
163 async.current = i < l && current;
\r
166 X.Timer.once( 0, X.Dom._asyncCreateTree, [ null, null, elmProgress, async || { stack : stack, current : i < l && current, done : done } ] );
\r
168 elmProgress.style.width = ( ( 1 - done / Node._chashe.length ) * 100 | 0 ) + '%';
\r
176 X.Dom.asyncDispatch( 0, { type : X.Dom.Event.DOM_BUILDER_COMPLETE } );
\r
177 elmProgress.parentNode ? elmProgress.parentNode.removeChild( elmProgress ) : ( elmProgress.outerHTML = '' );
\r
178 delete X.Dom._asyncCreateTree;
\r
179 delete X.Dom._bindElementToXnode;
\r
182 X.Dom._bindElementToXnode =
\r
184 (function( parent, xnode, current ){
\r
185 var elems = current.elems,
\r
188 xtext = current.xtext,
\r
189 skipCleanup = current.skipCleanup,
\r
190 inPreTag = current.inPreTag,
\r
193 xnode.parent = parent;
\r
195 for( ; current.j < m; ++current.j ){
\r
196 elm = elems[ current.j ];
\r
198 if( ( elm.nodeType !== 1 && elm.nodeType !== 3 ) || tag === '!' || ( tag && tag.charAt( 0 ) === '/' ) ){
\r
199 elm.parentNode.removeChild( elm );
\r
203 if( xnode._xnodeType === 1 ){
\r
204 if( elm.nodeType === 3 ){
\r
205 //if( !( text = elm.data ) || ( text = X.Dom.cleanupWhiteSpace( text ) ) === ' ' ){
\r
206 // elm.parentNode.removeChild( elm );
\r
209 alert( '[' +parent._tag + '>' +xnode._tag + '] !== ' + elm.nodeType + '\n' + elm.data );
\r
211 if( xnode._tag.toUpperCase() !== tag ){
\r
212 alert( '[' +parent._tag + '>' +xnode._tag + ' ' + (xnode._xnodes ? xnode._xnodes.length : '' ) + '] !== ' + tag + ' ' + (elm.childNodes ? elm.childNodes.length : '' ) + '\n' + elm.outerHTML );
\r
214 xnode._rawNode = elm;
\r
215 //if( ( doc = elm.ownerDocument || elm.document ) && ( doc.createElement( 'p' ).tagName === doc.createElement( 'P' ).tagName ) ){
\r
216 if( tag.charAt( 0 ) === '/' ) tag = tag.slice( 1 );
\r
217 xnode._tag = tag; // .toUpperCase()
\r
219 xnode._root = parent._root;
\r
220 elm.UID = xnode._uid;
\r
222 if( tag === 'TEXTAREA' ){
\r
223 xnode.attr( 'value', xnode.html() ).empty();
\r
224 current.xtext = null;
\r
226 if( !xnode.hasClass( 'skip-cleanup' ) && ( X.Dom.cleanupTagNames[ tag.toLowerCase() ] || tag === 'SCRIPT' || xnode.hasClass( 'cleanup-target' ) ) ){ // ie で body 内の script が2度よばれるのに対処
\r
227 //alert( '[' +parent._tag + '>' + xnode._tag + '] remove ... ' );
\r
230 if( elm.childNodes && elm.childNodes.length ){
\r
231 //alert( '[' +parent._tag + '>' + xnode._tag + ' ' + (xnode._xnodes ? xnode._xnodes.length : '' ) + '] === ' + tag + ' ' + (elm.childNodes ? elm.childNodes.length : '' ) + ' Hit\n' + elm.outerHTML );
\r
232 current.xtext = null;
\r
237 xnodes : X.copyArray( xnode._xnodes ),
\r
241 l : xnode._xnodes.length,
\r
242 elems : X.copyArray( elm.childNodes ),
\r
244 skipCleanup : skipCleanup || 0 <= X.Dom.skipCleanupTagNames.indexOf( tag.toLowerCase() )
\r
252 if( elm.nodeType !== 3 ){
\r
253 if( !( text = xnode._text ) || ( text = X.Dom.cleanupWhiteSpace( text ) ) === ' ' ){
\r
254 console.log( '[' +parent._tag + '>' + xnode._uid + '] destroy ... ' );
\r
258 alert( parent._tag + '>' + '"' + xnode._text + '" !== ' + tag + '\n' + elm.outerHTML );
\r
263 xnode._rawNode = elm;
\r
264 xnode._root = parent._root;
\r
265 if( !skipCleanup ){
\r
266 if( !( text = xnode._text ) || ( text = X.Dom.cleanupWhiteSpace( text ) ) === ' ' ){
\r
267 console.log( '[' +parent._tag + '>' + xnode._uid + '] destroy ... ' );
\r
271 xtext.text( xtext._text + text );
\r
272 console.log( '[' +parent._tag + '>' + xnode._uid + '] xtext,destroy ... ' );
\r
275 //alert( parent._tag + '>' + '"' + text + '"\n' + elm.data );
\r
276 xnode.text( text );
\r
280 xtext.text( xtext._text + xnode._text );
\r
281 console.log( '[' +parent._tag + '>' + xnode._uid + '] xtext,destroy ... ' );
\r
284 current.xtext = xtext || xnode;
\r
288 (function ( parent, xnode, current ){
\r
289 var elems = current.elems,
\r
292 xtext = current.xtext,
\r
293 skipCleanup = current.skipCleanup,
\r
296 xnode.parent = parent;
\r
298 if( xnode._xnodeType === 3 ){
\r
299 //alert( X.Dom.cleanupWhiteSpace( xnode._text ) );
\r
300 if( !skipCleanup ){
\r
301 if( !( text = xnode._text ) || ( text = X.Dom.cleanupWhiteSpace( text ) ) === ' ' ){
\r
305 //alert( 'xtext ' + text.charCodeAt( 0 ) + ' : ' + text.length );
\r
306 xtext.text( xtext._text + text );
\r
309 //alert( 'xnode ' + text.charCodeAt( 0 ) + ' : ' + text.length );
\r
310 xnode.text( text );
\r
314 //alert( 'skip ' + text.charCodeAt( 0 ) + ' : ' + text.length );
\r
315 xtext.text( xtext._text + xnode._text );
\r
319 current.xtext = xtext || xnode;
\r
323 if( xnode._xnodeType !== 1 ){
\r
324 //alert( xnode._xnodeType )
\r
328 for( ; j < m; ++j, ++current.j ){
\r
332 * 未知のタグについては、閉じタグも含めてタグ名扱いになる
\r
334 if( tag === '!' || tag.charAt( 0 ) === '/' ){
\r
335 //alert( '## ' + tag );
\r
338 if( xnode._tag !== tag ){
\r
339 alert( xnode._tag + ' ' + ' !== ' + tag + '\nxnode.html():' + xnode.attr('cite') + '\nelm.outerHTML:' + elm.outerHTML );
\r
343 xnode._rawNode = elm;
\r
344 xnode._root = parent._root;
\r
345 //xnode._tag = X.Dom.DTD.TAG_FIX[ tag ] || tag;
\r
346 if( !xnode.hasClass( 'skip-cleanup' ) && ( X.Dom.cleanupTagNames[ tag.toLowerCase() ] || tag === 'SCRIPT' || xnode.hasClass( 'cleanup-target' ) ) ){
\r
351 !xnode._id && elm.setAttribute( 'id', 'ie4uid' + xnode._uid );
\r
352 elm.setAttribute( 'UID', xnode._uid );
\r
354 tag === 'INPUT' && (
\r
356 ( xnode._attrs = { type : 'text' } ) :
\r
357 !xnode._attrs.type || ( xnode._attrs.type = 'text' )
\r
360 current.xtext = null;
\r
362 if( tag === 'TEXTAREA' ){
\r
363 xnode.attr( 'value', xnode.html() ).empty();
\r
365 if( xnode._xnodes && xnode._xnodes.length ){
\r
368 xnodes : X.copyArray( xnode._xnodes ),
\r
372 l : xnode._xnodes.length,
\r
373 elems : X.copyArray( elm.children ),
\r
375 skipCleanup : skipCleanup || 0 <= X.Dom.skipCleanupTagNames.indexOf( tag.toLowerCase() )
\r
382 if( !xnode._rawNode ){
\r
383 alert( xnode._tag + ' ' + xnode._id + ' !== none...' );
\r
387 ( current.flag & 6 ) && ( parent._dirty |= X.Dom.Dirty.IE4_TEXTNODE_FIX );
\r