* 解析
(html)
| 194 | * 解析 |
| 195 | */ |
| 196 | function parse(html) { |
| 197 | const r = { |
| 198 | children: [], |
| 199 | } |
| 200 | const stack = [r] |
| 201 | |
| 202 | stack.last = function() { |
| 203 | return this[this.length - 1] |
| 204 | } |
| 205 | |
| 206 | tokenize(html, { |
| 207 | start(tagName, attrs, unary) { |
| 208 | const node = { |
| 209 | type: 'element', |
| 210 | tagName, |
| 211 | attrs, |
| 212 | unary, |
| 213 | children: [], |
| 214 | } |
| 215 | |
| 216 | stack.last().children.push(node) |
| 217 | |
| 218 | if (!unary) { |
| 219 | stack.push(node) |
| 220 | } |
| 221 | }, |
| 222 | // eslint-disable-next-line no-unused-vars |
| 223 | end(tagName) { |
| 224 | const node = stack.pop() |
| 225 | |
| 226 | if (node.tagName === 'table') { |
| 227 | // 补充插入 tbody |
| 228 | let hasTbody = false |
| 229 | |
| 230 | for (const child of node.children) { |
| 231 | if (child.tagName === 'tbody') { |
| 232 | hasTbody = true |
| 233 | break |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | if (!hasTbody) { |
| 238 | node.children = [{ |
| 239 | type: 'element', |
| 240 | tagName: 'tbody', |
| 241 | attrs: [], |
| 242 | unary: false, |
| 243 | children: node.children, |
| 244 | }] |
| 245 | } |
| 246 | } |
| 247 | }, |
| 248 | text(content) { |
| 249 | content = content.trim() |
| 250 | if (!content) return |
| 251 | |
| 252 | stack.last().children.push({ |
| 253 | type: 'text', |
nothing calls this directly
no test coverage detected