3.53.0 • Published 13 days ago

@wordpress/dom v3.53.0

Weekly downloads
20,235
License
GPL-2.0-or-later
Repository
github
Last release
13 days ago

DOM

DOM utilities module for WordPress.

Installation

Install the module

npm install @wordpress/dom --save

API

computeCaretRect

Get the rectangle for the selection in a container.

Parameters

  • win Window: The window of the selection.

Returns

  • DOMRect | null: The rectangle.

documentHasSelection

Check whether the current document has a selection. This includes focus in input fields, textareas, and general rich-text selection.

Parameters

  • doc Document: The document to check.

Returns

  • boolean: True if there is selection, false if not.

documentHasTextSelection

Check whether the current document has selected text. This applies to ranges of text in the document, and not selection inside <input> and <textarea> elements.

See: https://developer.mozilla.org/en-US/docs/Web/API/Window/getSelection#Related_objects.

Parameters

  • doc Document: The document to check.

Returns

  • boolean: True if there is selection, false if not.

documentHasUncollapsedSelection

Check whether the current document has any sort of (uncollapsed) selection. This includes ranges of text across elements and any selection inside textual <input> and <textarea> elements.

Parameters

  • doc Document: The document to check.

Returns

  • boolean: Whether there is any recognizable text selection in the document.

focus

Object grouping focusable and tabbable utils under the keys with the same name.

getFilesFromDataTransfer

Gets all files from a DataTransfer object.

Parameters

  • dataTransfer DataTransfer: DataTransfer object to inspect.

Returns

  • File[]: An array containing all files.

getOffsetParent

Returns the closest positioned element, or null under any of the conditions of the offsetParent specification. Unlike offsetParent, this function is not limited to HTMLElement and accepts any Node (e.g. Node.TEXT_NODE).

Related

Parameters

  • node Node: Node from which to find offset parent.

Returns

  • Node | null: Offset parent.

getPhrasingContentSchema

Get schema of possible paths for phrasing content.

Related

Parameters

  • context [string]: Set to "paste" to exclude invisible elements and sensitive data.

Returns

  • Partial<ContentSchema>: Schema.

getRectangleFromRange

Get the rectangle of a given Range. Returns null if no suitable rectangle can be found.

Parameters

  • range Range: The range.

Returns

  • DOMRect?: The rectangle.

getScrollContainer

Given a DOM node, finds the closest scrollable container node or the node itself, if scrollable.

Parameters

  • node Element | null: Node from which to start.
  • direction ?string: Direction of scrollable container to search for ('vertical', 'horizontal', 'all'). Defaults to 'vertical'.

Returns

  • Element | undefined: Scrollable container node, if found.

insertAfter

Given two DOM nodes, inserts the former in the DOM as the next sibling of the latter.

Parameters

  • newNode Node: Node to be inserted.
  • referenceNode Node: Node after which to perform the insertion.

Returns

  • void:

isEmpty

Recursively checks if an element is empty. An element is not empty if it contains text or contains elements with attributes such as images.

Parameters

  • element Element: The element to check.

Returns

  • boolean: Whether or not the element is empty.

isEntirelySelected

Check whether the contents of the element have been entirely selected. Returns true if there is no possibility of selection.

Parameters

  • element HTMLElement: The element to check.

Returns

  • boolean: True if entirely selected, false if not.

isFormElement

Detects if element is a form element.

Parameters

  • element Element: The element to check.

Returns

  • boolean: True if form element and false otherwise.

isHorizontalEdge

Check whether the selection is horizontally at the edge of the container.

Parameters

  • container HTMLElement: Focusable element.
  • isReverse boolean: Set to true to check left, false for right.

Returns

  • boolean: True if at the horizontal edge, false if not.

isNumberInput

Check whether the given element is an input field of type number.

Parameters

  • node Node: The HTML node.

Returns

  • node is HTMLInputElement: True if the node is number input.

isPhrasingContent

Find out whether or not the given node is phrasing content.

Related

Parameters

  • node Node: The node to test.

Returns

  • boolean: True if phrasing content, false if not.

isRTL

Whether the element's text direction is right-to-left.

Parameters

  • element Element: The element to check.

Returns

  • boolean: True if rtl, false if ltr.

isTextContent

Parameters

  • node Node:

Returns

  • boolean: Node is text content

isTextField

Check 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.

See: https://html.spec.whatwg.org/#textFieldSelection

Parameters

  • node Node: The HTML element.

Returns

  • node is HTMLElement: True if the element is an text field, false if not.

isVerticalEdge

Check whether the selection is vertically at the edge of the container.

Parameters

  • container HTMLElement: Focusable element.
  • isReverse boolean: Set to true to check top, false for bottom.

Returns

  • boolean: True if at the vertical edge, false if not.

placeCaretAtHorizontalEdge

Places the caret at start or end of a given element.

Parameters

  • container HTMLElement: Focusable element.
  • isReverse boolean: True for end, false for start.

placeCaretAtVerticalEdge

Places the caret at the top or bottom of a given element.

Parameters

  • container HTMLElement: Focusable element.
  • isReverse boolean: True for bottom, false for top.
  • rect [DOMRect]: The rectangle to position the caret with.

remove

Given a DOM node, removes it from the DOM.

Parameters

  • node Node: Node to be removed.

Returns

  • void:

removeInvalidHTML

Given a schema, unwraps or removes nodes, attributes and classes on HTML.

Parameters

  • HTML string: The HTML to clean up.
  • schema import('./clean-node-list').Schema: Schema for the HTML.
  • inline boolean: Whether to clean for inline mode.

Returns

  • string: The cleaned up HTML.

replace

Given two DOM nodes, replaces the former with the latter in the DOM.

Parameters

  • processedNode Element: Node to be removed.
  • newNode Element: Node to be inserted in its place.

Returns

  • void:

replaceTag

Replaces the given node with a new node with the given tag name.

Parameters

  • node Element: The node to replace
  • tagName string: The new tag name.

Returns

  • Element: The new node.

safeHTML

Strips scripts and on* attributes from HTML.

Parameters

  • html string: HTML to sanitize.

Returns

  • string: The sanitized HTML.

unwrap

Unwrap the given node. This means any child nodes are moved to the parent.

Parameters

  • node Node: The node to unwrap.

Returns

  • void:

wrap

Wraps the given node with a new node with the given tag name.

Parameters

  • newNode Element: The node to insert.
  • referenceNode Element: The node to wrap.

Contributing to this package

This is an individual package that's part of the Gutenberg project. The project is organized as a monorepo. It's made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to npm and used by WordPress as well as other software projects.

To find out more about contributing to this package or Gutenberg as a whole, please read the project's main contributor guide.

3.53.0

13 days ago

3.52.0

26 days ago

3.51.1

28 days ago

3.51.0

1 month ago

3.50.0

2 months ago

3.49.0

2 months ago

3.48.0

3 months ago

3.43.0

6 months ago

3.47.0

4 months ago

3.40.0

7 months ago

3.40.1

7 months ago

3.44.0

5 months ago

3.37.0

9 months ago

3.41.0

7 months ago

3.45.0

5 months ago

3.38.0

8 months ago

3.42.13

4 months ago

3.42.12

4 months ago

3.42.11

5 months ago

3.26.2

5 months ago

3.42.10

5 months ago

3.42.0

6 months ago

3.42.1

6 months ago

3.42.2

6 months ago

3.42.3

6 months ago

3.46.0

4 months ago

3.42.4

5 months ago

3.35.2

5 months ago

3.39.0

7 months ago

3.42.5

5 months ago

3.42.6

5 months ago

3.42.7

5 months ago

3.42.8

5 months ago

3.42.9

5 months ago

3.36.0

9 months ago

3.35.0

10 months ago

3.33.0

10 months ago

3.35.1

9 months ago

3.34.0

10 months ago

3.31.0

11 months ago

3.28.0

1 year ago

3.32.0

11 months ago

3.29.0

1 year ago

3.30.0

12 months ago

3.20.0

1 year ago

3.24.0

1 year ago

3.17.0

2 years ago

3.17.2

1 year ago

3.17.1

1 year ago

3.6.1

2 years ago

3.6.0

2 years ago

3.12.0

2 years ago

3.21.0

1 year ago

3.14.0

2 years ago

3.18.0

1 year ago

3.13.0

2 years ago

3.5.0

2 years ago

3.25.0

1 year ago

3.22.0

1 year ago

3.15.0

2 years ago

3.9.0

2 years ago

3.19.0

1 year ago

3.4.0

2 years ago

3.8.0

2 years ago

3.10.0

2 years ago

3.4.1

2 years ago

3.26.0

1 year ago

3.26.1

1 year ago

3.23.0

1 year ago

3.16.0

2 years ago

3.11.0

2 years ago

3.7.0

2 years ago

3.27.0

1 year ago

3.2.6

2 years ago

3.2.5

2 years ago

3.2.7

2 years ago

3.3.1

2 years ago

3.3.0

2 years ago

3.3.2

2 years ago

3.2.4

2 years ago

2.0.9

3 years ago

2.8.1

3 years ago

3.2.3

3 years ago

2.13.2

3 years ago

2.15.1

3 years ago

2.5.3

3 years ago

2.16.3

3 years ago

2.2.8

3 years ago

3.1.5

3 years ago

3.2.2

3 years ago

3.2.1

3 years ago

3.2.0

3 years ago

3.1.4

3 years ago

3.1.2

3 years ago

3.1.1

3 years ago

3.0.0

3 years ago

3.1.0

3 years ago

2.18.0

3 years ago

2.17.2

3 years ago

2.17.1

3 years ago

2.17.0

3 years ago

2.16.2

3 years ago

2.16.1-next.0

3 years ago

2.16.1

3 years ago

2.16.0

3 years ago

2.15.0

3 years ago

2.14.0

4 years ago

2.13.1

4 years ago

2.13.0

4 years ago

2.12.0

4 years ago

2.11.1-rc.0

4 years ago

2.11.0

4 years ago

2.10.0

4 years ago

2.9.0

4 years ago

2.8.0

4 years ago

2.7.0

4 years ago

2.6.0

4 years ago

2.5.2

4 years ago

2.5.1

4 years ago

2.5.0

5 years ago

2.4.0

5 years ago

2.3.0

5 years ago

2.2.5

5 years ago

2.2.4

5 years ago

2.2.3

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.0

5 years ago

2.0.8

5 years ago

2.0.7

5 years ago

2.0.6

5 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

1.0.0-alpha.3

6 years ago

1.0.0-alpha.2

6 years ago

1.0.0-alpha.1

6 years ago

1.0.0-alpha.0

6 years ago