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

another version of Element.prototype.contains that considers slotted elements as children #1214

Open
canalun opened this issue Jul 2, 2023 · 2 comments
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest topic: nodes topic: shadow Relates to shadow trees (as defined in DOM)

Comments

@canalun
Copy link

canalun commented Jul 2, 2023

Hi, hope you all are well :)

motivation

Currently, Element.prototype.contains() doesn't see slotted elements as children.

#shadow-root
  <div>
    <slot></slot>
  </div>
<span>this is slotted</span>
div.contains(span)
=> false

ref: the latest version of DOM Living Standard: https://dom.spec.whatwg.org/#dom-node-contains

This behavior is sometimes problematic when <template> is used, and this kind of situation can possibly increase by the appearance of declarative shadow dom.

Of course, the alternative can be implemented on the side of each developer.
But it's not so simple to implement it, especially taking the calculation cost into account.
So it'll be helpful if another version of the function that considers slotted elements as children is provided by default.

draft of implementation for discussion

Basically, it's enough to apply Element.prototype.contains() recursively while drilling up to parentNode or assignedSlot.
This implementation can detect shadow-including descendant also.

Element.prototype.containsIncludingSlot(element)
1. if element is null, return false
2. if this and element have the same root node, return this.contains(element).
3. let parentNode is as follows:
  - if element.assignedSlot is not null, element.assignedSlot
  - else if element.parentElement is not null, element.parentElement
  - else if element.parentNode is not null, element.parentNode.host (i.e. the function doesn't go across frames)
  - else, null
4. return this.containsIncludingSlot(element.assignedSlot).

I think it's a bit too complicated to let each developer to implement by themselves.
Would like to hear opinions here :)

@WebReflection
Copy link

I wonder if contains(span, {slotted: true}) would be a better solution, but I agree there is some need for this, even though the tree might mislead eyes.

@canalun
Copy link
Author

canalun commented Jul 4, 2023

@WebReflection Thank you for giving your thoughts!!
I agree with your idea about the interface. :)

@annevk annevk added topic: shadow Relates to shadow trees (as defined in DOM) needs implementer interest Moving the issue forward requires implementers to express interest addition/proposal New features or enhancements topic: nodes labels Jul 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest topic: nodes topic: shadow Relates to shadow trees (as defined in DOM)
Development

No branches or pull requests

3 participants