html_compose.custom_element

 1from typing import Iterable, Mapping
 2
 3from .attributes import BaseAttribute, GlobalAttrs
 4from .base_element import BaseElement
 5from .base_types import Resolvable
 6from .util_funcs import safe_name
 7
 8
 9class CustomElement(BaseElement):
10    """
11    Custom HTML element
12    """
13
14    tag = "UNSET"
15    is_void = False
16
17    def __init__(
18        self,
19        attrs: Iterable[BaseAttribute]
20        | Mapping[str, Resolvable]
21        | Iterable[
22            BaseAttribute | Iterable[BaseAttribute] | Mapping[str, Resolvable]
23        ]
24        | None = None,
25        id: str | None = None,
26        class_: str | list | None = None,
27        children: list | None = None,
28    ):
29        """
30        Initialize a custom HTML element
31
32
33        Parameters
34        ----------
35        `attrs`:
36            A list or dictionary of attributes for the element
37
38        `id` :
39            The element's ID
40
41        `class_` :
42            Classes to which the element belongs
43
44        `children` :
45            A list of child elements. Defaults to None.
46
47        """
48        tag = self.__class__.tag
49        if tag == "UNSET":
50            raise ValueError(
51                "CustomElement must be created with a tag name using the create() method."
52            )
53        super().__init__(
54            self.__class__.tag,
55            void_element=self.__class__.is_void,
56            attrs=attrs,
57            children=children,
58        )
59        if not (id is None or id is False):
60            self._process_attr("id", id)
61        if not (class_ is None or class_ is False):
62            self._process_attr("class", class_)
63
64    class hint(GlobalAttrs):
65        pass
66
67    _ = hint
68
69    @staticmethod
70    def create(tag: str, void_element: bool = False) -> type["CustomElement"]:
71        """
72        Create a new element class with the given tag and void_element flag.
73        This method is a factory for creating new element classes.
74        """
75        return type(
76            safe_name(tag),
77            (CustomElement,),
78            {"tag": tag, "is_void": void_element},
79        )
class CustomElement(html_compose.base_element.BaseElement):
10class CustomElement(BaseElement):
11    """
12    Custom HTML element
13    """
14
15    tag = "UNSET"
16    is_void = False
17
18    def __init__(
19        self,
20        attrs: Iterable[BaseAttribute]
21        | Mapping[str, Resolvable]
22        | Iterable[
23            BaseAttribute | Iterable[BaseAttribute] | Mapping[str, Resolvable]
24        ]
25        | None = None,
26        id: str | None = None,
27        class_: str | list | None = None,
28        children: list | None = None,
29    ):
30        """
31        Initialize a custom HTML element
32
33
34        Parameters
35        ----------
36        `attrs`:
37            A list or dictionary of attributes for the element
38
39        `id` :
40            The element's ID
41
42        `class_` :
43            Classes to which the element belongs
44
45        `children` :
46            A list of child elements. Defaults to None.
47
48        """
49        tag = self.__class__.tag
50        if tag == "UNSET":
51            raise ValueError(
52                "CustomElement must be created with a tag name using the create() method."
53            )
54        super().__init__(
55            self.__class__.tag,
56            void_element=self.__class__.is_void,
57            attrs=attrs,
58            children=children,
59        )
60        if not (id is None or id is False):
61            self._process_attr("id", id)
62        if not (class_ is None or class_ is False):
63            self._process_attr("class", class_)
64
65    class hint(GlobalAttrs):
66        pass
67
68    _ = hint
69
70    @staticmethod
71    def create(tag: str, void_element: bool = False) -> type["CustomElement"]:
72        """
73        Create a new element class with the given tag and void_element flag.
74        This method is a factory for creating new element classes.
75        """
76        return type(
77            safe_name(tag),
78            (CustomElement,),
79            {"tag": tag, "is_void": void_element},
80        )

Custom HTML element

CustomElement( attrs: Union[Iterable[html_compose.base_attribute.BaseAttribute], Mapping[str, Union[NoneType, str, float, int, bool, Iterable[str | float | int | bool], Mapping[str | float | int | bool, bool]]], Iterable[Union[html_compose.base_attribute.BaseAttribute, Iterable[html_compose.base_attribute.BaseAttribute], Mapping[str, Union[NoneType, str, float, int, bool, Iterable[str | float | int | bool], Mapping[str | float | int | bool, bool]]]]], NoneType] = None, id: str | None = None, class_: str | list | None = None, children: list | None = None)
18    def __init__(
19        self,
20        attrs: Iterable[BaseAttribute]
21        | Mapping[str, Resolvable]
22        | Iterable[
23            BaseAttribute | Iterable[BaseAttribute] | Mapping[str, Resolvable]
24        ]
25        | None = None,
26        id: str | None = None,
27        class_: str | list | None = None,
28        children: list | None = None,
29    ):
30        """
31        Initialize a custom HTML element
32
33
34        Parameters
35        ----------
36        `attrs`:
37            A list or dictionary of attributes for the element
38
39        `id` :
40            The element's ID
41
42        `class_` :
43            Classes to which the element belongs
44
45        `children` :
46            A list of child elements. Defaults to None.
47
48        """
49        tag = self.__class__.tag
50        if tag == "UNSET":
51            raise ValueError(
52                "CustomElement must be created with a tag name using the create() method."
53            )
54        super().__init__(
55            self.__class__.tag,
56            void_element=self.__class__.is_void,
57            attrs=attrs,
58            children=children,
59        )
60        if not (id is None or id is False):
61            self._process_attr("id", id)
62        if not (class_ is None or class_ is False):
63            self._process_attr("class", class_)

Initialize a custom HTML element

Parameters

attrs: A list or dictionary of attributes for the element

id : The element's ID

class_ : Classes to which the element belongs

children : A list of child elements. Defaults to None.

tag = 'UNSET'
is_void = False
@staticmethod
def create( tag: str, void_element: bool = False) -> type[CustomElement]:
70    @staticmethod
71    def create(tag: str, void_element: bool = False) -> type["CustomElement"]:
72        """
73        Create a new element class with the given tag and void_element flag.
74        This method is a factory for creating new element classes.
75        """
76        return type(
77            safe_name(tag),
78            (CustomElement,),
79            {"tag": tag, "is_void": void_element},
80        )

Create a new element class with the given tag and void_element flag. This method is a factory for creating new element classes.

class CustomElement.hint(html_compose.attributes.global_attrs.GlobalAttrs):
65    class hint(GlobalAttrs):
66        pass

This module contains classes for all global attributes. Elements can inherit it so the element can be a reference to our attributes