(html, handler)
| 97 | |
| 98 | var special = makeMap('script,style'); |
| 99 | function HTMLParser(html, handler) { |
| 100 | var index; |
| 101 | var chars; |
| 102 | var match; |
| 103 | var stack = []; |
| 104 | var last = html; |
| 105 | |
| 106 | stack.last = function () { |
| 107 | return this[this.length - 1]; |
| 108 | }; |
| 109 | |
| 110 | while (html) { |
| 111 | chars = true; // Make sure we're not in a script or style element |
| 112 | |
| 113 | if (!stack.last() || !special[stack.last()]) { |
| 114 | // Comment |
| 115 | if (html.indexOf('<!--') == 0) { |
| 116 | index = html.indexOf('-->'); |
| 117 | |
| 118 | if (index >= 0) { |
| 119 | if (handler.comment) { |
| 120 | handler.comment(html.substring(4, index)); |
| 121 | } |
| 122 | |
| 123 | html = html.substring(index + 3); |
| 124 | chars = false; |
| 125 | } // end tag |
| 126 | |
| 127 | } else if (html.indexOf('</') == 0) { |
| 128 | match = html.match(endTag); |
| 129 | |
| 130 | if (match) { |
| 131 | html = html.substring(match[0].length); |
| 132 | match[0].replace(endTag, parseEndTag); |
| 133 | chars = false; |
| 134 | } // start tag |
| 135 | |
| 136 | } else if (html.indexOf('<') == 0) { |
| 137 | match = html.match(startTag); |
| 138 | |
| 139 | if (match) { |
| 140 | html = html.substring(match[0].length); |
| 141 | match[0].replace(startTag, parseStartTag); |
| 142 | chars = false; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if (chars) { |
| 147 | index = html.indexOf('<'); |
| 148 | var text = index < 0 ? html : html.substring(0, index); |
| 149 | html = index < 0 ? '' : html.substring(index); |
| 150 | |
| 151 | if (handler.chars) { |
| 152 | handler.chars(text); |
| 153 | } |
| 154 | } |
| 155 | } else { |
| 156 | html = html.replace(new RegExp('([\\s\\S]*?)<\/' + stack.last() + '[^>]*>'), function (all, text) { |
no test coverage detected