Skip to content

documentFragment.firstChild returns null if first child is e.g. <tr></tr> #1687

Open
@danieldiekmeier

Description

Describe the bug

When you put a standalone <tr></tr> element in a <template> and try to get it out with .firstChild, you get null instead. This also happens with <tbody> or <td>, but NOT with <li>.

To Reproduce

import { GlobalRegistrator } from '@happy-dom/global-registrator';
GlobalRegistrator.register();

const html = `<tr></tr>`

const elem = document.createElement('template');
elem.innerHTML = html;
const fragment = elem.content;

console.log(fragment.firstChild)

This prints null.

If you use something like const html =

instead, it returns the
` node.

Expected behavior

When you run this code in the browser, it returns the node <tr></tr>:

Screenshot 2025-01-13 14 27 09@2x

Device:

System:
  OS: macOS 15.0.1
  CPU: (10) arm64 Apple M1 Max
  Memory: 9.37 GB / 64.00 GB
  Shell: 5.9 - /bin/zsh
Binaries:
  Node: 22.13.0 - ~/.local/share/mise/installs/node/22/bin/node
  npm: 10.9.2 - ~/.local/share/mise/installs/node/22/bin/npm
  pnpm: 9.12.3 - ~/.local/share/mise/installs/node/22/bin/pnpm
npmPackages:
  @happy-dom/global-registrator: ^16.5.3 => 16.5.3
  happy-dom: ^16.5.3 => 16.5.3

Additional context
Add any other context about the problem here.

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions