| @@ -18,6 +18,7 @@ | |||||
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||
| # SOFTWARE. | # SOFTWARE. | ||||
| from typing import Any, Optional | |||||
| import html.entities as htmlentities | import html.entities as htmlentities | ||||
| from ._base import Node | from ._base import Node | ||||
| @@ -28,9 +29,15 @@ __all__ = ["HTMLEntity"] | |||||
| class HTMLEntity(Node): | class HTMLEntity(Node): | ||||
| """Represents an HTML entity, like `` ``, either named or unnamed.""" | """Represents an HTML entity, like `` ``, either named or unnamed.""" | ||||
| def __init__(self, value, named=None, hexadecimal=False, hex_char="x"): | |||||
| def __init__( | |||||
| self, | |||||
| value: Any, | |||||
| named: Optional[bool] = None, | |||||
| hexadecimal: bool = False, | |||||
| hex_char: str = "x", | |||||
| ): | |||||
| super().__init__() | super().__init__() | ||||
| self._value = value | |||||
| self._value = str(value) | |||||
| if named is None: # Try to guess whether or not the entity is named | if named is None: # Try to guess whether or not the entity is named | ||||
| try: | try: | ||||
| int(value) | int(value) | ||||
| @@ -49,50 +56,25 @@ class HTMLEntity(Node): | |||||
| self._hexadecimal = hexadecimal | self._hexadecimal = hexadecimal | ||||
| self._hex_char = hex_char | self._hex_char = hex_char | ||||
| def __str__(self): | |||||
| def __str__(self) -> str: | |||||
| if self.named: | if self.named: | ||||
| return "&{};".format(self.value) | return "&{};".format(self.value) | ||||
| if self.hexadecimal: | if self.hexadecimal: | ||||
| return "&#{}{};".format(self.hex_char, self.value) | return "&#{}{};".format(self.hex_char, self.value) | ||||
| return "&#{};".format(self.value) | return "&#{};".format(self.value) | ||||
| def __strip__(self, **kwargs): | |||||
| def __strip__(self, **kwargs: Any) -> Optional[str]: | |||||
| if kwargs.get("normalize"): | if kwargs.get("normalize"): | ||||
| return self.normalize() | return self.normalize() | ||||
| return self | |||||
| return str(self) | |||||
| @property | @property | ||||
| def value(self): | |||||
| def value(self) -> str: | |||||
| """The string value of the HTML entity.""" | """The string value of the HTML entity.""" | ||||
| return self._value | return self._value | ||||
| @property | |||||
| def named(self): | |||||
| """Whether the entity is a string name for a codepoint or an integer. | |||||
| For example, ``Σ``, ``Σ``, and ``Σ`` refer to the same | |||||
| character, but only the first is "named", while the others are integer | |||||
| representations of the codepoint. | |||||
| """ | |||||
| return self._named | |||||
| @property | |||||
| def hexadecimal(self): | |||||
| """If unnamed, this is whether the value is hexadecimal or decimal.""" | |||||
| return self._hexadecimal | |||||
| @property | |||||
| def hex_char(self): | |||||
| """If the value is hexadecimal, this is the letter denoting that. | |||||
| For example, the hex_char of ``"ሴ"`` is ``"x"``, whereas the | |||||
| hex_char of ``"ሴ"`` is ``"X"``. Lowercase and uppercase ``x`` | |||||
| are the only values supported. | |||||
| """ | |||||
| return self._hex_char | |||||
| @value.setter | @value.setter | ||||
| def value(self, newval): | |||||
| def value(self, newval: Any) -> None: | |||||
| newval = str(newval) | newval = str(newval) | ||||
| try: | try: | ||||
| int(newval) | int(newval) | ||||
| @@ -122,11 +104,22 @@ class HTMLEntity(Node): | |||||
| self._named = False | self._named = False | ||||
| self._value = newval | self._value = newval | ||||
| @property | |||||
| def named(self) -> bool: | |||||
| """Whether the entity is a string name for a codepoint or an integer. | |||||
| For example, ``Σ``, ``Σ``, and ``Σ`` refer to the same | |||||
| character, but only the first is "named", while the others are integer | |||||
| representations of the codepoint. | |||||
| """ | |||||
| return self._named | |||||
| @named.setter | @named.setter | ||||
| def named(self, newval): | |||||
| def named(self, newval: bool) -> None: | |||||
| newval = bool(newval) | newval = bool(newval) | ||||
| if newval and self.value not in htmlentities.entitydefs: | if newval and self.value not in htmlentities.entitydefs: | ||||
| raise ValueError("entity value {!r} is not a valid name".format(self.value)) | raise ValueError("entity value {!r} is not a valid name".format(self.value)) | ||||
| if not newval: | if not newval: | ||||
| try: | try: | ||||
| int(self.value, 16) | int(self.value, 16) | ||||
| @@ -138,21 +131,36 @@ class HTMLEntity(Node): | |||||
| ) from exc | ) from exc | ||||
| self._named = newval | self._named = newval | ||||
| @property | |||||
| def hexadecimal(self) -> bool: | |||||
| """If unnamed, this is whether the value is hexadecimal or decimal.""" | |||||
| return self._hexadecimal | |||||
| @hexadecimal.setter | @hexadecimal.setter | ||||
| def hexadecimal(self, newval): | |||||
| def hexadecimal(self, newval: bool) -> None: | |||||
| newval = bool(newval) | newval = bool(newval) | ||||
| if newval and self.named: | if newval and self.named: | ||||
| raise ValueError("a named entity cannot be hexadecimal") | raise ValueError("a named entity cannot be hexadecimal") | ||||
| self._hexadecimal = newval | self._hexadecimal = newval | ||||
| @property | |||||
| def hex_char(self) -> str: | |||||
| """If the value is hexadecimal, this is the letter denoting that. | |||||
| For example, the hex_char of ``"ሴ"`` is ``"x"``, whereas the | |||||
| hex_char of ``"ሴ"`` is ``"X"``. Lowercase and uppercase ``x`` | |||||
| are the only values supported. | |||||
| """ | |||||
| return self._hex_char | |||||
| @hex_char.setter | @hex_char.setter | ||||
| def hex_char(self, newval): | |||||
| def hex_char(self, newval: str) -> None: | |||||
| newval = str(newval) | newval = str(newval) | ||||
| if newval not in ("x", "X"): | if newval not in ("x", "X"): | ||||
| raise ValueError(newval) | raise ValueError(newval) | ||||
| self._hex_char = newval | self._hex_char = newval | ||||
| def normalize(self): | |||||
| def normalize(self) -> str: | |||||
| """Return the unicode character represented by the HTML entity.""" | """Return the unicode character represented by the HTML entity.""" | ||||
| if self.named: | if self.named: | ||||
| return chr(htmlentities.name2codepoint[self.value]) | return chr(htmlentities.name2codepoint[self.value]) | ||||