/*jshint -W030 */ var tagRE = /(?:|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g; var parseTag = require('./parse-tag'); // re-used obj for quick lookups of components var empty = Object.create ? Object.create(null) : {}; // common logic for pushing a child node onto a list function pushTextNode(list, html, level, start, ignoreWhitespace) { // calculate correct end of the content slice in case there's // no tag after the text node. var end = html.indexOf('<', start); var content = html.slice(start, end === -1 ? undefined : end); // if a node is nothing but whitespace, collapse it as the spec states: // https://www.w3.org/TR/html4/struct/text.html#h-9.1 if (/^\s*$/.test(content)) { content = ' '; } // don't add whitespace-only text nodes if they would be trailing text nodes // or if they would be leading whitespace-only text nodes: // * end > -1 indicates this is not a trailing text node // * leading node is when level is -1 and list has length 0 if ((!ignoreWhitespace && end > -1 && level + list.length >= 0) || content !== ' ') { list.push({ type: 'text', content: content }); } } module.exports = function parse(html, options) { options || (options = {}); options.components || (options.components = empty); var result = []; var current; var level = -1; var arr = []; var byTag = {}; var inComponent = false; html.replace(tagRE, function (tag, index) { if (inComponent) { if (tag !== ('')) { return; } else { inComponent = false; } } var isOpen = tag.charAt(1) !== '/'; var isComment = tag.indexOf('