- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 222
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
Incorrect implementation of NamedNodeMap/Element.attributes #728
Comments
Hi @lukasbach @capricorn86! I am also running into this problem... I'm using Microsoft's Fast design system to create a web-component library that is tested using the latest versions of Vitest and happy-dom. Registering my custom elements with the window works great, but as soon as I try to render a custom element, an exception is thrown in Element.ts at the same line mentioned above. My tests always fail with By adding some logging to the compiled happy-dom code for v9.9.2, I was able to confirm that an autofocus attribute was being removed twice for a custom button element. On the second call, that attribute was not found and the error is thrown. Let me know If I can be of any help! Here is a full stack trace:
|
Whoops, sorry for the reference spam. Anyway, I made a contribution which would hopefully solve this issue in #876, would be cool if someone can have a look. |
Thank you for contributing @lukasbach! 🌟 I haven't had time to look into this in detail, but my spontaneous thought is that we need to find a solution where we update the |
…lement.attributes property. It will now reflect any changes done to it on the Element itself.
…tation-of-namednodemapelementattributes #728@patch: Improves support for NamedNodeMap, which is used as the E…
@lukasbach There is now a fix released 🙂 You can read more about the release here: |
@jaa134 your issue should be resolved now. |
Describe the bug
There seems to be some inconsistency in how a the attributes array reference is handled by an
Element
instance which differs from how real browsers handle it. The following example looks like a very rare corner case, but is based on the real implementation of Microsofts Fast Framework, which to my understanding means that happy-dom is currently incompatible with microsoft fast.TLDR: From my understanding, the issue comes down to
Element.attributes
being a dynamic getter which returns a new instance NamedNodeMap, and while this map has access to the original element and always returns the most recent attributes when iterating on it, doing an array-style access returns values from the state of the creation of the NamedNodeMap since they were assigned only once, which is incorrect.To Reproduce
I've reproduced the issue with the following code in a vitest that is running in an happy-dom environment:
This returns the following output:
Expected behavior
The reference to the
attributes
array should return the most up-to-date values when doing array-style value read operations on it.More practical example
While this example looks like it can be worked-around easily, this is based on the implementation of @microsoft/fast-element: https://github.com/microsoft/fast/blob/master/packages/web-components/fast-element/src/templating/compiler.ts#L152
And since that implementation is correct and works in the browser, it is only possible to get this working by fixing the issue in happy-dom. This is how this looks at runtime:
This will cause attributes to be attempted to be removed twice, breaking at
happy-dom/packages/happy-dom/src/nodes/element/Element.ts
Line 929 in e91b864
Culprit
My understanding is that this is caused by this line:
happy-dom/packages/happy-dom/src/nodes/element/Element.ts
Line 256 in e91b864
The attributes are assigned to the NamedNodeMap only once when it is created, and will become stale once the actual attributes change.
Device:
vitest 0.27.1, happy-dom 8.2.6
More Details
This could maybe be solved by using a proxy above the named node map, which would use the most recent attribute value when doing an indexed access on it rather than assigning indexed values on creation time. I could maybe look into creating a PR for this, let me know if there is interest.
The text was updated successfully, but these errors were encountered: