Skip to content

Cannot use well-known attribute names with custom elements #45

Closed
@hesxenon

Description

@hesxenon

Initial checklist

Affected package

https://esm.sh/hast-util-to-html@9?bundle

Steps to reproduce

See here: https://jsbin.com/jelewudote/3/edit?html,console,output

Copy Pasta'd repro for later reference
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <script>
    console.clear();
    class Foo extends HTMLElement {
      connectedCallback() {
        console.log(`rendered: ${this.getAttribute("selected")}`);
      }
    }
    customElements.define("my-foo", Foo)
  </script>

  <script type="module">
    import {toHtml} from "https://esm.sh/hast-util-to-html@9?bundle"

    console.log(`serialized: ${toHtml({
      type: "element",
      children: [],
      properties: {
        selected: "some value",
      },
      tagName: "my-foo",
    })}`)
  </script>
  
  <my-foo selected="some value"></my-foo>
</body>
</html>

Basically:

  1. define a custom element which relies on the value of an attribute that has a well-known name
  2. serialize such a custom element with an "unexpected" value for the well-known name

Actual behavior

Serialization enforces the well-known definition over its intended usage.

"serialized: <my-foo selected></my-foo>"

Expected behavior

Serialization should not enforce the well-known definition in the case of custom elements. It should align with what is rendered by the browser.

"serialized: <my-foo selected="some value"></my-foo>"

Runtime

Firefox/Chrome

Package manager

No response

Operating system

No response

Build and bundle tools

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions