(node)
| 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) |
no test coverage detected