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

Function removeNonInteractive

lib/html.js:132–178  ·  view source on GitHub ↗
(node)

Source from the content-addressed store, hash-verified

130
131 // Function to remove non-interactive elements recursively
132 function removeNonInteractive(node) {
133 if (node.nodeName !== '#document') {
134 const parent = node.parentNode
135 const index = parent.childNodes.indexOf(node)
136
137 if (isFilteredOut(node)) {
138 parent.childNodes.splice(index, 1)
139 return true
140 }
141
142 // keep texts for interactive elements
143 if ((isInteractive(parent) || hasMeaningfulText(parent)) && node.nodeName === '#text') {
144 node.value = node.value.trim().slice(0, 200)
145 if (!node.value) return false
146 return true
147 }
148
149 if (
150 // if parent is interactive, we may need child element to match
151 !isInteractive(parent) &&
152 !isInteractive(node) &&
153 !hasInteractiveDescendant(node) &&
154 !hasMeaningfulText(node)
155 ) {
156 parent.childNodes.splice(index, 1)
157 return true
158 }
159 }
160
161 if (node.attrs) {
162 // Filter and keep allowed attributes, accessibility attributes
163 node.attrs = node.attrs.filter(attr => {
164 if (attr.name === 'class') {
165 attr.value = filterClassValue(attr.value)
166 }
167 return allowedAttrs.includes(attr.name)
168 })
169 }
170
171 if (node.childNodes) {
172 for (let i = node.childNodes.length - 1; i >= 0; i--) {
173 const childNode = node.childNodes[i]
174 removeNonInteractive(childNode)
175 }
176 }
177 return false
178 }
179
180 // Remove non-interactive elements starting from the root element
181 removeNonInteractive(document)

Callers 1

Calls 7

isFilteredOutFunction · 0.85
isInteractiveFunction · 0.85
hasMeaningfulTextFunction · 0.85
hasInteractiveDescendantFunction · 0.85
filterClassValueFunction · 0.85
filterMethod · 0.80
includesMethod · 0.80

Tested by

no test coverage detected