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

MutationObserver disconnect fails if no elements are observed #659

Closed
spudly opened this issue Nov 18, 2022 · 1 comment
Closed

MutationObserver disconnect fails if no elements are observed #659

spudly opened this issue Nov 18, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@spudly
Copy link

spudly commented Nov 18, 2022

Describe the bug
If you call disconnect() on a MutationObserver instance that is not yet observing any elements, it will fail with this error: TypeError: Cannot read properties of null (reading '_unobserve')

To Reproduce

import {Window} from 'happy-dom';

const window = new Window();

const observer = new window.MutationObserver(() => {});
observer.disconnect();

Expected behavior

No error

Screenshots

n/a

Device:

  • OS: MacOS
  • Browser: N/A (NodeJS / Jest / @happy-dom/jest-environment)
  • Version [e.g. 22]

Additional context

The bug is here: https://github.com/capricorn86/happy-dom/blob/master/packages/happy-dom/src/mutation-observer/MutationObserver.ts#L58

this.target is null until observe() is called, so if you call disconnect before that, it throws the error. I think it should probably be looping over a list (WeakSet?) of observed elements and calling _unobserve on each one.

@spudly spudly added the bug Something isn't working label Nov 18, 2022
victorwedo added a commit to victorwedo/happy-dom that referenced this issue Jan 30, 2023
victorwedo added a commit to victorwedo/happy-dom that referenced this issue Jan 30, 2023
capricorn86 added a commit that referenced this issue Jan 30, 2023
…sconnect

#659@trivial: Make sure MutationObserver target is not null before disconnecting
@capricorn86
Copy link
Owner

Thank you for reporting @spudly! 🙂

Big thanks to @victorwedo for making a fix! ⭐

The fix has now been released.

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

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

No branches or pull requests

2 participants