Class Selector


class Selector
A JavaScript implementation of a CSS3 Query Selector.

For more information, please visit http://www.w3.org/TR/css3-selectors/.

Authors:
Henrik Lindqvist <henrik.lindqvist@llamalab.com>
Version:
Defined in Selector.js

Constructor Summary
Selector(string pattern, [Object context])
Pre-compiles an Selector query.

Function Summary
Array exec([Object context])
Execute a selector query.
Hook for patching result Array.
Hook for patching result Element’s.
string toSource()
Returns a string representing the source code of the Selector.
string toString()
Returns a string representing the query source pattern.

Constructor Details

constructor Selector

Selector(string pattern, [Object context])
Pre-compiles an Selector query.

When creating a new instance of the Selector, the query are pre-compiled for later execution with a call to exec.

The constructor can also be called as a function, without the new keyword. Then the query is executed against the optional context element, otherwise current document.

Example:

   // Constructor syntax:
   new Selector('div > p').exec(document).forEach(function (e) {
     e.style.color = 'red';
   });
   // Or,  shorthand syntax:
   Selector('div > p', document).forEach(function (e) {
     e.style.color = 'red';
   });
 
Parameters:
pattern - selector pattern.
[context] - document or context element.

Function Details

function exec

Array exec([Object context])
Execute a selector query.

Example:

   new Selector('div > p').exec(document).forEach(function (e) {
     e.style.color = 'red';
   });
 
Parameters:
[context] - document or context element, otherwise current document.
Returns:
Non-live Array with matching elements.

function patchArray

Array patchArray(Array a)
Hook for patching result Array.

When using the Selector within you own framework you can add this function to extend the resulting Array before it’sthey are returned by exec.

This function is not defined by default, since calling it unneccesarily affects performance.

Parameters:
a - the result array.
Returns:
the patched Array.
See also:

function patchElement

Element patchElement(Element e)
Hook for patching result Element’s.

When using the Selector within you own framework you can add this function to extend the resulting Element’s before they are returned by exec.

This function is not defined by default, since calling it unneccesarily affects performance.

Parameters:
e - the result element.
Returns:
the patched Element.
See also:

function toSource

string toSource()
Returns a string representing the source code of the Selector.
Returns:
source code.

function toString

string toString()
Returns a string representing the query source pattern.
Returns:
source pattern.