You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
What is the URL of a CSSStyleSheet created via the constructor? #10
The CSSOM spec says location is specified when created, but naively I thought it can be deferred until it is added to somewhere (e.g. .moreStyleSheets). But in that case at least resource fetching for @import and url() would not work until the location becomes clear, which is not ideal.
Is there any situation that copying document base URL becomes problematic?
Given the discussion in #23 I think it would make sense to treat using the document base URL as a security feature. That might also have some impact on #26 as well, but that's getting beyond my pay grade.
A naive solution would be to use the document URL that the current running inline script belongs to (I don't think anyone except Blink cares about HTML imports, but in that case document.currentScript.ownerDocument.baseURI), for classic external scripts its base URL, and for module scripts, import.meta.url.
At this moment a CSSStyleSheet is constructed from string, I think the above is a reasonable default.
Of which HTMLElement? The one the event handler is on? Is that also what happens if the event handler was assigned via .onclick to some function?
The simplest thing, of course, is to use the base url of the document of the constructor's Realm global (which is a Window, presumably). Still have to decide whether it gets copied or aliased, of course.
Let's decide on using document base url of the constructor's Realm global.
Is that aliased, or snapshotted at the point when the constructor is called?
As far as I imagine I'd go with snapshot when the constructor is called, but my imagination can be very small-scoped. What's the case when aliasing is useful...? In other words, does dynamic change of a document's base url happen so often, or even if it is rare, can following the change by aliasing be useful?
I'm not sure there's an obvious case, but what do inline stylesheets end up doing? We should probably at least consider matching them here.
Hmm, interesting, so if pushState() changes the document's URL, and delayed loading of @import or url() may have non deterministic behavior when UA aliases the URL. Let me dig into what the spec says and what browser behaves.
the base URL is that of the style sheet itself, not that of the styled source document.
Style sheets embedded within a document have the base URL associated with their container.
What “browser eccentricities”?
Theoretically, browsers should re-resolve any relative URLs, including fragment-only URLs, whenever the document’s base URL changes (such as through mutation of the base element, or calling pushState()). In many cases they don’t, however, and so without special handling, fragment-only URLs will suddenly become cross-document references (pointing at the previous base URL) and break in many of the places they’re used.
So... in many cases browser don't recalculate url() whenever base URL changes :(
Related to #2, maybe we should hang the factory method under Document so that the URL association to the document is clear, but still needs to clarify the case for standalone constructor. Let me work on spec update.
I think the constructor is only useful if there’s an instance method to parse style sheets asynchronously. That’s only useful if the spec for custom element styles proposal allows modification of the style sheet after the element has been defined which so far they seem opposed to despite my push back. Ultimately there might not be any real use case for the constructor.
Maybe my last comment was not clear, but rather than
StyleSheet.createFromString(text)
, hanging the API on document
document.createCSSStyleSheet(text)
can make it clear about the association of the constructed stylesheet with the document (much like <style> or <link rel=stylesheet>), then how url() is resolved becomes more obvious.
The blob: URI could be used as a key in a hash map (when used by @import), to get to the actual CSSStyleSheet object synchronously, bypassing the streaming protocol.
There are obviously more efficient ways of passing the dependency (through a JS API), I'm just trying to find a way to reuse pre-existing concepts.
@import feels like a natural way to manage dependencies, and blob: URIs provide unique ids to local data.
The blob: URI could be used as a key in a hash map (when used by @import)
Browsers already do this, for all URIs, if the URI has been parsed into a sheet.
and blob: URIs provide unique ids to local data.
Thinking of blob: as "local data" can be misleading: it can be stored on disk, and retrieving it might then be slower than getting something off the network, depending on the disk and the network.
Activity
TakayoshiKochi commentedon Apr 6, 2018
The CSSOM spec says location is specified when created, but naively I thought it can be deferred until it is added to somewhere (e.g.
.moreStyleSheets). But in that case at least resource fetching for@importandurl()would not work until the location becomes clear, which is not ideal.Is there any situation that copying document base URL becomes problematic?
calebdwilliams commentedon Apr 24, 2018
Given the discussion in #23 I think it would make sense to treat using the document base URL as a security feature. That might also have some impact on #26 as well, but that's getting beyond my pay grade.
TakayoshiKochi commentedon Apr 25, 2018
A naive solution would be to use the document URL that the current running inline script belongs to (I don't think anyone except Blink cares about HTML imports, but in that case
document.currentScript.ownerDocument.baseURI), for classic external scripts its base URL, and for module scripts,import.meta.url.At this moment a CSSStyleSheet is constructed from string, I think the above is a reasonable default.
bzbarsky commentedon Apr 25, 2018
Why would there necessarily be a running inline script?
calebdwilliams commentedon Apr 25, 2018
@bzbarsky I would assume in that case it would be
HTMLElement.ownerDocument.baseURIbased on the comments above.bzbarsky commentedon Apr 25, 2018
Of which HTMLElement? The one the event handler is on? Is that also what happens if the event handler was assigned via
.onclickto some function?The simplest thing, of course, is to use the base url of the document of the constructor's Realm global (which is a Window, presumably). Still have to decide whether it gets copied or aliased, of course.
TakayoshiKochi commentedon May 22, 2018
@bzbarsky I might have misused the term, I meant
<script>withoutsrc=attribute.Let's decide on using document base url of the constructor's Realm global. Let me scratch
import.meta.urlI mentioned above.Any objections / opinions?
bzbarsky commentedon May 22, 2018
The term is correct for that, but in my example above there is no
<script>involved at all.Is that aliased, or snapshotted at the point when the constructor is called?
TakayoshiKochi commentedon May 23, 2018
As far as I imagine I'd go with snapshot when the constructor is called, but my imagination can be very small-scoped. What's the case when aliasing is useful...? In other words, does dynamic change of a document's base url happen so often, or even if it is rare, can following the change by aliasing be useful?
bzbarsky commentedon May 23, 2018
I'm not sure there's an obvious case, but what do inline stylesheets end up doing? We should probably at least consider matching them here.
Often enough, with pushState and single-page apps....
TakayoshiKochi commentedon May 25, 2018
Hmm, interesting, so if pushState() changes the document's URL, and delayed loading of
@importorurl()may have non deterministic behavior when UA aliases the URL. Let me dig into what the spec says and what browser behaves.TakayoshiKochi commentedon May 25, 2018
I think this is the latest reference
https://drafts.csswg.org/css-values-4/#relative-urls
TakayoshiKochi commentedon Jul 3, 2018
Found a note below the part that I quoted above:
https://drafts.csswg.org/css-values-4/#local-urls
So... in many cases browser don't recalculate
url()whenever base URL changes :(Related to #2, maybe we should hang the factory method under
Documentso that the URL association to the document is clear, but still needs to clarify the case for standalone constructor. Let me work on spec update.calebdwilliams commentedon Jul 3, 2018
I think the constructor is only useful if there’s an instance method to parse style sheets asynchronously. That’s only useful if the spec for custom element styles proposal allows modification of the style sheet after the element has been defined which so far they seem opposed to despite my push back. Ultimately there might not be any real use case for the constructor.
TakayoshiKochi commentedon Jul 4, 2018
Maybe my last comment was not clear, but rather than
, hanging the API on document
can make it clear about the association of the constructed stylesheet with the document (much like
<style>or<link rel=stylesheet>), then howurl()is resolved becomes more obvious.rakina commentedon Sep 12, 2018
#35 resolves this - We use the
Document's base URL.pygy commentedon Feb 2, 2020
Couldn't a
blob:URI be a nice fit WRT both synchronous@importusage (assuming @import would be restricted to blobs in constructed sheets) and CSP?bzbarsky commentedon Feb 2, 2020
How, exactly?
blob:URL loads are most definitely not synchronous.pygy commentedon Feb 2, 2020
The
blob:URI could be used as a key in a hash map (when used by@import), to get to the actualCSSStyleSheetobject synchronously, bypassing the streaming protocol.There are obviously more efficient ways of passing the dependency (through a JS API), I'm just trying to find a way to reuse pre-existing concepts.
@importfeels like a natural way to manage dependencies, andblob:URIs provide unique ids to local data.bzbarsky commentedon Feb 2, 2020
Browsers already do this, for all URIs, if the URI has been parsed into a sheet.
Thinking of
blob:as "local data" can be misleading: it can be stored on disk, and retrieving it might then be slower than getting something off the network, depending on the disk and the network.pygy commentedon Feb 2, 2020
This is true of blobs in general, but in this case we're not fetching the blob, but the CSSStyleSheet directly.
If it has been paged out, setting up the dependency will be slow, no matter how the dep is defined (through
@importor through JS).