Skip to content

Commit

Permalink
Use <!doctypehtml> (without the space) where possible
Browse files Browse the repository at this point in the history
When both the `useShortDoctype` and the `removeTagWhitespace` options are enabled, we can remove the space from the doctype altogether.

It’s invalid HTML (which is already expected in the output with the `removeTagWhitespace` option enabled), but it still triggers standards mode.

https://html.spec.whatwg.org/multipage/parsing.html#parse-error-missing-whitespace-before-doctype-name
https://twitter.com/mathias/status/1048098677703753729

Closes #969.
  • Loading branch information
mathiasbynens committed Oct 5, 2018
1 parent df720b3 commit 7e8406e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/htmlminifier.js
Expand Up @@ -1235,7 +1235,9 @@ function minify(value, options, partialMarkup) {
buffer.push(text);
},
doctype: function(doctype) {
buffer.push(options.useShortDoctype ? '<!doctype html>' : collapseWhitespaceAll(doctype));
buffer.push(options.useShortDoctype ? '<!doctype' +
(options.removeTagWhitespace ? '' : ' ') + 'html>' :
collapseWhitespaceAll(doctype));
},
customAttrAssign: options.customAttrAssign,
customAttrSurround: options.customAttrSurround
Expand Down
5 changes: 5 additions & 0 deletions tests/minifier.js
Expand Up @@ -410,6 +410,11 @@ QUnit.test('doctype normalization', function(assert) {
assert.equal(minify(input, { useShortDoctype: false }), input);
assert.equal(minify(input, { useShortDoctype: true }), output);

assert.equal(minify(input, {
useShortDoctype: true,
removeTagWhitespace: true
}), '<!doctypehtml>');

input = '<!DOCTYPE\nhtml>';
assert.equal(minify(input, { useShortDoctype: false }), '<!DOCTYPE html>');
assert.equal(minify(input, { useShortDoctype: true }), output);
Expand Down

0 comments on commit 7e8406e

Please sign in to comment.