WhakerPy 0.8

https://sourceforge.net/projects/whakerpy/

Module whakerpy.htmlmaker

Class BaseTagNode

Description

A node to represents an HTML element with attributes.

An HTML element without content is called an empty node. It has a start tag but neither a content nor an end tag. Compared to the parent class BaseNode, this class adds 2 members:

  1. the required element tag;
  2. its optional attributes.

For example, it can deal with elements like:

  • <tag />
  • <tag k=v />
  • <tag k1=v2 k2=v2 k3 />

Constructor

Create a new empty node.

Parameters
  • parent: (str) Parent identifier
  • identifier: (str) This node identifier
  • tag: (str) The element tag. Converted in lower case.
  • attributes: (dict) key=(str) value=(str or None)
Raises
  • NodeInvalidIdentifierError
  • NodeTagError
  • TypeError
View Source
def __init__(self, parent: str | None, identifier: str, tag: str, attributes: dict=dict()):
    """Create a new empty node.

    :param parent: (str) Parent identifier
    :param identifier: (str) This node identifier
    :param tag: (str) The element tag. Converted in lower case.
    :param attributes: (dict) key=(str) value=(str or None)
    :raises: NodeInvalidIdentifierError:
    :raises: NodeTagError:
    :raises: TypeError:

    """
    super(BaseTagNode, self).__init__(parent, identifier)
    tag = str(tag)
    self.__tag = tag.lower()
    self._attributes = dict()
    if isinstance(attributes, dict) is False:
        raise TypeError('Expected a dict for the attributes argument of BaseTagNode().')
    for key in attributes:
        value = attributes[key]
        self.add_attribute(key, value)

Public functions

tag

Return the HTML tag.

View Source
@property
def tag(self) -> str:
    """Return the HTML tag. """
    return self.__tag

check_attribute

Raises NodeAttributeError if key is not a valid attribute.

Parameters
  • key: (any) An attribute
Raises
  • NodeAttributeError: The attribute can't be assigned to this element.
  • NodeAttributeError: if given key can't be converted to string
Returns
  • key(str) valid key
View Source
def check_attribute(self, key) -> str:
    """Raises NodeAttributeError if key is not a valid attribute.

        :param key: (any) An attribute
        :raises: NodeAttributeError: The attribute can't be assigned to this element.
        :raises: NodeAttributeError: if given key can't be converted to string
        :return: key (str) valid key

        """
    try:
        key = str(key)
        key = key.lower()
    except Exception:
        raise NodeAttributeError(key)
    if key not in HTML_GLOBAL_ATTR and key.startswith('data-') is False and (key not in HTML_VISIBLE_ATTR) and (key not in HTML_TAG_ATTR.keys()) and (key not in ARIA_TAG_ATTR.keys()):
        raise NodeAttributeError(key)
    return key

get_attribute_keys

Return the list of attribute keys.

View Source
def get_attribute_keys(self) -> list:
    """Return the list of attribute keys. """
    return [k for k in self._attributes.keys()]

set_attribute

Set a property to the node. Delete the existing one, if any.

Parameters
  • key: Key property
  • value: (str or list)
Raises
  • NodeAttributeError: The attribute can't be assigned to this element.
  • NodeAttributeError: if given key can't be converted to string
Returns
  • key(str) valid assigned key
View Source
def set_attribute(self, key: str, value) -> str:
    """Set a property to the node. Delete the existing one, if any.

        :param key: Key property
        :param value: (str or list)
        :raises: NodeAttributeError: The attribute can't be assigned to this element.
        :raises: NodeAttributeError: if given key can't be converted to string
        :return: key (str) valid assigned key

        """
    key = self.check_attribute(key)
    if isinstance(value, (list, tuple)) is True:
        value = ' '.join(value)
    self._attributes[key] = value
    return key

add_attribute

Add a property to the node. Append the value if existing.

Parameters
  • key: (str) Key property
  • value
Raises
  • NodeAttributeError: The attribute can't be assigned to this element.
  • NodeAttributeError: if given key can't be converted to string
Returns
  • key(str) valid assigned key
View Source
def add_attribute(self, key: str, value) -> str:
    """Add a property to the node. Append the value if existing.

        :param key: (str) Key property
        :param value:
        :raises: NodeAttributeError: The attribute can't be assigned to this element.
        :raises: NodeAttributeError: if given key can't be converted to string
        :return: key (str) valid assigned key

        """
    if key not in self._attributes:
        self.set_attribute(key, value)
    elif self._attributes[key] is not None:
        self._attributes[key] += ' ' + value
    else:
        self._attributes[key] = value
    return key

get_attribute_value

Return the attribute value if the node has this attribute.

Parameters
  • key: (str) Attribute key
Returns
  • (str | None) Value or None if the attribute does not exist or has no value
View Source
def get_attribute_value(self, key: str):
    """Return the attribute value if the node has this attribute.

        :param key: (str) Attribute key
        :return: (str | None) Value or None if the attribute does not exist or has no value

        """
    if key in self._attributes:
        return self._attributes[key]
    return None

has_attribute

Return true if the node has the attribute.

Parameters
  • key: (str) Attribute key
Returns
  • (bool)
View Source
def has_attribute(self, key: str) -> bool:
    """Return true if the node has the attribute.

        :param key: (str) Attribute key
        :return: (bool)

        """
    return key in self._attributes

remove_attribute

Remove the attribute to the node.

Parameters
  • key: (str) Attribute key
View Source
def remove_attribute(self, key: str) -> None:
    """Remove the attribute to the node.

        :param key: (str) Attribute key

        """
    if key in self._attributes:
        del self._attributes[key]

remove_attribute_value

Remove the value of an attribute of the node.

Parameters
  • key: (str) Attribute key
  • value: (str) Attribute value
View Source
def remove_attribute_value(self, key: str, value: str) -> None:
    """Remove the value of an attribute of the node.

        :param key: (str) Attribute key
        :param value: (str) Attribute value

        """
    if key in self._attributes:
        values = self._attributes[key].split(' ')
        if value in values:
            values.remove(value)
            if len(values) == 0:
                del self._attributes[key]
            else:
                self.set_attribute(key, ' '.join(values))

nb_attributes

Return the number of attributes.

View Source
def nb_attributes(self) -> int:
    """Return the number of attributes. """
    return len(self._attributes)

serialize

Override. Serialize the node into HTML.

Parameters
  • nbs: (int) Number of spaces for the indentation
Returns
  • (str)
View Source
def serialize(self, nbs: int=4) -> str:
    """Override. Serialize the node into HTML.

        :param nbs: (int) Number of spaces for the indentation
        :return: (str)

        """
    indent = ' ' * nbs
    html = indent + '<' + self.__tag
    for key in self._attributes:
        html += ' '
        html += key
        value = self._attributes[key]
        if value is not None:
            html += '="'
            html += value
            html += '"'
    html += ' />\n'
    return html