MCPcopy Create free account
hub / github.com/CoderOpen/waimai / parseHtml

Function parseHtml

front/common/html-parser.js:272–350  ·  view source on GitHub ↗
(html)

Source from the content-addressed store, hash-verified

270}
271
272function parseHtml(html) {
273 html = removeDOCTYPE(html);
274 var stacks = [];
275 var results = {
276 node: 'root',
277 children: []
278 };
279 HTMLParser(html, {
280 start: function start(tag, attrs, unary) {
281 var node = {
282 name: tag
283 };
284
285 if (attrs.length !== 0) {
286 node.attrs = parseAttrs(attrs);
287 }
288
289 if (unary) {
290 var parent = stacks[0] || results;
291
292 if (!parent.children) {
293 parent.children = [];
294 }
295
296 parent.children.push(node);
297 } else {
298 stacks.unshift(node);
299 }
300 },
301 end: function end(tag) {
302 var node = stacks.shift();
303 if (node.name !== tag) console.error('invalid state: mismatch end tag');
304
305 if (stacks.length === 0) {
306 results.children.push(node);
307 } else {
308 var parent = stacks[0];
309
310 if (!parent.children) {
311 parent.children = [];
312 }
313
314 parent.children.push(node);
315 }
316 },
317 chars: function chars(text) {
318 var node = {
319 type: 'text',
320 text: text
321 };
322
323 if (stacks.length === 0) {
324 results.children.push(node);
325 } else {
326 var parent = stacks[0];
327
328 if (!parent.children) {
329 parent.children = [];

Callers

nothing calls this directly

Calls 3

removeDOCTYPEFunction · 0.85
HTMLParserFunction · 0.85
parseAttrsFunction · 0.85

Tested by

no test coverage detected