Question

Since I learned to serve XHTML pages as XML, I have started noticing something odd: whenever I view an XHTML page in the Firefox source code viewer, the DOCTYPE is always marked as an error. According to the tooltip I get from mousing over it, the error in question is a "stray doctype". From what I understand, a "stray doctype" means that there is an extra DOCTYPE in the middle of the document where it doesn't belong, which is certainly not the case here.

Here's an example - this markup will pass validation, and display correctly in all modern browsers:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--FF source viewer will mark the preceding two lines as an error.-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
    <meta http-equiv="content-type"
      content="application/xhtml+xml; charset=utf-8" />
    <title>Sample XHTML Page</title>
  </head>
  <body>
    <p>This is an example.</p>
  </body>
</html>

This error message is especially odd, considering that these pages pass validation perfectly, and that a single parsing error would normally break the page.

Was it helpful?

Solution

I am the developer of this feature. You have found a bug. (Filed just now.) Thanks.

View Source syntax highlighting is based on the HTML parser, because our XML parser is not suited for the purpose and XML is rare enough that it doesn't make sense to put resources into implementing proper XML View Source. Hence, the XML View Source feature is a hack on the HTML parser and this aspect doesn't work quite right.

OTHER TIPS

The error appears because the file is saved as UTF-8 BOM instead of UTF-8. Open the file in Notepad and change its encoding.

In addition to @Public Sphere's answer.

This warning can also occur when using <!DOCTYPE html>. Probably the same warning is then also shown for the <html>, <head> and <body> tags (stray start tag "html").

To check if UTF-8 BOM is the problem:

  • Click the 'network' tab
  • Click the first request
  • On right details panel, click 'Response' tab and expand 'Response Payload'
  • You'll see the raw response now. A red dot is in front of the doctype line, and on hover it displays "\ufeff"

To easily find the files that could cause the problem, you can, in Linux, use this grep to find all files with BOM:

grep -rl $'\xEF\xBB\xBF' .
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top