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

Maximum call stack size exceeded when appending parent to child #643

Closed
sionzee opened this issue Nov 5, 2022 · 1 comment · Fixed by #799
Closed

Maximum call stack size exceeded when appending parent to child #643

sionzee opened this issue Nov 5, 2022 · 1 comment · Fixed by #799
Labels
bug Something isn't working

Comments

@sionzee
Copy link

sionzee commented Nov 5, 2022

Describe the bug
Whenever you try to move the parent to the child node and listen to the mutation observer on that node, you will end up in a recursive call. The child will have a parent node, and the parent node will have the child node in childList.

To Reproduce
Quick reproduction code:

const i = document.createElement('i')
new MutationObserver((mutations) => {}).observe(i, {subtree: true, childList: true, attributeOldValue: true, attributes: true, characterData: true, characterDataOldValue: true})

const b = document.createElement('b')
i.appendChild(b)

// Woops -- Mistake
b.appendChild(i)

// Fix the mistake
i.appendChild(b)

Expected behavior
The parent should be appended to the child, and the child's parent should be set to null or the parent element of the parent. In my code, the parent is temporarily null, and I will append it later to the existing node. It is worth checking what the original DOM is doing in this case and going that way.

Additional context

The error:

RangeError: Maximum call stack size exceeded
 ❯ HTMLElement._observe ../../node_modules/.pnpm/happy-dom@7.6.6/node_modules/happy-dom/lib/nodes/node/Node.js:408:25
 ❯ HTMLElement._observe ../../node_modules/.pnpm/happy-dom@7.6.6/node_modules/happy-dom/lib/nodes/node/Node.js:411:22
 ❯ HTMLElement._observe ../../node_modules/.pnpm/happy-dom@7.6.6/node_modules/happy-dom/lib/nodes/node/Node.js:411:22
 ❯ HTMLElement._observe ../../node_modules/.pnpm/happy-dom@7.6.6/node_modules/happy-dom/lib/nodes/node/Node.js:411:22
 ❯ HTMLElement._observe ../../node_modules/.pnpm/happy-dom@7.6.6/node_modules/happy-dom/lib/nodes/node/Node.js:411:22
 ❯ HTMLElement._observe ../../node_modules/.pnpm/happy-dom@7.6.6/node_modules/happy-dom/lib/nodes/node/Node.js:411:22
...

I was debugging the code, and it is stuck in the Node.ts file on this function:

	public _observe(listener: MutationListener): void {
		this._observers.push(listener);
		if (listener.options.subtree) {
			for (const node of this.childNodes) {
				(<Node>node)._observe(listener);  // <------ node === this.parentNode
			}
		}
	}
@sionzee sionzee added the bug Something isn't working label Nov 5, 2022
btea added a commit to btea/happy-dom that referenced this issue Feb 27, 2023
capricorn86 added a commit to btea/happy-dom that referenced this issue Apr 18, 2023
capricorn86 added a commit to btea/happy-dom that referenced this issue Apr 18, 2023
capricorn86 added a commit to btea/happy-dom that referenced this issue Apr 19, 2023
capricorn86 added a commit that referenced this issue Apr 19, 2023
#643@patch: Append node should not be parent.
@capricorn86
Copy link
Owner

Thank you for reporting @sionzee! 🙂

Big thanks to @btea for your contribution! 🌟

You can read more about the release here:
https://github.com/capricorn86/happy-dom/releases/tag/v9.8.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants