{"record":{"id":"45b48d21358678a7","repo":"can1357/oh-my-pi","slug":"this-node-has-no-owner-document","errorCode":null,"errorMessage":"This node has no owner document","messagePattern":"This node has no owner document","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/utils/src/dom/core.ts","lineNumber":318,"sourceCode":"\t\tif (other.contains(this)) return 10;\n\t\tconst root = this.ownerDocument ?? this;\n\t\tconst nodes: Node[] = [];\n\t\tconst visit = (node: Node): void => {\n\t\t\tnodes.push(node);\n\t\t\tfor (const child of node.childNodes) visit(child);\n\t\t};\n\t\tvisit(root);\n\t\treturn nodes.indexOf(this) < nodes.indexOf(other) ? 4 : 2;\n\t}\n\n\tsetOwnerDocument(document: Document): void {\n\t\tif (this.nodeType !== NodeType.DOCUMENT) this.ownerDocument = document;\n\t\tfor (const child of this.childNodes) child.setOwnerDocument(document);\n\t}\n\n\tdocumentForCreation(): Document {\n\t\tif (this instanceof Document) return this;\n\t\tif (!this.ownerDocument) throw new Error(\"This node has no owner document\");\n\t\treturn this.ownerDocument;\n\t}\n}\n\n/** Text node. */\nexport class Text extends Node {\n\tdata: string;\n\tserializeRaw: boolean;\n\n\tconstructor(data: string, ownerDocument: Document | null = null, serializeRaw = false) {\n\t\tsuper(NodeType.TEXT, \"#text\", ownerDocument);\n\t\tthis.data = data;\n\t\tthis.serializeRaw = serializeRaw;\n\t}\n\n\t/** Number of UTF-16 code units. */\n\tget length(): number {\n\t\treturn this.data.length;","sourceCodeStart":300,"sourceCodeEnd":336,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/utils/src/dom/core.ts#L300-L336","documentation":"Node.documentForCreation returns the owning Document for factory calls (createElement, createTextNode, etc.). It returns `this` when the node is itself a Document, otherwise requires `ownerDocument` to have been set via setOwnerDocument; nodes created outside a document (never passed through setOwnerDocument) have no owner and throw. Internally, child-mutating operations (appendChild, insertBefore, append, prepend, textContent, replaceWith) call this to create text nodes from strings, so the error surfaces there too.","triggerScenarios":"Appending/inserting/prepending a raw string child to a node whose ownerDocument was never assigned — i.e. a node constructed directly with `new Element(...)`/`new Text(...)` rather than via document.createElement and not attached through setOwnerDocument; calling documentForCreation() directly on such a detached node.","commonSituations":"Constructing DOM nodes with `new` in library/test code and mutating them before passing them through any Document; building a subtree standalone and calling textContent = '...' before adopting it; nodes deserialized/rehydrated without re-running setOwnerDocument.","solutions":["Create nodes through the document API (document.createElement / createTextNode) so ownerDocument is set automatically.","For standalone-built nodes, call the document's adoption path (setOwnerDocument) — attaching the node to a document tree does this recursively.","Ensure the root of your detached subtree is created by a Document so ownership propagates down to all children.","If rehydrating nodes, re-run setOwnerDocument(document) on the root before mutating it."],"exampleFix":"// before: node built with new, no owner document\nconst el = new Element(\"div\");\nel.textContent = \"hi\"; // throws: no owner document\n\n// after: create via the document\nconst el = document.createElement(\"div\");\nel.textContent = \"hi\"; // ok","handlingStrategy":"validation","validationCode":"if (!node.ownerDocument && !(node instanceof Document)) {\n  node.setOwnerDocument(document); // adopt before mutating\n}\nnode.textContent = \"hi\"; // safe: owner document now exists","typeGuard":"function hasOwnerDocument(node: Node): boolean {\n  return node instanceof Document || node.ownerDocument != null;\n}","tryCatchPattern":"try {\n  node.appendChild(text);\n} catch (err) {\n  if (err instanceof Error && err.message === \"This node has no owner document\") {\n    node.setOwnerDocument(document);\n    node.appendChild(text); // retry after adoption\n  } else {\n    throw err;\n  }\n}","preventionTips":["Create all nodes via document.createElement/createTextNode rather than `new Element(...)`/`new Text(...)`.","Run setOwnerDocument on the root of any standalone/rehydrated subtree before mutating it.","Attach detached subtrees to the document before setting textContent or appending string children."],"tags":["dom","owner-document","node-creation"],"backgroundTag":"dom-missing-owner-document","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}