Re: DOM comments inside a <style> element

On Mon, 27 Dec 2010 12:40:18 +0100, Boris Zbarsky <bzbarsky@mit.edu> wrote:

> On 12/27/10 4:33 AM, Daniel Glazman wrote:
>> <style type="text/css">p { color: red; }</style>
>> <style id="mystyles" type="text/css"></style>
>> <script>
>> var c = document.createComment("p { color: green; }");
>> document.getElementById("mystyles).appendChild(c);
>> <script>
>> <p>This paragraph should be green</p>
>>
>> However, the serialization of the test's DOM tree just above after
>> script's execution will create a document instance containing a
>> valid and parsed stylesheet since the HTML parsing
>> rules create an exception if raw text contents of a style element start
>> with the literal string "<!--"...
>
> It's not HTML parsing rules that create an exception.  HTML parsing  
> rules for <style> are to just parse everything inside as CDATA.  It's  
> CSS parsing rules that then go ahead and ignore the "<!--".
>
> In HTML, there is no way to produce a comment node there via the parser.
>
>> I think WebKit is right here. For consistency reasons, comments
>> programmatically added to the children of a <style> element should be
>> considered as the textual concatenation of "<!--", their textual data
>> and "-->".
>
> Consistency with what?
>
> Right now, the behavior is purely based on the DOM: the textContent of  
> the <style> element is passed to the CSS parser.  At least apparently in  
> non-Webkit browsers.

http://software.hixie.ch/utilities/js/live-dom-viewer/saved/752

Opera uses textContent, Firefox uses just child text and CDATA nodes.


"For styling languages that consist of pure text, user agents must  
evaluate style elements by passing the concatenation of the contents of  
all the text nodes that are direct children of the style element (not any  
other nodes such as comments or elements), in tree order, to the style  
system."
http://www.whatwg.org/specs/web-apps/current-work/complete/semantics.html#title-on-style

The spec matches Firefox. The spec is consistent between <style> and  
<script> here. I think the spec is good as it is.


> It sounds like you're proposing some other algorithm for determining the  
> data to pass to the CSS parser.  What algorithm?
>
> -Boris


-- 
Simon Pieters
Opera Software

Received on Monday, 27 December 2010 12:12:02 UTC