Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cssom-1] Incorrect handling of media in Create a constructed CSSStyleSheet? #11778

Open
cdoublev opened this issue Feb 25, 2025 · 1 comment
Labels
cssom-1 Current Work

Comments

@cdoublev
Copy link
Collaborator

cdoublev commented Feb 25, 2025

Step 12 of the procedure requires serializing a media query list from media when it is not a string:

  1. If the media attribute of options is a string, create a MediaList object from the string and assign it as sheet’s media. Otherwise, serialize a media query list from the attribute and then create a MediaList object from the resulting string and set it as sheet’s media.

I doubt that the procedure for serializing a media query list expects a MediaList (cf. CSSStyleSheetInit).

Besides, the media query list associated to the new MediaList should be updated whenever the attribute (of the owner node) is updated:

If this property is specified to an attribute of the owner node, the media must be set to the return value of invoking create a MediaList object steps for the value of that attribute. Whenever the attribute is set, changed or removed, the media’s mediaText attribute must be set to the new value of the attribute, or to null if the attribute is absent.

For example:

<style media="(width: 0px)" id="style">
  * { color: red }
</style>
<p>I become red after 1s</p>
<script>
  const sheet = document.styleSheets[0]
  sheet.media.mediaText // (width: 0px)
  setTimeout(() => {
    style.setAttribute('media', '(width > 0px)')
    style.getAttribute('media') // (width > 0px)
    sheet.media.mediaText // (width > 0px)
  }, 1000)
</script>

So I suspect that step 12 should parse a media query list from media when it is a string, create a MediaList object with it, and assign it as sheet's media. Otherwise, if it is a MediaList, it should create a new MediaList with the media query list associated to media.

@cdoublev cdoublev added the cssom-1 Current Work label Feb 25, 2025
@cdoublev
Copy link
Collaborator Author

I think step 12 confused me by refering to "the attribute" (of the owner node), whereas the procedure creates a constructed style sheet, so there can be no attribute.

The HTML spec says that the media attribute of the owner node is a reference:

media: The media attribute of element.

Note: This is a reference to the (possibly absent at this time) attribute, rather than a copy of the attribute's current value. CSSOM defines what happens when the attribute is dynamically set, changed, or removed.

I do not know what is a reference to an HTML attribute. Wouldn't it work if the HTML spec said to set CSSStyleSheet.media.mediaText whenever the media attribute is updated?

Then a MediaList used to construct a CSSStyleSheet could be reflected bidirectionally, by sharing their internal media query list, as suggested in my previous comment.

cdoublev added a commit to cdoublev/css that referenced this issue Feb 26, 2025
CSSOM is assumed to be wrong, since it wants to serialize a media query list
from MediaList, which would represent "the attribute".

When creating a constructed style sheet, the provided MediaList is now reflected
bidirectionally.

When creating a non-constructed style sheet, we expect the owner node to set
CSSStyleSheet.media.mediaText whenever its media attribute is updated.

w3c/csswg-drafts#11778
cdoublev added a commit to cdoublev/css that referenced this issue Feb 26, 2025
CSSOM is assumed to be wrong, since it wants to serialize a media query list
from MediaList, which would represent "the attribute".

When creating a constructed style sheet, the provided MediaList is now reflected
bidirectionally.

When creating a non-constructed style sheet, we expect the owner node to set
CSSStyleSheet.media.mediaText whenever its media attribute is updated.

w3c/csswg-drafts#11778
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cssom-1 Current Work
Projects
None yet
Development

No branches or pull requests

1 participant