dom.js
- Source
Members
(static, constant) $
Finds a single DOM element matching selector
within the optional context
of another DOM element (defaulting to document
).
- Source
(static, constant) $$
Finds a all DOM elements matching selector
within the optional context
of another DOM element (defaulting to document
).
- Source
Methods
(static) addClass(element, …classesToAdd) → {Element}
Add a class name to an element.
Name | Type | Attributes | Description |
---|---|---|---|
element | Element | Element to add class name to. | |
classesToAdd | string | <repeatable> | One or more class name to add. |
- Source
The DOM element with the added class name.
- Type:
- Element
(static) appendContent(el, content) → {Element}
Normalizes and appends content to an element.
Name | Type | Description |
---|---|---|
el | Element | Element to append normalized content to. |
content | module:dom~ContentDescriptor | A content descriptor value. |
- Source
The element with appended normalized content.
- Type:
- Element
(static) blockTextSelection()
Attempt to block the ability to select text.
- Source
(static) computedStyle(el, prop)
A safe getComputedStyle.
This is needed because in Firefox, if the player is loaded in an iframe with display:none
, then getComputedStyle
returns null
, so, we do a null-check to make sure that the player doesn't break in these cases.
Name | Type | Description |
---|---|---|
el | Element | The element you want the computed style of |
prop | string | The property name you want |
- Source
(static) copyStyleSheetsToWindow(win)
Copy document style sheets to another window.
Name | Type | Description |
---|---|---|
win | Window | The window element you want to copy the document style sheets to. |
- Source
(static) createEl(tagNameopt, propertiesopt, attributesopt, contentopt) → {Element}
Creates an element and applies properties, attributes, and inserts content.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
tagName | string | <optional> | 'div' | Name of tag to be created. |
properties | Object | <optional> | {} | Element properties to be applied. |
attributes | Object | <optional> | {} | Element attributes to be applied. |
content | module:dom~ContentDescriptor | <optional> | A content descriptor object. |
- Source
The element that was created.
- Type:
- Element
(static) emptyEl(el) → {Element}
Empties the contents of an element.
Name | Type | Description |
---|---|---|
el | Element | The element to empty children from |
- Source
The element with no children
- Type:
- Element
(static) findPosition(el) → {module:dom~module:dom~Position}
Get the position of an element in the DOM.
Uses getBoundingClientRect
technique from John Resig.
Name | Type | Description |
---|---|---|
el | Element | Element from which to get offset. |
- Source
The position of the element that was passed in.
- Type:
- module:dom~module:dom~Position
(static) getAttribute(el, attribute) → {string}
Get the value of an element's attribute.
Name | Type | Description |
---|---|---|
el | Element | A DOM element. |
attribute | string | Attribute to get the value of. |
- Source
The value of the attribute.
- Type:
- string
(static) getAttributes(tag) → {Object}
Get an element's attribute values, as defined on the HTML tag.
Attributes are not the same as properties. They're defined on the tag or with setAttribute.
Name | Type | Description |
---|---|---|
tag | Element | Element from which to get tag attributes. |
- Source
All attributes of the element. Boolean attributes will be true
or false
, others will be strings.
- Type:
- Object
(static) getBoundingClientRect(el) → {Object|undefined}
Identical to the native getBoundingClientRect
function, but ensures that the method is supported at all (it is in all browsers we claim to support) and that the element is in the DOM before continuing.
This wrapper function also shims properties which are not provided by some older browsers (namely, IE8).
Additionally, some browsers do not support adding properties to a ClientRect
/DOMRect
object; so, we shallow-copy it with the standard properties (except x
and y
which are not widely supported). This helps avoid implementations where keys are non-enumerable.
Name | Type | Description |
---|---|---|
el | Element | Element whose |
- Source
Always returns a plain object - or undefined
if it cannot.
- Type:
- Object |
undefined
(static) getPointerPosition(el, event) → {module:dom~module:dom~Coordinates}
Get the pointer position within an element.
The base on the coordinates are the bottom left of the element.
Name | Type | Description |
---|---|---|
el | Element | Element on which to get the pointer position on. |
event | Event | Event object. |
- Source
A coordinates object corresponding to the mouse position.
- Type:
- module:dom~module:dom~Coordinates
(static) hasClass(element, classToCheck) → {boolean}
Check if an element has a class name.
Name | Type | Description |
---|---|---|
element | Element | Element to check |
classToCheck | string | Class name to check for |
- Source
Throws an error if
classToCheck
has white space.- Type
- Error
Will be true
if the element has a class, false
otherwise.
- Type:
- boolean
(static) insertContent(el, content) → {Element}
Normalizes and inserts content into an element; this is identical to appendContent()
, except it empties the element first.
Name | Type | Description |
---|---|---|
el | Element | Element to insert normalized content into. |
content | module:dom~ContentDescriptor | A content descriptor value. |
- Source
The element with inserted normalized content.
- Type:
- Element
(static) isEl(value) → {boolean}
Determines, via duck typing, whether or not a value is a DOM element.
Name | Type | Description |
---|---|---|
value | * | The value to check. |
- Source
Will be true
if the value is a DOM element, false
otherwise.
- Type:
- boolean
(static) isInFrame() → {boolean}
Determines if the current DOM is embedded in an iframe.
- Source
Will be true
if the DOM is embedded in an iframe, false
otherwise.
- Type:
- boolean
(static) isReal() → {boolean}
Whether the current DOM interface appears to be real (i.e. not simulated).
- Source
Will be true
if the DOM appears to be real, false
otherwise.
- Type:
- boolean
(static) isSingleLeftClick(event) → {boolean}
Check if an event was a single left click.
Name | Type | Description |
---|---|---|
event | MouseEvent | Event object. |
- Source
Will be true
if a single left click, false
otherwise.
- Type:
- boolean
(static) isTextNode(value) → {boolean}
Determines, via duck typing, whether or not a value is a text node.
Name | Type | Description |
---|---|---|
value | * | Check if this value is a text node. |
- Source
Will be true
if the value is a text node, false
otherwise.
- Type:
- boolean
(static) normalizeContent(content) → {Array}
Normalizes content for eventual insertion into the DOM.
This allows a wide range of content definition methods, but helps protect from falling into the trap of simply writing to innerHTML
, which could be an XSS concern.
The content for an element can be passed in multiple types and combinations, whose behavior is as follows:
Name | Type | Description |
---|---|---|
content | module:dom~ContentDescriptor | A content descriptor value. |
- Source
All of the content that was passed in, normalized to an array of elements or text nodes.
- Type:
- Array
(static) prependTo(child, parent)
Insert an element as the first child node of another
Name | Type | Description |
---|---|---|
child | Element | Element to insert |
parent | Element | Element to insert child into |
- Source
(static) removeAttribute(el, attribute)
Remove an element's attribute.
Name | Type | Description |
---|---|---|
el | Element | A DOM element. |
attribute | string | Attribute to remove. |
- Source
(static) removeClass(element, …classesToRemove) → {Element}
Remove a class name from an element.
Name | Type | Attributes | Description |
---|---|---|---|
element | Element | Element to remove a class name from. | |
classesToRemove | string | <repeatable> | One or more class name to remove. |
- Source
The DOM element with class name removed.
- Type:
- Element
(static) setAttribute(el, attribute, value)
Set the value of an element's attribute.
Name | Type | Description |
---|---|---|
el | Element | A DOM element. |
attribute | string | Attribute to set. |
value | string | Value to set the attribute to. |
- Source
(static) setAttributes(el, attributesopt)
Apply attributes to an HTML element.
Name | Type | Attributes | Description |
---|---|---|---|
el | Element | Element to add attributes to. | |
attributes | Object | <optional> | Attributes to be applied. |
- Source
(static) textContent(el, text) → {Element}
Injects text into an element, replacing any existing contents entirely.
Name | Type | Description |
---|---|---|
el | HTMLElement | The element to add text content into |
text | string | The text content to add. |
- Source
The element with added text content.
- Type:
- Element
(static) toggleClass(element, classToToggle, predicateopt) → {Element}
Adds or removes a class name to/from an element depending on an optional condition or the presence/absence of the class name.
Name | Type | Attributes | Description |
---|---|---|---|
element | Element | The element to toggle a class name on. | |
classToToggle | string | The class that should be toggled. | |
predicate | boolean | | <optional> | See the return value for module:dom~module:dom~PredicateCallback |
- Source
The element with a class that has been toggled.
- Type:
- Element
(static) unblockTextSelection()
Turn off text selection blocking.
- Source
Type Definitions
ContentDescriptor()
This is a mixed value that describes content to be injected into the DOM via some method. It can be of the following types:
Type | Description |
---|---|
string | The value will be normalized into a text node. |
Element | The value will be accepted as-is. |
Text | A TextNode. The value will be accepted as-is. |
Array | A one-dimensional array of strings, elements, text nodes, or functions. These functions should return a string, element, or text node (any other return value, like an array, will be ignored). |
Function | A function, which is expected to return a string, element, text node, or array - any of the other possible values described above. This means that a content descriptor could be a function that returns an array of functions, but those second-level functions must return strings, elements, or text nodes. |
- Source
Coordinates
Represents x and y coordinates for a DOM element or mouse pointer.
- Object
Name | Type | Description |
---|---|---|
x | number | x coordinate in pixels |
y | number | y coordinate in pixels |
- Source
Position
Represents the position of a DOM element on the page.
- Object
Name | Type | Description |
---|---|---|
left | number | Pixels to the left. |
top | number | Pixels from the top. |
- Source
PredicateCallback(element, classToToggle) → {boolean|undefined}
The callback definition for toggleClass.
Name | Type | Description |
---|---|---|
element | Element | The DOM element of the Component. |
classToToggle | string | The |
- Source
If true
is returned, the classToToggle
will be added to the element
. If false
, the classToToggle
will be removed from the element
. If undefined
, the callback will be ignored.
- Type:
- boolean |
undefined