Skip to content

html-element

Obsidian-runtime-agnostic helpers for working with HTML elements.

Several helpers here mirror behavior that the Obsidian runtime injects onto the DOM prototypes (createEl/createDiv/createSpan/createSvg/createFragment, Node.prototype.instanceOf, Element.prototype.setText/setAttrs). To stay independent of the Obsidian runtime, that behavior is reimplemented here in standard DOM, ported from Obsidian’s own implementation in obsidian.asar/enhance.js. See the obsidian-versions project; revalidate the port when Obsidian changes enhance.js (there is a reminder in CLAUDE.md).

Interface Description
CreateHtmlElementInfo The element information createElAsync and its siblings apply: the element-type-agnostic subset of Obsidian’s DomElementInfo, declared here so this module needs no Obsidian typings. An Obsidian DomElementInfo value is assignable to it.
CreateSvgElementInfo The SVG element information createSvgAsync applies: the subset of Obsidian’s SvgElementInfo it supports, declared here so this module needs no Obsidian typings.
ValidatorElement A HTML element that can be validated.
Function Description
createDivAsync Creates a div asynchronously.
createElAsync Creates an element asynchronously.
createFragmentAsync Creates a DocumentFragment asynchronously.
createSpanAsync Creates a span asynchronously.
createSvgAsync Creates a svg asynchronously.
ensureLoaded Ensures that the given element is loaded.
getDocumentWindow Gets the window a document belongs to. A document is the one node whose ownerDocument is null, so the ownerDocument-chasing lookup used for ordinary nodes resolves a document to the wrong window. Reading defaultView off the document itself is what keeps a pop-out window’s coordinates measured against that window.
getZIndex Gets the z-index of the given element.
isElementVisibleInOffsetParent Checks if the element is visible in the offset parent.
isLoaded Checks if the element is loaded.
onAncestorScrollOrResize Adds an event listener to the ancestor nodes of the given node.
toPx Converts a number to a string with ‘px’ appended.
waitUntilConnected Waits until the given element is connected to the DOM.