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

Don't allow adopting DocumentFragment of a template element #744

Closed
rniwa opened this issue Mar 30, 2019 · 12 comments · Fixed by #754
Closed

Don't allow adopting DocumentFragment of a template element #744

rniwa opened this issue Mar 30, 2019 · 12 comments · Fixed by #754
Labels
topic: shadow Relates to shadow trees (as defined in DOM)

Comments

@rniwa
Copy link
Collaborator

rniwa commented Mar 30, 2019

Right now, we allow adopting a DocumentFragment of a template element because DocumentFragment never have a parent.

The subtree of such a DocumentFragment is still associated with the template element yet its owner document is of the adopted document. As far as I know, this is only case in which such an oddity arises because ShadowRoot is forbidden from being adopted into another tree, and every other node type would have a parent as long as it's part of a document and therefore will be removed before the adoption.

I don't think we should allow this. https://github.com/web-platform-tests/wpt/blob/master/html/semantics/scripting-1/the-template-element/template-element/template-content-hierarcy.html explicitly tests this.

@rniwa
Copy link
Collaborator Author

rniwa commented Mar 30, 2019

I suspect throwing is probably not web compatible at this point so I'd suggest silently returning early.

@annevk
Copy link
Member

annevk commented Apr 5, 2019

I'm not sure it's that simple. E.g., replace all can be passed a ShadowRoot and will adopt, from what I can tell. It will end up empty too, but still probably not what we want?

I think the problem is with DocumentFragment's host is that we only tried to prevent truly broken cases and did not analyze other side effects enough.

@annevk annevk added needs tests Moving the issue forward requires someone to write tests topic: shadow Relates to shadow trees (as defined in DOM) labels Apr 5, 2019
@annevk
Copy link
Member

annevk commented Apr 5, 2019

Ah great, Firefox throws on div2.appendChild(shadow); and Chrome and Safari do not change the node document of shadow:

test(() => {
  const doc1 = new Document(),
        doc2 = new Document(),
        div1 = doc1.appendChild(document.createElement("div")),
        div2 = doc2.appendChild(document.createElement("div")),
        shadow = div1.attachShadow({mode: "closed"}),
        shadowChild = shadow.appendChild(new Text());
  div2.appendChild(shadow);
  assert_equals(div2.firstChild, shadowChild);
  assert_equals(shadow.ownerDocument, doc2);
});

(My replace all example is a little misleading as currently that can only be invoked with newly created nodes at the moment, but append/pre-insert has the same issue.)

@rniwa
Copy link
Collaborator Author

rniwa commented Apr 5, 2019

We definitely don't implement such a semantic for ShadowRoot so that's just a spec bug.

@annevk
Copy link
Member

annevk commented Apr 8, 2019

It's not "just" a specification bug if implementations are doing different things. As far as I know this wasn't fully considered either so it's not clear to me what the resolution ought to be.

@rniwa
Copy link
Collaborator Author

rniwa commented Apr 11, 2019

It's not "just" a specification bug if implementations are doing different things. As far as I know this wasn't fully considered either so it's not clear to me what the resolution ought to be.

Well, we clearly don't have any implementation that just adopts the node, and I don't think we ever want that anyway. So it's an oversight that we never spec'ed this behavior. So I agree there is a disagreement as to what exactly happens when a ShadowRoot gets adopted but I don't think the answer to that can never be it gets successfully adopted. That would make no sense.

@annevk
Copy link
Member

annevk commented Apr 11, 2019

If I insert a node that has an attached ShadowRoot into a different document, it wouldn't be adopted? I suspect you didn't mean to say that.

I suppose what we need to do is that when you adopt a node that has a host, that needs to be handled in a special way. But if you hit such a node due to recursion, that should not be handled in a special way.

@rniwa
Copy link
Collaborator Author

rniwa commented Apr 12, 2019

If I insert a node that has an attached ShadowRoot into a different document, it wouldn't be adopted? I suspect you didn't mean to say that.

Obviously, we should adopt ShadowRoot if its host is adopted. What we don't want to allow is adopting ShadowRoot without adopting its host.

I suppose what we need to do is that when you adopt a node that has a host, that needs to be handled in a special way.

That might be a neat idea to handle both document fragment types. But really, the only place we need a check is https://dom.spec.whatwg.org/#dom-document-adoptnode because all other public DOM APIs would adopt a node either throws on ShadowRoot or takes the children of DocumentFragment so there is no danger of adopting a node.

On a completely unrelated note, I wish DOM / HTML spec had spec assertions like ECMA. That would clarify a lot of subtle conditions like this.

@annevk
Copy link
Member

annevk commented Apr 12, 2019

I don't follow. adoptNode() throws on ShadowRoot. It's the other places that are potentially problematic.

We can use assertions and you are free to add them via PRs (or request them to be added during review of a PR): https://infra.spec.whatwg.org/#assertions.

@rniwa
Copy link
Collaborator Author

rniwa commented Apr 12, 2019

@annevk : The case I'm concerned about is the content DocumentFragment of a HTMLTemplateElement. As far as I can tell, ShadowRoot doesn't have this issue.

@annevk
Copy link
Member

annevk commented Apr 15, 2019

Adopt entry points:

@annevk
Copy link
Member

annevk commented Apr 15, 2019

Maybe in part the misunderstanding here is that the specification is wrong for DocumentFragment in general when it comes to those other points and adoption should never happen for it.

annevk added a commit to web-platform-tests/wpt that referenced this issue Apr 15, 2019
Tests for whatwg/dom#744.

The assumption here is an early return for DocumentFragment that has a host by adoptNode() and that adoption generally doesn't affect DocumentFragment otherwise (only its children).
annevk added a commit that referenced this issue Apr 15, 2019
And do not allow non-null host DocumentFragment nodes to be adopted.

This means that a DocumentFragment is no longer implicitly adopted and a DocumentFragment with a non-null host cannot be put in a "weird" state.

Tests: web-platform-tests/wpt#16348.

Fixes #744.
annevk added a commit that referenced this issue Dec 6, 2019
And do not allow non-null host DocumentFragment nodes to be adopted.

This means that a DocumentFragment is no longer implicitly adopted and a DocumentFragment with a non-null host cannot be put in a "weird" state.

Tests: web-platform-tests/wpt#16348.

Fixes #744.
annevk added a commit to web-platform-tests/wpt that referenced this issue Dec 7, 2019
Tests for whatwg/dom#744.

The assumption here is an early return for DocumentFragment that has a host by adoptNode() and that adoption generally doesn't affect DocumentFragment otherwise (only its children).
@annevk annevk removed the needs tests Moving the issue forward requires someone to write tests label Dec 11, 2019
annevk added a commit that referenced this issue Dec 11, 2019
Also do not allow DocumentFragment nodes with host to be adopted. This means that a DocumentFragment is no longer implicitly adopted and a DocumentFragment with a (non-null) host cannot be put in a "weird" state.

Tests: web-platform-tests/wpt#16348.

Fixes #744.
jgraham pushed a commit to web-platform-tests/wpt that referenced this issue Dec 11, 2019
Tests for whatwg/dom#744.

The assumption here is an early return for DocumentFragment that has a host by adoptNode() and that adoption generally doesn't affect DocumentFragment otherwise (only its children).
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this issue Dec 16, 2019
Automatic update from web-platform-tests
Adoption and DocumentFragment (#16348)

Tests for whatwg/dom#744.

The assumption here is an early return for DocumentFragment that has a host by adoptNode() and that adoption generally doesn't affect DocumentFragment otherwise (only its children).

--

wpt-commits: cca4170a0c8a6a9b8398eb53364ec0bba7ead7cb
wpt-pr: 16348
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified-and-comments-removed that referenced this issue Dec 17, 2019
Automatic update from web-platform-tests
Adoption and DocumentFragment (#16348)

Tests for whatwg/dom#744.

The assumption here is an early return for DocumentFragment that has a host by adoptNode() and that adoption generally doesn't affect DocumentFragment otherwise (only its children).

--

wpt-commits: cca4170a0c8a6a9b8398eb53364ec0bba7ead7cb
wpt-pr: 16348

UltraBlame original commit: 766599c3407c7c86a9b858a0f5e2403d9a8a98c9
gecko-dev-updater pushed a commit to marco-c/gecko-dev-comments-removed that referenced this issue Dec 17, 2019
Automatic update from web-platform-tests
Adoption and DocumentFragment (#16348)

Tests for whatwg/dom#744.

The assumption here is an early return for DocumentFragment that has a host by adoptNode() and that adoption generally doesn't affect DocumentFragment otherwise (only its children).

--

wpt-commits: cca4170a0c8a6a9b8398eb53364ec0bba7ead7cb
wpt-pr: 16348

UltraBlame original commit: 766599c3407c7c86a9b858a0f5e2403d9a8a98c9
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified that referenced this issue Dec 17, 2019
Automatic update from web-platform-tests
Adoption and DocumentFragment (#16348)

Tests for whatwg/dom#744.

The assumption here is an early return for DocumentFragment that has a host by adoptNode() and that adoption generally doesn't affect DocumentFragment otherwise (only its children).

--

wpt-commits: cca4170a0c8a6a9b8398eb53364ec0bba7ead7cb
wpt-pr: 16348

UltraBlame original commit: 766599c3407c7c86a9b858a0f5e2403d9a8a98c9
jamienicol pushed a commit to jamienicol/gecko that referenced this issue Dec 17, 2019
Automatic update from web-platform-tests
Adoption and DocumentFragment (#16348)

Tests for whatwg/dom#744.

The assumption here is an early return for DocumentFragment that has a host by adoptNode() and that adoption generally doesn't affect DocumentFragment otherwise (only its children).

--

wpt-commits: cca4170a0c8a6a9b8398eb53364ec0bba7ead7cb
wpt-pr: 16348
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: shadow Relates to shadow trees (as defined in DOM)
Development

Successfully merging a pull request may close this issue.

2 participants