hensei-web/node_modules/htmlnano/lib/modules/removeRedundantAttributes.js
2020-09-11 06:44:42 -07:00

65 lines
No EOL
1.7 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = removeRedundantAttributes;
var redundantAttributes = {
'form': {
'method': 'get'
},
'input': {
'type': 'text'
},
'button': {
'type': 'submit'
},
'script': {
'language': 'javascript',
'type': 'text/javascript',
// Remove attribute if the function returns false
'charset': function charset(node) {
// The charset attribute only really makes sense on “external” SCRIPT elements:
// http://perfectionkills.com/optimizing-html/#8_script_charset
return node.attrs && !node.attrs.src;
}
},
'style': {
'media': 'all',
'type': 'text/css'
},
'link': {
'media': 'all'
}
};
/** Removes redundant attributes */
function removeRedundantAttributes(tree) {
var tags = Object.keys(redundantAttributes);
var tagMatchRegExp = new RegExp('^(' + tags.join('|') + ')$');
tree.match({
tag: tagMatchRegExp
}, function (node) {
var tagRedundantAttributes = redundantAttributes[node.tag];
node.attrs = node.attrs || {};
for (var _i = 0, _Object$keys = Object.keys(tagRedundantAttributes); _i < _Object$keys.length; _i++) {
var redundantAttributeName = _Object$keys[_i];
var tagRedundantAttributeValue = tagRedundantAttributes[redundantAttributeName];
var isRemove = false;
if (typeof tagRedundantAttributeValue === 'function') {
isRemove = tagRedundantAttributeValue(node);
} else if (node.attrs[redundantAttributeName] === tagRedundantAttributeValue) {
isRemove = true;
}
if (isRemove) {
delete node.attrs[redundantAttributeName];
}
}
return node;
});
return tree;
}