|
| 1 | +// Based on package html-parse-stringify2 |
| 2 | +// Expanded to handle webcomponents |
| 3 | + |
| 4 | +var attrRE, lookup, parseTag, pushCommentNode, pushTextNode, tagRE; |
| 5 | + |
| 6 | +tagRE = /(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g; |
| 7 | + |
| 8 | +attrRE = /([^\t\n\f \/><"'=]+)|(['"])(.*?)\2/g; |
| 9 | + |
| 10 | +lookup = { |
| 11 | + area: true, |
| 12 | + base: true, |
| 13 | + br: true, |
| 14 | + col: true, |
| 15 | + embed: true, |
| 16 | + hr: true, |
| 17 | + img: true, |
| 18 | + input: true, |
| 19 | + keygen: true, |
| 20 | + link: true, |
| 21 | + menuitem: true, |
| 22 | + meta: true, |
| 23 | + param: true, |
| 24 | + source: true, |
| 25 | + track: true, |
| 26 | + wbr: true |
| 27 | +}; |
| 28 | + |
| 29 | +parseTag = function (tag) { |
| 30 | + var i, key, res; |
| 31 | + i = 0; |
| 32 | + key = void 0; |
| 33 | + res = { |
| 34 | + type: 'tag', |
| 35 | + name: '', |
| 36 | + voidElement: false, |
| 37 | + attrs: {}, |
| 38 | + children: [] |
| 39 | + }; |
| 40 | + tag.replace(attrRE, function (match) { |
| 41 | + if (i % 2) { |
| 42 | + key = match; |
| 43 | + } else { |
| 44 | + if (i === 0) { |
| 45 | + if (lookup[match] || tag.charAt(tag.length - 2) === '/') { |
| 46 | + res.voidElement = true; |
| 47 | + } |
| 48 | + res.name = match; |
| 49 | + } else { |
| 50 | + res.attrs[key] = match.replace(/^['"]|['"]$/g, ''); |
| 51 | + } |
| 52 | + } |
| 53 | + i++; |
| 54 | + }); |
| 55 | + return res; |
| 56 | +}; |
| 57 | + |
| 58 | +// common logic for pushing a child node onto a list |
| 59 | +pushTextNode = function (list, html, start) { |
| 60 | + var content, end; |
| 61 | + // calculate correct end of the content slice in case there's |
| 62 | + // no tag after the text node. |
| 63 | + end = html.indexOf('<', start); |
| 64 | + content = html.slice(start, end === -1 ? void 0 : end); |
| 65 | + if (!/^\s*$/.test(content)) { |
| 66 | + list.push({ |
| 67 | + type: 'text', |
| 68 | + content: content |
| 69 | + }); |
| 70 | + } |
| 71 | +}; |
| 72 | + |
| 73 | +pushCommentNode = function (list, tag) { |
| 74 | + var content; |
| 75 | + // calculate correct end of the content slice in case there's |
| 76 | + // no tag after the text node. |
| 77 | + content = tag.replace('<!--', '').replace('-->', ''); |
| 78 | + if (!/^\s*$/.test(content)) { |
| 79 | + list.push({ |
| 80 | + type: 'comment', |
| 81 | + content: content |
| 82 | + }); |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | + |
| 87 | +module.exports = function (html) { |
| 88 | + var arr, byTag, current, level, result; |
| 89 | + result = []; |
| 90 | + current = void 0; |
| 91 | + level = -1; |
| 92 | + arr = []; |
| 93 | + byTag = {}; |
| 94 | + html.replace(tagRE, function (tag, index) { |
| 95 | + var isComment, isOpen, nextChar, parent, start; |
| 96 | + isOpen = tag.charAt(1) !== '/'; |
| 97 | + isComment = tag.indexOf('<!--') === 0; |
| 98 | + start = index + tag.length; |
| 99 | + nextChar = html.charAt(start); |
| 100 | + parent = void 0; |
| 101 | + if (isOpen && !isComment) { |
| 102 | + level++; |
| 103 | + current = parseTag(tag); |
| 104 | + if (!current.voidElement && nextChar && nextChar !== '<') { |
| 105 | + pushTextNode(current.children, html, start); |
| 106 | + } |
| 107 | + byTag[current.tagName] = current; |
| 108 | + // if we're at root, push new base node |
| 109 | + if (level === 0) { |
| 110 | + result.push(current); |
| 111 | + } |
| 112 | + parent = arr[level - 1]; |
| 113 | + if (parent) { |
| 114 | + parent.children.push(current); |
| 115 | + } |
| 116 | + arr[level] = current; |
| 117 | + } |
| 118 | + if (isComment) { |
| 119 | + if (level < 0) { |
| 120 | + pushCommentNode(result, tag); |
| 121 | + } else { |
| 122 | + pushCommentNode(arr[level].children, tag); |
| 123 | + } |
| 124 | + } |
| 125 | + if (isComment || !isOpen || current.voidElement) { |
| 126 | + if (!isComment) { |
| 127 | + level--; |
| 128 | + } |
| 129 | + if (nextChar !== '<' && nextChar) { |
| 130 | + // trailing text node |
| 131 | + // if we're at the root, push a base text node. otherwise add as |
| 132 | + // a child to the current node. |
| 133 | + parent = level === -1 ? result : arr[level].children; |
| 134 | + pushTextNode(parent, html, start); |
| 135 | + } |
| 136 | + } |
| 137 | + }); |
| 138 | + return result; |
| 139 | +}; |
0 commit comments