Dom

A class to represent a DOM structure - a collection of nodes. Class Dom
implements Countable , IteratorAggregate , Traversable
Methods Summary
protected static
# createInvalidContentException( $content )
Create invalid content exception for the specified content.
protected static
# traverseMatch( SDom\Dom $dom , array $selectorTokens , SDom\Node\NodeInterface $node , SDom\Node\NodeInterface $effectiveRoot )
Traverse the specified node and all of its child nodes recursively (any node type is accepted, but only element nodes are processed) and match against the specified selector tokens. Add all nodes that match at least one of the selector tokens to the specified Dom collection and return it. The supplied $effectiveRoot node will be considered the root of the tree, even if there are more ancestors. Child nodes will be treated, when matching, as if they don't have a parent node.
protected static
# findFirstElement( array $nodes )
Loop over the supplied array of nodes and return the first matched element node or NULL.
protected static
# findFirstInnermostElement( array $nodes , SDom\Node\Element $fallback = NULL )
Loop over the supplied array of nodes and find the first matched element node. If it has child nodes, find the first matched element child, then recurse until the inner-most element node with no children (element nodes) is found and return it. If the array does not contain an element node, return NULL.
public
# __construct( mixed $content = NULL )
Dom constructor. Create new collection from the specified content. If the content is NULL, create empty collection. If the content is a Dom instance, copy the nodes into the new collection. If the content is a NodeInterface, add it to the collection. If the content is a Token, convert it to the corresponding NodeInterface (tree) and add it to the collection. If the content is a TokenCollection, convert it to NodeInterface instances and add them to the collection. If the content is a string, parse as HTML and treat as TokenCollection. Strings with all whitespace produce empty collections. Strings with invalid HTML throw an exception.
public
# __toString( )
Return the concatenated string representation of all nodes in the collection.
public
# getIterator( )
Return an \ArrayIterator with all nodes wrapped in a Dom instance.

Implementation of

public
# count( )
Return the number of wrapped nodes.

Implementation of

public
# get( int $index = NULL )
Retrieve a NodeInterface instance for the specified index, or an array of all NodeInterface instances if index is not specified. Throw \OutOfBoundsException exception if the specified index is out of bounds.
public
# add( $content )
Add the specified content to the end of the collection.
public
# clear( )
Clear the collection.
public
# eq( int $index )
Retrieve a new Dom collection where the set of matched elements is reduced to the one at the specified index. If the specified index is out of bounds an empty collection is returned.
public
# first( )
Retrieve a new Dom collection where the set of matched elements is reduced to the first in the set. If the collection is empty a new empty collection is returned.
public
# last( )
Retrieve a new Dom collection where the set of matched elements is reduced to the last in the set. If the collection is empty a new empty collection is returned.
public
# children( )
Get a new Dom collection with the immediate child nodes of each of the Element nodes in the collection.
public
# append( $content )
Insert content after all immediate child nodes of each Element node in the collection. If any node derived from the content already has a parent node, a cloned copy will be used instead and it will be assigned a new parent node. This means that, if appended to more than one Element node, references to each appended node will only point to the very first insertion. E.g. if the same node is appended to two or more Element nodes, its reference will point to the node with the first Element as parent. Nodes appended to all other Element nodes will be cloned copies. The same rule applies to child nodes of appended nodes, at any depth, as the whole sub-tree is cloned recursively.
public
# prepend( $content )
Insert content before all immediate child nodes of each Element node in the collection. If any node derived from the content already has a parent node, a cloned copy will be used instead and it will be assigned a new parent node. This means that, if prepended to more than one Element node, references to each prepended node will only point to the very first insertion. E.g. if the same node is prepended to two or more Element nodes, its reference will point to the node with the first Element as parent. Nodes prepended to all other Element nodes will be cloned copies. The same rule applies to child nodes of prepended nodes, at any depth, as the whole sub-tree is cloned recursively. If the supplied content resolves to a collection of nodes, they will be prepended as a group, keeping the order.
public
# before( $content )
Insert content before each Element node in the collection. If an element in the collection does not have a parent node it will be skipped / ignored. If any node derived from the content already has a parent node, a cloned copy will be used instead and it will be assigned a new parent node. This means that, if inserted before more than one Element node, references to each inserted node will only point to the very first successful insertion. E.g. if the same node is inserted before two or more Element nodes, its reference will point to the node inserted before the first eligible Element in the collection (an Element without a parent node is not eligible and will be ignored). Nodes inserted before all other Element nodes will be cloned copies. The same rule applies to child nodes of inserted nodes, at any depth, as the whole sub-tree is cloned recursively.
public
# after( $content )
Insert content after each Element node in the collection. If an element in the collection does not have a parent node it will be skipped / ignored. If any node derived from the content already has a parent node, a cloned copy will be used instead and it will be assigned a new parent node. This means that, if inserted after more than one Element node, references to each inserted node will only point to the very first successful insertion. E.g. if the same node is inserted after two or more Element nodes, its reference will point to the node inserted after the first eligible Element in the collection (an Element without a parent node is not eligible and will be ignored). Nodes inserted after all other Element nodes will be cloned copies. The same rule applies to child nodes of inserted nodes, at any depth, as the whole sub-tree is cloned recursively.
public
# wrap( $content )
Wrap a clone of the supplied content around each node with a parent (not only element nodes) in the collection. If the content resolves to a collection of more than one wrapping element node, use only the first one. If the wrapping element has children use a single-element-per-level sub-tree, where the wrapping element is the root and may contain one and only one element node, which may contain another one and only one element node and so on. Wrap the current collection nodes in clones of this sub-tree as immediate children of the inner-most element node of the tree. If a node in the collection does not have a parent element, ignore it. If the wrapping collection does not contain at least one element node, do nothing. Return the original collection for chaining.
public
# wrapInner( $content )
Wrap a clone of the supplied content around each child node (not only element nodes) of nodes in the collection. This function works exactly like wrap() except it wraps the children of the nodes in the collection instead. Return the original collection for chaining.
public
# find( string $selector )
Return a new Dom collection of all the descendants of each Element node in the current collection, filtered by the specified CSS selector.
public
# addClass( string $className )
Adds the specified class(es) to each Element node in the collection.
public
# removeClass( string $className = NULL )
Remove a single class, multiple classes, or all classes from each Element node in the collection.
public
# hasClass( string $className )
Return TRUE if at least one of the Element nodes in the collection has the specified class assigned.
public
# attr( string $name , string $value = NULL )
Get the value of an attribute for the first Element node in the collection. Set the value of an attribute for each Element node in the collection.
public
# removeAttr( string $name )
Remove an attribute from each Element node in the collection.
public
# text( string $text = NULL )
Get the combined text contents of each Element node in the collection, including their descendants. Set the content of each Element node in the collection to the specified text.
public
# html( string $html = NULL )
Get the HTML contents of the first Element node in the collection. Set the HTML contents of each Element node in the collection.
Properties Summary
protected static Symfony\Component\CssSelector\Parser\Parser $selectorParser
Singleton instance to a CSS selector parser.
# NULL
protected static SDom\SelectorMatcher $selectorMatcher
Singleton instance to a CSS selector matcher.
# NULL
protected static Kevintweber\HtmlTokenizer\HtmlTokenizer $tokenizer
Singleton instance to an HTML tokenizer.
# NULL
protected \SDom\Node\NodeInterface[] $nodes
Collection of nodes.
# NULL