Skip to content

Commit

Permalink
Move isHTMLElement into core (#4977)
Browse files Browse the repository at this point in the history
  • Loading branch information
zurfyx committed Sep 8, 2023
1 parent 29f77c2 commit aa1e99a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
21 changes: 3 additions & 18 deletions packages/lexical-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ import {
DEPRECATED_$isGridSelection,
EditorState,
ElementNode,
isHTMLAnchorElement,
isHTMLElement,
Klass,
LexicalEditor,
LexicalNode,
} from 'lexical';
import invariant from 'shared/invariant';

export {$splitNode};
export {$splitNode, isHTMLAnchorElement, isHTMLElement};

export type DFSNode = Readonly<{
depth: number;
Expand Down Expand Up @@ -495,23 +497,6 @@ export function $wrapNodeInElement(
return elementNode;
}

/**
* @param x - The element being tested
* @returns Returns true if x is an HTML anchor tag, false otherwise
*/
export function isHTMLAnchorElement(x: Node): x is HTMLAnchorElement {
return isHTMLElement(x) && x.tagName === 'A';
}

/**
* @param x - The element being testing
* @returns Returns true if x is an HTML element, false otherwise.
*/
export function isHTMLElement(x: Node | EventTarget): x is HTMLElement {
// @ts-ignore-next-line - strict check on nodeType here should filter out non-Element EventTarget implementors
return x.nodeType === 1;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type ObjectKlass<T> = new (...args: any[]) => T;

Expand Down
17 changes: 17 additions & 0 deletions packages/lexical/src/LexicalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1573,3 +1573,20 @@ export function $getChildrenRecursively(node: LexicalNode): Array<LexicalNode> {
}
return nodes;
}

/**
* @param x - The element being tested
* @returns Returns true if x is an HTML anchor tag, false otherwise
*/
export function isHTMLAnchorElement(x: Node): x is HTMLAnchorElement {
return isHTMLElement(x) && x.tagName === 'A';
}

/**
* @param x - The element being testing
* @returns Returns true if x is an HTML element, false otherwise.
*/
export function isHTMLElement(x: Node | EventTarget): x is HTMLElement {
// @ts-ignore-next-line - strict check on nodeType here should filter out non-Element EventTarget implementors
return x.nodeType === 1;
}
2 changes: 2 additions & 0 deletions packages/lexical/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ export {
$setSelection,
$splitNode,
getNearestEditorFromDOMNode,
isHTMLAnchorElement,
isHTMLElement,
isSelectionCapturedInDecoratorInput,
isSelectionWithinEditor,
} from './LexicalUtils';
Expand Down
8 changes: 5 additions & 3 deletions packages/lexical/src/nodes/LexicalParagraphNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import type {
} from './LexicalElementNode';
import type {RangeSelection} from 'lexical';

import {isHTMLElement} from '@lexical/utils';

import {$applyNodeReplacement, getCachedClassNameArray} from '../LexicalUtils';
import {
$applyNodeReplacement,
getCachedClassNameArray,
isHTMLElement,
} from '../LexicalUtils';
import {ElementNode} from './LexicalElementNode';
import {$isTextNode} from './LexicalTextNode';

Expand Down
2 changes: 1 addition & 1 deletion packages/lexical/src/nodes/LexicalTextNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import type {
RangeSelection,
} from '../LexicalSelection';

import {isHTMLElement} from '@lexical/utils';
import {IS_FIREFOX} from 'shared/environment';
import invariant from 'shared/invariant';

Expand Down Expand Up @@ -65,6 +64,7 @@ import {
$setCompositionKey,
getCachedClassNameArray,
internalMarkSiblingsAsDirty,
isHTMLElement,
toggleTextFormatType,
} from '../LexicalUtils';
import {$createLineBreakNode} from './LexicalLineBreakNode';
Expand Down

0 comments on commit aa1e99a

Please sign in to comment.