-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create helpers for working with the element as part esl-util DO…
…M helpers
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import {getWindow} from './window'; | ||
|
||
/** | ||
* Get an object containing the values of all CSS properties of an element. | ||
* @param element - element for which to get the computed style | ||
* */ | ||
export function getComputedStyle(element: Element): CSSStyleDeclaration { | ||
return getWindow(element).getComputedStyle(element); | ||
} | ||
|
||
/** | ||
* Get the Element that is the root element of the document. | ||
* @param element - element for which to get the document element | ||
* */ | ||
export function getDocumentElement(element: Element | Window): Element { | ||
return ((element instanceof Window | ||
? element.document | ||
: element.ownerDocument) || window.document).documentElement; | ||
} | ||
|
||
/** | ||
* Get the name of node. | ||
* @param element - element for which to get the name | ||
*/ | ||
export function getNodeName(element?: Node | Window): string { | ||
return element && !(element instanceof Window)? (element.nodeName).toLowerCase() : ''; | ||
} | ||
|
||
/** | ||
* Get the parent of the specified element in the DOM tree. | ||
* @param element - element for which to get the parent | ||
*/ | ||
export function getParentNode(element: Element | ShadowRoot): Node { | ||
if (getNodeName(element) === 'html') { | ||
return element; | ||
} | ||
|
||
return (element instanceof ShadowRoot | ||
? element.host | ||
: element.assignedSlot || element.parentNode) || getDocumentElement(element as Element); | ||
} |