|
| 1 | +/*jshint -W030 */ |
| 2 | +var tagRE = /<[a-zA-Z\-\!\/](?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])*>/g |
| 3 | + |
| 4 | +var parseTag = require('./parse-tag') |
| 5 | +// re-used obj for quick lookups of components |
| 6 | +var empty = Object.create ? Object.create(null) : {} |
| 7 | + |
| 8 | +module.exports = function parse(html, options) { |
| 9 | + options || (options = {}) |
| 10 | + options.components || (options.components = empty) |
| 11 | + var result = [] |
| 12 | + var current |
| 13 | + var level = -1 |
| 14 | + var arr = [] |
| 15 | + var byTag = {} |
| 16 | + var inComponent = false |
| 17 | + |
| 18 | + if (html.indexOf('<') !== 0) { |
| 19 | + var end = html.indexOf('<') |
| 20 | + result.push({ |
| 21 | + type: 'text', |
| 22 | + content: end === -1 ? html : html.substring(0, end), |
| 23 | + }) |
| 24 | + } |
| 25 | + |
| 26 | + html.replace(tagRE, function (tag, index) { |
| 27 | + if (inComponent) { |
| 28 | + if (tag !== '</' + current.name + '>') { |
| 29 | + return |
| 30 | + } else { |
| 31 | + inComponent = false |
| 32 | + } |
| 33 | + } |
| 34 | + var isOpen = tag.charAt(1) !== '/' |
| 35 | + var start = index + tag.length |
| 36 | + var nextChar = html.charAt(start) |
| 37 | + var parent |
| 38 | + |
| 39 | + if (isOpen) { |
| 40 | + level++ |
| 41 | + |
| 42 | + current = parseTag(tag) |
| 43 | + if (current.type === 'tag' && options.components[current.name]) { |
| 44 | + current.type = 'component' |
| 45 | + inComponent = true |
| 46 | + } |
| 47 | + |
| 48 | + if ( |
| 49 | + !current.voidElement && |
| 50 | + !inComponent && |
| 51 | + nextChar && |
| 52 | + nextChar !== '<' |
| 53 | + ) { |
| 54 | + current.children.push({ |
| 55 | + type: 'text', |
| 56 | + content: html.slice(start, html.indexOf('<', start)), |
| 57 | + }) |
| 58 | + } |
| 59 | + |
| 60 | + byTag[current.tagName] = current |
| 61 | + |
| 62 | + // if we're at root, push new base node |
| 63 | + if (level === 0) { |
| 64 | + result.push(current) |
| 65 | + } |
| 66 | + |
| 67 | + parent = arr[level - 1] |
| 68 | + |
| 69 | + if (parent) { |
| 70 | + parent.children.push(current) |
| 71 | + } |
| 72 | + |
| 73 | + arr[level] = current |
| 74 | + } |
| 75 | + |
| 76 | + if (!isOpen || current.voidElement) { |
| 77 | + if ( |
| 78 | + level > -1 && |
| 79 | + (current.voidElement || current.name === tag.slice(2, tag.indexOf(' '))) |
| 80 | + ) { |
| 81 | + level-- |
| 82 | + } |
| 83 | + |
| 84 | + if (!inComponent && nextChar !== '<' && nextChar) { |
| 85 | + // trailing text node |
| 86 | + // if we're at the root, push a base text node. otherwise add as |
| 87 | + // a child to the current node. |
| 88 | + parent = level === -1 ? result : arr[level].children |
| 89 | + |
| 90 | + // calculate correct end of the content slice in case there's |
| 91 | + // no tag after the text node. |
| 92 | + var end = html.indexOf('<', start) |
| 93 | + var content = html.slice(start, end === -1 ? undefined : end) |
| 94 | + // if a node is nothing but whitespace, no need to add it. |
| 95 | + if (!/^\s*$/.test(content)) { |
| 96 | + parent.push({ |
| 97 | + type: 'text', |
| 98 | + content: content, |
| 99 | + }) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + current = arr[level] |
| 104 | + } |
| 105 | + }) |
| 106 | + |
| 107 | + return result |
| 108 | +} |
0 commit comments