dom.js
Members
-
static,constant module:dom.$
-
Finds a single DOM element matching
selectorwithin the optionalcontextof another DOM element (defaulting todocument). -
static,constant module:dom.$$
-
Finds a all DOM elements matching
selectorwithin the optionalcontextof another DOM element (defaulting todocument).
Methods
-
staticmodule:dom.addClass(element, classesToAdd){Element}
-
Add a class name to an element.
Name Type Description elementElement Element to add class name to.
classesToAddstring repeatable One or more class name to add.
Returns:
Type Description Element The DOM element with the added class name. -
staticmodule:dom.appendContent(el, content){Element}
-
Normalizes and appends content to an element.
Name Type Description elElement Element to append normalized content to.
contentmodule:dom~ContentDescriptor A content descriptor value.
Returns:
Type Description Element The element with appended normalized content. -
staticmodule:dom.blockTextSelection()
-
Attempt to block the ability to select text.
-
staticmodule:dom.computedStyle(el, prop)
-
A safe getComputedStyle.
This is needed because in Firefox, if the player is loaded in an iframe with
display:none, thengetComputedStylereturnsnull, so, we do a null-check to make sure that the player doesn't break in these cases.Name Type Description elElement The element you want the computed style of
propstring The property name you want
-
staticmodule:dom.copyStyleSheetsToWindow(win)
-
Copy document style sheets to another window.
Name Type Description winWindow The window element you want to copy the document style sheets to.
-
staticmodule:dom.createEl(tagName, properties, attributes, content){Element}
-
Creates an element and applies properties, attributes, and inserts content.
Name Type Default Description tagNamestring 'div' optional Name of tag to be created.
propertiesObject {} optional Element properties to be applied.
attributesObject {} optional Element attributes to be applied.
contentmodule:dom~ContentDescriptor optional A content descriptor object.
Returns:
Type Description Element The element that was created. -
staticmodule:dom.emptyEl(el){Element}
-
Empties the contents of an element.
Name Type Description elElement The element to empty children from
Returns:
Type Description Element The element with no children -
staticmodule:dom.findPosition(el){module:dom~module:dom~Position}
-
Get the position of an element in the DOM.
Uses
getBoundingClientRecttechnique from John Resig.Name Type Description elElement Element from which to get offset.
Returns:
Type Description module:dom~module:dom~Position The position of the element that was passed in. -
staticmodule:dom.getAttribute(el, attribute){string}
-
Get the value of an element's attribute.
Name Type Description elElement A DOM element.
attributestring Attribute to get the value of.
Returns:
Type Description string The value of the attribute. -
staticmodule:dom.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 tagElement Element from which to get tag attributes.
Returns:
Type Description Object All attributes of the element. Boolean attributes will be trueorfalse, others will be strings. -
staticmodule:dom.getBoundingClientRect(el){Object|undefined}
-
Identical to the native
getBoundingClientRectfunction, 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/DOMRectobject; so, we shallow-copy it with the standard properties (exceptxandywhich are not widely supported). This helps avoid implementations where keys are non-enumerable.Name Type Description elElement Element whose
ClientRectwe want to calculate.Returns:
Type Description Object | undefined Always returns a plain object - or undefinedif it cannot. -
staticmodule:dom.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 elElement Element on which to get the pointer position on.
eventEvent Event object.
Returns:
Type Description module:dom~module:dom~Coordinates A coordinates object corresponding to the mouse position. -
staticmodule:dom.hasClass(element, classToCheck){boolean}
-
Check if an element has a class name.
Name Type Description elementElement Element to check
classToCheckstring Class name to check for
Throws:
Type When Error Throws an error if
classToCheckhas white space.Returns:
Type Description boolean Will be trueif the element has a class,falseotherwise. -
staticmodule:dom.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 elElement Element to insert normalized content into.
contentmodule:dom~ContentDescriptor A content descriptor value.
Returns:
Type Description Element The element with inserted normalized content. -
staticmodule:dom.isEl(value){boolean}
-
Determines, via duck typing, whether or not a value is a DOM element.
Name Type Description value* The value to check.
Returns:
Type Description boolean Will be trueif the value is a DOM element,falseotherwise. -
staticmodule:dom.isInFrame(){boolean}
-
Determines if the current DOM is embedded in an iframe.
Returns:
Type Description boolean Will be trueif the DOM is embedded in an iframe,falseotherwise. -
staticmodule:dom.isReal(){boolean}
-
Whether the current DOM interface appears to be real (i.e. not simulated).
Returns:
Type Description boolean Will be trueif the DOM appears to be real,falseotherwise. -
staticmodule:dom.isSingleLeftClick(event){boolean}
-
Check if an event was a single left click.
Name Type Description eventMouseEvent Event object.
Returns:
Type Description boolean Will be trueif a single left click,falseotherwise. -
staticmodule:dom.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.
Returns:
Type Description boolean Will be trueif the value is a text node,falseotherwise. -
staticmodule:dom.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 contentmodule:dom~ContentDescriptor A content descriptor value.
Returns:
Type Description Array All of the content that was passed in, normalized to an array of elements or text nodes. -
staticmodule:dom.prependTo(child, parent)
-
Insert an element as the first child node of another
Name Type Description childElement Element to insert
parentElement Element to insert child into
-
staticmodule:dom.removeAttribute(el, attribute)
-
Remove an element's attribute.
Name Type Description elElement A DOM element.
attributestring Attribute to remove.
-
staticmodule:dom.removeClass(element, classesToRemove){Element}
-
Remove a class name from an element.
Name Type Description elementElement Element to remove a class name from.
classesToRemovestring repeatable One or more class name to remove.
Returns:
Type Description Element The DOM element with class name removed. -
staticmodule:dom.setAttribute(el, attribute, value)
-
Set the value of an element's attribute.
Name Type Description elElement A DOM element.
attributestring Attribute to set.
valuestring Value to set the attribute to.
-
staticmodule:dom.setAttributes(el, attributes)
-
Apply attributes to an HTML element.
Name Type Description elElement Element to add attributes to.
attributesObject optional Attributes to be applied.
-
staticmodule:dom.textContent(el, text){Element}
-
Injects text into an element, replacing any existing contents entirely.
Name Type Description elHTMLElement The element to add text content into
textstring The text content to add.
Returns:
Type Description Element The element with added text content. -
staticmodule:dom.toggleClass(element, classToToggle, predicate){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 Description elementElement The element to toggle a class name on.
classToTogglestring The class that should be toggled.
predicateboolean | PredicateCallback optional See the return value for
module:dom~module:dom~PredicateCallbackReturns:
Type Description Element The element with a class that has been toggled. -
staticmodule:dom.unblockTextSelection()
-
Turn off text selection blocking.
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 stringThe value will be normalized into a text node. ElementThe value will be accepted as-is. TextA TextNode. The value will be accepted as-is. ArrayA 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). FunctionA 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. -
CoordinatesObject
-
Represents x and y coordinates for a DOM element or mouse pointer.
Properties:
Name Type Description xnumber x coordinate in pixels
ynumber y coordinate in pixels
-
PositionObject
-
Represents the position of a DOM element on the page.
Properties:
Name Type Description leftnumber Pixels to the left.
topnumber Pixels from the top.
-
PredicateCallback(element, classToToggle){boolean|undefined}
-
The callback definition for toggleClass.
Name Type Description elementElement The DOM element of the Component.
classToTogglestring The
classNamethat wants to be toggledReturns:
Type Description boolean | undefined If trueis returned, theclassToTogglewill be added to theelement, but not removed. Iffalse, theclassToTogglewill be removed from theelement, but not added. Ifundefined, the callback will be ignored.