HTMLPP HyperText Markup Language Page Parser Version 1.0.3

Introduction

HTMLPP is a PHP library for HTML code parsing. It allows you to parse a HTML code string, build the relative DOM structure and work on it with methods similar to Javascript.
Javascript sintax to create and append a div element to the next sibling of the first child of the body:
							var elem=document.createElement("div");
							document.body.childNodes[0].nextSibling.appendChild(elm);
						
Same operation in PHP with HTMLPP:
							$elem=&$document->createElement("div");
							$document->body->childNodes[0]->nextSibling->appendChild(elm);
						

Features

  • HTML parsing
    • Simple tags
    • Tags without closures
    • Autoclosing tags
    • Doctype, text and comment parsing
    • Modern browser parsing behaviour
      • Add head,body and html tags if they're not present
      • Wrap table content inside the tbody if it's not present
  • Dom traversing
    • Access to the parent node using the parentNode property
    • Access to child nodes using the childNodes array property
    • Access to sibling nodes using nextSibling and previousSibling properties
    • Access to the owner document with ownerDocument property
    • Document shortcuts to body, head and doctype
  • Dom manipulation
    • Append nodes with appendChild, append and other methods
    • Remove nodes with removeChild and remove methods
    • Replace nodes with replaceChild and replace methods
  • Attributes and style manipulation
    • Add, remove, set and get methods for attributes
    • Add, remove, set and get methods for style properties
  • Node searching functions on every element
    • getElementById
    • getElementsByTagName
    • getElementsByClassName
    • getElementsBySelector
      • Full featured support for Css3 selectors
      • Support for other non-standard selectors
    • Node iterator class for personalized filter functions
  • Dom collections with JQuery like methods
    • Add, remove and filter elements in the collection
    • Change the current collection by searching in its elements siblings, child nodes or parent nodes
    • Manipulate elements in the collection

Usage

There are 4 steps to follow to use the class
  1. Include the HTMLPP.php file
  2. Create a new instance of the HTMLPP class
  3. Load HTML code
  4. Parse and get the document
							require_once "HTMLPP.php";						/*include the file*/
							$HTML=new HTMLPP;								/*Create the instance*/
							$HTML->loadHTMLFile("http://www.mysite.com");	/*Load the code*/
							$document=& $HTML->getDocument();				/*Parse and get the document*/
						

Changelog

1.0.3

  • Fixed bug in getAttribute() method
  • Fixed bug in getStyle() method
  • Fixed bug on attributes parsing

1.0.2

  • Fixed error on selector parsing
  • Now every element is closed at the end of its parent code if no closing tag is found
  • Better support for textarea tag
  • Fixed bug on attributes parsing (thanks Mike)

1.0.1

  • Fixed some bugs in elements parsing regexp
  • Fixed a bug in doctype parsing
  • Fixed some problems in the parser class
  • Fixed a bug in HTMLFilterIterator::find() function when pass HTML_SEARCH_DESCENDANT as iteration type

1.0

  • First release
Main HTMLPP class

Public properties

code

StringThe code to be parsed. This variable is set by the loading functions.

document

ObjectReference to the parsed document. This is created by calling the getDocument function.

Public methods

loadHTML

Load HTML code by string.
Arguments
String $string HTML code string
Example
									$HTML=new HTMLPP;
									$HTML->loadHTML("
Example
");

loadHTMLFile

Get and load HTML code from a local file or a URL.
Arguments
String $path HTML file path
Bool $useCurl If it's true(default) and the function cannot load the contents directly it uses cUrl functions
Array $curlOpt An array (format: option=>value) for curl settings
Example
									$HTML=new HTMLPP;
									$HTML->loadHTMLFile("http://www.mysite.com");
								

stripComments

Strip all comments in the code. This function must be called before call the getDocument function. Example
									$HTML=new HTMLPP;
									$HTML->loadHTML("
Example
"); $HTML->stripComments(); //Now all comments are removed $document=& $HTML->getDocument();

getDocument

Return a reference to the parsed document.
Return
Object Reference to the parsed document Example
									$HTML=new HTMLPP;
									$HTML->loadHTMLFile("http://www.mysite.com");
									$document=& $HTML->getDocument();
								

render

Return document HTML code as a string.
Return
String Document HTML code Example
									$HTML=new HTMLPP;
									$HTML->loadHTMLFile("http://www.mysite.com");
									$document=& $HTML->getDocument();
									echo $HTML->render();
								
HTML elements, comments, text, the doctype and the document are instances of this class. It provides methods to work on the DOM tree and manipulate elements.
The document has some additional methods to create elements.

Predefined constants

Node type constants

ELEMENT_NODEHTML element node type
TEXT_NODEText node type
COMMENT_NODEComment node type
DOCUMENT_NODEDocument node type
DOCUMENT_TYPE_NODEDoctype node type

Public properties

childNodes

ArrayArray that contains every child node of the element

parentNode

ObjectReference to the parent node of the element or null if there isn't

previousSibling

ObjectReference to the previous sibling of the element or null if there isn't

nextSibling

ObjectReference to the next sibling of the element or null if there isn't

firstChild

ObjectReference to the first child node of the element or null if there isn't

lastChild

ObjectReference to the last child node of the element or null if there isn't

ownerDocument

ObjectReference to the document object

attributes

ObjectObject that contains element's attributes

style

ObjectObject that contains element's style properties

index

IntElement's index in its parent node childNodes array

nodeType

IntElement's node type. It's value is one of the node type constants

nodeName

StringElement's node name

nodeValue

StringElement's node value

tagName

StringElement's tag name. Null for document, doctype, comments and text nodes

textContent

StringIt contains the text of text and comments nodes and the code inside script and style tags

name

StringThis property is owned only by the doctype element and it's the doctype name

publicId

StringThis property is owned only by the doctype element and it's the doctype public id

systemId

StringThis property is owned only by the doctype element and it's the doctype system id

Document special methods

createElement

Create a new element.
Arguments
String $tag Element's tag name
Return
Object Created element
Example
										$element=& $document->createElement('div'); //Create a new div element
									

createTextNode

Create a new text node.
Arguments
String $text Text inside the node
Return
Object Created text node
Example
										$element=& $document->createTextNode('example text');
									

createComment

Create a new comment node.
Arguments
String $text Comment text
Return
Object Created comment node
Example
										$element=& $document->createComment('comment text');
									

createDocumentType

Create a new doctype node.
Arguments
String $name Name
String $publicId Public id
String $systemId System id
Return
Object Created doctype node
Example
										$element=& $document->createDocumentType('html','-//W3C//DTD XHTML 1.0 Strict//EN','http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
									

Public methods

setAttributes

Set multiple attributes
Arguments
mixed $attributes Array or object of attributes in attribute=>value format or html attribute string
Return
Object Reference to the current element
Example
										$element=& $document->body->childNodes[2];
										$element->setAttributes(array("class"=>"myclass","id"=>"myid"));	//array
										$element->setAttributes("class='myclass' id='myid'");	//html attribute string
									

setAttribute

Set an attribute
Arguments
string $name Attribute name
string $val Attribute value
Return
Object Reference to the current element
Example
										$element=& $document->body->firstChild;
										$element->setAttribute("name","myname");
									

hasAttribute

Check if the element has a given attribute
Arguments
string $name Attribute name
Return
Bool True if the element has that attribute, otherwise false
Example
										if($element->parentNode->hasAttribute("id"))
										{
											...
										}
									

getAttribute

Return the value of the given attribute or null if the element hasn't that attribute
Arguments
string $name Attribute name
Return
Mixed The attribute value or null if the element hasn't that attribute
Example
										$id=$element->getAttribute("id");
									

removeAttribute

Remove the given attribute from the element
Arguments
string $name Attribute name
Return
Object Reference to the current element
Example
										$element->setAttribute("id","myid");
										$element->removeAttribute("id");
										$id=$element->getAttribute("id");	//null because it was removed
									

hasAttributes

Check if the element has at least one attribute
Return
Bool True if the element has at least one attribute, otherwise false
Example
										$element=&$document->createElement("span");
										$check=$element->hasAttributes();	//False
										$element->setAttribute("id","myid");
										$check=$element->hasAttributes();	//True
									

emptyAttributes

Remove all attributes from the element
Return
Object Reference to the current element
Example
										$check=$element->hasAttributes();	//True
										$element->emptyAttributes();
										$check=$element->hasAttributes();	//False
									

setStyles

Set multiple style properties
Arguments
mixed $styles Array or object of style properties in style=>value format or css style string
Return
Object Reference to the current element
Example
										$element=& $document->body->childNodes[2];
										$element->setStyles(array("padding"=>"5px","z-index"=>"2"));	//array
										$element->setStyles("padding:5px; z-index:2");	//css style string
									

setStyle

Set a style property
Arguments
string $name Style property name
string $val Style property value
Return
Object Reference to the current element
Example
										$element=& $document->body->firstChild;
										$element->setStyle("margin","1px");
									

hasStyle

Check if the element has a given style property
Arguments
string $name Style property name
Return
Bool True if the element has that style property, otherwise false
Example
										if($element->parentNode->hasStyle("background"))
										{
											...
										}
									

getStyle

Return the value of the given style property or null if the element hasn't that style property
Arguments
string $name Style property name
Return
Mixed The style property value or null if the element hasn't that style property
Example
										$color=$element->getStyle("color");
									

removeStyle

Remove the given style property from the element
Arguments
string $name Style property name
Return
Object Reference to the current element
Example
										$element->setStyle("font-size","12px");
										$element->removeStyle("font-size");
										$size=$element->getStyle("font-size");	//null because it was removed
									

hasStyles

Check if the element has at least one style property
Return
Bool True if the element has at least one style property, otherwise false
Example
										$element=&$document->createElement("span");
										$check=$element->hasStyles();	//False
										$element->setStyle("text-decoration","underline");
										$check=$element->hasStyles();	//True
									

emptyStyles

Remove all style properties from the element
Return
Object Reference to the current element
Example
										$check=$element->hasStyles();	//True
										$element->emptyStyles();
										$check=$element->hasStyles();	//False
									

setCssText

Same as setStyles, but it empties the style object before setting new style properties
Arguments
string $style Style properties in css formatted string
Return
Object Reference to the current element
Example
										$element->setCssText("background:red;margin:0");
									

getCssText

Get the style object in css formatted string
Return
String Css formatted string of the style object
Example
										$element=&$document->createElement("table");
										$element->setCssText("background:red; margin:0");
										echo $element->getCssText(); //returns "background:red;margin:0"
									

setInnerHTML

Set element's inner code by parsing the given HTML string
Arguments
string $HTML HTML code
Return
Object Reference to the current element
Example
										$element=& $document->head->firstChild->nextSibling;
										echo count($element->childNodes);	//0: this element hasn't got any child node
										$element->setInnerHTML("<title>Example</title>");
										echo count($element->childNodes);	//1: Now the element has a child node
										echo $element->childNodes[0]->tagName;	//1: The child node is a title element
									

getInnerHTML

Get the HTML code inside the element.
Return
String HTML code inside the element
Example
										$element->setInnerHTML("<title>Example</title>");
										echo $element->getInnerHTML();	//Prints "<title>Example</title>"
									

getOuterHTML

Get the HTML code of the element and its innerHTML.
Return
String HTML element code and the code inside it
Example
										$element=& $document->createElement("span");
										$element->setInnerHTML("<div>Example</div>");
										echo $element->getOuterHTML();	//Prints "<span><div>Example</div></span>"
									

getTextContent

Return the text content of the element and its children
Return
String Text content
Example
										$element=& $document->createElement("span");
										$element->setInnerHTML("This is an <div>Example</div>");
										echo $element->getTextContent();	//Prints "This is an Example"
									

getOuterTextContent

Return the getTextContent function applied on the current element and its sibling nodes
Return
String Text content
Example
										$element=& $document->createElement("span");
										$element->setInnerHTML("<span>This is an </span><div>Example</div>");
										echo $element->childNodes[0]->getOuterTextContent();	//Prints "This is an Example"
									

getChildAt

Get the child node at the specified position or null if there's no element at that index
Arguments
int $index Child node index
Return
Object HTML element or null if there's no element at that index
Example
										$child=& $element->getChildAt(2);
										$child=& $element->->childNodes[2];	//Same operation
									

getElementsByTagName

Get elements inside the current node with the given tag name. It returns an HTMLCollection object.
Arguments
string $tag Tag name
Return
Object HTML collection
Example
										$allDivs=& $document->body->getElementsByTagName("div");	//Find all div elements inside the body
									

getElementsByClassName

Get elements inside the current node with the given class name. It returns an HTMLCollection object.
Arguments
string $class Class name
Return
Object HTML collection
Example
										$class=& $document->body->getElementsByClassName("myclass");	//Find all elements with the given class attribute
									

getElementById

Get the element with the given id inside the current node or null if there's no element with that id
Arguments
string $id Id
Return
Object HTML element or null
Example
										$myid=& $document->body->getElementsById("myid");	//Find the element with "myid" id
									

getElementsBySelector

Get every element inside the current element that matches the given css selector. For a list of all supported selectors see the HTMLCollection section.
Arguments
string $selector Css selector
Return
Object HTML collection
Example
										$col=& $document->getElementsBySelector("div.myclass");	//Find all divs with "myclass" class name
									

getElementsBySelector

Get every element inside the current element that matches the given css selector. For a list of all supported selectors see the HTMLCollection section.
Arguments
string $selector Css selector
Return
Object HTML collection
Example
										$col=& $document->getElementsBySelector("div.myclass");	//Find all divs with "myclass" class name
									

isSameNode

Return true if the given element and the current are the same node.
Arguments
object $element Element for the comparison
Return
Bool True if the given element and the current are the same node, otherwise false
Example
										$el1=& $document->getChildAt(0);
										$el2=& $document->childNodes[0];
										$check= $el1->isSameNode($el2);	//true
									

cloneNode

Return a copy of the node
Arguments
bool $deep If it's set to true child nodes are cloned too, otherwise(default) only the element is cloned
Return
Object Copy of the node
Example
										$copy= & $element->cloneNode();
									

hasChildNodes

Return true if the element has at least one child node
Return
Bool True if the element has at least one child node, otherwise false
Example
										$element->hasChildNodes();	//false
										$element->setInnerHTML("<div></div>");
										$element->hasChildNodes();	//true
									

appendChild

Insert the given element at the end of the current element's child nodes array
Arguments
object $element Element to insert
Return
Object Reference to the current element
Example
										$element=& $document->getElementById("element");
										$document->body->appendChild($element);	//Append the element to the body
									

appendTo

Insert the current element at the end of the given element's child nodes array
Arguments
object $element Element in which to insert the current element
Return
Object Reference to the current element
Example
										$element=& $document->getElementById("element");
										$element->appendTo($document->body);	//Append the element to the body
									

prependChild

Insert the given element at the beginning of the current element's child nodes array
Arguments
object $element Element to insert
Return
Object Reference to the current element
Example
										$element=& $document->getElementById("element");
										$document->body->prependChild($element);	//Prepend the element to the body
									

prependTo

Insert the current element at the beginning of the given element's child nodes array
Arguments
object $element Element in which to insert the current element
Return
Object Reference to the current element
Example
										$element=& $document->getElementById("element");
										$element->prependTo($document->body);	//Prepend the element to the body
									

insertChildAt

Insert the given element at the specified position of the current element's child nodes array
Arguments
object $element Element to insert
int $index Zero based position index
Return
Object Reference to the current element
Example
										$element=& $document->getElementById("element");
										$document->body->insertChildAt($element,1);	//Insert the element in second position
									

insertAt

Insert the current element at the specified position of the given element's child nodes array
Arguments
object $element Element in which to insert the current element
int $index Zero based position index
Return
Object Reference to the current element
Example
										$element=& $document->getElementById("element");
										$element->insertAt($document->body,2);	//Insert the element in second position
									

insertBefore

Insert the second element before the first in the current element child nodes collection
Arguments
object $element Before this element the second one will be inserted
object $node Element to insert
Return
Object Reference to the current element
Example
										$node=& $document->getElementById("element");
										$element=& $document->body->childNodes[1];
										$document->body->insertBefore($element,$node);	//Insert the element before the second child of the body
									

insertBeforeNode

Insert the current element before the given one
Arguments
object $element Before this element the current one will be inserted
Return
Object Reference to the current element
Example
										$node=& $document->getElementById("element");
										$element=& $document->body->childNodes[1];
										$node->insertBeforeNode($element);	//Insert the element before the second child of the body
									

insertAfter

Insert the second element after the first in the current element child nodes collection
Arguments
object $element After this element the second one will be inserted
object $node Element to insert
Return
Object Reference to the current element
Example
										$node=& $document->getElementById("element");
										$element=& $document->body->childNodes[1];
										$document->body->insertAfter($element,$node);	//Insert the element after the second child of the body
									

insertAfterNode

Insert the current element after the given one
Arguments
object $element After this element the current one will be inserted
Return
Object Reference to the current element
Example
										$node=& $document->getElementById("element");
										$element=& $document->body->childNodes[1];
										$node->insertAfterNode($element);	//Insert the element after the second child of the body
									

replaceChild

Replace the first element with the second one in the current element child nodes array
Arguments
object $element The element that must be replaced
object $replacement The element used to replace the first one
Return
Object Reference to the current element
Example
										$node=& $document->getElementById("element");
										$element=& $document->body->childNodes[1];
										$node->replaceChild($element,$node);	//Replace the first child of the body with the element
									

replace

Replace the given element with the current one
Arguments
object $element The element that must be replaced
Return
Object Reference to the current element
Example
										$node=& $document->getElementById("element");
										$element=& $document->body->childNodes[1];
										$node->replace($element);	//Replace the first child of the body with the element
									

removeChild

Remove the given element from the current element child nodes array
Arguments
object $element The element that must be removed
Return
Object Reference to the current element
Example
										$element=& $document->body->childNodes[0];
										$document->body->removeChild($element);	//Remove the first child of the body
									

removeChildAt

Remove element at the given index from the current element child nodes array
Arguments
int $index The zero based index of the element that must be removed
Return
Object Reference to the current element
Example
										$document->body->removeChild(0);	//Remove the first child of the body
									

removeChildAt

Remove the current element from its parent node child nodes array
Return
Object Reference to the current element
Example
										$document->body->childNodes[0]->remove();	//Remove the first child of the body
									

emptyChildNodes

Remove every child node from the current element
Return
Object Reference to the current element
Example
										$document->body->hasChildNodes();	//True
										$document->body->emptyChildNodes();	//Remove the first child of the body
										$document->body->hasChildNodes();	//False
									
HTML collection class. This class provides methods to select and manipulate a group of elements by searching them with css3 selectors queries.

Supported selectors

Tag name, class, id

tagGet elements by tag name.
#idGet elements by id
.classGet elements by class name.

Attributes

[attribute]Get elements that have the given attribute
[attribute=value]Get elements with the given attribute equal to the given value
[attribute!=value]Get elements with the given attribute different from the given value
[attribute$=value]Get elements with the given attribute that ends with the given value
[attribute|=value]Get elements with the given attribute that is equal to the given value or is equal to the given value with a hypen before and/or after
[attribute^=value]Get elements with the given attribute that starts with the given value
[attribute~=value]Get elements with the given attribute that is equal to the given value or is equal to the given value with a whitespace before and/or after
[attribute*=value]Get elements with the given attribute that contains the given value

Pseudo selectors

:first-childGet elements that are first child of their parent
:last-childGet elements that are last child of their parent
:only-childGet elements that are the only child of their parent
:nth-child(exp)Get elements that have an+b-1 siblings before them in the document tree
:nth-last-child(exp)Get elements that have an+b-1 siblings after them in the document tree
:oddGet elements in odd position
:evenGet elements in even position
:first-of-typeGet elements that are first child of their type in their parent's children
:last-of-typeGet elements that are lst child of their type in their parent's children
:only-of-typeGet elements that are the only child of their type in their parent's children
:nth-of-typeGet elements that have an+b-1 siblings of their type before them in the document tree
:nth-last-of-type(exp)Get elements that have an+b-1 siblings of their type after them in the document tree
:contains(text)Get elements that contain the given text
:has(selector)Get elements that contain at least one element that matches the given selector
:not(selector)Get elements that don't match the given selector
:emptyGet elements that don't contain any element or text node
:parentGet elements that are parent of at least one element or text node
:headerGet H1,H2,H3,H4,H5,H6 elements
:buttonGet button elements or input elements with type=button
:inputGet input, button, select and textarea elements
:textGet input elements with type=text
:resetGet input elements with type=reset
:fileGet input elements with type=file
:radioGet input elements with type=radio
:passwordGet input elements with type=password
:submitGet input elements with type=submit
:imageGet input elements with type=image
:hiddenGet input elements with type=hidden
:checkboxGet input elements with type=checkbox
:selectedGet selected elements
:checkedGet checked elements
:disabledGet disabled elements
:readonlyGet read-only elements
:enabledGet enabled elements
:lang(value)Get elements with the lang attribute equal to the given value
:rootGet HTML element

Combinators

" "(whitespace)Search in elements subtrees
">"Search in elements child nodes
"+"Search only in elements next sibling
"~"Search in all elements next sibling
For a better explanation on css3 selectors visit: http://www.w3.org/TR/css3-selectors/

Public properties

elements

ArrayArray that contains every element of the collection

length

IntNumber of elements in the collection

context

ObjectOriginal node from witch the collection was generated

Constructor

HTMLCollection

Class constructor
Arguments
object $contextThe node from which to start searching
string $selectorAn optional selector for immediatly fill the collection
Example
									$element=& $document->body;
									$coll=new HTMLCollection($element,"div"); //Contains every div element inside the body
									$coll=new HTMLCollection($element);		//Empty collection
								

Public methods

merge

Merge the current collection with another element, array of element or another collection
Arguments
mixed $elementElement, array of elements, collection, array of collections
Return
Object Reference to the current collection
Example
									$col=& $document->getElementsByTagName("span");
									$col->merge($document->body);	//Add the body element to the collection
									$divs=& $document->getElementsByTagName("div");
									$col->merge($divs);	//Merge with another collection
								

add

Add another set of elements that match the selector to the current collection
Arguments
string $selectorSelector
Return
Object Reference to the current collection
Example
									$col=& $document->getElementsByClassName("class");
									$col->add("div#myid");	//Add all div elements with "myid" id to the collection
								

unique

Remove dupliacated elements from the collection.
Return
Object Reference to the current collection
Example
									$col=& $document->getElementsBySelector("div span");
									$col->unique();	//Now there are no duplicated elements
								

each

Apply a function to all elements in the collection
Arguments
function $fnFunction returned by create_function. It receives two parameters: the element passed as reference and it's index in the collection
array$argsArray of additional arguments to pass into the function
Return
Array Array of every result returned by each function execution
Example
									$col=& $document->getElementsBySelector("p a:first-child");
									$function=create_function(
											'$element,$index,$name',
											'$element->setAttribute("$name",$index);'
											);
									$args=array("rel");
									$col->each($function,$args);
								

setAttributes

Set multiple attributes for each element in the collection
Arguments
mixed $attributes Array or object of attributes in attribute=>value format or html attribute string
Return
Object Reference to the current collection
Example
										$col=& $document->body->getElementsByTagName("table");
										$col->setAttributes(array("class"=>"myclass","id"=>"myid"));	//array
										$col->setAttributes("class='myclass' id='myid'");	//html attribute string
									

setAttribute

Set an attribute for each element in the collection
Arguments
string $name Attribute name
string $val Attribute value
Return
Object Reference to the current collection
Example
										$col=& $document->body->getElementsByTagName("table");
										$col->setAttribute("name","myname");
									

getAttribute

Return an array of values of the given attribute for each element in the collection
Arguments
string $name Attribute name
Return
ArrayArray of attribute values
Example
										$col=& $document->body->getElementsByTagName("table");
										$array=$col->getAttribute("name");
									

removeAttribute

Remove the given attribute from each element in the collection
Arguments
string $name Attribute name
Return
Object Reference to the current collection
Example
										$col=& $document->body->getElementsByTagName("table");
										$col->removeAttribute("id");
									

emptyAttributes

Remove all attributes from each element
Return
Object Reference to the current collection
Example
										$col=& $document->body->getElementsByTagName("table");
										$col->emptyAttributes();
									

setStyles

Set multiple style properties for each element in the collection
Arguments
mixed $styles Array or object of style properties in style=>value format or css style string
Return
Object Reference to the current collection
Example
										$col=& $document->body->getElementsByTagName("span");
										$col->setStyles(array("padding"=>"5px","z-index"=>"2"));	//array
										$col->setStyles("padding:5px; z-index:2");	//css style string
									

setStyle

Set a style property for each element in the collection
Arguments
string $name Style property name
string $val Style property value
Return
Object Reference to the current collection
Example
										$col=& $document->body->getElementsByTagName("span");
										$col->setStyle("margin","1px");
									

getStyle

Return an array of values of the given style property for each element in the collection
Arguments
string $name Style property name
Return
ArrayArray of style properties
Example
										$col=& $document->body->getElementsByTagName("span");
										$colors=$col->getStyle("color");
									

removeStyle

Remove the given style property from each element in the collection
Arguments
string $name Style property name
Return
Object Reference to the current collection
Example
										$col=& $document->body->getElementsByTagName("span");
										$col->removeStyle("color");
									

emptyStyles

Remove all style properties from each element in the collection
Return
Object Reference to the current collection
Example
										$col=& $document->body->getElementsByTagName("span");
										$col->emptyStyles();
									

andSelf

Add the context of the collection at the beginning of the collection's elements array
Return
Object Reference to the current collection
Example
										$col=& $document->body->getElementsByTagName("span");
										$col->andSelf();	//Now the collection contains the body too
									

get

Return the element at the specified index or null if there isn't any element at that index
Arguments
int $index Zero based position index
Return
Mixed HTML elment at the specified position on null
Example
										$col=& $document->body->getElementsByTagName("span");
										$span=& $col->get(1); //Get the second element
									

copy

Returns a copy of the current collection
Return
Object Copy of the collection
Example
										$col=& $document->body->getElementsByTagName("span");
										$copy=& $col->copy();
									

eq

Reduce the collection to only one element that is positioned at the given index
Arguments
int $index Zero based position index
Return
Object Reference to the current collection
Example
										$col=& $document->body->getElementsByTagName("span");
										echo $col->length;	//10
										$col->eq(0);
										echo $col->length;	//1
									

slice

Reduce the collection by setting a range of elements positions. Elements at the start and end positions are included.
Arguments
int $start Start position. If it's a negative number it indecates the number of elements that must be taken before the position indicated by the second parameter
int $end End position. If it's not set the last element's position will be taken
Return
Object Reference to the current collection
Example
										$col->slice(2);	//Remove only the first two elements
										$col->slice(2,4);	//Get only elements in 2,3 and 4 positions
										$col->slice(-2);	//Get last two elements
									

filterByFunction

Filter the elements in the collection by passing them to a given function. If this function returns false the relative element is removed.
Arguments
function $fnFunction returned by create_function. It receives two parameters: the element passed as reference and it's index in the collection
array$argsArray of additional arguments to pass into the function
Return
Object Reference to the current collection
Example
										$col=& $document->getElementsByTagName("p");
										$function=create_function(
												'$element,$index,$name',
												'return $element->getAttribute("name")==$name;'
												);
										$args=array("test");
										$col->each($function,$args);	//Get all p elements with name=test
									

filter

Remove every element in the collection that doesn't match the given selector
Arguments
string $selectorSelector
Return
Object Reference to the current collection
Example
										$col=& $document->getElementsByTagName("p");
										$col->filter(".myclass");	//Get only p elements with class=myclass
									

is

Return true if at least one element matches the given selector
Arguments
string $selectorSelector
Return
Bool True if at least one element matches the given selector, otherwise false
Example
										$col=& $document->getElementsByTagName("p");
										if($col->is("[name=myname]"))	//True if at least one element has name=myname
										{
											...
										}
									

all

Return true if every element in the collection matches the given selector
Arguments
string $selectorSelector
Return
Bool True if every element in the collection matches the given selector, otherwise false
Example
										$col=& $document->getElementsByTagName("p");
										if($col->all("[name=myname]"))	//True if all p has name=myname
										{
											...
										}
									

remove

Remove from the collection every element that matches the given selector
Arguments
string $selectorSelector
Return
Object Reference to the current collection
Example
										$col=& $document->getElementsByTagName("p");
										$col->remove(".myclass");	//Remove every p with class=myclass
									

children

Replace the current collection with every direct child of each element in the collection that matches the given selector
Arguments
string $selectorSelector
Return
Object Reference to the current collection
Example
										$col=& $document->getElementsBySelector("a");
										$col->children("span.myclass");	//Now the collection contains every span element with class=myclass that is child of an a element matched by the first operation
									

find

Replace the current collection with every descendant child of each element in the collection that matches the given selector
Arguments
string $selectorSelector
Return
Object Reference to the current collection
Example
										$col=& $document->getElementsBySelector("a");
										$col->find("span.myclass");	//Now the collection contains every span element with class=myclass that is contained by an a element matched by the first operation
									

next

Replace the current collection with every first next sibling of each element in the collection that matches the given selector
Arguments
string $selectorSelector
Return
Object Reference to the current collection
Example
										$col=& $document->getElementsBySelector("a");
										$col->next("span");	//Now the collection contains every span element that is the nextSibling of an a element matched by the first operation
									

nextAll

Replace the current collection with every next sibling of each element in the collection that matches the given selector
Arguments
string $selectorSelector
Return
Object Reference to the current collection
Example
										$col=& $document->getElementsBySelector("a");
										$col->nextAll("span");	//Now the collection contains every span element that is one of the next siblings of an a element matched by the first operation
									

prev

Replace the current collection with every first previous sibling of each element in the collection that matches the given selector
Arguments
string $selectorSelector
Return
Object Reference to the current collection
Example
										$col=& $document->getElementsBySelector("a");
										$col->prev("span");	//Now the collection contains every span element that is the previousSibling of an a element matched by the first operation
									

prevAll

Replace the current collection with every previous sibling of each element in the collection that matches the given selector
Arguments
string $selectorSelector
Return
Object Reference to the current collection
Example
										$col=& $document->getElementsBySelector("a");
										$col->prevAll("span");	//Now the collection contains every span element that is one of the previous siblings of an a element matched by the first operation
									

siblings

Replace the current collection with every sibling of each element in the collection that matches the given selector
Arguments
string $selectorSelector
Return
Object Reference to the current collection
Example
										$col=& $document->getElementsBySelector("a");
										$col->siblings("span");	//Now the collection contains every span element that is one of the siblings of an a element matched by the first operation
									

parent

Replace the current collection with the parent node of each element in the collection that matches the given selector
Arguments
string $selectorSelector
Return
Object Reference to the current collection
Example
										$col=& $document->getElementsBySelector("a");
										$col->parent("span");	//Now the collection contains every span element that is the parentNode of an a element matched by the first operation
									

parents

Replace the current collection with every ancestor node of each element in the collection that matches the given selector
Arguments
string $selectorSelector
Return
Object Reference to the current collection
Example
										$col=& $document->getElementsBySelector("a");
										$col->parents("span");	//Now the collection contains every span element that contains an a element matched by the first operation
									
HTML filter iterator class. This class provides a way to build a DOMCollection executing a given function, choosing an iteration type and selecting which nodes will be included in the search.

Predefined constants

Iteration type constants

HTML_SEARCH_DESCENDANTearch in element's descendant nodes
HTML_SEARCH_CHILDRENSearch in element's children
HTML_SEARCH_NEXT_SIBLINGSearch only element's next sibling
HTML_SEARCH_ALL_NEXT_SIBLINGSSearch in element's next siblings
HTML_SEARCH_PREVIOUS_SIBLINGSearch only element's previous sibling
HTML_SEARCH_ALL_PREVIOUS_SIBLINGSSearch in element's previous siblings
HTML_SEARCH_SIBLINGSSearch in all element's siblings (next and previous)
HTML_SEARCH_PARENTSearch in element's parent node
HTML_SEARCH_ANCESTORSearch in element's ancestor nodes

Public methods

find

Static function that returns a collection of elements that return true if passed in the given function.
Arguments
mixed $contextElement, array of elements or collection from which to start searching
function $functionFilter function returned bt create_function. Two parameters will be passed: the element (as reference), the index of the element relative to the same level allowed elments
int $searchtypeOne of the iteration type constants
array $allowedElementsArray of node type constants. Only nodes with one of this type will be included in the search. If this variable is not set every node will be included.
Return
Object Dom collection
Example
									$context=& $document->body;	//Take the body as the context
									$func=create_function(
											'$element,$index',
											'if($element->hasAttributes()) return true; else return false;'
											);
									//Get all nodes with at least one attribute
									$col=& HTMLFilterIterator::find($context,$func);
									//Get all direct children with at least one attribute
									$col2=& HTMLFilterIterator::find($context,$func,HTML_SEARCH_CHILDREN);
									//Get all siblings of the context with at least one attribute
									$col3=& HTMLFilterIterator::find($context,$func,HTML_SEARCH_SIBLINGS);
								
Example
									$context=& $document->body;	//Take the body as the context								
									$func=create_function(
											'$element,$index',
											'if($element->textContent) return true; else return false;'
											);
									//Get all text nodes in the body sub tree that are not empty
									$col4=& HTMLFilterIterator::find(
											$context,
											$func,
											HTML_SEARCH_DESCENDANT,
											array(TEXT_NODE)
										);
								
Copyright 2009 Marco Marchiņ

Author: Marco Marchiņ

License:
This code is released under LGPL license. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.