var utils = require('./utils');
function trimWhitespace(str) {
- if (typeof str !== 'string') {
- return str;
- }
- return str.replace(/^[ \n\r\t\f]+/, '').replace(/[ \n\r\t\f]+$/, '');
+ return str && str.replace(/^[ \n\r\t\f]+/, '').replace(/[ \n\r\t\f]+$/, '');
}
function collapseWhitespaceAll(str) {
}
function cleanAttributeValue(tag, attrName, attrValue, options, attrs) {
- if (attrValue && isEventAttribute(attrName, options)) {
+ if (isEventAttribute(attrName, options)) {
attrValue = trimWhitespace(attrValue).replace(/^javascript:\s*/i, '');
return options.minifyJS(attrValue, true);
}
return (+numString).toString();
});
}
- else if (attrValue && options.customAttrCollapse && options.customAttrCollapse.test(attrName)) {
+ else if (options.customAttrCollapse && options.customAttrCollapse.test(attrName)) {
attrValue = attrValue.replace(/\n+|\r+|\s{2,}/g, '');
}
else if (tag === 'script' && attrName === 'type') {
}
function normalizeAttr(attr, attrs, tag, options) {
- var attrName = options.caseSensitive ? attr.name : attr.name.toLowerCase(),
+ var attrName = options.name(attr.name),
attrValue = attr.value;
if (options.decodeEntities && attrValue) {
return;
}
- attrValue = cleanAttributeValue(tag, attrName, attrValue, options, attrs);
+ if (attrValue) {
+ attrValue = cleanAttributeValue(tag, attrName, attrValue, options, attrs);
+ }
if (options.removeEmptyAttributes &&
canDeleteEmptyAttribute(tag, attrName, attrValue, options)) {
function processOptions(values) {
var options = {
+ name: function(name) {
+ return name.toLowerCase();
+ },
canCollapseWhitespace: canCollapseWhitespace,
canTrimWhitespace: canTrimWhitespace,
html5: true,
};
Object.keys(values).forEach(function(key) {
var value = values[key];
- if (key === 'log') {
+ if (key === 'caseSensitive') {
+ if (value) {
+ options.name = identity;
+ }
+ }
+ else if (key === 'log') {
if (typeof value === 'function') {
options.log = value;
}
function attrNames(attrs) {
return attrs.map(function(attr) {
- return options.caseSensitive ? attr.name : attr.name.toLowerCase();
+ return options.name(attr.name);
});
}
}
for (var i = 0, len = attrs.length; i < len; i++) {
var attr = attrs[i];
- if (classChain && (options.caseSensitive ? attr.name : attr.name.toLowerCase()) === 'class') {
+ if (classChain && attr.value && options.name(attr.name) === 'class') {
classChain.add(trimWhitespace(attr.value).split(/[ \t\n\f\r]+/).filter(shouldSkipUIDs));
}
else if (options.processScripts && attr.name.toLowerCase() === 'type') {
html5: options.html5,
start: function(tag, attrs, unary, unarySlash, autoGenerated) {
- var lowerTag = tag.toLowerCase();
-
- if (lowerTag === 'svg') {
+ if (tag.toLowerCase() === 'svg') {
options = Object.create(options);
- options.keepClosingSlash = true;
options.caseSensitive = true;
+ options.keepClosingSlash = true;
+ options.name = identity;
}
-
- tag = options.caseSensitive ? tag : lowerTag;
-
+ tag = options.name(tag);
currentTag = tag;
charsPrevTag = tag;
if (!inlineTextTags(tag)) {
}
},
end: function(tag, attrs, autoGenerated) {
- var lowerTag = tag.toLowerCase();
- if (lowerTag === 'svg') {
+ if (tag.toLowerCase() === 'svg') {
options = Object.getPrototypeOf(options);
}
- tag = options.caseSensitive ? tag : lowerTag;
+ tag = options.name(tag);
// check if current tag is in a whitespace stack
if (options.collapseWhitespace) {