Stripping leading line feeds

The HTML5 parser ignores a leading line feed character in <pre> and 
<textarea>. Current browsers do things differently in some cases.

http://software.hixie.ch/utilities/js/live-dom-viewer/?%3C%21DOCTYPE%20html%3E%0A%3Cstyle%3Ebody%20*%20%7B%20border%3A%201px%20solid%20%7D%3C/style%3E%0A%3Cbody%20onload%3D%22for%28var%20i%3D0%3Bi%3Cdocument.body.childNodes.length%3B++i%29w%28%27%3E%3E%3E%27+document.body.childNodes%5Bi%5D.firstChild.nodeValue+%27%3C%3C%3C%27%29%7D%22%3E%3Cdiv%3E%0A%0Adiv%0A%0A%3C/div%3E%3Cspan%20style%3D%22display%3A%20block%22%3E%0A%0Aspan%0A%0A%3C/span%3E%3Cp%20style%3D%22display%3A%20block%22%3E%0A%0Ap%0A%0A%3C/p%3E%3Cdiv%20style%3D%22white-space%3A%20pre%22%3E%0A%0Adiv%20white-space%3Apre%0A%0A%3C/div%3E%3Cspan%20style%3D%22white-space%3A%20pre%3B%20display%3A%20block%22%3E%0A%0Aspan%20white-space%3Apre%0A%0A%3C/span%3E%3Cp%20style%3D%22white-space%3A%20pre%3B%20display%3A%20block%22%3E%0A%0Ap%20white-space%3Apre%0A%0A%3C/p%3E%3Cpre%3E%0A%0Apre%0A%0A%3C/pre%3E%3Ctextarea%20rows%3D5%3E%0A%0Atextarea%0A%0A%3C/textarea%3E%3Cxmp%3E%0A%0Axmp%0A%0A%3C/xmp%3E%3Clisting%3E%0A%0Alisting%0A%0A%3C/
listing%3E%3Cplaintext%3E%0A%0Aplaintext%0A%0A

Elements where the leading line feed is removed:

IE 7: <pre>, <textarea>, <xmp>, <listing>, <plaintext> (and other 
elements like <div> are totally weird, particular when comparing the 
four different views and when adding CSS, and also it differs in quirks 
vs standards mode, and also it sometimes differs when loading a real 
page vs loading with document.write)

Firefox 2: <pre>, <textarea>, <listing>

Opera 9.2: <pre>, <listing> (and <textarea> seems to be handled outside 
the parser)

Safari 3: <pre>, <listing> (and <textarea> seems to be handled outside 
the parser)

HTML5: <pre>, <textarea>

(I didn't look at any elements other than those in the above example.)


In particular, <listing> is consistent between all browsers but missing 
from HTML5.

IE's behaviour seems slightly useful for <xmp> and <plaintext>, since it 
lets you write

   <xmp>
   <!DOCTYPE HTML>
   <title>An example HTML5 document</title>
   <p>...
   </xmp>

and not get an unexpected blank line at the top.

-- 
Philip Taylor
philip@zaynar.demon.co.uk

Received on Saturday, 18 August 2007 16:37:05 UTC