Skip to content

What should adoptNode return for a DocumentFragment with a non-null host? #930

Closed
@twilco

Description

@twilco

https://dom.spec.whatwg.org/#dom-document-adoptnode

Step 3 of the adoptNode algorithm states:

If node is a DocumentFragment node whose host is non-null, then return.

I'm assuming this means return nothing (null), where the other cases are either throwing a DOMException or returning a Node.

However, this is contrary to the Document interface IDL, which is:

[CEReactions] Node adoptNode(Node node);

instead of:

[CEReactions] Node? adoptNode(Node node);


Given this:

<html>
<body>
<script>
  const createTemplateElement = (doc) => {
    let template = doc.createElement('template')
    let p = doc.createElement('p')
    p.appendChild(doc.createTextNode('hello'))
    template.appendChild(p)
    return template
  }

  let newDoc = document.implementation.createHTMLDocument()
  newDoc.body.appendChild(createTemplateElement(newDoc))
  let template = newDoc.getElementsByTagName('template')[0]

  // `template.content` is a `DocumentFragment` with a non-null host (the host is the <template>)
  // <template> creation steps from https://html.spec.whatwg.org/multipage/scripting.html#the-template-element:
  //   1. Let doc be the template element's node document's appropriate template contents owner document.
  //   2. Create a DocumentFragment object whose node document is doc and host is the template element.
  //   3. Set the template element's template contents to the newly created DocumentFragment object.
  console.log(document.adoptNode(template.content))
</script>
</body>
</html>

Firefox, Chromium, and Safari all returned the DocumentFragment from adoptNode.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions