[whatwg/dom] cloneNode with nodes with resources such as <link rel="stylesheet"> (Issue #1370)

KurtCattiSchmidt created an issue (whatwg/dom#1370)

### What is the issue with the DOM Standard?

I discovered this bug in Chromium: https://issues.chromium.org/issues/396174245

This issue is that when calling `cloneNode` on a `<link rel="stylesheet" href="foo.css">`, Chromium doesn't fetch the resource on the clone until the next frame, causing an FOUC. Firefox and Safari are able to render the cloned styles immediately (no FOUC). 

(note that this issue only really impacts Shadow DOM - otherwise cloning the `<link>` will just be cloning styles that have already been applied, and thus no FOUC)

I have a potential fix for this that has some nice perf benefits (https://chromium-review.googlesource.com/c/chromium/src/+/6427105), but @noamr rightfully questioned whether my fix is spec-compliant, or whether the spec needs to be updated.

The [spec](https://dom.spec.whatwg.org/#dom-node-clonenode) for `cloneNode` does not mention anything particular for nodes with associated resources (`<link rel="stylesheet">`, `<input type="file">`, `<img src=foo.png">`, etc). But maybe it should. 

My fix follows the order of steps listed in the spec, and thus does not return a value for the `sheet` property in the clone until the `href` attribute is copied to the clone, per the steps in the spec (which would otherwise start a fetch), but it could also immediately expose the resource in the clone (assuming the original element containing the resource is loaded).

I came across this related issue https://github.com/whatwg/html/issues/9638, that perhaps could be generalized into "any element with an associated resource is immediately available when cloned". Would that make sense as a spec update, or is there some reason why this is not possible to generalize? 

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/1370
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/dom/issues/1370@github.com>

Received on Tuesday, 22 April 2025 00:20:20 UTC