Skip to content

Commit

Permalink
fix(cdk): correct assert for svg and document element
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode committed May 4, 2022
1 parent 17658ab commit eb2e745
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions projects/cdk/classes/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@ export const tuiAssert = {

type PossibleNode = Node | Element | EventTarget | null;

interface DefaultView {
Element: typeof Node;
HTMLElement: typeof Node;
HTMLDocument: typeof Node;
}

export function tuiAssertIsHTMLElement(node?: PossibleNode): asserts node is HTMLElement {
const document = (node as Node)?.ownerDocument;
const defaultView = document?.defaultView as unknown as {HTMLElement: typeof Node};
const isHTMLElement = node instanceof defaultView?.HTMLElement;
const defaultView = document?.defaultView as unknown as DefaultView;
const isHTMLElement =
node instanceof defaultView?.HTMLElement ||
node instanceof defaultView?.Element ||
node instanceof defaultView?.HTMLDocument;

tuiAssert.assert(isHTMLElement, 'Node is not an HTMLElement');
tuiAssert.assert(isHTMLElement, 'Node is not an Element');
}

0 comments on commit eb2e745

Please sign in to comment.