* @example * htmlParser(htmlString, { * start: function(tag, attrs, unary) {}, * end: function(tag) {}, * chars: function(text) {}, * comment: function(text) {} * }); * * @param {string} html string * @param {object} handler
(html, handler)
| 273 | * @param {object} handler |
| 274 | */ |
| 275 | function htmlParser(html, handler) { |
| 276 | if (typeof html !== 'string') { |
| 277 | if (html === null || typeof html === 'undefined') { |
| 278 | html = ''; |
| 279 | } else { |
| 280 | html = '' + html; |
| 281 | } |
| 282 | } |
| 283 | var index, chars, match, stack = [], last = html, text; |
| 284 | stack.last = function() { return stack[ stack.length - 1 ]; }; |
| 285 | |
| 286 | while (html) { |
| 287 | text = ''; |
| 288 | chars = true; |
| 289 | |
| 290 | // Make sure we're not in a script or style element |
| 291 | if (!stack.last() || !specialElements[ stack.last() ]) { |
| 292 | |
| 293 | // White space |
| 294 | if (WHITE_SPACE_REGEXP.test(html)) { |
| 295 | match = html.match(WHITE_SPACE_REGEXP); |
| 296 | |
| 297 | if (match) { |
| 298 | var mat = match[0]; |
| 299 | if (handler.whitespace) handler.whitespace(match[0]); |
| 300 | html = html.replace(match[0], ''); |
| 301 | chars = false; |
| 302 | } |
| 303 | //Comment |
| 304 | } else if (SINGLE_COMMENT_REGEXP.test(html)) { |
| 305 | match = html.match(SINGLE_COMMENT_REGEXP); |
| 306 | |
| 307 | if (match) { |
| 308 | if (handler.comment) handler.comment(match[1]); |
| 309 | html = html.replace(match[0], ''); |
| 310 | chars = false; |
| 311 | } |
| 312 | // DOCTYPE |
| 313 | } else if (DOCTYPE_REGEXP.test(html)) { |
| 314 | match = html.match(DOCTYPE_REGEXP); |
| 315 | |
| 316 | if (match) { |
| 317 | html = html.replace(match[0], ''); |
| 318 | chars = false; |
| 319 | } |
| 320 | // end tag |
| 321 | } else if (BEGING_END_TAGE_REGEXP.test(html)) { |
| 322 | match = html.match(END_TAG_REGEXP); |
| 323 | |
| 324 | if (match) { |
| 325 | html = html.substring(match[0].length); |
| 326 | match[0].replace(END_TAG_REGEXP, parseEndTag); |
| 327 | chars = false; |
| 328 | } |
| 329 | |
| 330 | // start tag |
| 331 | } else if (BEGIN_TAG_REGEXP.test(html)) { |
| 332 | match = html.match(START_TAG_REGEXP); |
no test coverage detected