Skip to content
This repository was archived by the owner on Jul 30, 2026. It is now read-only.

DOM Helpers

Josh edited this page Aug 6, 2017 · 7 revisions

This class contains DOM-traversal helpers. This is largely focused around making certain \DOMDocument and \DOMXPath tasks easier, but some functionality is independent of those extensions.

Use:
blobfolio\common\dom

get_nodes_by_class()

Get an array of nodes containing one or more specified classes.

Arguments

Type Description Notes Default
DOMDocument|DOMElement Parent object.
array Class(es).
bool Match all. If TRUE, only nodes with every passed class will be returned. Otherwise nodes matching any class will be returned. FALSE

Returns

Returns an array containing the matching nodes, if any.

Example

$foo = \blobfolio\common\dom::get_nodes_by_class($dom, 'apples');

load_svg()

Generate a \DOMDocument object from a string containing SVG code. This performs some initial setup optimized for SVG content.

Arguments

Type Description
string SVG code.

Returns

Returns a \DOMDocument object (empty on failure).

Example

$svg = file_get_contents('foo.svg');
$dom = \blobfolio\common\dom::load_svg($svg);

parse_css()

This is a handy little parser that will take CSS rules (e.g. from <style> tags or a .css file) and hand it back to you in an easily parsable array format.

Arguments

Type Description
string Style code.

Returns

Returns an array containing the parsed styles.

Example

//from a .css file
$css = @file_get_contents('css/styles.css');
//or something like...
$css = $dom->getElementsByTagName('style')->item(0)->nodeValue;

//then parse
$foo = \blobfolio\common\dom::parse_css($css);

/*
Array(
    //example of a regular old rule
    0 => Array(
        [@] => FALSE
        [nested] => FALSE
        [selectors] => Array(
            0 => .blobfolio-about-logo svg
        )
        [rules] => Array(
            [transition] => color .3s ease;
            [display] => block;
            [width] => 100%;
            [height] => auto;
        )
        [raw] => .blobfolio-about-logo svg{transition:color .3s ease;display:block;width:100%;height:auto;}
    ),
    //example @ rule with regular definitions
    1 => Array(
        [@] => font-face
        [nested] => FALSE
        [selectors] => Array(
            0 => @font-face
        )
        [rules] => Array(
            [font-family] => "TotallyRealFont";
            [src] => url ("../font/3197DB_0_0.woff2") format ("woff2"), url ("../font/3197DB_0_0.woff") format ("woff"), url ("../font/3197DB_0_0.ttf") format ("truetype");
            [font-weight] => 700;
            [font-style] => normal;
        )
        [raw] => @font-face{font-family:"TotallyRealFont";src:url ("../font/3197DB_0_0.woff2") format ("woff2"), url ("../font/3197DB_0_0.woff") format ("woff"), url ("../font/3197DB_0_0.ttf") format ("truetype");font-weight:700;font-style:normal;}
    ),
    //example @ rule without any {...} block
    2 => Array(
        [@] => import
        [nested] => FALSE
        [selectors] => Array()
        [rules] => Array(
            0 => @import url (https://fonts.googleapis.com/css?family=Open+Sans);
        )
        [raw] => @import url (https://fonts.googleapis.com/css?family=Open+Sans);
    ),
    //example nested @ rule
    3 => Array(
        [@] => media
        [nested] => TRUE
        [selector] => @media screen only and (min-width: 10em)
        [nest] => Array (
            0 => Array(
                [@] => FALSE
                [nested] => FALSE
                [selectors] => Array(
                    0 => #debug-log
                )
                [rules] => Array(
                    [background] => red;
                )
                [raw] => #debug-log{background:red;}
            )
        )
        [raw] => @media screen only and (min-width: 10em){#debug-log{background:red;}}
    )
)
*/

remove_namespace()

Remove a namespace and any corresponding tags from a \DOMDocument object.

Arguments

Type Description
DOMDocument DOM object.
string Namespace.

Returns

Returns TRUE or FALSE

Example

\blobfolio\common\dom::remove_namespace($dom, 'foobar');

remove_node()

Remove a \DOMElement or \DOMNode.

Arguments

Type Description
DOMElement|DOMNode Node object.

Returns

Returns TRUE or FALSE

Example

$styles = $dom->getElementsByTagName('style');
while($styles->length > 1){
    \blobfolio\common\dom::remove_node($styles->item(0));
}

remove_nodes()

Remove one or more nodes.

Arguments

Type Description
DOMNodeList Nodes object.

Returns

Returns TRUE or FALSE

Example

$styles = $dom->getElementsByTagName('style');
\blobfolio\common\dom::remove_nodes($styles);

save_svg()

Extract the SVG code from a DOMDocument object (presumably one created with dom::load_svg()).

Arguments

Type Description
DOMDocument DOM object.

Returns

Returns a string, either with the SVG code or empty on failure.

Example

$svg = \blobfolio\common\dom::save_svg($dom);

CLI

Constants

Dom Helpers

Files and Paths

Formatting

General Data Helpers

Images

Multi-Byte Wrappers

Sanitizing and Validation

Typecasting

Clone this wiki locally