Can Comments Appear Before the Doctype Declaration

Can comments appear before the DOCTYPE declaration?

Writing the <!DOCTYPE> first is certainly best practice.

I remember strange problems a long, long time ago where some browser (probably IE6) ignored a <!DOCTYPE> because there was something seemingly innocent before it - I think just whitespace, but maybe it was a comment. In any case, it was a horrible, horrible bug to have to track down, and there's certainly never any good reason to have comments or whitespace before the <!DOCTYPE>.

Writing the <!DOCTYPE> first is, I'd say, just something experienced web developers do to avoid horrible, elusive bugs.

Is it bad practice to put a comment before !DOCTYPE HTML ?

Putting a comment before the Doctype will trigger quirks mode in some browsers. Since the entire purpose of the Doctype (as of HTML 5) is to avoid quirks mode, yes, this is bad practise.

Screenshot of validator

xslt output doctype before comments

Saxon is right, the specifications are clear: when serializing the result document the doctype declaration must be put immediately before the first element. This is specified in both XSLT 1.0 and XSLT 2.0:

If the doctype-system attribute is specified, the xml output method should output a document type declaration immediately before the first element.

To solve this you can add manually the doctype declaration in your document, using the xsl:text element with the disable-output-escaping attribute set to yes. For example you can output a standard HTML5 doctype, without any public identifier, which would be otherwise impossible to do in XSLT.

<xsl:text disable-output-escaping="yes"><!DOCTYPE html></xsl:text>

HTML 5 - Do I need to declare DOCTYPE on includes and pages with decorator

The DOCTYPE declaration should be the first line of the HTML document rendered by the result. You can also place space characters, comments, or BOM character before the DOCTYPE. See Conformant documents in the HTML syntax. Pages are decorated mostly in the head or body of the document but a DOCTYPE should be before these tags and before any tag. You place DOCTYPE on every page at the first line, either included or manual, and then use this page with decorator. The DOCTYPE declaration should be rendered only once per HTML document, you shouldn't include it with every page that is <s:include/>-ed in the main page.

Does the DOCTYPE declaration have to be the first tag in an HTML document?

Yes, DOCTYPE must be the first data on the page: http://www.w3schools.com/tags/tag_DOCTYPE.asp

jQuery interact with text outputted before doctype declation

You can access the elements using $('body').contents() because the browser interprets them as elements of the body. When the browser sees text before the doctype declaration, it shifts that text and the contents of the head into the body as it's way of trying to build a viable DOM even though the html was not valid.

Since the browser has reorganized the content, you cannot guess what the first element of the head was supposed to be. This script lets you set the element that was supposed to be the first element of the head. The script will then access the elements before the one you set and provides you with information regarding whether the element is a text node or DOM element.

You have to interact with the text nodes using vanilla js but you can use jQuery for the others.

// the script needs to know what should have been the first element in the head
const firstElementInHead = $( 'title' );
// remove all nodes prior to the intended content
$('body').contents().each( function() {
if ( $(this).index() < firstElementInHead.index() ) {
$(this).remove();
}
});

<br /><b>Notice</b>: Undefined property: bla bla blap on line <b>16</b><br /><!doctype html>
<html lang="en">
<head> <title>The Broken Page</title> <meta charset="utf-8">

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> // the script needs to know what should have been the first element in the head const firstElementInHead = $('title'); // log the nodes that apear prior to the intended content $('body').contents().each(function() { if ($(this).index() < firstElementInHead.index()) { if (this.nodeType == Node.TEXT_NODE) { console.log('This is a text node. You can change it by setting this.nodeValue, remove it by calling this.remove(), or other vanilla JS operations.'); console.log(this); } else { console.log('This is a DOM element. You can interact with it using jQuery the same way you interact with any normal DOM element.'); console.log(this); } } }); </script></head>
<body style="padding:0; margin:0;"> <p style="padding:0; margin:0; background:red;">Test</p></body>
</html>

Where does doctype declaration go?

That doctype declaration is invalid and will cause at least some browsers to go into weird compatibility modes.

W3c (at w3.org), on a page called html5/syntax.html, says "a DOCTYPE is a required preamble" which I interpret to mean it is required and that it must come first.

It also says it must consist of the following components in this order:

  1. A string that is an ASCII case-insensitive match for the string <!DOCTYPE.
  2. One or more space characters.
  3. A string that is an ASCII case-insensitive match for the string html.
  4. Optionally, a DOCTYPE legacy string or an obsolete permitted DOCTYPE string.
  5. Zero or more space characters.
  6. A > (U+003E) character.

Comments in DTD definitions

In SGML, comments starting with and ending in -- can appear anywhere, or multiple times in a markup declaration; in XML, a markup declaration must either contain just a single comment, or another markup declaration:

<!-- valid in XML -->
<!-- only -- -- valid -- -- in -- -- SGML -->

As a consequence of XML being defined as an SGML subset, the text string -- isn't allowed to appear in XML comments anywhere.



Related Topics



Leave a reply



Submit