webcomponents / polyfills

Web Components Polyfills
BSD 3-Clause "New" or "Revised" License
1.14k stars 167 forks source link

[ShadyDOM] assignedSlot returns null for host children, until assignedSlot is called for any element in shadow tree #90

Open tomalec opened 5 years ago

tomalec commented 5 years ago

Description

I'm trying to inspect the host.children[~].assignedSlot. (Preferably, from within shadow root).

But in Polyfilled browsers it's null. I noticed that, if I call assignedSlot on any element from host.shadowRoot.querySelector('*').assignedSlot, shadow tree get synchronously "rendered", and host.children[~].assignedSlot starts to return correct value.

It feels like assignedSlot getter renders incorrect tree. It renders a tree that contains the given element instead of the tree that may contain a slot in question - parent element's shadow tree.

Live Demo

https://jsbin.com/tapaqoy/3/edit?html,console,output

Steps to Reproduce

  1. Create shadow-host
  2. Append <div slot="given"> to shadow host
  3. Attach shadow to shadow-host, which contains <slot name="given">
  4. Read div.assignedSlot
<template id="shadowRoot">
  <slot name="given"></slot>
</template>
<div id="shadowHost">
  <div slot="given" id="slottable">I'm distributed</div>
</div>

<script>
   shadowHost.attachShadow({mode: 'open'})
     .appendChild(document.importNode(shadowRoot.content, true));
   const slottable = shadowHost.querySelector('[slot]');

   console.log(slottable.assignedSlot); // null
   // trick Polymer to render this shadow tree to make `assignedSlot` of element accessible
   shadowHost.firstChild.assignedSlot; // does not have to be a slot

   console.log(slottable.assignedSlot); // [HTMLSlotElement]
</script>

Expected Results

A reference to the slot should be given

Actual Results

It returns null

Browsers Affected

Versions

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.