animate/webGl/my-threejs-test/node_modules/htmlnano/lib/modules/deduplicateAttributeValues.mjs

41 lines
1.2 KiB
JavaScript
Raw Normal View History

2024-06-24 09:24:00 +00:00
import { attributesWithLists } from './collapseAttributeWhitespace.mjs';
/** Deduplicate values inside list-like attributes (e.g. class, rel) */
export function onAttrs() {
return (attrs) => {
const newAttrs = attrs;
Object.keys(attrs).forEach(attrName => {
if (! attributesWithLists.has(attrName)) {
return;
}
if (typeof attrs[attrName] !== 'string') {
return;
}
const attrValues = attrs[attrName].split(/\s/);
const uniqeAttrValues = new Set();
const deduplicatedAttrValues = [];
attrValues.forEach((attrValue) => {
if (! attrValue) {
// Keep whitespaces
deduplicatedAttrValues.push('');
return;
}
if (uniqeAttrValues.has(attrValue)) {
return;
}
deduplicatedAttrValues.push(attrValue);
uniqeAttrValues.add(attrValue);
});
newAttrs[attrName] = deduplicatedAttrValues.join(' ');
});
return newAttrs;
};
}