Class goog.dom.DomHelper

code »

Create an instance of a DOM helper with a new document object.

Constructor

goog.dom.DomHelper ( opt_document )
Parameters
opt_document: Document=
Document object to associate with this DOM helper.
Show:

Instance Methods

code »$ ( element )Element

Alias for getElement.

Parameters
element: (string|Element)
Element ID or a DOM node.
Returns
The element with the given ID, or the node passed in.
code »$$ ( opt_tag, opt_class, opt_el ){length: number}
Deprecated: Use DomHelper getElementsByTagNameAndClass.

Alias for getElementsByTagNameAndClass.

Parameters
opt_tag: ?string=
Element tag name.
opt_class: ?string=
Optional class name.
opt_el: Element=
Optional element to look in.
Returns
Array-like list of elements (only a length property and numerical indices are guaranteed to exist).
code »$dom ( tagName, opt_attributes, var_args )!Element

Alias for createDom.

Parameters
tagName: string
Tag to create.
opt_attributes: (Object|string)=
If object, then a map of name-value pairs for attributes. If a string, then this is the className of the new element.
var_args: ...goog.dom.Appendable
Further DOM nodes or strings for text nodes. If one of the var_args is an array, its children will be added as childNodes instead.
Returns
Reference to a DOM node.
code »append ( parent, var_args )

Appends a node with text or other nodes.

Parameters
parent: !Node
The node to append nodes to.
var_args: ...goog.dom.Appendable
The things to append to the node. If this is a Node it is appended as is. If this is a string then a text node is appended. If this is an array like object then fields 0 to length - 1 are appended.
code »appendChild ( parent, child )

Appends a child to a node.

Parameters
parent: Node
Parent.
child: Node
Child.

Determines if the given node can contain children, intended to be used for HTML generation.

Parameters
node: Node
The node to check.
Returns
Whether the node can contain children.
code »compareNodeOrder ( node1, node2 )number

Compares the document order of two nodes, returning 0 if they are the same node, a negative number if node1 is before node2, and a positive number if node2 is before node1. Note that we compare the order the tags appear in the document so in the tree text the B node is considered to be before the I node.

Parameters
node1: Node
The first node to compare.
node2: Node
The second node to compare.
Returns
0 if the nodes are the same node, a negative number if node1 is before node2, and a positive number if node2 is before node1.
code »contains ( parent, descendant )boolean

Whether a node contains another node.

Parameters
parent: Node
The node that should contain the other node.
descendant: Node
The node to test presence of.
Returns
Whether the parent node contains the descendent node.
code »createDom ( tagName, opt_attributes, var_args )!Element

Returns a dom node with a set of attributes. This function accepts varargs for subsequent nodes to be added. Subsequent nodes will be added to the first node as childNodes. So: createDom('div', null, createDom('p'), createDom('p')); would return a div with two child paragraphs An easy way to move all child nodes of an existing element to a new parent element is: createDom('div', null, oldElement.childNodes); which will remove all child nodes from the old element and add them as child nodes of the new DIV.

Parameters
tagName: string
Tag to create.
opt_attributes: (Object|string)=
If object, then a map of name-value pairs for attributes. If a string, then this is the className of the new element.
var_args: ...goog.dom.Appendable
Further DOM nodes or strings for text nodes. If one of the var_args is an array or NodeList, its elements will be added as childNodes instead.
Returns
Reference to a DOM node.

Creates a new element.

Parameters
name: string
Tag name.
Returns
The new element.
code »createTable ( rows, columns, opt_fillWithNbsp )!Element

Create a table.

Parameters
rows: number
The number of rows in the table. Must be >= 1.
columns: number
The number of columns in the table. Must be >= 1.
opt_fillWithNbsp: boolean=
If true, fills table entries with nsbps.
Returns
The created table.
code »createTextNode ( content )!Text

Creates a new text node.

Parameters
content: (number|string)
Content.
Returns
The new text node.

Find the deepest common ancestor of the given nodes.

Parameters
var_args: ...Node
The nodes to find a common ancestor of.
Returns
The common ancestor of the nodes, or null if there is none. null will only be returned if two or more of the nodes are from different documents.
code »findNode ( root, p )(Node|undefined)

Finds the first descendant node that matches the filter function. This does a depth first search.

Parameters
root: Node
The root of the tree to search.
p: function(Node): boolean
The filter function.
Returns
The found node or undefined if none is found.
code »findNodes ( root, p )Array.<Node>

Finds all the descendant nodes that matches the filter function. This does a depth first search.

Parameters
root: Node
The root of the tree to search.
p: function(Node): boolean
The filter function.
Returns
The found nodes or an empty array if none are found.

Flattens an element. That is, removes it and replace it with its children.

Parameters
element: Element
The element to flatten.
Returns
The original element, detached from the document tree, sans children, or undefined if the element was already not in the document.

Determines the active element in the given document.

Parameters
opt_doc: Document=
The document to look in.
Returns
The active element.
code »getAncestor ( element, matcher, opt_includeNode, opt_maxSearchSteps )Node

Walks up the DOM hierarchy returning the first ancestor that passes the matcher function.

Parameters
element: Node
The DOM node to start with.
matcher: function(Node): boolean
A function that returns true if the passed node matches the desired criteria.
opt_includeNode: boolean=
If true, the node itself is included in the search (the first call to the matcher will pass startElement as the node to test).
opt_maxSearchSteps: number=
Maximum number of levels to search up the dom.
Returns
DOM node that matched the matcher, or null if there was no match.
code »getAncestorByClass ( element, class )Element

Walks up the DOM hierarchy returning the first ancestor that has the passed class name. If the passed element matches the specified criteria, the element itself is returned.

Parameters
element: Node
The DOM node to start with.
class: string
The class name to match.
Returns
The first ancestor that matches the passed criteria, or null if none match.
code »getAncestorByTagNameAndClass ( element, opt_tag, opt_class )Element

Walks up the DOM hierarchy returning the first ancestor that has the passed tag name and/or class name. If the passed element matches the specified criteria, the element itself is returned.

Parameters
element: Node
The DOM node to start with.
opt_tag: ?(goog.dom.TagName|string)=
The tag name to match (or null/undefined to match only based on class name).
opt_class: ?string=
The class name to match (or null/undefined to match only based on tag name).
Returns
The first ancestor that matches the passed criteria, or null if no match is found.
code »getChildren ( element )!(Array|NodeList)

Returns an array containing just the element children of the given element.

Parameters
element: Element
The element whose element children we want.
Returns
An array or array-like list of just the element children of the given element.

Gets the document object being used by the dom library.

Returns
Document object.

Calculates the height of the document.

Returns
The height of the document.

Gets the document scroll distance as a coordinate object.

Returns
Object with properties 'x' and 'y'.

Gets the document scroll element.

Returns
Scrolling element.

Gets the dom helper object for the document where the element resides.

Parameters
opt_node: Node=
If present, gets the DomHelper for this node.
Returns
The DomHelper.
code »getElement ( element )Element

Alias for getElementById. If a DOM node is passed in then we just return that.

Parameters
element: (string|Element)
Element ID or a DOM node.
Returns
The element with the given ID, or the node passed in.
code »getElementByClass ( className, opt_el )Element

Returns the first element we find matching the provided class name.

Parameters
className: string
the name of the class to look for.
opt_el: (Element|Document)=
Optional element to look in.
Returns
The first item found with the class name provided.
code »getElementsByClass ( className, opt_el ){length: number}

Returns an array of all the elements with the provided className.

Parameters
className: string
the name of the class to look for.
opt_el: (Element|Document)=
Optional element to look in.
Returns
The items found with the class name provided.
code »getElementsByTagNameAndClass ( opt_tag, opt_class, opt_el ){length: number}

Looks up elements by both tag and class name, using browser native functions (querySelectorAll, getElementsByTagName or getElementsByClassName) where possible. The returned array is a live NodeList or a static list depending on the code path taken.

Parameters
opt_tag: ?string=
Element tag name or * for all tags.
opt_class: ?string=
Optional class name.
opt_el: (Document|Element)=
Optional element to look in.
Returns
Array-like list of elements (only a length property and numerical indices are guaranteed to exist).

Returns the first child node that is an element.

Parameters
node: Node
The node to get the first child element of.
Returns
The first child node of node that is an element.

Cross browser function for getting the document element of an iframe.

Parameters
iframe: Element
Iframe element.
Returns
The frame content document.
code »getFrameContentWindow ( frame )Window

Cross browser function for getting the window of a frame or iframe.

Parameters
frame: Element
Frame element.
Returns
The window associated with the given frame.

Returns the last child node that is an element.

Parameters
node: Node
The node to get the last child element of.
Returns
The last child node of node that is an element.

Returns the first next sibling that is an element.

Parameters
node: Node
The node to get the next sibling element of.
Returns
The next sibling of node that is an element.

Returns the next node in source order from the given node.

Parameters
node: Node
The node.
Returns
The next node in the DOM tree, or null if this was the last node.
code »getNodeAtOffset ( parent, offset, opt_result )Node

Returns the node at a given offset in a parent node. If an object is provided for the optional third parameter, the node and the remainder of the offset will stored as properties of this object.

Parameters
parent: Node
The parent node.
offset: number
The offset into the parent node.
opt_result: Object=
Object to be used to store the return value. The return value will be stored in the form {node: Node, remainder: number} if this object is provided.
Returns
The node at the given offset.

Returns the text length of the text contained in a node, without markup. This is equivalent to the selection length if the node was selected, or the number of cursor movements to traverse the node. Images & BRs take one space. New lines are ignored.

Parameters
node: Node
The node whose text content length is being calculated.
Returns
The length of node's text content.
code »getNodeTextOffset ( node, opt_offsetParent )number

Returns the text offset of a node relative to one of its ancestors. The text length is the same as the length calculated by goog.dom.getNodeTextLength.

Parameters
node: Node
The node whose offset is being calculated.
opt_offsetParent: Node=
Defaults to the node's owner document's body.
Returns
The text offset.
code »getOuterHtml ( element )string

Gets the outerHTML of a node, which islike innerHTML, except that it actually contains the HTML of the node itself.

Parameters
element: Element
The element to get the HTML of.
Returns
The outerHTML of the given element.

Returns the owner document for a node.

Parameters
node: Node
The node to get the document for.
Returns
The document owning the node.

Returns an element's parent, if it's an Element.

Parameters
element: Element
The DOM element.
Returns
The parent, or null if not an Element.

Returns the first previous sibling that is an element.

Parameters
node: Node
The node to get the previous sibling element of.
Returns
The first previous sibling of node that is an element.

Returns the previous node in source order from the given node.

Parameters
node: Node
The node.
Returns
The previous node in the DOM tree, or null if this was the first node.

Gets an element by id, asserting that the element is found. This is used when an element is expected to exist, and should fail with an assertion error if it does not (if assertions are enabled).

Parameters
id: string
Element ID.
Returns
The element with the given ID, if it exists.
code »getRequiredElementByClass ( className, opt_root )!Element

Ensures an element with the given className exists, and then returns the first element with the provided className.

Parameters
className: string
the name of the class to look for.
opt_root: (!Element|!Document)=
Optional element or document to look in.
Returns
The first item found with the class name provided.
Throws
goog.asserts.AssertionError
Thrown if no element is found.

Returns the text contents of the current node, without markup. New lines are stripped and whitespace is collapsed, such that each character would be visible. In browsers that support it, innerText is used. Other browsers attempt to simulate it via node traversal. Line breaks are canonicalized in IE.

Parameters
node: Node
The node from which we are getting content.
Returns
The text content.

Gets the dimensions of the viewport.

Parameters
opt_window: Window=
Optional window element to test. Defaults to the window of the Dom Helper.
Returns
Object with values 'width' and 'height'.
code »getWindow ( )!Window

Gets the window object associated with the document.

Returns
The window associated with the given document.

Converts an HTML string into a node or a document fragment. A single Node is used if the htmlString only generates a single node. If the htmlString generates multiple nodes then these are put inside a DocumentFragment.

Parameters
htmlString: string
The HTML string to convert.
Returns
The resulting node.
code »insertChildAt ( parent, child, index )

Insert a child at a given index. If index is larger than the number of child nodes that the parent currently has, the node is inserted as the last child node.

Parameters
parent: Element
The element into which to insert the child.
child: Node
The element to insert.
index: number
The index at which to insert the new child node. Must not be negative.
code »insertSiblingAfter ( newNode, refNode )

Inserts a new node after an existing reference node (i.e., as the next sibling). If the reference node has no parent, then does nothing.

Parameters
newNode: Node
Node to insert.
refNode: Node
Reference node to insert after.
code »insertSiblingBefore ( newNode, refNode )

Inserts a new node before an existing reference node (i.e., as the previous sibling). If the reference node has no parent, then does nothing.

Parameters
newNode: Node
Node to insert.
refNode: Node
Reference node to insert before.

Returns true if the browser is in "CSS1-compatible" (standards-compliant) mode, false otherwise.

Returns
True if in CSS1-compatible mode.

Whether the object looks like an Element.

Parameters
obj: ?
The object being tested for Element likeness.
Returns
Whether the object looks like an Element.
code »isFocusable ( element )boolean

Returns true if the element can be focused, i.e. it has a tab index that allows it to receive keyboard focus (tabIndex >= 0), or it is an element that natively supports keyboard focus.

Parameters
element: Element
Element to check.
Returns
Whether the element allows keyboard focus.

Returns true if the element has a tab index that allows it to receive keyboard focus (tabIndex >= 0), false otherwise. Note that some elements natively support keyboard focus, even if they have no tab index.

Parameters
element: Element
Element to check.
Returns
Whether the element has a tab index that allows keyboard focus.

Whether the object looks like a DOM node.

Parameters
obj: ?
The object being tested for node likeness.
Returns
Whether the object looks like a DOM node.

Returns true if the object is a NodeList. To qualify as a NodeList, the object must have a numeric length property and an item function (which has type 'string' on IE for some reason).

Parameters
val: Object
Object to test.
Returns
Whether the object is a NodeList.

Returns true if the specified value is a Window object. This includes the global window for HTML pages, and iframe windows.

Parameters
obj: ?
Variable to test.
Returns
Whether the variable is a window.

Removes all the child nodes on a DOM node.

Parameters
node: Node
Node to remove children from.
code »removeNode ( node )Node

Removes a node from its parent.

Parameters
node: Node
The node to remove.
Returns
The node removed if removed; else, null.
code »replaceNode ( newNode, oldNode )

Replaces a node in the DOM tree. Will do nothing if oldNode has no parent.

Parameters
newNode: Node
Node to insert.
oldNode: Node
Node to replace.
code »setDocument ( document )

Sets the document object.

Parameters
document: !Document
Document object.
code »setFocusableTabIndex ( element, enable )

Enables or disables keyboard focus support on the element via its tab index. Only elements for which goog.dom.isFocusableTabIndex returns true (or elements that natively support keyboard focus, like form elements) can receive keyboard focus. See http://go/tabindex for more info.

Parameters
element: Element
Element whose tab index is to be changed.
enable: boolean
Whether to set or remove a tab index on the element that supports keyboard focus.
code »setProperties ( element, properties )

Sets a number of properties on a node.

Parameters
element: Element
DOM node to set properties on.
properties: Object
Hash of property:value pairs.
code »setTextContent ( node, text )

Sets the text content of a node, with cross-browser support.

Parameters
node: Node
The node to change the text content of.
text: (string|number)
The value that should replace the node's content.

Instance Properties

Reference to the document object to use