Skip to content

Commit 6842ce3

Browse files
Add support for DangerousRawHtml
1 parent 12154d4 commit 6842ce3

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

pyhtml/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@
254254
from .__tags import input_, object_
255255

256256
__all__ = [
257+
'DangerousRawHtml',
257258
'Tag',
258259
'Comment',
259260
'input_',
@@ -375,6 +376,7 @@
375376

376377

377378
from .__tags import (
379+
DangerousRawHtml,
378380
html,
379381
base,
380382
head,

pyhtml/__tag_base.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ class Comment(Tag):
129129
Note that this does not render as a `<comment>` tag
130130
"""
131131
def __init__(self, text: str) -> None:
132+
"""
133+
An HTML comment.
134+
135+
Renders as:
136+
137+
```html
138+
<!-- [comment text] -->
139+
```
140+
141+
Note that this does not render as a `<comment>` tag
142+
"""
132143
self.comment_data = text
133144
super().__init__()
134145

pyhtml/__tags/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
# Re-export renamed versions of tags
77
from .renames import input_, object_
88
from .input import input
9+
from .dangerous_raw_html import DangerousRawHtml
910

1011
# Copy this into pyhtml/__tags/__init__.py
1112
__all__ = [
1213
# This one is generated by hand
1314
'input',
15+
# These are extra features of PyHTML enhanced
16+
'DangerousRawHtml',
1417
# These two are renamed
1518
'input_',
1619
'object_',
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
# PyHTML Enhanced / Tags / Dangerous raw HTML
3+
4+
Definition for the DangerousRawHtml tag.
5+
"""
6+
from ..__tag_base import Tag
7+
8+
9+
class DangerousRawHtml(Tag):
10+
"""
11+
Raw HTML as a string. This is embedded directly within the rendered output.
12+
13+
## Warning
14+
15+
This will blindly accept any text as HTML, which is EXTREMELY DANGEROUS!
16+
(Mis)using this could result in issues ranging from broken output to major
17+
security vulnerabilities such as
18+
[cross-site scripting](https://en.wikipedia.org/wiki/Cross-site_scripting).
19+
20+
Do not use this unless absolutely necessary.
21+
"""
22+
def __init__(self, text: str) -> None:
23+
"""
24+
Raw HTML as a string. This is embedded directly within the rendered
25+
output.
26+
27+
## Warning
28+
29+
This will blindly accept any text as HTML, which is EXTREMELY
30+
DANGEROUS! (Mis)using this could result in issues ranging from broken
31+
output to major security vulnerabilities such as
32+
[cross-site scripting](https://en.wikipedia.org/wiki/Cross-site_scripting).
33+
34+
Do not use this unless absolutely necessary.
35+
"""
36+
self.html_data = text
37+
super().__init__()
38+
39+
def __call__(self):
40+
raise TypeError('DangerousRawHtml tags are not callable')
41+
42+
def _get_tag_name(self) -> str:
43+
# Ignore coverage since this is only implemented to satisfy inheritance
44+
# and is never used since we override _render
45+
return '!!!DANGEROUS RAW HTML!!!' # pragma: no cover
46+
47+
def _render(self) -> list[str]:
48+
return self.html_data.splitlines()

tests/basic_rendering_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
br,
1818
input,
1919
a,
20+
DangerousRawHtml,
2021
)
2122

2223

@@ -248,3 +249,21 @@ def test_boolean_tag_attributes_false():
248249
Attributes with value `False` are skipped
249250
"""
250251
assert str(input(readonly=False)) == "<input/>"
252+
253+
254+
def test_dangerous_raw_html():
255+
"""
256+
Is raw HTML rendered correctly?
257+
"""
258+
assert str(DangerousRawHtml("<script>alert(1)</script>")) \
259+
== "<script>alert(1)</script>"
260+
261+
assert str(
262+
html(
263+
DangerousRawHtml("<script>alert(1)</script>")
264+
)
265+
) == "\n".join([
266+
"<html>",
267+
" <script>alert(1)</script>",
268+
"</html>",
269+
])

0 commit comments

Comments
 (0)