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 14703 - Integrate style sheet loading with CSSOM
Summary: Integrate style sheet loading with CSSOM
Status: RESOLVED FIXED
Alias: None
Product: WHATWG
Classification: Unclassified
Component: HTML (show other bugs)
Version: unspecified
Hardware: Other other
: P3 normal
Target Milestone: Unsorted
Assignee: Ian 'Hixie' Hickson
QA Contact: contributor
URL: http://www.whatwg.org/specs/web-apps/...
Whiteboard: CSSOM
Keywords:
: 17165 (view as bug list)
Depends on:
Blocks: 21622 23483 23684
  Show dependency treegraph
 
Reported: 2011-11-05 18:07 UTC by contributor
Modified: 2014-01-13 22:45 UTC (History)
9 users (show)

See Also:


Attachments

Description contributor 2011-11-05 18:07:55 UTC
Specification: http://www.whatwg.org/specs/web-apps/current-work/
Multipage: http://www.whatwg.org/C#the-link-element
Complete: http://www.whatwg.org/c#the-link-element

Comment:
CSSOM defines algorithms named "create a style sheet" and various other
similar ones.  These need to be explicitly invoked somehow so that <link
rel=stylesheet> elements correctly contribute to document.styleSheets.

Posted from: 162.112.38.5
User agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:10.0a1) Gecko/20111029 Firefox/10.0a1
Comment 1 Ian 'Hixie' Hickson 2011-11-11 00:26:51 UTC
This basically means replacing the part of the spec that says "Otherwise, the LinkStyle interface's sheet attribute must return a StyleSheet object with the following properties".

Anne: How do I set the URL, title, media, etc, of the style sheet?
Comment 2 Ian 'Hixie' Hickson 2011-11-11 00:27:00 UTC
http://dev.w3.org/csswg/cssom/#create-a-style-sheet
Comment 3 Ian 'Hixie' Hickson 2011-12-07 23:36:00 UTC
Anyone know who is editing this spec now? I should make sure they're not planning on changing this.
Comment 4 contributor 2012-07-18 07:59:13 UTC
This bug was cloned to create bug 17999 as part of operation convergence.
Comment 5 Ian 'Hixie' Hickson 2012-08-09 16:50:21 UTC
(punting since CSSOM editorship recently changed and they are probably still getting up to speed)
Comment 6 Ian 'Hixie' Hickson 2012-10-19 19:20:52 UTC
Glenn, Shane: Can you help me out here? What do I need to do? See in particular my questions in comment 1.

(Also, is there somewhere to report bugs on CSSOM? The spec seems to have had some mistakes introduced, e.g. some of the method definitions are no longer normative, lacking "must" invokations, etc.)
Comment 7 Glenn Adams 2012-10-22 01:23:23 UTC
(In reply to comment #6)
> Glenn, Shane: Can you help me out here? What do I need to do? See in
> particular my questions in comment 1.
> 
> (Also, is there somewhere to report bugs on CSSOM? The spec seems to have
> had some mistakes introduced, e.g. some of the method definitions are no
> longer normative, lacking "must" invokations, etc.)

I gather you are asking that the prose description of the algorithm specify appropriate parameters that you can pass arguments from the prose in HTML that invokes these algorithms, yes?

There is a CSSOM component under the CSS product in BZ [1].

[1] https://www.w3.org/Bugs/Public/buglist.cgi?query_format=advanced&product=CSS&component=CSSOM&bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&order=Importance&list_id=948
Comment 8 Ian 'Hixie' Hickson 2012-11-13 22:46:44 UTC
(note to self: see also bug 19924)
Comment 9 Ian 'Hixie' Hickson 2012-11-29 00:24:30 UTC
This will also need to handle CORS-cross-origin style sheets somehow.
Comment 10 Ian 'Hixie' Hickson 2012-11-29 00:45:56 UTC
And be more explicit about only having sheets be in .styleSheets while they're in the document.
Comment 11 Boris Zbarsky 2012-11-29 03:56:29 UTC
Ian asked me to update this with what Gecko does for cross-origin sheet loads.

We return the stylesheet from .sheet for such loads.  The URI of the sheet is the URI that was listed in the page originally (so the pre-redirect URI, hence not able to leak any information the page did not have).  The page can also read/set the sheet's media.  Attempts to get .cssRules on the sheet throw a security exception.
Comment 12 Ian 'Hixie' Hickson 2012-12-01 22:31:14 UTC
*** Bug 17165 has been marked as a duplicate of this bug. ***
Comment 13 Ian 'Hixie' Hickson 2012-12-01 22:31:40 UTC
From bz in bug 17165:

In particular, at what points are the stylesheets actually loaded/parsed respectively?  What happens if the DOM inside <style> is mutated?  What happens if the CSSOM is mutated and then attributes that affect the styling behavior (e.g. title, rel, media) are changed?

Also, how changes to rel and media interact with .disabled.
Comment 14 Ian 'Hixie' Hickson 2013-03-25 19:34:28 UTC
zcorpan, this may be relevant to your new interests
Comment 15 Tab Atkins Jr. 2013-03-25 20:36:43 UTC
(In reply to comment #14)
> zcorpan, this may be relevant to your new interests

Also, note the parsing algorithms now present in the Syntax spec.  Feel free to invoke them; this is what I wrote them for.
Comment 16 Simon Pieters 2013-06-26 14:11:19 UTC
(In reply to comment #9)
> This will also need to handle CORS-cross-origin style sheets somehow.

This is possible now. Set or unset the origin-clean flag when creating the CSS style sheet.

(In reply to comment #11)
> Ian asked me to update this with what Gecko does for cross-origin sheet
> loads.
> 
> We return the stylesheet from .sheet for such loads.  The URI of the sheet
> is the URI that was listed in the page originally (so the pre-redirect URI,
> hence not able to leak any information the page did not have).

I made .href return pre-redirect URL for same-origin as well. Should that be changed? (If so please file a new bug on CSSOM.)

> The page can
> also read/set the sheet's media.  Attempts to get .cssRules on the sheet
> throw a security exception.

Specced.

(In reply to comment #13)
> From bz in bug 17165:
> 
> In particular, at what points are the stylesheets actually loaded/parsed
> respectively?

This isn't well specified yet.

> What happens if the DOM inside <style> is mutated?

HTML should say to create a new CSS style sheet when that happens.

> What
> happens if the CSSOM is mutated and then attributes that affect the styling
> behavior (e.g. title, rel, media) are changed?

For title and media, the stylesheet gets updated in place. HTML needs to invoke create a CSS style sheet with media and title being the relevant attributes (rather than the value of the attributes). See https://dvcs.w3.org/hg/csswg/diff/0b21977c2ed3/cssom/Overview.src.html

> Also, how changes to rel and media interact with .disabled.

When rel is changed, HTML should say to create a new CSS style sheet. Per spec currently, .disabled doesn't change based on the alternate flag or media. It's possible that the spec is wrong on that point, though.
Comment 17 Simon Pieters 2013-06-26 14:18:07 UTC
HTML should have an algorithm for <style> and an algorithm for <link> that are similar to http://dev.w3.org/csswg/cssom/#requirements-on-user-agents-implementing-the-xml-stylesheet-processing-instruction

They should run when an element is inserted or removed from the document, when type is set/changed/removed, for <style> when textContent is changed, for <link> when rel/href/crossorigin is set/changed/removed.

The "fetch a CSS style sheet" algoritm takes care of the quirk of applying non-text/css resources, so that can be dropped from HTML.
Comment 18 Boris Zbarsky 2013-06-26 14:38:26 UTC
> I made .href return pre-redirect URL for same-origin as well

Yes, correct.

> For title and media, the stylesheet gets updated in place. HTML needs to invoke
> create a CSS style sheet with media and title being the relevant attributes
> (rather than the value of the attributes).

I don't follow this, sorry.  Especially the combination of "updated in place" and "create a CSS style sheet".  The question is whether CSSOM modifications to a sheet are lost when title/media change on the DOM node; I think they are in some UAs but not others.  Of particular interest is what happens here if the sheet is not even done loading or parsing when these attributes change.

> It's possible that the spec is wrong on that point, though.

I suspect it is, since the load-time value of @media does affect .disabled.
Comment 19 Simon Pieters 2013-06-26 15:11:59 UTC
(In reply to comment #18)
> I don't follow this, sorry.  Especially the combination of "updated in
> place" and "create a CSS style sheet".

Ah, yeah, I meant *when* it creates a CSS style sheet (e.g. because the <link> was inserted to the document)...

> The question is whether CSSOM
> modifications to a sheet are lost when title/media change on the DOM node; I
> think they are in some UAs but not others.

I propose that the modifications should not be lost.

> Of particular interest is what
> happens here if the sheet is not even done loading or parsing when these
> attributes change.

That's a good point. https://www.w3.org/Bugs/Public/show_bug.cgi?id=22478

> I suspect it is, since the load-time value of @media does affect .disabled.

https://www.w3.org/Bugs/Public/show_bug.cgi?id=22479

Thanks!
Comment 20 Simon Sapin 2013-10-29 21:31:01 UTC
I just added the concept of "environment encoding" to CSS Syntax, and
defined it there for <link rel=stylesheet> :

http://dev.w3.org/csswg/css-syntax/#environment-encoding
http://dev.w3.org/csswg/css-syntax/#environment-encoding-html

The latter part should be moved to the HTML spec.

Note: I included the charset attribute even though it’s marked obsolete in HTML, as this is what implementations do. I’d be in favor of dropping it if someone want to convince implementers, but in the meantime the spec should reflect reality.
Comment 21 Simon Pieters 2013-10-30 14:10:49 UTC
(In reply to Simon Sapin from comment #20)
> Note: I included the charset attribute even though it’s marked obsolete in
> HTML, as this is what implementations do. I’d be in favor of dropping it if
> someone want to convince implementers, but in the meantime the spec should
> reflect reality.

It looks like the charset attribute is used in the wild.

In http://webdevdata.org june data set, I see 322 instances of <link charset> that's different from the document's charset (out of 2359 <link charset>s in total, in ~53,000 pages).

$ find ./ -name "*.html.txt" -print0 | xargs -0 -n1 -P8 grep -HnEio "<link\s([^>]*\s)?charset\s*=[^>]+>" >> ~/Desktop/link-charset.txt
$ cd ~/Desktop
$ python find-interesting-charsets.py > link-charset-different-from-content-type.txt

See http://lists.w3.org/Archives/Public/www-archive/2013Oct/0049.html for the script and the output.

annevk said in #whatwg:
I don't see the point in dropping <link charset> / <?xml-stylesheet charset?> as long as you still inherit from the document
both are in control of a potential attacker
so given that, the compatibility risk does not seem worth it
Comment 22 Simon Sapin 2013-10-30 14:13:59 UTC
Does this mean the charset attribute should be "unobsoleted" from the spec? I’m fine with keeping the current author requirements (must not use) but implementation requirements should be specified.
Comment 23 Ian 'Hixie' Hickson 2013-10-30 17:51:16 UTC
Yup, sounds like it. Can you file a separate bug for adding charset="" support to the spec? Then I'll use this bug to track connecting the HTML spec to the various algorithms in the CSS spec(s).
Comment 24 Ian 'Hixie' Hickson 2014-01-10 00:20:29 UTC
Is there something I should call when the <link> or <style> no longer applies?

Should I really reload the stylesheet if rel="stylesheet" changes to rel="alternate stylesheet"? Surely we just change the alternate flag on the fly somehow (like passing the "create..." algorithm some sort of lambda that can tell you if it's alternative or not). Especially since we're tracking the title="" in real time too, which can do the same thing, basically.
Comment 25 Boris Zbarsky 2014-01-10 00:42:44 UTC
What do UAs do?

Gecko will in fact reload stylesheets if you mess with some of those attributes, for what it's worth.  But that's just because it's a lot easier than trying to update various complicated state in-place...
Comment 26 Simon Pieters 2014-01-13 17:49:26 UTC
(In reply to Ian 'Hixie' Hickson from comment #24)
> Is there something I should call when the <link> or <style> no longer
> applies?

http://dev.w3.org/csswg/cssom/#remove-a-css-style-sheet

http://dev.w3.org/csswg/cssom/#requirements-on-user-agents-implementing-the-xml-stylesheet-processing-instruction step 2 does this.

> Should I really reload the stylesheet if rel="stylesheet" changes to
> rel="alternate stylesheet"?

Yeah, I think so. It's simpler to implement and I'm not aware of use cases for it. Possibly blunt stylesheet switchers that don't use the API for switching stylesheets, but that doesn't seem like something we should optimize for (unless a lot of pages do that, but that seems unlikely).

> Surely we just change the alternate flag on the
> fly somehow (like passing the "create..." algorithm some sort of lambda that
> can tell you if it's alternative or not). Especially since we're tracking
> the title="" in real time too, which can do the same thing, basically.

In Blink, as far as I can tell, rel reloads but title doesn't. Not sure about Gecko.

http://software.hixie.ch/utilities/js/live-dom-viewer/saved/2737
http://software.hixie.ch/utilities/js/live-dom-viewer/saved/2738
(check Network inspector)
Comment 27 Boris Zbarsky 2014-01-13 18:09:42 UTC
The relevant Gecko code is as follows:

1)  If the attribute being changed is not href, rel, title, media, type, do nothing.

2)  If the attribute is being unset, or if it's being set and is one of title/media/type, or if it's being set and its name is "rel" and the new value doesn't include "stylesheet", do a forced update.

3)  Otherwise, do a regular update.

The difference between a "forced" update and a "regular" update is that for a regular update if there is already a stylesheet (which is created when the node is inserted in the DOM, note) and its URI matches the new URI we do nothing.  So basically, changing from rel="stylesheet" to rel="alternate stylesheet" or vice versa is more or less a no-op, as is changing the "href" attribute in ways that don't change the final resolved URI (e.g. from relative to absolute).

In either case, if the update happens (so is forced, or the regular update found a URI difference), we go ahead and remove the old sheet and load a new one.  Whether that ends up hitting the network depends on the object cache in the per-document style loader object, so while you get a new DOM CSSStyleSheet object there might not actually be any network access here.
Comment 28 contributor 2014-01-13 22:45:55 UTC
Checked in as WHATWG revision r8390.
Check-in comment: Update how the interaction with CSS style sheets and CSSOM is defined.
http://html5.org/tools/web-apps-tracker?from=8389&to=8390