Skip to content

Commit 8b5c3f7

Browse files
Fix crash for type attribute without value (#133)
Co-authored-by: Sibiraj <20282546+sibiraj-s@users.noreply.github.com>
1 parent 0a6ad22 commit 8b5c3f7

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/htmlminifier.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,12 @@ const keepScriptsMimetypes = new Set([
166166
'module'
167167
]);
168168

169-
function isScriptTypeAttribute(attrValue) {
169+
function isScriptTypeAttribute(attrValue = '') {
170170
attrValue = trimWhitespace(attrValue.split(/;/, 2)[0]).toLowerCase();
171171
return attrValue === '' || executableScriptsMimetypes.has(attrValue);
172172
}
173173

174-
function keepScriptTypeAttribute(attrValue) {
174+
function keepScriptTypeAttribute(attrValue = '') {
175175
attrValue = trimWhitespace(attrValue.split(/;/, 2)[0]).toLowerCase();
176176
return keepScriptsMimetypes.has(attrValue);
177177
}
@@ -189,7 +189,7 @@ function isExecutableScript(tag, attrs) {
189189
return true;
190190
}
191191

192-
function isStyleLinkTypeAttribute(attrValue) {
192+
function isStyleLinkTypeAttribute(attrValue = '') {
193193
attrValue = trimWhitespace(attrValue).toLowerCase();
194194
return attrValue === '' || attrValue === 'text/css';
195195
}

tests/minifier.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,12 @@ test('removing javascript type attributes', async () => {
11071107
output = '<script>alert(1)</script>';
11081108
expect(await minify(input, { removeScriptTypeAttributes: true })).toBe(output);
11091109

1110+
// https://github.com/terser/html-minifier-terser/issues/132
1111+
input = '<script type>alert(1)</script>';
1112+
expect(await minify(input, { removeScriptTypeAttributes: false })).toBe(input);
1113+
output = '<script>alert(1)</script>';
1114+
expect(await minify(input, { removeScriptTypeAttributes: true })).toBe(output);
1115+
11101116
input = '<script type="modules">alert(1)</script>';
11111117
expect(await minify(input, { removeScriptTypeAttributes: false })).toBe(input);
11121118
output = '<script type="modules">alert(1)</script>';
@@ -1138,6 +1144,12 @@ test('removing type="text/css" attributes', async () => {
11381144
output = '<style>.foo { color: red }</style>';
11391145
expect(await minify(input, { removeStyleLinkTypeAttributes: true })).toBe(output);
11401146

1147+
// https://github.com/terser/html-minifier-terser/issues/132
1148+
input = '<style type>.foo { color: red }</style>';
1149+
expect(await minify(input, { removeStyleLinkTypeAttributes: false })).toBe(input);
1150+
output = '<style>.foo { color: red }</style>';
1151+
expect(await minify(input, { removeStyleLinkTypeAttributes: true })).toBe(output);
1152+
11411153
input = '<style type="text/css">.foo { color: red }</style>';
11421154
expect(await minify(input, { removeStyleLinkTypeAttributes: false })).toBe(input);
11431155
output = '<style>.foo { color: red }</style>';
@@ -1154,6 +1166,11 @@ test('removing type="text/css" attributes', async () => {
11541166
output = '<link rel="stylesheet" href="http://example.com">';
11551167
expect(await minify(input, { removeStyleLinkTypeAttributes: true })).toBe(output);
11561168

1169+
// https://github.com/terser/html-minifier-terser/issues/132
1170+
input = '<link rel="stylesheet" type href="http://example.com">';
1171+
output = '<link rel="stylesheet" href="http://example.com">';
1172+
expect(await minify(input, { removeStyleLinkTypeAttributes: true })).toBe(output);
1173+
11571174
input = '<link rel="alternate" type="application/atom+xml" href="data.xml">';
11581175
expect(await minify(input, { removeStyleLinkTypeAttributes: true })).toBe(input);
11591176
});

0 commit comments

Comments
 (0)