Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/htmlminifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import maps (type="importmap") are also often used and should be minified. Could we use an array of acceptable types?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great shout! I've implemented that in 25e77e8. It adds support for import maps and makes it much simpler to implement a new format (it will just be a matter of adding it to the array of acceptable types). I've added more types to the list as well, let me know if you think there are any others I've missed!

return minifyJson(text, options);
}

if (currentAttrs[i].name.toLowerCase() === 'type' &&
options.processScripts.indexOf(currentAttrs[i].value) > -1) {
return minify(text, options);
Expand All @@ -395,6 +400,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 <body> if followed by <noscript>
Expand Down
18 changes: 18 additions & 0 deletions tests/minifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -2822,6 +2822,24 @@ QUnit.test('processScripts', function(assert) {
}), output);
});

QUnit.test('processScripts application/ld+json', function(assert) {
var input = '<script type="application/ld+json">{"foo": "bar"}\n\n</script>';
var output = '<script type="application/ld+json">{"foo":"bar"}</script>';
assert.equal(minify(input, {
collapseWhitespace: true,
processScripts: ['application/ld+json']
}), output);
});

QUnit.test('processScripts application/ld+json (invalid/malformed)', function(assert) {
var input = '<script type="application/ld+json">{"foo: "bar"}\n\n</script>';
var output = '<script type="application/ld+json">{"foo: "bar"}</script>';
assert.equal(minify(input, {
collapseWhitespace: true,
processScripts: ['application/ld+json']
}), output);
});

QUnit.test('ignore', function(assert) {
var input, output;

Expand Down