HTMLElement, :
function unused(e) {
const s0 = JSON.stringify(window.getComputedStyle(e));
const c = Array.from(e.classList.values());
if (c.length !== 0) {
c.forEach(cc => {
e.classList.remove(cc);
const s = JSON.stringify(window.getComputedStyle(e));
if (s0 === s) {
console.log('class ${cc} is unused');
}
e.classList.add(cc);
});
}
const id = e.id;
if (id) {
e.removeAttribute("id");
const s = JSON.stringify(window.getComputedStyle(e));
if (s0 === s) {
console.log('id ${id} is unused');
}
e.id = id;
}
}
, window.getComputedStyle(), , .
(If you want an HTML version with extraneous classes and identifiers removed, change the code so that classes are re-added only if they affect the styling, call the function recursively on all child nodes, and then use DOM to serialize.) XMLSerializer. serializeToString
source
share