New issue
Advanced search Search tips
Note: Color blocks (like or ) mean that a user may not be available. Tooltip shows the reason.

Issue 658119 link

Starred by 2 users

Issue metadata

Status: WontFix
Owner:
Last visit > 30 days ago
Closed: Oct 2016
Cc:
Components:
EstimatedDays: ----
NextAction: ----
OS: ----
Pri: 3
Type: Bug



Sign in to add a comment

HTML Custom Elements v1 - Nested custom elements and compositon - The parent element can' see it's children in connectedcallback

Reported by ephemere...@gmail.com, Oct 21 2016

Issue description

Chrome Version       : Version 54.0.2840.59 m (64-bit)
Other browsers tested: None, Custom Elements v1 is only implemented in Chrome yet.
  
What steps will reproduce the problem?

See the attached HTML code file. Tested with Chrome dev tools.

*****First - Expected behavior and working

(1) Create 1 custom element that will become a container.

(2) Nest native HTML elements in the container:

    <custom-container>
         <child></child>
         ...
         <child></child>
    </custom-container>

(3) In the container's connectedCallback method modify the children (for instance their innerHTML).

(4) Load the page and break into the container's connectedCallback method.


Expected behavior: The container's connectedCallback CAN SEE all of it's children and can perform actions on them.

This is the expected behavior for a container that manipulates it's children and is essential for composition.

Three ways to verify this, first break into the container's connectedCallback method:

        a) Output the chidren node in the console
        b) Simply look at the content of the container in the Elements view.
	c) Let it run and look at the HTML page results. Children will be modified.


*****Second - Unexpected behavior and NOT working

(1) Create 2 custom elements, one the container, the other the child.
(2) Nest these elements like before

    <custom-container>
         <custom-child></custom-child>
         ...
         <custom-child></custom-child>
    </custom-container>


(3) In the container's connectedCallback method modify the children (for instance their innerHTML).

(4) Load the page and break into the container's connectedCallback method.


Unexpected behavior: The container's connectedCallback can't see any of it's children.

This is an unexpected behavior and prevents the container from manipulating it's children.

Putting a break point in connectedCallback we can clearly see that the container is empty (console or Elements view). 

The output of the HTML page clearly shows that the container could not manipulate it's children.


Question:

-Is there another callback that should be used instead for custom elements when using composition? I could not find any.


*****Important: The creation sequence of custom elements using composition SHOULD NOT differ from the native ones, it defies the whole purpose of having custom elements behaving like native ones and will lead to disastrous side effects.

For now I can't implement custom elements composition and my project is on hold.
 
Test.php
1.5 KB View Download
****Update****

I found an example of a working container on Github

Fancy tab
From: ebidel
https://gist.github.com/ebidel/2d2bb0cdec3f2a16cf519dbaa791ce1b

I simplified it to the bones to compare it to my code (the one I provided before).

There was my surprise, after many hours of pruning code and shuffling things around: I could also break his code if I moved the HTML containers elements instances AFTER the scripts defining his custom elements (classes and define).

I did the reverse and moved my containers instances BEFORE my classes definitions scripts. EUREKA! My code worked. (I attached the modified working code).

This is counter intuitive. In most languages and platforms, especially HTML, we first define our elements and rules, then we use instances of those definitions. Even in JavaScript, classes definitions are not hoisted (contrarily to functions) and MUST precede any instance creation.

In this case, it will only work if we use something undefined and ignored (partially) by the interpreter at first and then define it officially later. OUCH!

Looking at the custom element specs, https://developers.google.com/web/fundamentals/getting-started/primers/customelements#upgrades

it seems that this is called UPGRADING an element i.e. using it before it is defined and defining it later. It insured in my case and ebidel's case that all the children already existed in the DOM when the container's'connectedCallback was called. 

In retrospect, defining custom elements before using them (typically with a definitions inclusion at the beginning of the HTML file) will not work for containers of custom elements. Also, no more can we look at the innerHTML and child nodes from a parent node's perspective during parsing.

*****I must remind you that if the inner children were native elements (instead of custom), everything worked as expected initially and all the children are ALREADY created when the container is called, even if we define the custom container BEFORE the elements' instantiation (inclusion at the begining).

It is too schizophrenic to have 2 different types of logic, one for native elements, and a new ugly beast for the custom elements. We DO WANT custom elements to be created and behave like native elements. It should be transparent to the coder. It is the whole point of custom elements! If my understanding is right, the implementation or specs is probably flawed here. 

I would like clarifications about this behavior. I'm convinced many will profit from it. I find really suspicious that I would have to do those includes at the end of the HTML page. It definitively breaks so many rules about HTML that the solution I found can be qualified at best as ugly, hacky and temporary.

Hopefully I totally missed something crucial and someone will enlighten me. 


Test.php
1.5 KB View Download
Components: Blink>WebComponents

Comment 3 by tkent@chromium.org, Oct 24 2016

Components: -Blink>WebComponents Blink>HTML>CustomElements
Blink>WebComponents is deprecated.

Cc: esprehn@chromium.org domenic@chromium.org
Owner: dominicc@chromium.org
Status: WontFix (was: Unconfirmed)
CCing domenic and esprehn FYI re: custom elements design, microtask control, etc.

I'm closing this as WontFix because I think this is working as intended and per the spec, even though the behavior is surprising. domenic can sanity check me on that.

Some contributors to the spec considered a callback for "end tag parsed" which would be a good time to inspect children if you were doing "one shot" processing of children, however IIRC the decision was to not add this callback yet.

If you want a custom element's behavior to depend on its children you should use a MutationObserver or have the children coordinate with the container through events/methods/etc.

Sign in to add a comment