Add script content preservation during formatting

This commit is contained in:
Chris Wanstrath 2026-03-27 14:45:54 -07:00
parent 780937a368
commit 07237e833b

View File

@ -54,7 +54,25 @@ const minify = function(el) {
const convert = {
comment: [],
line: []
line: [],
script: []
};
const preserveScripts = function (el) {
convert.script = [];
el = el.replace(/(<script[^>]*>)([\s\S]*?)(<\/script>)/gi, function (match, open, content, close) {
if (!content.trim()) return match;
convert.script.push(content);
return open + '[#s# : ' + (convert.script.length - 1) + ' : #s#]' + close;
});
return el;
};
const restoreScripts = function (el) {
convert.script.forEach(function (content, index) {
el = el.replace('[#s# : ' + index + ' : #s#]', content);
});
return el;
};
const comment = function (el) {
@ -125,11 +143,13 @@ const tidy = function (el) {
const render = function (el, opt) {
el = closing(el);
el = preserveScripts(el);
el = comment(el);
el = entity(el);
el = minify(el);
el = line(el);
el = tidy(el);
el = restoreScripts(el);
return el;
};