2 * Original code by Erik John Resig (ejohn.org)
3 * http://ejohn.org/blog/pure-javascript-html-parser/
9 var alphabets = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
10 whiteSpace = '\t\r\n\f\b ';
12 // Empty Elements - HTML 4.01
13 var empty = X.Dom.DTD.EMPTY;
15 // Block Elements - HTML 4.01
16 var block = {address:1,applet:1,blockquote:1,button:1,center:1,dd:1,del:1,dir:1,div:1,dl:1,dt:1,fieldset:1,form:1,frameset:1,hr:1,iframe:1,ins:1,isindex:1,li:1,map:1,menu:1,noframes:1,noscript:1,object:1,ol:1,p:1,pre:1,script:1,table:1,tbody:1,td:1,tfoot:1,th:1,thead:1,tr:1,ul:1};
18 // Inline Elements - HTML 4.01
19 var inline = {a:1,abbr:1,acronym:1,applet:1,b:1,basefont:1,bdo:1,big:1,br:1,button:1,cite:1,code:1,del:1,dfn:1,em:1,font:1,i:1,iframe:1,img:1,input:1,ins:1,kbd:1,label:1,map:1,object:1,q:1,s:1,samp:1,script:1,select:1,small:1,span:1,strike:1,strong:1,sub:1,sup:1,textarea:1,tt:1,u:1,'var':1};
21 // Elements that you can, intentionally, leave open
22 // (and which close themselves)
23 var closeSelf = {colgroup:1,dd:1,dt:1,li:1,options:1,p:1,tbody:1,td:1,tfoot:1,th:1,thead:1,tr:1}; // add tbody
26 var plainText = { plaintext : 1, xmp : 1 };
33 colgroup : { caption : 1 },
34 thead : { caption : 1, colgroup : 1 },
35 tfoot : { caption : 1, colgroup : 1, thead : 1, tbody : 1 },
36 tbody : { caption : 1, colgroup : 1, thead : 1, tfoot : 1 }
39 * http://www.tohoho-web.com/html/tbody.htm
40 * HTML4.01では、ヘッダとフッタを先読みして表示するために、<tbody> よりも <tfoot> の方を先に記述しなくてはならないと定義されています。
41 * IE5.0 などでは HEAD → BODY → FOOT の順に表示するのですが、
42 * <tfoot> に未対応の古いブラウザでは、HEAD → FOOT → BODY の順に表示されてしまいます。
43 * また、HTML5 では、<tfoot> と <tbody> の順番はどちらでもよいことになりました。
46 // Attributes that have their values filled in disabled="disabled"
47 var fillAttrs = X.Dom.Attr.noValue; //{checked:1,compact:1,declare:1,defer:1,disabled:1,ismap:1,multiple:1,nohref:1,noresize:1,noshade:1,nowrap:1,readonly:1,selected:1};
49 // Special Elements (can contain anything)
50 var special = {script:1,style:1};
52 X.Dom.Parser = function( html, handler ) {
55 chars, last, text, index;
59 last = stack[ stack.length - 1 ];
61 // Make sure we're not in a script or style element
62 if ( last && special[ last.toLowerCase() ] === 1 ) {
63 if( 0 <= ( index = _parseEndTag( stack, handler, html ) ) ){
64 //handler.chars( html.substring( 0, index ) );
65 html = html.substring( index );
69 if ( html.indexOf("<!--") === 0 ) {
70 if ( 0 < ( index = html.indexOf("-->") ) ) {
71 handler.comment( html.substring( 4, index ) );
72 html = html.substring( index + 3 );
77 } else if ( html.indexOf("</") === 0 ) {
78 if ( 2 < ( index = _parseEndTag( stack, handler, html ) ) ) {
79 html = html.substring( index );
84 } else if ( html.indexOf("<") === 0 ) {
85 if( index = _parseStartTag( stack, last, handler, html ) ){
86 html = html.substring( index );
89 if( index === false ){
95 index = html.indexOf("<");
97 text = index < 0 ? html : html.substring( 0, index );
98 html = index < 0 ? '' : html.substring( index );
100 handler.chars( text );
105 if ( html === lastHtml ){
112 // Clean up any remaining tags
113 parseEndTag( stack, handler );
116 function _parseStartTag( stack, last, handler, html ){
122 chr, start, attrName, quot, escape;
124 while( i < l && phase < 9 ){
125 chr = html.charAt( i );
128 chr === '<' && ( ++phase );
130 case 1 : // タグ名の開始を待つ
131 alphabets.indexOf( chr ) !== -1 && ( ++phase && ( start = i ) );
133 case 2 : // タグ名の終わりの空白文字を待つ
134 whiteSpace.indexOf( chr ) !== -1 ?
135 ( ++phase && ( tagName = html.substring( start, i ) ) ) :
136 ( chr === '>' || ( empty = html.substr( i, 2 ) === '/>' ) ) &&
137 ( ( tagName = html.substring( start, i ) ) && ( phase = 9 ) );
139 case 3 : // 属性名の開始を待つ
140 alphabets.indexOf( chr ) !== -1 ?
141 ( ++phase && ( start = i ) ) :
142 ( chr === '>' || ( empty = html.substr( i, 2 ) === '/>' ) ) &&
145 case 4 : // 属性名の終わりを待つ
147 ( ( phase = 6 ) && ( attrName = html.substring( start, i ) ) ) :
148 whiteSpace.indexOf( chr ) !== -1 &&
149 ( ( phase = 5 ) && ( attrName = html.substring( start, i ) ) );
151 case 5 : // 属性の = または次の属性または htmlタグの閉じ
152 alphabets.indexOf( chr ) !== -1 ?
153 ( ( phase = 4 ) && ( attrs[ attrs.length ] = attrName ) && ( start = i ) ) :
156 ( chr === '>' || ( empty = html.substr( i, 2 ) === '/>' ) ) &&
157 ( ( phase = 9 ) && ( attrs[ attrs.length ] = attrName ) );
159 case 6 : // 属性値の開始 quot を待つ
160 ( chr === '"' || chr === "'" ) ?
161 ( ( phase = 7 ) && ( quot = chr ) && ( start = i + 1 ) ):
162 whiteSpace.indexOf( chr ) === -1 &&
163 ( ( phase = 8 ) && ( start = i ) ); // no quot
165 case 7 : //属性値の閉じ quot を待つ
166 !escape && ( chr === quot ) && ( phase = 3 ) && saveAttr( attrs, attrName, html.substring( start, i ) );
168 case 8 : //閉じ quot のない属性の値
169 whiteSpace.indexOf( chr ) !== -1 ?
170 ( ( phase = 3 ) && saveAttr( attrs, attrName, html.substring( start, i ) ) ) :
171 ( chr === '>' || ( empty = html.substr( i, 2 ) === '/>' ) ) &&
172 ( ( phase = 9 ) && saveAttr( attrs, attrName, html.substring( start, i ) ) );
175 escape = chr === '\\' && !escape; // \\\\ is not escape for "
179 if( parseStartTag( stack, last, handler, tagName, attrs, empty, i ) === false ) return false;
185 function _parseEndTag( stack, handler, html ){
192 while( i < l && phase < 9 ){
193 chr = html.charAt( i );
196 html.substr( i, 2 ) === '</' && ( ++phase && ++i );
198 case 1 : // タグ名の開始を待つ
199 alphabets.indexOf( chr ) !== -1 && ( ++phase && ( start = i ) );
201 case 2 : // タグ名の終わりの空白文字を待つ
202 whiteSpace.indexOf( chr ) !== -1 && ( ++phase );
203 ( chr === '>' ) && ( phase = 9 );
204 ( phase !== 2 ) && ( tagName = html.substring( start, i ) );
206 case 3 : // 属性名の開始を待つ
207 chr === '>' && ( phase = 9 );
213 parseEndTag( stack, handler, tagName );
219 function saveAttr( attrs, name, value ){
220 name = name.toLowerCase();
221 value = fillAttrs[ name ] === 1 ? name : value;
222 attrs[ attrs.length ] = {
226 value.indexOf( '"' ) !== -1 ?
227 value.split( '"' ).join( '\\"' ).split( '\\\\"' ).join( '\\"' ) :
232 function parseStartTag( stack, last, handler, tagName, attrs, unary, index ) {
233 var tagLower = tagName.toLowerCase();
234 if ( block[ tagLower ] === 1 ) {
235 while ( last && inline[ last.toLowerCase() ] === 1 ) {
236 parseEndTag( stack, handler, last );
237 last = stack[ stack.length - 1 ];
240 closeSelf[ tagLower ] === 1 && ( last === tagName || ( sisters[ tagLower ] && sisters[ tagLower ][ last.toLowerCase() ] === 1 ) ) && parseEndTag( stack, handler, last );
241 unary = empty[ tagLower ] === 1 || !!unary;
242 !unary && ( stack[ stack.length ] = tagName );
244 return handler.start( tagName, attrs, unary, index );
247 function parseEndTag( stack, handler, tagName ) {
248 var pos = 0, i = stack.length;
249 // If no tag name is provided, clean shop
251 // Find the closest opened tag of the same type
253 for ( pos = i; 0 <= pos; )
254 if ( stack[ --pos ] === tagName )
258 // Close all the open elements, up the stack
260 handler.end( stack[ --i ] );
262 // Remove the open elements from the stack
269 X.Dom._htmlStringToXNode = {
272 err : function( html ){
273 this.flat.length = 0;
274 this.ignoreError !== true && X.Notification.warn( 'X.Dom.Parser() error ' + html );
276 start : function( tagName, attrs, noChild, length ){
281 attr, name, i, _attrs; //, toIndex;
283 xnode = nest[ l - 1 ].create( tagName );
285 xnode = flat[ flat.length ] = X.Dom.Node.create( tagName );
287 if( !noChild ) nest[ l ] = xnode;
288 if( i = attrs.length ){
289 //toIndex = X.Dom.Attr.toIndex;
292 if( attr = attrs[ --i ] ){
293 if( typeof attr === 'string' ){
295 //i = toIndex[ name ];
296 //_attrs[ ( i || i === 0 ) ? i : name ] = name;
297 _attrs[ name ] = true;
300 //i = toIndex[ name ];
301 //_attrs[ ( i || i === 0 ) ? i : name ] = attr.escaped;
302 _attrs[ name ] = attr.escaped;
306 xnode.attr( _attrs );
310 0 < this.nest.length && ( --this.nest.length );
312 chars : function( text ){
313 if( this.nest.length ){
314 this.nest[ this.nest.length - 1 ].createText( text );
316 this.flat[ this.flat.length ] = X.Dom.Node.createText( text );
319 comment : X.emptyFunction
322 X.Dom.parse = function( html, ignoreError ){
323 var worker = X.Dom._htmlStringToXNode, ret;
325 worker.nest.length = 0;
326 worker.ignoreError = ignoreError;
327 X.Dom.Parser( html, worker );