libdom

Browser library that normalizes DOM operations

API Documentation

Table of Contents

String

xmlEncode(subject)

Parameter

Name Type Description
subject String Raw String value to encode.

Returns

Value Type Description
String(with HTML entities) String Encoded String.

xmlDecode(subject)

Parameter

Name Type Description
subject String HTML encoded String to decode.

Returns

Value Type Description
String(raw String with decoded HTML entities) String Decoded String.

DOM

is(node[, nodeType[,...]])

Parameter

Name Type Description
node Mixed any value to inspect
[nodeType] Number (optional, multiple) nodeType filter (e.g. Element = 1, Document = 9)

Returns

Value Type Description
true Boolean if [node] parameter is an HTML DOM node.
false Boolean if not an HTML DOM node.

isView(defaultView)

Parameter

Name Type Description
defaultView Mixed any value to inspect

Returns

Value Type Description
true Boolean if [defaultView] parameter is an instance of window or defaultView.
false Boolean if not an instance of window or defaultView.

contains(ancestor, descendant)

Parameter

Name Type Description
ancestor Node DOM Node of nodeType: Element, Document, or Fragment.
descendant Node DOM Node of any nodeType.

Returns

Value Type Description
true Boolean if [ancestor] contains [descendant] in DOM tree.
false Boolean if [ancestor] doesn’t contain [descendant] node.

select(dom, selector)

Parameter

Name Type Description
dom Node DOM Node of nodeType: Element, or Document
selector String Browser supported CSS selector.

Returns

Value Type Description
Array(HTMLElement,..) Array May be empty if no node matched the selector.

add(element, config[, before])

Parameter

Name Type Description
element Node DOM Node of nodeType: Element, or Fragment
config Node or Object if Node of nodeType: Element, Fragment, Comment, Text, and Processing Instruction then it will be inserted directly. If config is Object, It will be created and configured as DOM Element.
[before] Node or Number or null (optional) config Node will be inserted before this node. If Number, then it will be inserted in that position. If null, then [config] Node will be appended. (default is null)

Returns

Value Type Description
HTMLElement Node The inserted config Node (or newly the created Node if config is Object).

move(nodes, element)

Parameter

Name Type Description
nodes Array(Node) or Node DOM Node of nodeType: Element, Comment, Text, or Processing Instruction. Or, an Array of those nodes.
element Node Element Node as parent Node of nodes

Returns

Value Type Description
HTMLElement Node The element parent Node.

replace(node, config[, destroy])

Parameter

Name Type Description
node Node DOM Node of nodeType: Element, or Fragment
config Node or Object if Node of nodeType: Element, Fragment, Comment, Text, and Processing Instruction then it will be inserted directly. If config is Object, It will be created and configured as DOM Element.
[destroy] Boolean (optional) when true and node is an element Node, all DOM events registered to the element is destroyed. (default is false)

Returns

Value Type Description
HTMLElement Node The inserted config Node as replacement (or newly the created Node if config is Object).

remove(node[, destroy])

Parameter

Name Type Description
node Node DOM Node of nodeType: Element, Fragment, Comment, Text, or Processing Instruction.
[destroy] Boolean (optional) when true and node is an element Node, all DOM events registered to the element is destroyed. (default is false)

Returns

Value Type Description
HTMLElement Node The removed (or destroyed) Node node.

Cascading Stylesheet

addClass(element, classNames)

Parameter

Name Type Description
element Node Element Node.
classNames String or Array(String) CSS class to append

Returns

Value Type Description
Object(libdom) Object The libdom module for chaining method calls.

removeClass(element, classNames)

Parameter

Name Type Description
element Node Element Node.
classNames String or Array(String) CSS class names to remove.

Returns

Value Type Description
Object(libdom) Object The libdom module for chaining method calls.

computedStyle(element, ruleNames)

Parameter

Name Type Description
element Node Element Node.
ruleNames String or Array(String) (multiple if String) list of CSS rule names or CSS rule name if String.

Returns

Value Type Description
Object([ruleName]:[value], … ) Object The computed style extracted limited to the given ruleNames.

stylize(element, rules[, value])

Parameter

Name Type Description
element Node Element Node.
rules String or Object CSS rules Object to apply or individually set CSS rule String with value parameter.
[value] Mixed (optional) only applicable if rules parameter is String

Returns

Value Type Description
Object(libdom) Object libdom module.

stylify(element)

Parameter

Name Type Description
element Node Element Node.

Returns

Value Type Description
Object(CSS rules) Object CSS rules extracted.

Color

parseColor(subject)

Parameter

Name Type Description
subject String String in hex, rgb, rba, hsl, or hsl format.

Returns

Value Type Description
Number(12234) Number subject is in valid color format and succesfully parsed.
null Null subject is contains invalid color format, malformed or not recognized.

parseColorType(subject)

[type:String, isHexFormat:Boolean, items:String|Array]

Examples:

[‘hex’, true, ‘ff00aa’]

[‘rgba’, false, [‘255’, ‘120’, ‘100’, ‘0.5’]]

Parameter

Name Type Description
subject String String in hex, rgb, rba, hsl, or hsl format.

Returns

Value Type Description
Array(type, isHexFormat, items) Array subject is in valid color format and succesfully parsed.
null Null subject is contains invalid color format, malformed or not recognized.

formatColor(colorValue[, type])

Parameter

Name Type Description
colorValue Number The Number presentation of color which may be generated from parseColor(subject:String).
[type] String (optional) Supported color types that should be one of the following values: "hex", "rgb", "rgba", "hsl", or "hsla".

Returns

Value Type Description
“#ff00ff” String If type is "hex".
“rgb(255,255,255)” String If type is "rgb".
“rgba(255,255,255,1)” String If type is "rgba".
“hsl(240,100%,50%)” String If type is "hsl".
“hsl(120,100%,50%,1)” String If type is "hsla".
null Null type is not a supported color format.

Events

on(observable, type, handler[, context])

Parameter

Name Type Description
observable Mixed Object supporting DOM Events (e.g. Node, Window, XMLHttpRequest).
type String event type or name (e.g. “load” for onload events).
handler Function callback of event dispatch.
[context] Mixed (optional) this object inside the handler which defaults to Window (or undefined in strict mode) if omitted.

Returns

Value Type Description
Function(remove listener) Function The function that removes the event listener.

un(observable, type, handler[, context])

Parameter

Name Type Description
observable Mixed Object supporting DOM Events (e.g. Node, Window, XMLHttpRequest).
type String event type or name (e.g. “load” for onload events).
handler Function callback of event dispatch.
[context] Mixed (optional) this object inside the handler which defaults to Window (or undefined in strict mode) if omitted.

Returns

Value Type Description
Object(libdom) Object libdom module.

purge([observable, [type, [handler[, context]]]])

Example

  • purge(window, "load"); remove all “load” event listeners of window
  • purge(window); remove all event listeners of window
  • purge(document.body, "mousemove", callback); remove all “mousemove” event listeners of document.body having callback event handler.
  • purge() remove all event listeners. This is called automatically before webpage unloads.

Parameter

Name Type Description
[observable] Mixed (optional) Object supporting DOM Events (e.g. Node, Window, XMLHttpRequest).
[type] String (optional) event type or name (e.g. “load” for onload events).
[handler] Function (optional) callback of event dispatch.
[context] Mixed (optional) this object inside the handler which defaults to Window (or undefined in strict mode) if omitted.

Returns

Value Type Description
Object(libdom) Object libdom module.

dispatch(observable, type, properties)

Parameter

Name Type Description
observable Mixed Object supporting DOM Events (e.g. Node, Window, XMLHttpRequest).
type String event type or name (e.g. “load” for onload events).
properties Object Event Object property overrides.

Returns

Value Type Description
Object(libdom) Object libdom module.

destructor(handler)

Parameter

Name Type Description
handler Function callback function to run when Window is unloaded or destroyed.

Returns

Value Type Description
Object(libdom) Object libdom module.

Dimension and Positioning

offset(element[, x, y])

Parameter

Name Type Description
element Window, Document Node or Element Node Accepts DOM Window, Document Node, or Element Node.
[x] Number (optional) horizontal pixel offset relative to document page.
[y] Number (optional) vertical pixel offset relative to document page.

Returns

Value Type Description
Object(libdom) Object libdom module is returned when x, and y is provided. (Setter mode)
Array(Number) Array Array(x, y) offset is returned when x, and y is omitted. (Getter mode)

Note:

Setting element offset doesn’t work on position: static styled elements.


size(element[, width, height])

Parameter

Name Type Description
element Window, Document Node or Element Node Accepts DOM Window, Document Node, or Element Node.
[width] Number (optional) pixel width.
[height] Number (optional) pixel height.

Returns

Value Type Description
Object(libdom) Object libdom module is returned when width, and height is provided. (Setter mode)
Array(Number) Array Array(width, height) size is returned when width, and height is omitted. (Getter mode)

box(element[, x, y, width, height])

Parameter

Name Type Description
element Window, Document Node or Element Node Accepts DOM Window, Document Node, or Element Node.
[x] Number (optional) horizontal pixel offset relative to document page.
[y] Number (optional) vertical pixel offset relative to document page.
[width] Number (optional) pixel width.
[height] Number (optional) pixel height.

Returns

Value Type Description
Object(libdom) Object libdom module is returned when x, y, width, and height is provided. (Setter mode)
Array(Number) Array Array(x, y, right, bottom, width, height) box is returned when x, y, width, and height is omitted. (Getter mode)

scroll(dom[, x, y])

Parameter

Name Type Description
dom Window, Document Node or Element Node Accepts DOM Window, Document Node, or Element Node.
[x] Number (optional) horizontal pixel scroll offset.
[y] Number (optional) vertical pixel scroll offset.

Returns

Value Type Description
Object(libdom) Object libdom module is returned when x, y is provided. (Setter mode)
Array(Number) Array Array(x, y) scroll offsets is returned when x, and y is omitted. (Getter mode)

screen(dom)

Parameter

Name Type Description
dom Window, Document Node or Element Node Accepts DOM Window, Document Node, or Element Node.

Returns

Value Type Description
Array(Number) Array Array(x, y, width, height) viewport box

Node Selection

highlight(from, to)

Parameter

Name Type Description
from Element Node Element where selection starts.
to Element Node Element where selection ends.

Returns

Value Type Description
Object(libdom) Object libdom module.

unhighlightable(element[, disableSelect])

Parameter

Name Type Description
element Element Node Element to enable or disable selection/highlighting.
[disableSelect] Boolean (optional) true to disable selection/highlighting or false otherwise. (default value is true)

Returns

Value Type Description
Object(libdom) Object libdom module.

clearHighlight([documentNode])

Parameter

Name Type Description
[documentNode] Document Node (optional) Document Node to clear selection or highlights. (default value is current document)

Returns

Value Type Description
Object(libdom) Object libdom module.

Animation

transition(callback, from, to[, type[, duration]])


animateStyle(element, styles[, type])

Parameter

Name Type Description
element Element Node The Element to animate.
styles Object The CSS rules and values to animate (may include non-Number CSS rule values).
type String The easing type to use in the animation.

Returns

Value Type Description
Object(libdom) Object libdom module.