From ab4c9d6e3e11ab207f545728c918895d5cb4c5f8 Mon Sep 17 00:00:00 2001 From: Simone Date: Sun, 1 Dec 2024 18:45:11 +0000 Subject: [PATCH 1/3] Minify content within script tag of type="application/ld+json" --- src/htmlminifier.js | 16 ++++++++++++++++ tests/minifier.js | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/htmlminifier.js b/src/htmlminifier.js index d7efa991..6f2a054c 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -387,6 +387,11 @@ function cleanConditionalComment(comment, options) { function processScript(text, options, currentAttrs) { for (var i = 0, len = currentAttrs.length; i < len; i++) { + if (currentAttrs[i].name.toLowerCase() === 'type' && + currentAttrs[i].value === 'application/ld+json') { + return minifyJson(text); + } + if (currentAttrs[i].name.toLowerCase() === 'type' && options.processScripts.indexOf(currentAttrs[i].value) > -1) { return minify(text, options); @@ -395,6 +400,17 @@ function processScript(text, options, currentAttrs) { return text; } +function minifyJson(text) { + try { + // Try parsing and re-stringifying the JSON + return JSON.stringify(JSON.parse(text)); + } + catch (e) { + // If JSON is malformed, return the original text + return text; + } +} + // Tag omission rules from https://html.spec.whatwg.org/multipage/syntax.html#optional-tags // with the following deviations: // - retain if followed by