46 lines
No EOL
1.2 KiB
JavaScript
46 lines
No EOL
1.2 KiB
JavaScript
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports["default"] = collapseAttributeWhitespace;
|
|
|
|
var _collapseAttributeWhitespace = require("./collapseAttributeWhitespace");
|
|
|
|
/** Deduplicate values inside list-like attributes (e.g. class, rel) */
|
|
function collapseAttributeWhitespace(tree) {
|
|
tree.walk(function (node) {
|
|
if (!node.attrs) {
|
|
return node;
|
|
}
|
|
|
|
Object.keys(node.attrs).forEach(function (attrName) {
|
|
var attrNameLower = attrName.toLowerCase();
|
|
|
|
if (!_collapseAttributeWhitespace.attributesWithLists.has(attrNameLower)) {
|
|
return;
|
|
}
|
|
|
|
var attrValues = node.attrs[attrName].split(/\s/);
|
|
var uniqeAttrValues = new Set();
|
|
var deduplicatedAttrValues = [];
|
|
attrValues.forEach(function (attrValue) {
|
|
if (!attrValue) {
|
|
// Keep whitespaces
|
|
deduplicatedAttrValues.push('');
|
|
return;
|
|
}
|
|
|
|
if (uniqeAttrValues.has(attrValue)) {
|
|
return;
|
|
}
|
|
|
|
deduplicatedAttrValues.push(attrValue);
|
|
uniqeAttrValues.add(attrValue);
|
|
});
|
|
node.attrs[attrName] = deduplicatedAttrValues.join(' ');
|
|
});
|
|
return node;
|
|
});
|
|
return tree;
|
|
} |