Skip to content

toHtml produces invalid HTML for void elements inside <foreignObject> #49

@remcohaszing

Description

@remcohaszing

Initial checklist

Affected package

[email protected], hast-util-to-html@8

Steps to reproduce

Run the following script to stringify a <br> element inside <foreignObject>:

import { toHtml } from 'hast-util-to-html'

console.log(
  toHtml({
    type: 'root',
    children: [
      {
        type: 'element',
        tagName: 'svg',
        properties: {},
        children: [
          {
            type: 'element',
            tagName: 'foreignObject',
            properties: {},
            children: [
              {
                type: 'element',
                tagName: 'br',
                properties: {},
                children: []
              }
            ]
          }
        ]
      }
    ]
  })
)

Actual behavior

This produces the following string. The void <br> element has a closing tag.

<svg><foreignObject><br></br></foreignObject></svg>

Interestingly Chrome and Firefox interpret this HTML string differently.

Expected behavior

The <br> element isn’t closed. It may or may not self-close.

Runtime

Node.js 24.11.1

Package manager

npm

Operating system

Pop!_OS 22.04

Build and bundle tools

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    🤞 phase/openPost is being triaged manually

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions