This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 19924 - Additional rule for HTML parser
Summary: Additional rule for HTML parser
Status: RESOLVED WORKSFORME
Alias: None
Product: WHATWG
Classification: Unclassified
Component: HTML (show other bugs)
Version: unspecified
Hardware: PC Windows NT
: P2 normal
Target Milestone: Unsorted
Assignee: Ian 'Hixie' Hickson
QA Contact: contributor
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-11-09 18:33 UTC by manish.tripathi.777
Modified: 2012-11-13 22:46 UTC (History)
2 users (show)

See Also:


Attachments

Description manish.tripathi.777 2012-11-09 18:33:31 UTC
Suggestion for the "text insertion mode" of HTML parser:  add 'An end tag whose tag name is "style"' rule similar to the "script" one, to notify "style" elements that their contents is ready to be processed. Otherwise, there is no way for the HTMLStyleElement to find out when to start parsing embedded stylesheet rules.
Comment 1 Ian 'Hixie' Hickson 2012-11-11 18:56:21 UTC
The style element _always_ applies its contents. So there's no need to tell it to start doing so.
Comment 2 manish.tripathi.777 2012-11-11 20:09:18 UTC
Okay. Just wanted to point out that it is not obvious from the specs __when_exactly__ style element should apply its contents. The parser algorithm is built in such a way, that the style element doesn't know it's contents has changed. Specifically, after the style tag is opened, the parser inserts an empty text node into the style element (and obviously content cannot be applied at this point, because it is empty), and then only starts appending characters to the text node. The style element has no way of knowing that the parser has __finished__ inserting characters so that the contents can be applied.
Comment 3 Ian 'Hixie' Hickson 2012-11-12 20:17:55 UTC
It doesn't need to know when it's finished. It should start applying the style rules when it's empty ("" is a valid CSS style sheet), and then each time a character is added, it should reparse the style sheet and apply it. This is covered by this statement:

# For styling languages that consist of pure text (as opposed to XML), user
# agents must evaluate style elements by passing the concatenation of the
# contents of all the Text nodes that are children of the style element
# (not any other nodes such as comments or elements), in tree order, to the
# style system.

(This text is likely to be clarified as part of bug 14703.)
Comment 4 manish.tripathi.777 2012-11-12 20:42:17 UTC
hmm... suppose the contents of the style element is made up of 10 000 characters representing a CSS stylesheet. The parser appends characters one by one. So the style sheet has to be parsed 10 000 times? Doesn't it look a little inefficient? 

It is understandable that _after_ the document has been parsed, any modification to the contents of the style element must be applied (i.e. the stylesheet has to be re-parsed). But why do we need to apply every inserted character during the initial parsing? Would it not be better to populate the contents of the style element, and apply it _after_ the style tag is closed? 

I want to emphasize, that the above said applies _only_ to the initial HTML parsing algorithm. When/if the contents of the style element is altered by a script, it must be applied and re-parsed immediately, by all means and no doubt.
Comment 5 Ian 'Hixie' Hickson 2012-11-13 00:34:31 UTC
So long as the result is indistinguishable, the browser can implement this as efficiently as it wants. In practice, the browser will probably wait until the next time it renders the document before even looking at the <style> element.

There's no difference between when the document is parsed and when it's dynamically modified, as far as this goes. At least, that's the theory.
Comment 6 manish.tripathi.777 2012-11-13 05:33:23 UTC
I see your point. So in practice, we will just set "dirty" flag on the style element whenever its contents is changed, and re-process the stylesheet whenever the browser needs to access any style-dependent data.

I guess that was your initial intention in the phrase

# For styling languages that consist of pure text (as opposed to XML), user
# agents must evaluate style elements by passing the concatenation of the
# contents of all the Text nodes that are children of the style element
# (not any other nodes such as comments or elements), in tree order, to the
# style system.

I just failed to understand it correctly.
Comment 7 Ian 'Hixie' Hickson 2012-11-13 22:46:12 UTC
Yeah the text is poorly written. I hope to improve it as part of bug 14703.