improve parser logic
authoralexlamsl <alexlamsl@gmail.com>
Fri, 12 Feb 2016 20:11:12 +0000 (04:11 +0800)
committeralexlamsl <alexlamsl@gmail.com>
Fri, 12 Feb 2016 20:11:12 +0000 (04:11 +0800)
src/htmlparser.js

index 9e1e6c5..0d598b3 100644 (file)
   }
 
   var HTMLParser = global.HTMLParser = function( html, handler ) {
-    var index, chars, match, stack = [], last = html, prevTag, nextTag;
+    var index, match, stack = [], last = html, prevTag, nextTag;
     stack.last = function() {
       var last = this[ this.length - 1 ];
       return last && last.tag;
     var attr = attrForHandler(handler);
 
     while ( html ) {
-      chars = true;
-
       // Make sure we're not in a script or style element
       if ( !stack.last() || !special[ stack.last() ] ) {
 
             }
             html = html.substring( index + 3 );
             prevTag = '';
-            chars = false;
+            continue;
           }
         }
 
             }
             html = html.substring( index + 2 );
             prevTag = '';
-            chars = false;
+            continue;
           }
         }
 
         // Ignored elements?
-        else if ( /^<\?/.test( html ) ) {
+        if ( /^<\?/.test( html ) ) {
           index = html.indexOf( '?>', 2 );
           if ( index >= 0 ) {
             if ( handler.chars ) {
             }
             html = html.substring( index + 2 );
             prevTag = '';
+            continue;
           }
         }
 
-        else if ( /^<%/.test( html ) ) {
+        if ( /^<%/.test( html ) ) {
           index = html.indexOf( '%>', 2 );
           if ( index >= 0 ) {
             if ( handler.chars ) {
             }
             html = html.substring( index + 2 );
             prevTag = '';
+            continue;
           }
         }
 
         // Doctype:
-        else if ( (match = doctype.exec( html )) ) {
+        if ( (match = doctype.exec( html )) ) {
           if ( handler.doctype ) {
             handler.doctype( match[0] );
           }
           html = html.substring( match[0].length );
-          chars = false;
+          prevTag = '';
+          continue;
         }
 
         // End tag:
-        else if ( /^<\//.test( html ) ) {
+        if ( /^<\//.test( html ) ) {
           match = html.match( endTag );
-
           if ( match ) {
             html = html.substring( match[0].length );
             match[0].replace( endTag, parseEndTag );
             prevTag = '/' + match[1].toLowerCase();
-            chars = false;
+            continue;
           }
 
         }
         // Start tag:
-        else if ( /^</.test( html ) ) {
+        if ( /^</.test( html ) ) {
           match = html.match( startTag );
           if ( match ) {
             html = html.substring( match[0].length );
             match[0].replace( startTag, parseStartTag );
             prevTag = match[1].toLowerCase();
-            chars = false;
+            continue;
           }
         }
 
-        if ( chars ) {
-          index = html.indexOf('<');
+        index = html.indexOf('<');
 
-          var text = index < 0 ? html : html.substring( 0, index );
-          html = index < 0 ? '' : html.substring( index );
+        var text = index < 0 ? html : html.substring( 0, index );
+        html = index < 0 ? '' : html.substring( index );
 
-          // next tag
-          tagMatch = html.match( startTag );
+        // next tag
+        tagMatch = html.match( startTag );
+        if (tagMatch) {
+          nextTag = tagMatch[1];
+        }
+        else {
+          tagMatch = html.match( endTag );
           if (tagMatch) {
-            nextTag = tagMatch[1];
+            nextTag = '/' + tagMatch[1];
           }
           else {
-            tagMatch = html.match( endTag );
-            if (tagMatch) {
-              nextTag = '/' + tagMatch[1];
-            }
-            else {
-              nextTag = '';
-            }
-          }
-
-          if ( handler.chars ) {
-            handler.chars(text, prevTag, nextTag);
+            nextTag = '';
           }
+        }
 
+        if ( handler.chars ) {
+          handler.chars(text, prevTag, nextTag);
         }
+        prevTag = '';
 
       }
       else {