DOM
The@wordpress/dom package provides a comprehensive set of utilities for working with the DOM in WordPress. It includes functions for selection management, element manipulation, focus handling, and HTML sanitization.
Installation
You can install the package using npm:Selection Utilities
documentHasSelection( doc )
Checks whether the current document has a selection, including focus in input fields, textareas, and general rich-text selection.The document to check.
True if there is selection, false if not.
documentHasTextSelection( doc )
Checks whether the current document has selected text. This applies to ranges of text in the document, not selection inside<input> and <textarea> elements.
The document to check.
True if there is text selection, false if not.
documentHasUncollapsedSelection( doc )
Checks whether the current document has any sort of uncollapsed selection, including ranges of text and any selection inside textual inputs.The document to check.
Whether there is any recognizable text selection in the document.
isEntirelySelected( element )
Checks whether the contents of the element have been entirely selected.The element to check.
True if entirely selected, false if not.
isSelectionForward( selection )
Returns true if the given selection object is in the forward direction.Selection object to check.
Whether the selection is forward.
Caret Positioning
computeCaretRect( win )
Gets the rectangle for the selection in a container.The window of the selection.
The rectangle.
getRectangleFromRange( range )
Gets the rectangle of a given Range. Returnsnull if no suitable rectangle can be found. Use this instead of Range.getBoundingClientRect(), which is often broken for collapsed ranges.
The range.
The rectangle.
placeCaretAtHorizontalEdge( container, isReverse )
Places the caret at start or end of a given element.Focusable element.
True for end, false for start.
placeCaretAtVerticalEdge( container, isReverse, rect )
Places the caret at the top or bottom of a given element.Focusable element.
True for bottom, false for top.
The rectangle to position the caret with.
Edge Detection
isHorizontalEdge( container, isReverse )
Checks whether the selection is horizontally at the edge of the container.Focusable element.
Set to true to check left, false for right.
True if at the horizontal edge, false if not.
isVerticalEdge( container, isReverse )
Checks whether the selection is vertically at the edge of the container.Focusable element.
Set to true to check top, false for bottom.
True if at the vertical edge, false if not.
Element Type Detection
isFormElement( element )
Detects if element is a form element.The element to check.
True if form element, false otherwise.
isTextField( node )
Checks whether the given element is a text field, where text field is defined by the ability to select within the input, or that it is contenteditable.The HTML element.
True if the element is a text field, false if not.
isNumberInput( node )
Checks whether the given element is an input field of type number.The HTML node.
True if the node is number input.
Content Type Utilities
isPhrasingContent( node )
Finds out whether or not the given node is phrasing content.The node to test.
True if phrasing content, false if not.
isTextContent( node )
Checks if a node is text content.The node to check.
Whether the node is text content.
getPhrasingContentSchema( context )
Gets schema of possible paths for phrasing content.Set to “paste” to exclude invisible elements and sensitive data.
Schema object.
Element Manipulation
insertAfter( newNode, referenceNode )
Inserts a node in the DOM as the next sibling of another node.Node to be inserted.
Node after which to perform the insertion.
remove( node )
Removes a node from the DOM.Node to be removed.
replace( processedNode, newNode )
Replaces one DOM node with another.Node to be removed.
Node to be inserted in its place.
replaceTag( node, tagName )
Replaces a node with a new node with the given tag name.The node to replace.
The new tag name.
The new node.
wrap( newNode, referenceNode )
Wraps a node with a new node with the given tag name.The node to insert.
The node to wrap.
unwrap( node )
Unwraps the given node, moving any child nodes to the parent.The node to unwrap.
isEmpty( element )
Recursively checks if an element is empty. An element is not empty if it contains text or elements with attributes such as images.The element to check.
Whether or not the element is empty.
Layout Utilities
getOffsetParent( node )
Returns the closest positioned element, or null under any of the conditions of the offsetParent specification. Unlike offsetParent, this function accepts any Node (e.g., Node.TEXT_NODE).Node from which to find offset parent.
Offset parent.
getScrollContainer( node, direction )
Finds the closest scrollable container node or the node itself, if scrollable.Node from which to start.
Direction of scrollable container to search for (‘vertical’, ‘horizontal’, ‘all’).
Scrollable container node, if found.
isRTL( element )
Checks whether the element’s text direction is right-to-left.The element to check.
True if rtl, false if ltr.
HTML Sanitization
safeHTML( html )
Strips scripts and on* attributes from HTML.HTML to sanitize.
The sanitized HTML.
removeInvalidHTML( HTML, schema, inline )
Cleans up HTML by removing or unwrapping nodes, attributes, and classes based on a schema.The HTML to clean up.
Schema for the HTML.
Whether to clean for inline mode.
The cleaned up HTML.
File Handling
getFilesFromDataTransfer( dataTransfer )
Gets all files from a DataTransfer object.DataTransfer object to inspect.
An array containing all files.
Focus Management
focus
Object groupingfocusable and tabbable utilities under the keys with the same name.