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

Fix: Fragile cloneNode implementation #1284

Merged
merged 2 commits into from
Mar 10, 2024
Merged

Conversation

itutto
Copy link
Contributor

@itutto itutto commented Mar 1, 2024

Fixes break of immediate attributeChanged callback when using cloneNode.

Sample to recreate the issue

// sample.mjs
import * as happyDom from 'happy-dom';

const win = new happyDom.Window();
const doc = win.document;

class Test extends win.HTMLElement {
    attributeChangedCallback() {
        // This will fail because the cloned element does not have a tagname (yet).
        this.querySelectorAll('label');
        /**
         * SelectorItem.js:42
            if (this.tagName !== '*' && this.tagName !== element[PropertySymbol.tagName].toUpperCase()) {
         */
    }
}
Test.observedAttributes = ['my-test'];

win.customElements.define('x-test',Test);

const template = doc.createElement('template');
template.innerHTML = `<template><x-test my-test="fail"></x-test></template>`;
template.content.cloneNode(true)

Run

node sample.mjs

What is the fix?

I propose to move the tagName, localName and namespaceUri initialization before the attribute clone.

@itutto itutto requested a review from capricorn86 as a code owner March 1, 2024 18:09
Copy link
Owner

@capricorn86 capricorn86 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution @itutto! 🌟

@capricorn86 capricorn86 enabled auto-merge March 10, 2024 13:13
@capricorn86 capricorn86 merged commit 1eaa5f5 into capricorn86:master Mar 10, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants