MCPcopy Index your code
hub / github.com/codeceptjs/CodeceptJS / cleanHtml

Function cleanHtml

lib/html.js:40–74  ·  view source on GitHub ↗
(html)

Source from the content-addressed store, hash-verified

38const DROP_ATTRS = new Set(['style'])
39
40function cleanHtml(html) {
41 const document = parse(html)
42
43 function walk(node) {
44 if (!node) return false
45
46 if (DROP_TAGS.has(node.nodeName) || (node.nodeName === 'script' && !(node.attrs || []).some(a => a.name === 'src'))) {
47 const parent = node.parentNode
48 const idx = parent.childNodes.indexOf(node)
49 if (idx >= 0) parent.childNodes.splice(idx, 1)
50 return true
51 }
52
53 if (node.attrs) {
54 node.attrs = node.attrs.filter(attr => {
55 if (DROP_ATTRS.has(attr.name)) return false
56 if (attr.name === 'class') {
57 attr.value = filterClassValue(attr.value)
58 if (!attr.value) return false
59 }
60 return true
61 })
62 }
63
64 if (node.childNodes) {
65 for (let i = node.childNodes.length - 1; i >= 0; i--) {
66 walk(node.childNodes[i])
67 }
68 }
69 return false
70 }
71
72 walk(document)
73 return serialize(document)
74}
75
76const defaultHtmlOpts = {
77 interactiveElements: ['a', 'input', 'button', 'select', 'textarea', 'option'],

Callers 2

html_test.jsFile · 0.90
formatHtmlFunction · 0.85

Calls 1

walkFunction · 0.70

Tested by

no test coverage detected