Skip to content

Commit 826aa39

Browse files
committed
preserve input options (#910)
1 parent a2ae883 commit 826aa39

File tree

1 file changed

+110
-126
lines changed

1 file changed

+110
-126
lines changed

src/htmlminifier.js

Lines changed: 110 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -613,113 +613,107 @@ function identity(value) {
613613
return value;
614614
}
615615

616-
function processOptions(options) {
617-
['html5', 'includeAutoGeneratedTags'].forEach(function(key) {
618-
if (!(key in options)) {
619-
options[key] = true;
620-
}
621-
});
622-
623-
if (typeof options.log !== 'function') {
624-
options.log = identity;
625-
}
626-
627-
if (!options.canCollapseWhitespace) {
628-
options.canCollapseWhitespace = canCollapseWhitespace;
629-
}
630-
if (!options.canTrimWhitespace) {
631-
options.canTrimWhitespace = canTrimWhitespace;
632-
}
633-
634-
if (!('ignoreCustomComments' in options)) {
635-
options.ignoreCustomComments = [/^!/];
636-
}
637-
638-
if (!('ignoreCustomFragments' in options)) {
639-
options.ignoreCustomFragments = [
616+
function processOptions(values) {
617+
var options = {
618+
canCollapseWhitespace: canCollapseWhitespace,
619+
canTrimWhitespace: canTrimWhitespace,
620+
html5: true,
621+
ignoreCustomComments: [/^!/],
622+
ignoreCustomFragments: [
640623
/<%[\s\S]*?%>/,
641624
/<\?[\s\S]*?\?>/
642-
];
643-
}
644-
645-
if (!options.minifyURLs) {
646-
options.minifyURLs = identity;
647-
}
648-
if (typeof options.minifyURLs !== 'function') {
649-
var minifyURLs = options.minifyURLs;
650-
if (typeof minifyURLs === 'string') {
651-
minifyURLs = { site: minifyURLs };
652-
}
653-
else if (typeof minifyURLs !== 'object') {
654-
minifyURLs = {};
655-
}
656-
options.minifyURLs = function(text) {
657-
try {
658-
return RelateUrl.relate(text, minifyURLs);
659-
}
660-
catch (err) {
661-
options.log(err);
662-
return text;
625+
],
626+
includeAutoGeneratedTags: true,
627+
log: identity,
628+
minifyCSS: identity,
629+
minifyJS: identity,
630+
minifyURLs: identity
631+
};
632+
Object.keys(values).forEach(function(key) {
633+
var value = values[key];
634+
if (key === 'log') {
635+
if (typeof value === 'function') {
636+
options.log = value;
663637
}
664-
};
665-
}
666-
667-
if (!options.minifyJS) {
668-
options.minifyJS = identity;
669-
}
670-
if (typeof options.minifyJS !== 'function') {
671-
var minifyJS = options.minifyJS;
672-
if (typeof minifyJS !== 'object') {
673-
minifyJS = {};
674638
}
675-
(minifyJS.parse || (minifyJS.parse = {})).bare_returns = false;
676-
options.minifyJS = function(text, inline) {
677-
var start = text.match(/^\s*<!--.*/);
678-
var code = start ? text.slice(start[0].length).replace(/\n\s*-->\s*$/, '') : text;
679-
minifyJS.parse.bare_returns = inline;
680-
var result = UglifyJS.minify(code, minifyJS);
681-
if (result.error) {
682-
options.log(result.error);
683-
return text;
639+
else if (key === 'minifyCSS' && typeof value !== 'function') {
640+
if (!value) {
641+
return;
684642
}
685-
return result.code.replace(/;$/, '');
686-
};
687-
}
688-
689-
if (!options.minifyCSS) {
690-
options.minifyCSS = identity;
691-
}
692-
if (typeof options.minifyCSS !== 'function') {
693-
var minifyCSS = options.minifyCSS;
694-
if (typeof minifyCSS !== 'object') {
695-
minifyCSS = {};
696-
}
697-
options.minifyCSS = function(text, type) {
698-
text = text.replace(/(url\s*\(\s*)("|'|)(.*?)\2(\s*\))/ig, function(match, prefix, quote, url, suffix) {
699-
return prefix + quote + options.minifyURLs(url) + quote + suffix;
700-
});
701-
try {
702-
if (type === 'inline') {
703-
text = wrapInlineCSS(text);
704-
}
705-
else if (type === 'media') {
706-
text = wrapMediaQuery(text);
643+
if (typeof value !== 'object') {
644+
value = {};
645+
}
646+
options.minifyCSS = function(text, type) {
647+
text = text.replace(/(url\s*\(\s*)("|'|)(.*?)\2(\s*\))/ig, function(match, prefix, quote, url, suffix) {
648+
return prefix + quote + options.minifyURLs(url) + quote + suffix;
649+
});
650+
try {
651+
if (type === 'inline') {
652+
text = wrapInlineCSS(text);
653+
}
654+
else if (type === 'media') {
655+
text = wrapMediaQuery(text);
656+
}
657+
text = new CleanCSS(value).minify(text).styles;
658+
if (type === 'inline') {
659+
text = unwrapInlineCSS(text);
660+
}
661+
else if (type === 'media') {
662+
text = unwrapMediaQuery(text);
663+
}
664+
return text;
707665
}
708-
text = new CleanCSS(minifyCSS).minify(text).styles;
709-
if (type === 'inline') {
710-
text = unwrapInlineCSS(text);
666+
catch (err) {
667+
options.log(err);
668+
return text;
711669
}
712-
else if (type === 'media') {
713-
text = unwrapMediaQuery(text);
670+
};
671+
}
672+
else if (key === 'minifyJS' && typeof value !== 'function') {
673+
if (!value) {
674+
return;
675+
}
676+
if (typeof value !== 'object') {
677+
value = {};
678+
}
679+
(value.parse || (value.parse = {})).bare_returns = false;
680+
options.minifyJS = function(text, inline) {
681+
var start = text.match(/^\s*<!--.*/);
682+
var code = start ? text.slice(start[0].length).replace(/\n\s*-->\s*$/, '') : text;
683+
value.parse.bare_returns = inline;
684+
var result = UglifyJS.minify(code, value);
685+
if (result.error) {
686+
options.log(result.error);
687+
return text;
714688
}
715-
return text;
689+
return result.code.replace(/;$/, '');
690+
};
691+
}
692+
else if (key === 'minifyURLs' && typeof value !== 'function') {
693+
if (!value) {
694+
return;
716695
}
717-
catch (err) {
718-
options.log(err);
719-
return text;
696+
if (typeof value === 'string') {
697+
value = { site: value };
720698
}
721-
};
722-
}
699+
else if (typeof value !== 'object') {
700+
value = {};
701+
}
702+
options.minifyURLs = function(text) {
703+
try {
704+
return RelateUrl.relate(text, value);
705+
}
706+
catch (err) {
707+
options.log(err);
708+
return text;
709+
}
710+
};
711+
}
712+
else {
713+
options[key] = value;
714+
}
715+
});
716+
return options;
723717
}
724718

725719
function uniqueId(value) {
@@ -817,9 +811,7 @@ function createSortFns(value, options, uidIgnore, uidAttr) {
817811
}
818812

819813
function minify(value, options, partialMarkup) {
820-
options = options || {};
821-
var optionsStack = [];
822-
processOptions(options);
814+
options = processOptions(options || {});
823815
if (options.collapseWhitespace) {
824816
value = collapseWhitespace(value, options, true, true);
825817
}
@@ -850,22 +842,25 @@ function minify(value, options, partialMarkup) {
850842
uidIgnore = uniqueId(value);
851843
var pattern = new RegExp('^' + uidIgnore + '([0-9]+)$');
852844
if (options.ignoreCustomComments) {
853-
options.ignoreCustomComments.push(pattern);
845+
options.ignoreCustomComments = options.ignoreCustomComments.slice();
854846
}
855847
else {
856-
options.ignoreCustomComments = [pattern];
848+
options.ignoreCustomComments = [];
857849
}
850+
options.ignoreCustomComments.push(pattern);
858851
}
859852
var token = '<!--' + uidIgnore + ignoredMarkupChunks.length + '-->';
860853
ignoredMarkupChunks.push(group1);
861854
return token;
862855
});
863856

864-
function escapeFragments(text) {
865-
return text.replace(uidPattern, function(match, prefix, index) {
866-
var chunks = ignoredCustomMarkupChunks[+index];
867-
return chunks[1] + uidAttr + index + chunks[2];
868-
});
857+
function escapeFragments(fn) {
858+
return function(text, type) {
859+
return fn(text.replace(uidPattern, function(match, prefix, index) {
860+
var chunks = ignoredCustomMarkupChunks[+index];
861+
return chunks[1] + uidAttr + index + chunks[2];
862+
}), type);
863+
};
869864
}
870865

871866
var customFragments = options.ignoreCustomFragments.map(function(re) {
@@ -878,17 +873,11 @@ function minify(value, options, partialMarkup) {
878873
if (!uidAttr) {
879874
uidAttr = uniqueId(value);
880875
uidPattern = new RegExp('(\\s*)' + uidAttr + '([0-9]+)(\\s*)', 'g');
881-
var minifyCSS = options.minifyCSS;
882-
if (minifyCSS) {
883-
options.minifyCSS = function(text, type) {
884-
return minifyCSS(escapeFragments(text), type);
885-
};
876+
if (options.minifyCSS) {
877+
options.minifyCSS = escapeFragments(options.minifyCSS);
886878
}
887-
var minifyJS = options.minifyJS;
888-
if (minifyJS) {
889-
options.minifyJS = function(text, inline) {
890-
return minifyJS(escapeFragments(text), inline);
891-
};
879+
if (options.minifyJS) {
880+
options.minifyJS = escapeFragments(options.minifyJS);
892881
}
893882
}
894883
var token = uidAttr + ignoredCustomMarkupChunks.length;
@@ -962,14 +951,9 @@ function minify(value, options, partialMarkup) {
962951
var lowerTag = tag.toLowerCase();
963952

964953
if (lowerTag === 'svg') {
965-
optionsStack.push(options);
966-
var nextOptions = {};
967-
for (var key in options) {
968-
nextOptions[key] = options[key];
969-
}
970-
nextOptions.keepClosingSlash = true;
971-
nextOptions.caseSensitive = true;
972-
options = nextOptions;
954+
options = Object.create(options);
955+
options.keepClosingSlash = true;
956+
options.caseSensitive = true;
973957
}
974958

975959
tag = options.caseSensitive ? tag : lowerTag;
@@ -1055,7 +1039,7 @@ function minify(value, options, partialMarkup) {
10551039
end: function(tag, attrs, autoGenerated) {
10561040
var lowerTag = tag.toLowerCase();
10571041
if (lowerTag === 'svg') {
1058-
options = optionsStack.pop();
1042+
options = Object.getPrototypeOf(options);
10591043
}
10601044
tag = options.caseSensitive ? tag : lowerTag;
10611045

0 commit comments

Comments
 (0)