Use <!doctypehtml> (without the space) where possible
authorMathias Bynens <mathias@qiwi.be>
Fri, 5 Oct 2018 14:09:27 +0000 (16:09 +0200)
committerMathias Bynens <mathias@qiwi.be>
Fri, 5 Oct 2018 14:09:27 +0000 (16:09 +0200)
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.

src/htmlminifier.js
tests/minifier.js

index 9fe9e07..dccf4ca 100644 (file)
@@ -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
index 30502d2..08dc657 100644 (file)
@@ -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);