class HtmlAugmenter (View source)

A tool that can augment HTML for example by adding arbitrary attributes.

This is used in order to add meta data arguments to content elements in the Backend.

Usage:

$html = '

Some HTML code
'; $result = (new HtmlAugmenter())->addAttributes($html, array('foo' => 'bar', 'bar' => 'baz'));

// will return '

Some HTML code</div'

Methods

string
addAttributes(string $html, array $attributes, string $fallbackTagName = 'div', array $exclusiveAttributes = null)

Adds the given $attributes to the $html by augmenting the root element.

getHtmlRootElement(string $html)

Detects a unique root tag in the given $html string and returns its DOMNode representation - or NULL if no unique root element could be found

void
mergeAttributes(DOMNode $element, array $newAttributes)

Merges the attributes of $element with the given $newAttributes If an attribute exists in both collections, it is merged to " " (if both values differ)

string
renderAttributes(array $attributes)

Renders the given key/value pair to a valid attribute string in the format ="" ="".

bool
elementHasAttributes(DOMNode $element, array $attributes = null)

Checks whether the given $element contains at least one of the specified $attributes (case insensitive)

Details

string addAttributes(string $html, array $attributes, string $fallbackTagName = 'div', array $exclusiveAttributes = null)

Adds the given $attributes to the $html by augmenting the root element.

Attributes are merged with the existing root element's attributes. If no unique root node can be determined, a wrapping tag is added with all the given attributes. The name of this tag can be specified with $fallbackTagName.

Parameters

string $html

The HTML code to augment

array $attributes

Attributes to be added to the root element in the format array('' => '', ...)

string $fallbackTagName

The root element tag name if one needs to be added

array $exclusiveAttributes

A list of lowercase(!) attribute names that should be exclusive to the root element. If the existing root element contains one of these a new root element is wrapped

Return Value

string

protected DOMNode getHtmlRootElement(string $html)

Detects a unique root tag in the given $html string and returns its DOMNode representation - or NULL if no unique root element could be found

Parameters

string $html

Return Value

DOMNode

protected void mergeAttributes(DOMNode $element, array $newAttributes)

Merges the attributes of $element with the given $newAttributes If an attribute exists in both collections, it is merged to " " (if both values differ)

Parameters

DOMNode $element
array $newAttributes

Return Value

void

protected string renderAttributes(array $attributes)

Renders the given key/value pair to a valid attribute string in the format ="" ="".

..

Parameters

array $attributes

The attributes to render in the format array('' => '', ...)

Return Value

string

Exceptions

Exception

protected bool elementHasAttributes(DOMNode $element, array $attributes = null)

Checks whether the given $element contains at least one of the specified $attributes (case insensitive)

Parameters

DOMNode $element
array $attributes

array of attribute names to check (lowercase)

Return Value

bool

true if at least one of the $attributes is contained in the given $element, otherwise false