From 07237e833b055a0f379a8fabdb0012feb6178ce5 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Fri, 27 Mar 2026 14:45:54 -0700 Subject: [PATCH] Add script content preservation during formatting --- src/lib/html-formatter.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/lib/html-formatter.js b/src/lib/html-formatter.js index 9254e02..7144d11 100644 --- a/src/lib/html-formatter.js +++ b/src/lib/html-formatter.js @@ -54,7 +54,25 @@ const minify = function(el) { const convert = { comment: [], - line: [] + line: [], + script: [] +}; + +const preserveScripts = function (el) { + convert.script = []; + el = el.replace(/(]*>)([\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; };