WooCommerce Code Reference

Dom_Document_Helper
in package

This class should guarantee that our work with the DOMDocument is unified and safe.

Table of Contents

$dom  : DOMDocument
Instance of the DOMDocument.
__construct()  : mixed
Constructor.
find_element()  : DOMElement|null
Searches for the first appearance of the given tag name.
find_elements()  : array<int, DOMElement>
Returns every element matching the given tag name, in document order.
get_attribute_value()  : string
Returns the value of the given attribute from the given element.
get_attribute_value_by_tag_name()  : string|null
Searches for the first appearance of the given tag name and returns the value of specified attribute.
get_element_inner_html()  : string
Returns the inner HTML of the given element.
get_outer_html()  : string
Returns the outer HTML of the given element.
get_root_html()  : string
Serializes every top-level node of the loaded fragment back to HTML.
remove_element()  : void
Removes the given element from the document.
load_html()  : void
Loads the given HTML content into the DOMDocument.

Properties

Methods

get_attribute_value()

Returns the value of the given attribute from the given element.

public get_attribute_value(DOMElement $element, string $attribute) : string
Parameters
$element : DOMElement

The element to get the attribute value from.

$attribute : string

The attribute to get the value from.

Return values
string

get_attribute_value_by_tag_name()

Searches for the first appearance of the given tag name and returns the value of specified attribute.

public get_attribute_value_by_tag_name(string $tag_name, string $attribute) : string|null
Parameters
$tag_name : string

The tag name to search for.

$attribute : string

The attribute to get the value from.

Return values
string|null

get_root_html()

Serializes every top-level node of the loaded fragment back to HTML.

public get_root_html() : string

The document is loaded with LIBXML_HTML_NOIMPLIED (no implicit html/body wrapper), so the top-level nodes are the fragment's own roots. Useful for reading back what remains after elements have been removed.

Return values
string

remove_element()

Removes the given element from the document.

public remove_element(DOMElement $element) : void

A no-op when the element has already been detached (its parent is null), so removing the same node twice — e.g. two images sharing one wrapper — is safe.

Parameters
$element : DOMElement

The element to remove.

Return values
void