Skip to main content

Element

This class represents a structured document element consisting of a name, a sorted list of attributes, and a collection of child nodes. It provides functionality for appending content with automatic whitespace normalization, finalizing the element tree, and performing deep equality comparisons. Additionally, the class supports containment checks and occurrence counting for both text content and nested element structures.

Attributes

AttributeTypeDescription
namestringThe tag name of the element used for identification and string representation.
attributeslistA sorted list of key-value pairs representing the element's HTML or XML attributes.
childrenlist = []A list containing nested Element objects or strings representing the inner content of the element.

Constructor

Signature

def Element(
name: string,
attributes: iterable
)

Parameters

NameTypeDescription
namestringThe name of the element.
attributesiterableA collection of attribute key-value pairs to be sorted and stored.

Methods


append()

@classmethod
def append(
element: [Union](../../contrib/gis/db/models/functions/union.md?sid=django_contrib_gis_db_models_functions_union)[[Element](element.md?sid=django_test_html_element), str]
) - > null

Adds a child element or text string to this element, normalizing whitespace and merging adjacent text nodes.

Parameters

NameTypeDescription
element[Union](../../contrib/gis/db/models/functions/union.md?sid=django_contrib_gis_db_models_functions_union)[[Element](element.md?sid=django_test_html_element), str]The child element or text content to be added to the internal children list.

Returns

TypeDescription
nullNothing

finalize()

@classmethod
def finalize() - > null

Recursively cleans up the element tree by stripping trailing whitespace from text nodes and removing empty text children.

Returns

TypeDescription
nullNothing

count()

@classmethod
def count(
element: [Union](../../contrib/gis/db/models/functions/union.md?sid=django_contrib_gis_db_models_functions_union)[[Element](element.md?sid=django_test_html_element), str]
) - > int

Calculates the total number of times a specific element or text string appears within this element's hierarchy.

Parameters

NameTypeDescription
element[Union](../../contrib/gis/db/models/functions/union.md?sid=django_contrib_gis_db_models_functions_union)[[Element](element.md?sid=django_test_html_element), str]The target element or text string to be counted.

Returns

TypeDescription
intThe total count of matching elements or text occurrences.