Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move isHTMLElement into core #4977

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -155,6 +155,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