Skip to content

Commit

Permalink
Add whitespace sensitive tag support
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyGuthridge committed May 3, 2024
1 parent 8a23a05 commit 9356b7c
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 19 deletions.
5 changes: 4 additions & 1 deletion meta/tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,11 @@ div:
span:
base: StylableTag

pre:
base: WhitespaceSensitiveTag

textarea:
base: StylableTag
base: WhitespaceSensitiveTag
attributes:
required:
doc: Whether the input is required to submit the form it is contained within.
Expand Down
46 changes: 46 additions & 0 deletions meta/templates/class_attrs_WhitespaceSensitiveTag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class {name}({base}):
"""
{description}
{attr_docs_outer}
[View full documentation]({link})
"""
def __init__(
self,
*children: ChildrenType,
{attr_args}
**attributes: AttributeType,
) -> None:
"""
{description}
{attr_docs_inner}
[View full documentation]({link})
"""
attributes |= {
{attr_unions}
}
super().__init__(*children, **attributes)

def __call__( # type: ignore
self,
*children: ChildrenType,
{attr_args}
**attributes: AttributeType,
):
"""
{description}
{attr_docs_inner}
[View full documentation]({link})
"""
attributes |= {
{attr_unions}
}
return super().__call__(*children, **attributes)

def _get_default_attributes(self, given: dict[str, AttributeType]) -> dict[str, AttributeType]:
return {default_attrs}
2 changes: 1 addition & 1 deletion meta/templates/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
https://creativecommons.org/licenses/by-sa/2.5/
"""
from typing import Any, Optional, Union, Literal
from ..__tag_base import Tag, SelfClosingTag
from ..__tag_base import Tag, SelfClosingTag, WhitespaceSensitiveTag
from ..__types import AttributeType, ChildrenType
44 changes: 44 additions & 0 deletions pyhtml/__tag_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,47 @@ def _render(self, indent: int) -> list[str]:
]
else:
return [f"{' ' * indent}<{self._get_tag_name()}/>"]


class WhitespaceSensitiveTag(Tag):
"""
Whitespace-sensitive tags are tags where whitespace needs to be respected.
"""
def __init__(
self,
*children: ChildrenType,
**attributes: AttributeType,
) -> None:
attributes |= {}
super().__init__(*children, **attributes)

def __call__( # type: ignore
self,
*children: ChildrenType,
**attributes: AttributeType,
):
attributes |= {}
return super().__call__(*children, **attributes)

def _render(self, indent: int) -> list[str]:
attributes = util.filter_attributes(util.dict_union(
self._get_default_attributes(self.attributes),
self.attributes,
))

# Tag and attributes
output = f"{' ' * indent}<{self._get_tag_name()}"

if len(attributes):
output += f" {util.render_tag_attributes(attributes)}>"
else:
output += ">"

output += '\n'.join(util.render_children(
self.children,
self._escape_children(),
0,
))

output += f"</{self._get_tag_name()}>"
return output.splitlines()
2 changes: 1 addition & 1 deletion pyhtml/__tags/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""
# Re-export renamed versions of tags
from .renames import input_, object_
from .input import input
from .input_tag import input
from .dangerous_raw_html import DangerousRawHtml
from .comment import Comment

Expand Down
18 changes: 3 additions & 15 deletions pyhtml/__tags/generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
https://creativecommons.org/licenses/by-sa/2.5/
"""
from typing import Any, Optional, Union, Literal
from ..__tag_base import Tag, SelfClosingTag
from ..__tag_base import Tag, SelfClosingTag, WhitespaceSensitiveTag
from ..__types import AttributeType, ChildrenType

class html(Tag):
Expand Down Expand Up @@ -1857,7 +1857,7 @@ def _get_default_attributes(self, given: dict[str, AttributeType]) -> dict[str,
return {}


class pre(Tag):
class pre(WhitespaceSensitiveTag):
"""
Represents preformatted text which is to be presented exactly as written in the HTML file. The text is typically rendered using a non-proportional, or [monospaced](https://en.wikipedia.org/wiki/Monospaced_font), font. Whitespace inside this element is displayed as written.
Expand Down Expand Up @@ -5444,7 +5444,7 @@ def _get_default_attributes(self, given: dict[str, AttributeType]) -> dict[str,
return {'required': None, 'name': None, 'disabled': None, 'multiple': None}


class textarea(Tag):
class textarea(WhitespaceSensitiveTag):
"""
Represents a multi-line plain-text editing control, useful when you want to allow users to enter a sizeable amount of free-form text, for example, a comment on a review or feedback form.
Expand Down Expand Up @@ -5472,9 +5472,6 @@ def __init__(
maxlength: AttributeType = None,
wrap: Union[Literal['hard', 'soft'], None] = None,
readonly: Optional[bool] = None,
id: Optional[str] = None,
_class: Optional[str] = None,
style: Optional[str] = None,
**attributes: AttributeType,
) -> None:
"""
Expand All @@ -5493,9 +5490,6 @@ def __init__(
[View full documentation](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea)
"""
attributes |= {
'_class': _class,
'id': id,
'style': style,
'required': required,
'name': name,
'rows': rows,
Expand All @@ -5520,9 +5514,6 @@ def __call__( # type: ignore
maxlength: AttributeType = None,
wrap: Union[Literal['hard', 'soft'], None] = None,
readonly: Optional[bool] = None,
id: Optional[str] = None,
_class: Optional[str] = None,
style: Optional[str] = None,
**attributes: AttributeType,
):
"""
Expand All @@ -5541,9 +5532,6 @@ def __call__( # type: ignore
[View full documentation](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea)
"""
attributes |= {
'_class': _class,
'id': id,
'style': style,
'required': required,
'name': name,
'rows': rows,
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion pyhtml/__tags/renames.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
we still export the originals.
"""
from .generated import object as object_ # type: ignore
from .input import input as input_
from .input_tag import input as input_

__all__ = [
'object_',
Expand Down
29 changes: 29 additions & 0 deletions tests/whitespace_sensitive_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
# Test / Whitespace Sensitive Test
Tests for rendering whitespace sensitive tags.
"""
import pyhtml as p


def test_pre():
assert str(p.pre("hello\nworld")) == '\n'.join([
"<pre>hello",
"world</pre>",
])


def test_textarea():
assert str(p.textarea("hello\nworld")) == '\n'.join([
"<textarea>hello",
"world</textarea>",
])


def test_indentation_ignored():
assert str(p.body(p.pre("hello\nworld"))) == '\n'.join([
"<body>",
" <pre>hello",
"world</pre>",
"</body>",
])

0 comments on commit 9356b7c

Please sign in to comment.