capricorn86 / happy-dom

A JavaScript implementation of a web browser without its graphical user interface
MIT License
3.41k stars 207 forks source link

Comments are not instances of `Comment` when using `GlobalRegistrator` #1580

Open danieldiekmeier opened 3 weeks ago

danieldiekmeier commented 3 weeks ago

Describe the bug

When using GlobalRegistrator, instanceof Comment is always false, because the globally defined Comment is not the same as the one that is actually used to create comment instances.

Svelte 5 does this check somewhere deep in their template handling – that's how I stumbled upon the problem. I first thought it was a problem with Svelte Testing Library, see issue #407 over there.

To Reproduce

import { GlobalRegistrator } from '@happy-dom/global-registrator'
import { Comment as DirectComment } from 'happy-dom'

GlobalRegistrator.register({ url: 'http://localhost:3000', width: 1920, height: 1080 })

const elem = document.createElement('template')
elem.innerHTML = '<!-- hello -->'
const comment = elem.content.firstChild

console.log(comment instanceof Comment) // BUG: this prints `false`
console.log(comment instanceof DirectComment) // this prints `true`

Expected behavior

I expect (comment instanceof Comment) === true.

Device: