Skip to content

Reject document cloning and importing explicitly - #684

Open
olavoasantos wants to merge 1 commit into
preserve-clone-namespacesfrom
deliberate-document-cloning
Open

olavoasantos wants to merge 1 commit into
preserve-clone-namespacesfrom
deliberate-document-cloning

Conversation

@olavoasantos

@olavoasantos olavoasantos commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Problem

Document.cloneNode() and Document.importNode(document) entered the generic cloning path, which cannot deliberately construct a document node. Instead of declaring whether this unsupported operation was available, the polyfill leaked incidental internal TypeErrors.

Impact

Minor. Code that attempts to clone or import a document receives an implementation accident rather than a stable, recognizable DOM error, so it cannot reliably distinguish this unsupported operation from an unrelated programming failure.

Reproduction

const destination = new Window().document;

window.document.cloneNode(true);
destination.importNode(window.document, true);

Before this change, both calls failed through the generic clone implementation with incidental internal errors. They now throw a NotSupportedError with a clear operation-specific message; cloning and importing ordinary document descendants remains supported.

Change

Detect document nodes before entering the generic clone path. Both Document.importNode() and cloneNode() now reject them with a named NotSupportedError, using DOMException where available and an Error with the same name as a fallback.

Tests

Adds packages/polyfill/source/tests/document-clone-import-contract.test.ts, covering shallow and deep document cloning/importing, the error name and messages, the no-DOMException fallback, and successful cloning/importing of document.documentElement.

Stack

Validation

Fresh GitHub CI on restacked head 8d31750 passes:

  • lint;
  • type-check and build;
  • unit tests with coverage;
  • bundle-size checks;
  • Playwright end-to-end tests;
  • the classified Web Platform Test suite.

@olavoasantos
olavoasantos force-pushed the deliberate-document-cloning branch from 78a2911 to e07f423 Compare September 3, 2026 15:29
@olavoasantos
olavoasantos force-pushed the deliberate-document-cloning branch from e07f423 to 8d31750 Compare September 4, 2026 14:31
@olavoasantos
olavoasantos marked this pull request as ready for review September 14, 2026 18:41
@olavoasantos
olavoasantos force-pushed the deliberate-document-cloning branch from 8d31750 to bf0153c Compare September 16, 2026 13:57
@olavoasantos
olavoasantos force-pushed the deliberate-document-cloning branch from bf0153c to 0e8e21c Compare September 16, 2026 14:08
@olavoasantos
olavoasantos force-pushed the deliberate-document-cloning branch from 0e8e21c to 77e41b6 Compare September 16, 2026 20:13
@olavoasantos
olavoasantos removed this pull request from stack #700 September 16, 2026 20:14
@olavoasantos
olavoasantos added this pull request to stack #717 September 16, 2026 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants