Yahoo! UI Library

dom  3.0.0pr2

Yahoo! UI Library > dom > DOM
Search:
 
Filters

Class DOM

Provides DOM helper methods.

Methods

_bruteContains

private Boolean _bruteContains ( element , needle )
Brute force version of contains. Used for browsers without contains support for non-HTMLElement Nodes (textNodes, etc).
Parameters:
element <HTMLElement> The containing html element.
needle <HTMLElement> The html element that may be contained.
Returns: Boolean
Whether or not the element is or contains the needle.

_getDoc

private Object _getDoc ( element )
returns the appropriate document.
Parameters:
element <HTMLElement> optional Target element.
Returns: Object
The document for the given element or the default document.

_getRegExp

private RegExp _getRegExp ( str , flags )
Memoizes dynamic regular expressions to boost runtime performance.
Parameters:
str <String> The string to convert to a regular expression.
flags <String> optional An optinal string of flags.
Returns: RegExp
An instance of RegExp

_getWin

private Object _getWin ( element )
returns the appropriate window.
Parameters:
element <HTMLElement> optional Target element.
Returns: Object
The window for the given element or the default window.

addClass

void addClass ( element , className )
Adds a class name to a given DOM element.
Parameters:
element <HTMLElement> The DOM element.
className <String> the class name to add to the class attribute
Returns: void

ancestor

HTMLElement | null ancestor ( element , fn , all )
Finds the ancestor of the element.
Parameters:
element <HTMLElement> The html element.
fn <Function> optional An optional boolean test to apply. The optional function is passed the current DOM node being tested as its only argument. If no function is given, the parentNode is returned.
all <Boolean> optional Whether all node types should be scanned, or just element nodes.
Returns: HTMLElement | null
The matching DOM node or null if none found.

byId

HTMLElement | null byId ( id , doc )
Returns the HTMLElement with the given ID (Wrapper for document.getElementById).
Parameters:
id <String> the id attribute
doc <Object> optional The document to search. Defaults to current document
Returns: HTMLElement | null
The HTMLElement with the id, or null if none found.

byTag

Array byTag ( tag , root , fn )
Finds all elements with the given tag.
Parameters:
tag <String> The tag being search for.
root <HTMLElement> optional An optional root element to start from.
fn <Function> optional An optional boolean test to apply. The optional function is passed the current HTMLElement being tested as its only argument. If no function is given, all elements with the given tag are returned.
Returns: Array
The collection of matching elements.

children

Array children ( element , fn )
Finds all HTMLElement childNodes.
Parameters:
element <HTMLElement> The html element.
fn <Function> optional An optional boolean test to apply. The optional function is passed the current HTMLElement being tested as its only argument. If no function is given, all children are collected.
Returns: Array
The collection of child elements.

childrenByTag

Array childrenByTag ( element , tag , fn )
Finds all HTMLElement childNodes matching the given tag.
Parameters:
element <HTMLElement> The html element.
tag <String> The tag to search for.
fn <Function> optional An optional boolean test to apply. The optional function is passed the current HTMLElement being tested as its only argument. If no function is given, all children with the given tag are collected.
Returns: Array
The collection of child elements.

contains

Boolean contains ( element , needle )
Determines whether or not one HTMLElement is or contains another HTMLElement.
Parameters:
element <HTMLElement> The containing html element.
needle <HTMLElement> The html element that may be contained.
Returns: Boolean
Whether or not the element is or contains the needle.

docHeight

void docHeight ( )
Document height
Returns: void

docScrollX

void docScrollX ( )
Amount page has been scroll vertically
Returns: void

docScrollY

void docScrollY ( )
Amount page has been scroll horizontally
Returns: void

docWidth

void docWidth ( )
Document width
Returns: void

elementByAxis

HTMLElement | null elementByAxis ( element , axis , fn , all )
Searches the element by the given axis for the first matching element.
Parameters:
element <HTMLElement> The html element.
axis <String> The axis to search (parentNode, nextSibling, previousSibling).
fn <Function> optional An optional boolean test to apply.
all <Boolean> optional Whether all node types should be returned, or just element nodes. The optional function is passed the current HTMLElement being tested as its only argument. If no function is given, the first element is returned.
Returns: HTMLElement | null
The matching element or null if none found.

filterElementsBy

Array filterElementsBy ( elements , fn )
Filters a collection of HTMLElements by the given attributes.
Parameters:
elements <Array> The collection of HTMLElements to filter.
fn <Function> A boolean test to apply. The function is passed the current HTMLElement being tested as its only argument. If no function is given, all HTMLElements are kept.
Returns: Array
The filtered collection of HTMLElements.

firstByTag

HTMLElement firstByTag ( tag , root , fn )
Finds the first element with the given tag.
Parameters:
tag <String> The tag being search for.
root <HTMLElement> optional An optional root element to start from.
fn <Function> optional An optional boolean test to apply. The optional function is passed the current HTMLElement being tested as its only argument. If no function is given, the first match is returned.
Returns: HTMLElement
The matching element.

firstChild

HTMLElement | null firstChild ( element , fn )
Finds the firstChild of the given HTMLElement.
Parameters:
element <HTMLElement> The html element.
fn <Function> optional An optional boolean test to apply. The optional function is passed the current HTMLElement being tested as its only argument. If no function is given, the first found is returned.
Returns: HTMLElement | null
The first matching child html element.

getComputedStyle

String getComputedStyle ( An , att )
Returns the computed style for the given node.
Parameters:
An <HTMLElement> HTMLElement to get the style from.
att <String> The style property to get.
Returns: String
The computed value of the style property.

getStyle

void getStyle ( An , att )
Returns the current style value for the given property.
Parameters:
An <HTMLElement> HTMLElement to get the style from.
att <String> The style property to get.
Returns: void

getText

String getText ( element )
Returns the text content of the HTMLElement.
Parameters:
element <HTMLElement> The html element.
Returns: String
The text content of the element (includes text of any descending elements).

getX

Int getX ( element )
Gets the current X position of an element based on page coordinates. Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
Parameters:
element <object> The target element
Returns: Int
The X position of the element

getXY

Array getXY ( element )
Gets the current position of an element based on page coordinates. Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
Parameters:
element <object> The target element
Returns: Array
The XY position of the element TODO: test inDocument/display

getY

Int getY ( element )
Gets the current Y position of an element based on page coordinates. Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
Parameters:
element <object> The target element
Returns: Int
The Y position of the element

hasClass

Boolean hasClass ( element , className )
Determines whether a DOM element has the given className.
Parameters:
element <HTMLElement> The DOM element.
className <String> the class name to search for
Returns: Boolean
Whether or not the element has the given class.

inDoc

Boolean inDoc ( element , doc )
Determines whether or not the HTMLElement is part of the document.
Parameters:
element <HTMLElement> The containing html element.
doc <HTMLElement> optional The document to check.
Returns: Boolean
Whether or not the element is attached to the document.

inRegion

Boolean inRegion ( all , altRegion )
Check if any part of this node is in the passed region
Parameters:
all <Object} node2 The node to get the region from or an Object literal of the region $param {Boolean> Should all of the node be inside the region
altRegion <Object> An object literal containing the region for this node if we already have the data (for performance i.e. DragDrop)
Returns: Boolean
True if in region, false if not.

intersect

Object intersect ( element , element2 , altRegion )
Find the intersect information for the passes nodes.
Parameters:
element <HTMLElement> The first element
element2 <HTMLElement | Object> The element or region to check the interect with
altRegion <Object> An object literal containing the region for the first element if we already have the data (for performance i.e. DragDrop)
Returns: Object
Object literal containing the following intersection data: (top, right, bottom, left, area, yoff, xoff, inRegion)

inViewportRegion

Boolean inViewportRegion ( element , all , altRegion )
Check if any part of this element is in the viewport
Parameters:
element <HTMLElement> The DOM element.
all <Boolean> Should all of the node be inside the region
altRegion <Object> An object literal containing the region for this node if we already have the data (for performance i.e. DragDrop)
Returns: Boolean
True if in region, false if not.

lastChild

HTMLElement | null lastChild ( element , tag , fn )
Finds the lastChild of the given HTMLElement.
Parameters:
element <HTMLElement> The html element.
tag <String> The tag to search for.
fn <Function> optional An optional boolean test to apply. The optional function is passed the current HTMLElement being tested as its only argument. If no function is given, the first found is returned.
Returns: HTMLElement | null
The first matching child html element.

next

HTMLElement | null next ( element , fn , all )
Finds the next sibling of the element.
Parameters:
element <HTMLElement> The html element.
fn <Function> optional An optional boolean test to apply. The optional function is passed the current DOM node being tested as its only argument. If no function is given, the first sibling is returned.
all <Boolean> optional Whether all node types should be scanned, or just element nodes.
Returns: HTMLElement | null
The matching DOM node or null if none found.

previous

HTMLElement | null previous ( element , fn , all )
Finds the previous sibling of the element.
Parameters:
element <HTMLElement> The html element.
fn <Function> optional An optional boolean test to apply. The optional function is passed the current DOM node being tested as its only argument. If no function is given, the first sibling is returned.
all <Boolean> optional Whether all node types should be scanned, or just element nodes.
Returns: HTMLElement | null
The matching DOM node or null if none found.

region

Object region ( element )
Returns an Object literal containing the following about this element: (top, right, bottom, left)
Parameters:
element <HTMLElement> The DOM element.
Returns: Object
Object literal containing the following about this element: (top, right, bottom, left)

removeClass

void removeClass ( element , className )
Removes a class name from a given element.
Parameters:
element <HTMLElement> The DOM element.
className <String> the class name to remove from the class attribute
Returns: void

replaceClass

void replaceClass ( element , oldClassName , newClassName )
Replace a class with another class for a given element. If no oldClassName is present, the newClassName is simply added.
Parameters:
element <HTMLElement> The DOM element.
oldClassName <String> the class name to be replaced
newClassName <String> the class name that will be replacing the old class name
Returns: void

setStyle

void setStyle ( An , att , val )
Sets a style property for a given element.
Parameters:
An <HTMLElement> HTMLElement to apply the style to.
att <String> The style property to set.
val <String|Number> The value.
Returns: void

setStyles

void setStyles ( node , hash )
Sets multiple style properties.
Parameters:
node <HTMLElement> An HTMLElement to apply the styles to.
hash <Object> An object literal of property:value pairs.
Returns: void

setX

void setX ( element , x )
Set the X position of an html element in page coordinates, regardless of how the element is positioned. The element(s) must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
Parameters:
element <object> The target element
x <Int> The X values for new position (coordinates are page-based)
Returns: void

setXY

void setXY ( element , xy , noRetry )
Set the position of an html element in page coordinates. The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
Parameters:
element <object> The target element
xy <Array> Contains X & Y values for new position (coordinates are page-based)
noRetry <Boolean> By default we try and set the position a second time if the first fails
Returns: void

setY

void setY ( element , y )
Set the Y position of an html element in page coordinates, regardless of how the element is positioned. The element(s) must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
Parameters:
element <object> The target element
y <Int> The Y values for new position (coordinates are page-based)
Returns: void

toggleClass

void toggleClass ( element , className )
If the className exists on the node it is removed, if it doesn't exist it is added.
Parameters:
element <HTMLElement> The DOM element.
className <String> the class name to be toggled
Returns: void

viewportRegion

Object viewportRegion ( )
Returns an Object literal containing the following about the visible region of viewport: (top, right, bottom, left)
Returns: Object
Object literal containing the following about the visible region of the viewport: (top, right, bottom, left)

winHeight

void winHeight ( )
Returns the inner height of the viewport (exludes scrollbar).
Returns: void

winWidth

void winWidth ( )
Returns the inner width of the viewport (exludes scrollbar).
Returns: void


Copyright © 2008 Yahoo! Inc. All rights reserved.