react-saasify-chrisvxd
Version:
React components for Saasify web clients.
33 lines (27 loc) • 1.04 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = removeEmptyAttributes;
// Source: https://www.w3.org/TR/html4/sgml/dtd.html#events (Generic Attributes)
var safeToRemoveAttrs = new Set(['id', 'class', 'style', 'title', 'lang', 'dir', 'onclick', 'ondblclick', 'onmousedown', 'onmouseup', 'onmouseover', 'onmousemove', 'onmouseout', 'onkeypress', 'onkeydown', 'onkeyup']);
/** Removes empty attributes */
function removeEmptyAttributes(tree) {
tree.walk(function (node) {
if (!node.attrs) {
return node;
}
Object.keys(node.attrs).forEach(function (attrName) {
var attrNameLower = attrName.toLowerCase();
if (!safeToRemoveAttrs.has(attrNameLower)) {
return;
}
var attrValue = node.attrs[attrName];
if (attrValue === '' || (attrValue || '').match(/^\s+$/)) {
delete node.attrs[attrName];
}
});
return node;
});
return tree;
}
;