diff --git a/packages/dom/README.md b/packages/dom/README.md index c1f86f4010a843..c6b0cb07cba203 100644 --- a/packages/dom/README.md +++ b/packages/dom/README.md @@ -138,11 +138,11 @@ Given a DOM node, finds the closest scrollable container node. _Parameters_ -- _node_ `Element`: Node from which to start. +- _node_ `Element | null`: Node from which to start. _Returns_ -- `?Element`: Scrollable container node, if found. +- `Element | undefined`: Scrollable container node, if found. # **insertAfter** diff --git a/packages/dom/src/dom/get-scroll-container.js b/packages/dom/src/dom/get-scroll-container.js index 995db7431db826..0c5ac53cab113d 100644 --- a/packages/dom/src/dom/get-scroll-container.js +++ b/packages/dom/src/dom/get-scroll-container.js @@ -6,13 +6,13 @@ import getComputedStyle from './get-computed-style'; /** * Given a DOM node, finds the closest scrollable container node. * - * @param {Element} node Node from which to start. + * @param {Element | null} node Node from which to start. * - * @return {?Element} Scrollable container node, if found. + * @return {Element | undefined} Scrollable container node, if found. */ export default function getScrollContainer( node ) { if ( ! node ) { - return; + return undefined; } // Scrollable if scrollable height exceeds displayed... @@ -25,5 +25,5 @@ export default function getScrollContainer( node ) { } // Continue traversing - return getScrollContainer( node.parentNode ); + return getScrollContainer( /** @type {Element} */ ( node.parentNode ) ); } diff --git a/packages/dom/src/dom/input-field-has-uncollapsed-selection.js b/packages/dom/src/dom/input-field-has-uncollapsed-selection.js index 41835fb1899d7e..05f7fe1d2ab7ff 100644 --- a/packages/dom/src/dom/input-field-has-uncollapsed-selection.js +++ b/packages/dom/src/dom/input-field-has-uncollapsed-selection.js @@ -22,7 +22,10 @@ export default function inputFieldHasUncollapsedSelection( element ) { return false; } try { - const { selectionStart, selectionEnd } = element; + const { + selectionStart, + selectionEnd, + } = /** @type {HTMLInputElement | HTMLTextAreaElement} */ ( element ); return selectionStart !== null && selectionStart !== selectionEnd; } catch ( error ) { diff --git a/packages/dom/src/dom/replace-tag.js b/packages/dom/src/dom/replace-tag.js index dc70cf922c64a6..9bb74e9a631d51 100644 --- a/packages/dom/src/dom/replace-tag.js +++ b/packages/dom/src/dom/replace-tag.js @@ -1,3 +1,8 @@ +/** + * Internal dependencies + */ +import { assertIsDefined } from '../utils/assert-is-defined'; + /** * Replaces the given node with a new node with the given tag name. * @@ -13,6 +18,7 @@ export default function replaceTag( node, tagName ) { newNode.appendChild( node.firstChild ); } + assertIsDefined( node.parentNode, 'node.parentNode' ); node.parentNode.replaceChild( newNode, node ); return newNode; diff --git a/packages/dom/src/dom/replace.js b/packages/dom/src/dom/replace.js index ab9b05df3a08a8..e0591e464ed60a 100644 --- a/packages/dom/src/dom/replace.js +++ b/packages/dom/src/dom/replace.js @@ -1,6 +1,7 @@ /** * Internal dependencies */ +import { assertIsDefined } from '../utils/assert-is-defined'; import insertAfter from './insert-after'; import remove from './remove'; @@ -12,6 +13,7 @@ import remove from './remove'; * @return {void} */ export default function replace( processedNode, newNode ) { + assertIsDefined( processedNode.parentNode, 'processedNode.parentNode' ); insertAfter( newNode, processedNode.parentNode ); remove( processedNode ); } diff --git a/packages/dom/src/dom/wrap.js b/packages/dom/src/dom/wrap.js index 725a71f6c4b928..5cc9a7af243831 100644 --- a/packages/dom/src/dom/wrap.js +++ b/packages/dom/src/dom/wrap.js @@ -1,3 +1,8 @@ +/** + * Internal dependencies + */ +import { assertIsDefined } from '../utils/assert-is-defined'; + /** * Wraps the given node with a new node with the given tag name. * @@ -5,6 +10,7 @@ * @param {Element} referenceNode The node to wrap. */ export default function wrap( newNode, referenceNode ) { + assertIsDefined( referenceNode.parentNode, 'referenceNode.parentNode' ); referenceNode.parentNode.insertBefore( newNode, referenceNode ); newNode.appendChild( referenceNode ); } diff --git a/packages/dom/tsconfig.json b/packages/dom/tsconfig.json index 2c690ac4e0e7d7..8e47a4759dd01c 100644 --- a/packages/dom/tsconfig.json +++ b/packages/dom/tsconfig.json @@ -15,7 +15,9 @@ "src/dom/get-computed-style.js", "src/dom/get-range-height.js", "src/dom/get-rectangle-from-range.js", + "src/dom/get-scroll-container.js", "src/dom/hidden-caret-range-from-point.js", + "src/dom/input-field-has-uncollapsed-selection.js", "src/dom/is-edge.js", "src/dom/is-empty.js", "src/dom/is-element.js", @@ -24,13 +26,17 @@ "src/dom/is-number-input.js", "src/dom/is-text-field.js", "src/dom/is-selection-forward.js", + "src/dom/is-vertical-edge.js", "src/dom/insert-after.js", "src/dom/remove-invalid-html.js", "src/dom/place-caret-at-horizontal-edge.js", "src/dom/place-caret-at-vertical-edge.js", "src/dom/remove.js", "src/dom/strip-html.js", + "src/dom/replace-tag.js", + "src/dom/replace.js", "src/dom/unwrap.js", + "src/dom/wrap.js", "src/utils/**/*", "src/focusable.js", "src/phrasing-content.js",