Skip to content

Commit

Permalink
Adds a tags package as a namespace. Adds a gobal autoescape
Browse files Browse the repository at this point in the history
  • Loading branch information
byteface committed Apr 5, 2022
1 parent 13ad147 commit 29d938c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Basic useage...

```bash
from domonic import domonic
domonic.parseString('<somehtml...')
mydom = domonic.parseString('<somehtml...')
```

To quickly parse a webapge try the window module...
Expand Down Expand Up @@ -78,6 +78,28 @@ See the [docs/code](https://domonic.readthedocs.io/) for more features...

or examples in the [repo...](https://github.com/byteface/domonic/tree/master/examples)


### Namespace

Use the tags packaage if you want a namespace. i.e.

```python
import domonic.tags
print(domonic.tags.h1)
# or
import domonic.tags as tags
str(tags.div)
# or
import domonic.tags as html
print(html.span)
```

or just import what you need...

```python
from domonic import div, span, input as myinput, html as root
```

### html attributes

prepend attributes with an underscore ( avoids clashing with python keywords )
Expand Down
9 changes: 9 additions & 0 deletions domonic/dom.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
class Node(EventTarget):
""" An abstract base class upon which many other DOM API objects are based """

GLOBAL_AUTOESCAPE = False

ELEMENT_NODE: int = 1
TEXT_NODE: int = 3
CDATA_SECTION_NODE: int = 4
Expand Down Expand Up @@ -171,6 +173,9 @@ def __attributes__(self, ignore):
# print(e)

def __str__(self):
if Node.GLOBAL_AUTOESCAPE:
import html
content = html.escape(content)
return f"<{self.name}{self.__attributes__}>{self.content}</{self.name}>"

def __mul__(self, other):
Expand Down Expand Up @@ -431,6 +436,10 @@ def __format__(self, format_spec):
if isinstance(self, closed_tag):
return f"\n{dent}<{self.name}{self.__attributes__} />"

if Node.GLOBAL_AUTOESCAPE:
import html
content = html.escape(content)

size = len(str(content))
if size < 150 and wrap:
return f"\n{dent}<{self.name}{self.__attributes__}>{content}</{self.name}>"
Expand Down
1 change: 1 addition & 0 deletions domonic/tags/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from domonic.html import *

0 comments on commit 29d938c

Please sign in to comment.