@a11y-ngx/dom-helper v1.0.0
DOM Helper
Another set of tools to validate DOM stuff.
This library was generated with Angular CLI version 12.2.0.
Index
- Installation
- Methods
- The
getStyles()Method - The
getStyle()Method - The
isVisible()Method - The
isAriaHidden()Method - The
isInert()Method - The
isBoolean()Method - The
isNumeric()Method - The
hasAttribute()Method - The
getAttributeValue()Method - The
getAttributeNumericValue()Method - The
getBooleanValue()Method - The
getNumericValue()Method - The
tabbableElements()Method
- The
Installation
Install npm package:
npm install @a11y-ngx/dom-helper --saveImport
DOMHelperServiceinto your typescript file:
import { DOMHelperService } from '@a11y-ngx/dom-helper';
constructor(private DOMHelper: DOMHelperService) {}Methods
The getStyles() Method
To get all the element's computed styles.
Accepts two parameters:
elementof typeHTMLElement.pseudoElement(optional) of typestring.
this.DOMHelper.getStyles(element, '::before');The getStyle() Method
To get the property's value from the element's computed styles.
Accepts three parameters:
elementof typeHTMLElement.propertyof typekeyof CSSStyleDeclaration.pseudoElement(optional) of typestring.
this.DOMHelper.getStyle(element, 'visibility'); // => 'visible' / 'hidden' / etc.The getStyleBefore() Method
A shortcut of the getStyle() method to get the property's ::before pseudo element computed style.
Accepts two parameters:
elementof typeHTMLElement.propertyof typekeyof CSSStyleDeclaration.
this.DOMHelper.getStyleBefore(element, 'position'); // => 'absolute' / 'relative' / etc.The getStyleAfter() Method
A shortcut of the getStyle() method to get the property's ::after pseudo element computed style.
Accepts two parameters:
elementof typeHTMLElement.propertyof typekeyof CSSStyleDeclaration.
this.DOMHelper.getStyleAfter(element, 'zIndex'); // => '100' / etc.The isVisible() Method
To check for the element's visibility.
It will check for:
- The absence of
aria-hiddenattribute set totrue. - The absence of
inertattribute. - The element has size (width and height).
- The element has
visibilityset to'visible'.
Accepts a single parameter element of type HTMLElement and returns a boolean.
this.DOMHelper.isVisible(element); // => true / falseThe isAriaHidden() Method
To check if the aria-hidden attribute is set to true.
Accepts a single parameter element of type HTMLElement and returns a boolean.
this.DOMHelper.isAriaHidden(element); // => true / falseThe isInert() Method
To check if the inert attribute is present.
Accepts a single parameter element of type HTMLElement and returns a boolean.
this.DOMHelper.isInert(element); // => true / falseThe isBoolean() Method
To check if the given value is boolean or not.
Accepts a single parameter value of type unknown and returns a boolean.
this.DOMHelper.isBoolean('value'); // => false
this.DOMHelper.isBoolean(undefined); // => false
this.DOMHelper.isBoolean(0); // => false
this.DOMHelper.isBoolean(!0); // => true
this.DOMHelper.isBoolean('true'); // => true
this.DOMHelper.isBoolean('false'); // => true
this.DOMHelper.isBoolean(false); // => trueThe isNumeric() Method
To check if the given value is numeric or not.
Accepts a single parameter value of type unknown and returns a boolean.
this.DOMHelper.isNumeric(''); // => false
this.DOMHelper.isNumeric(NaN); // => false
this.DOMHelper.isNumeric('123,25'); // => false
this.DOMHelper.isNumeric(' 123 '); // => true
this.DOMHelper.isNumeric(' 123.25 '); // => true
this.DOMHelper.isNumeric(123); // => trueThe hasAttribute() Method
To check if the given attribute is present in the element.
Accepts two parameters:
elementof typeHTMLElement.attributeof typestring.
It will return a boolean confirming whether the attribute exists or not.
this.DOMHelper.hasAttribute(element, 'disabled'); // => true / falseThe getAttributeValue() Method
To get the value from an attribute.
Accepts two parameters:
elementof typeHTMLElement.attributeof typestring.
It will return the value as a string or null otherwise.
this.DOMHelper.getAttributeValue(element, 'type'); // => 'button' / 'checkbox' / etc.
this.DOMHelper.getAttributeValue(element, 'tabindex'); // => '-1' / nullThe getAttributeNumericValue() Method
To check and get the numeric value from an attribute.
Accepts three parameters:
elementof typeHTMLElement.attributeof typestring.decimals(optional) of typenumber. When unset, it will keep all available decimals.
It will make use of the isNumeric() method to verify that the value is numeric and return it as a number or null otherwise.
this.DOMHelper.getAttributeNumericValue(element, 'tabindex'); // => -1 / nullThe getBooleanValue() Method
To check and get the boolean value.
Accepts a single parameter value of type unknown.
It will make use of the isBoolean() method to verify that the value is boolean and return it as a boolean or null otherwise.
this.DOMHelper.getBooleanValue('value'); // => null
this.DOMHelper.getBooleanValue(undefined); // => null
this.DOMHelper.getBooleanValue(0); // => null
this.DOMHelper.getBooleanValue('false'); // => false
this.DOMHelper.getBooleanValue(false); // => false
this.DOMHelper.getBooleanValue(' true '); // => trueThe getNumericValue() Method
To check and get the numeric value.
Accepts two parameters:
valueof typeunknown.decimals(optional) of typenumber. When unset, it will keep all available decimals.
It will make use of the isNumeric() method to verify that the value is numeric and return it as a number or null otherwise.
this.DOMHelper.getNumericValue(''); // => null
this.DOMHelper.getNumericValue('value'); // => null
this.DOMHelper.getNumericValue(NaN); // => null
this.DOMHelper.getNumericValue(true); // => null
this.DOMHelper.getNumericValue(' 123 '); // => 123
this.DOMHelper.getNumericValue('123.25'); // => 123.25
this.DOMHelper.getNumericValue('123.25', 1); // => 123.3 // rounds up
this.DOMHelper.getNumericValue('123.99', 0); // => 124 // rounds up
this.DOMHelper.getNumericValue(456); // => 456The tabbableElements() Method
Check and returns all tabbable and visible elements within the given host.
Accepts a single parameter hostElement of type HTMLElement and returns an array of HTMLElement.
It will retrieve all possible tabbable elements and validate their visibility using the isVisible() method.
NOTE: The method will check for and return:
- All anchor elements with an
hrefattribute.- All area elements with an
hrefattribute.- All form elements that:
- Are
<input>(except typehidden) and notdisabled.- Are
<select>and notdisabled.- Are
<textarea>and notdisabled.- Are
<button>and notdisabled.- All elements with
tabindexattribute that:
- Does not have the attribute empty.
- Does not start the attribute with an hyphen (negative values).
- All elements with
contenteditableattribute that:
- Does have the attribute empty (means
trueby default).- Does not have the attribute with value set on
false.Considerations for the
<fieldset>element in forms:Fieldsets can be disabled, which means that all its children elements will be disabled as well, so:
- It will ignore all children elements from those
fieldsetthat aredisabled.- Nested
fieldsetwill follow the same rule:
- If the parent
fieldsetis notdisabledbut the nested one is, it will ignore all children elements from the nestedfieldsetonly.- If the parent
fieldsetisdisabledbut the nested one is not, it will ignore all elements from the parent down.
this.DOMHelper.tabbableElements(hostElement); // => [button, input, select, etc.]1 year ago