Closed Bug 1932508 Opened 2 years ago Closed 2 years ago

Calling document.adoptedStyleSheets[0].replace() triggers "Uncaught (in promise) DOMException: This method can only be called on modifiable style sheets"

Categories

(Core :: DOM: CSS Object Model, defect)

Firefox 132
defect

Tracking

()

RESOLVED INVALID

People

(Reporter: ruijia, Unassigned)

Details

Attachments

(4 files)

User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36

Steps to reproduce:

On Firefox 132.0.2 (issue seemed to have started in version 131)

Navigate to: https://codepen.io/jookyboi/pen/VwoJreW

Added Javascript:

const sheet = new CSSStyleSheet();
// Apply a rule to the sheet
sheet.replace("a { color: red; }");

// Apply the stylesheet to a document
document.adoptedStyleSheets = [sheet];

document.adoptedStyleSheets[0].replace("a { color: blue; }");

Added HTML:

<a>This is a link</a>

Actual results:

Uncaught exception thrown in console:
"Uncaught (in promise) DOMException: This method can only be called on modifiable style sheets"

Stylesheet not applied and anchor tag did not change color.

Expected results:

Exception should not be thrown and anchor tag should change color.

Latest stable Chrome (131.0.6778.69) rendered the correct results.

Component: Untriaged → DOM: CSS Object Model
Product: Firefox → Core
Attached file testcase 1 —

Here's a testcase, modified slightly from the linked codepen.

The div renders with a red background in Firefox, vs. a lime background in Chromium & WebKit.

(In reply to Rui from comment #0)

On Firefox 132.0.2 (issue seemed to have started in version 131)

Hmm, so for me at least, this didn't start in version 131 -- I can see the same issue with your codepen in Firefox 130.0 and older builds too (e.g. v123, didn't bother testing further back than that).

It's conceivable that there's something in the neighborhood that broke in version 131, but I don't think the testcases here get at that change right now.

If you were starting with some sort of larger testcase or site that worked in 130 and broke in 131, would you mind sharing that larger testcase (or a reduced version that works as expected in Firefox 130)?

Flags: needinfo?(ruijia)

Actually, I just noticed a few things:
(1) this doesn't require the use of adoptedStyleSheets; I get the same issue if I just move the second .replace() to happen directly on sheet.
(2) ...but everything works if I wait a bit (with setTimeout) before I do the second replace
(3) ...and everything works if I change the first replace to replaceSync

I think Firefox's behavior here is actually correct. MDN notes that replace() does its work asynchronously, per https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/replace , and it's allowed to throw if the sheet is flagged as "unmodifiable" (i.e. "readonly"). And the spec says that one of the first things replace() does is to set the readonly flag (with the flag only being unset as part of the async work that the returned promise is guarding):
https://drafts.csswg.org/cssom/#dom-cssstylesheet-replace

The replace(text) method must run the following steps:
...
3. Set the disallow modification flag.
4. In parallel, do these steps: [...]

("in parallel" represents the asynchronous work here that happens at the same time as the event loop)

So this is working as-expected. Not sure why Chromium and Safari give the behavior that they do here -- it looks like they do the work immediately here (not asynchronously), as can be seen by the fact that their replace() returns an already-fulfilled Promise() object (representing async work that has in fact already been done).

Status: UNCONFIRMED → RESOLVED
Closed: 2 years ago
Flags: needinfo?(ruijia)
Resolution: --- → INVALID

Per spec: to avoid this issue, you have to either:
(1) use replaceSync() instead of replace()
(2) Wait for the returned promise to be resolved (using .then() or await, depending on your syntactic preferences) before calling replace again.

Here's a testcase to demonstrate that await-ing the return value of first replace() call is sufficient to avoid this issue (because that ensures that the sheet is modifiable again, i.e. not readonly, when we start the second replace() call).

And here's a testcase to demonstrate that replaceSync has the same benefit (ensuring that the replace operation is done and the sheet is modifiable again by the time that we get to the second replace() call so that it doesn't insta-fail).

You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: