if (options.minifyJS) {
var wrappedCode = '(function(){' + attrValue + '})()';
var minified = minifyJS(wrappedCode, options.minifyJS);
- return minified.slice(12, minified.length - 4).replace(/"/g, '"');
+ return minified.slice(12, minified.length - 4);
}
return attrValue;
}
function normalizeAttribute(attr, attrs, tag, hasUnarySlash, index, options, isLast) {
var attrName = options.caseSensitive ? attr.name : attr.name.toLowerCase(),
- attrValue = options.preventAttributesEscaping ? attr.value : attr.escaped,
- attrQuote = options.preventAttributesEscaping ? attr.quote : (options.quoteCharacter === '\'' ? '\'' : '"'),
+ attrValue = attr.value,
+ attrQuote = attr.quote,
attrFragment,
emittedAttrValue;
if (attrValue !== undefined && !options.removeAttributeQuotes ||
!canRemoveAttributeQuotes(attrValue)) {
+ if (!options.preventAttributesEscaping) {
+ if (options.quoteCharacter !== undefined) {
+ attrQuote = options.quoteCharacter === '\'' ? '\'' : '"';
+ }
+ else {
+ var apos = (attrValue.match(/'/g) || []).length;
+ var quot = (attrValue.match(/"/g) || []).length;
+ attrQuote = apos < quot ? '\'' : '"';
+ }
+ if (attrQuote === '"') {
+ attrValue = attrValue.replace(/"/g, '"');
+ }
+ else {
+ attrValue = attrValue.replace(/'/g, ''');
+ }
+ }
emittedAttrValue = attrQuote + attrValue + attrQuote;
}
// make sure trailing slash is not interpreted as HTML self-closing tag
var insert = buffer.length;
for (var i = attrs.length; --i >= 0; ) {
if (lint) {
- lint.testAttribute(tag, attrs[i].name.toLowerCase(), attrs[i].escaped);
+ lint.testAttribute(tag, attrs[i].name.toLowerCase(), attrs[i].value);
}
token = normalizeAttribute(attrs[i], attrs, tag, hasUnarySlash, i, options, isLast);
if (token) {
attrs.push({
name: name,
value: value,
- escaped: value && value.replace(/(^|.)("+)/g, function(match) {
- return match.replace(/"/g, '"');
- }),
customAssign: customAssign || '=',
customOpen: customOpen || '',
customClose: customClose || '',
results += '<' + tag;
for ( var i = 0; i < attrs.length; i++ ) {
- results += ' ' + attrs[i].name + '="' + attrs[i].escaped + '"';
+ results += ' ' + attrs[i].name + '="' + (attrs[i].value || '').replace(/"/g, '"') + '"';
}
results += (unary ? '/' : '') + '>';
test('nested quotes', function() {
input = '<div data=\'{"test":"\\"test\\""}\'></div>';
- output = '<div data="{"test":"\\"test\\""}"></div>';
- equal(minify(input), output);
+ equal(minify(input), input);
+ equal(minify(input, { quoteCharacter: '\'' }), input);
+
+ output = '<div data="{"test":"\\"test\\""}"></div>';
+ equal(minify(input, { quoteCharacter: '"' }), output);
});
test('script minification', function() {
equal(minify(input, { minifyJS: true }), output);
input = '<a onclick="try{ dcsMultiTrack(\'DCS.dcsuri\',\'USPS\',\'WT.ti\') }catch(e){}"> foobar</a>';
- output = '<a onclick="try{dcsMultiTrack("DCS.dcsuri","USPS","WT.ti")}catch(e){}"> foobar</a>';
+ output = '<a onclick=\'try{dcsMultiTrack("DCS.dcsuri","USPS","WT.ti")}catch(e){}\'> foobar</a>';
equal(minify(input, { minifyJS: { mangle: false } }), output);
+ equal(minify(input, { minifyJS: { mangle: false }, quoteCharacter: '\'' }), output);
+
+ input = '<a onclick="try{ dcsMultiTrack(\'DCS.dcsuri\',\'USPS\',\'WT.ti\') }catch(e){}"> foobar</a>';
+ output = '<a onclick="try{dcsMultiTrack("DCS.dcsuri","USPS","WT.ti")}catch(e){}"> foobar</a>';
+
+ equal(minify(input, { minifyJS: { mangle: false }, quoteCharacter: '"' }), output);
input = '<a onClick="_gaq.push([\'_trackEvent\', \'FGF\', \'banner_click\']);"></a>';
- output = '<a onclick="_gaq.push(["_trackEvent","FGF","banner_click"])"></a>';
+ output = '<a onclick=\'_gaq.push(["_trackEvent","FGF","banner_click"])\'></a>';
equal(minify(input, { minifyJS: true }), output);
+ equal(minify(input, { minifyJS: true, quoteCharacter: '\'' }), output);
+
+ input = '<a onClick="_gaq.push([\'_trackEvent\', \'FGF\', \'banner_click\']);"></a>';
+ output = '<a onclick="_gaq.push(["_trackEvent","FGF","banner_click"])"></a>';
+
+ equal(minify(input, { minifyJS: true, quoteCharacter: '"' }), output);
input = '<button type="button" onclick=";return false;" id="appbar-guide-button"></button>';
output = '<button type="button" onclick="return!1" id="appbar-guide-button"></button>';