diff --git a/src/htmlminifier.js b/src/htmlminifier.js index d7efa991..8cdcd1ab 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -385,8 +385,22 @@ function cleanConditionalComment(comment, options) { }) : comment; } +var JSON_SCRIPT_TYPES = [ + 'application/json', + 'application/ld+json', + 'application/manifest+json', + 'application/vnd.geo+json', + 'importmap', + 'speculationrules', +]; + function processScript(text, options, currentAttrs) { for (var i = 0, len = currentAttrs.length; i < len; i++) { + if (currentAttrs[i].name.toLowerCase() === 'type' && + JSON_SCRIPT_TYPES.indexOf(currentAttrs[i].value) > -1) { + return minifyJson(text, options); + } + if (currentAttrs[i].name.toLowerCase() === 'type' && options.processScripts.indexOf(currentAttrs[i].value) > -1) { return minify(text, options); @@ -395,6 +409,16 @@ function processScript(text, options, currentAttrs) { return text; } +function minifyJson(text, options) { + try { + return JSON.stringify(JSON.parse(text)); + } + catch (err) { + options.log(err); + return text; + } +} + // Tag omission rules from https://html.spec.whatwg.org/multipage/syntax.html#optional-tags // with the following deviations: // - retain if followed by