Skip to content

Commit

Permalink
dom: Type the remaining files (#30841)
Browse files Browse the repository at this point in the history
* dom: Type the remaining files

* Wide fn signature to avoid type casts

* Remove duplicate dependencies comment block

* Remove vertical alignment
  • Loading branch information
sarayourfriend authored Apr 19, 2021
1 parent b11bc8d commit 619b2fe
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 52 deletions.
4 changes: 4 additions & 0 deletions packages/dom/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### New Feature

- Export type definitions

## 2.17.0 (2021-03-17)

## 2.11.0 (2020-06-15)
Expand Down
4 changes: 2 additions & 2 deletions packages/dom/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ _Parameters_
_Returns_
- `?Node`: Offset parent.
- `Node | null`: Offset parent.
<a name="getPhrasingContentSchema" href="#getPhrasingContentSchema">#</a> **getPhrasingContentSchema**
Expand Down Expand Up @@ -178,7 +178,7 @@ Returns true if there is no possibility of selection.
_Parameters_
- _element_ `Element`: The element to check.
- _element_ `HTMLElement`: The element to check.
_Returns_
Expand Down
1 change: 1 addition & 0 deletions packages/dom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"main": "build/index.js",
"module": "build-module/index.js",
"react-native": "src/index",
"types": "build-types",
"sideEffects": false,
"dependencies": {
"@babel/runtime": "^7.13.10",
Expand Down
3 changes: 2 additions & 1 deletion packages/dom/src/dom/document-has-uncollapsed-selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import inputFieldHasUncollapsedSelection from './input-field-has-uncollapsed-sel
export default function documentHasUncollapsedSelection( doc ) {
return (
documentHasTextSelection( doc ) ||
inputFieldHasUncollapsedSelection( doc.activeElement )
( !! doc.activeElement &&
inputFieldHasUncollapsedSelection( doc.activeElement ) )
);
}
13 changes: 9 additions & 4 deletions packages/dom/src/dom/get-offset-parent.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import getComputedStyle from './get-computed-style';
*
* @param {Node} node Node from which to find offset parent.
*
* @return {?Node} Offset parent.
* @return {Node | null} Offset parent.
*/
export default function getOffsetParent( node ) {
// Cannot retrieve computed style or offset parent only anything other than
// an element node, so find the closest element node.
let closestElement;
while ( ( closestElement = node.parentNode ) ) {
while ( ( closestElement = /** @type {Node} */ ( node.parentNode ) ) ) {
if ( closestElement.nodeType === closestElement.ELEMENT_NODE ) {
break;
}
Expand All @@ -30,9 +30,14 @@ export default function getOffsetParent( node ) {

// If the closest element is already positioned, return it, as offsetParent
// does not otherwise consider the node itself.
if ( getComputedStyle( closestElement ).position !== 'static' ) {
if (
getComputedStyle( /** @type {Element} */ ( closestElement ) )
.position !== 'static'
) {
return closestElement;
}

return closestElement.offsetParent;
// offsetParent is undocumented/draft
return /** @type {Node & { offsetParent: Node }} */ ( closestElement )
.offsetParent;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import isNumberInput from './is-number-input';
*
* See: https://developer.mozilla.org/en-US/docs/Web/API/Window/getSelection#Related_objects.
*
* @param {HTMLElement} element The HTML element.
* @param {Element} element The HTML element.
*
* @return {boolean} Whether the input/textareaa element has some "selection".
*/
Expand Down
8 changes: 4 additions & 4 deletions packages/dom/src/dom/is-edge.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import isInputOrTextArea from './is-input-or-text-area';
* horizontal position by default. Set `onlyVertical` to true to check only
* vertically.
*
* @param {Element} container Focusable element.
* @param {boolean} isReverse Set to true to check left, false to check right.
* @param {boolean} onlyVertical Set to true to check only vertical position.
* @param {Element} container Focusable element.
* @param {boolean} isReverse Set to true to check left, false to check right.
* @param {boolean} [onlyVertical=false] Set to true to check only vertical position.
*
* @return {boolean} True if at the edge, false if not.
*/
export default function isEdge( container, isReverse, onlyVertical ) {
export default function isEdge( container, isReverse, onlyVertical = false ) {
if ( isInputOrTextArea( container ) ) {
if ( container.selectionStart !== container.selectionEnd ) {
return false;
Expand Down
14 changes: 9 additions & 5 deletions packages/dom/src/dom/is-entirely-selected.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/**
* External dependencies
* Internal dependencies
*/
import { includes } from 'lodash';
import { assertIsDefined } from '../utils/assert-is-defined';
import isInputOrTextArea from './is-input-or-text-area';

/**
* Check whether the contents of the element have been entirely selected.
* Returns true if there is no possibility of selection.
*
* @param {Element} element The element to check.
* @param {HTMLElement} element The element to check.
*
* @return {boolean} True if entirely selected, false if not.
*/
export default function isEntirelySelected( element ) {
if ( includes( [ 'INPUT', 'TEXTAREA' ], element.nodeName ) ) {
if ( isInputOrTextArea( element ) ) {
return (
element.selectionStart === 0 &&
element.value.length === element.selectionEnd
Expand All @@ -25,7 +26,9 @@ export default function isEntirelySelected( element ) {

const { ownerDocument } = element;
const { defaultView } = ownerDocument;
assertIsDefined( defaultView, 'defaultView' );
const selection = defaultView.getSelection();
assertIsDefined( selection, 'selection' );
const range = selection.rangeCount ? selection.getRangeAt( 0 ) : null;

if ( ! range ) {
Expand All @@ -44,9 +47,10 @@ export default function isEntirelySelected( element ) {
}

const lastChild = element.lastChild;
assertIsDefined( lastChild, 'lastChild' );
const lastChildContentLength =
lastChild.nodeType === lastChild.TEXT_NODE
? lastChild.data.length
? /** @type {Text} */ ( lastChild ).data.length
: lastChild.childNodes.length;

return (
Expand Down
36 changes: 1 addition & 35 deletions packages/dom/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,6 @@
"types": [ "gutenberg-env" ]
},
"include": [
"src/data-transfer.js",
"src/dom/caret-range-from-point.js",
"src/dom/clean-node-list.js",
"src/dom/compute-caret-rect.js",
"src/dom/document-has-selection.js",
"src/dom/document-has-text-selection.js",
"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",
"src/dom/is-html-input-element.js",
"src/dom/is-input-or-text-area.js",
"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",
"src/tabbable.js"
"src/**/*"
]
}

0 comments on commit 619b2fe

Please sign in to comment.