Skip to content

Commit

Permalink
Adds some docs updates on Config settings
Browse files Browse the repository at this point in the history
  • Loading branch information
byteface committed Apr 10, 2022
1 parent 2cdab3e commit 489d665
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 11 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ page = div(span('Hello World'))
render(f"{page}", 'index.html') # notice use of f-string to pretty print the html
```

There's a few new rendering options. See DOMConfig.

```python
from domonic.dom import DOMConfig
print(DOMConfig.GLOBAL_AUTOESCAPE) # Default False
print(DOMConfig.RENDER_OPTIONAL_CLOSING_TAGS) # Default True
print(DOMConfig.RENDER_OPTIONAL_CLOSING_SLASH) # Defaults True
print(DOMConfig.SPACE_BEFORE_OPTIONAL_CLOSING_SLASH) # Default False
```

## DOM

DOM manipulation with python.
Expand Down
39 changes: 39 additions & 0 deletions domonic/dom.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class DOMConfig:
"""
GLOBAL_AUTOESCAPE: bool = False # Default is False
RENDER_OPTIONAL_CLOSING_TAGS: bool = True # Default is True
RENDER_OPTIONAL_CLOSING_SLASH: bool = True # on emtpy nodes should the last slash be rendered
SPACE_BEFORE_OPTIONAL_CLOSING_SLASH: bool = False # on emtpy nodes should there be a space before the closing slash?
HTMX_ENABLED: bool = False # Default is false
# NO_REPR: bool = True # objects always render?

Expand Down Expand Up @@ -63,6 +65,7 @@ class Node(EventTarget):
ENTITY_NODE: int = 6
NOTATION_NODE: int = 12

__isempty: bool = False # tells us if the node is empty i.e. has no content aka 'self closing'. in html that would be: area, base, br, col, embed, hr, img, input, link, meta, param, source, track, True
__context: list = None # private. tags will append to last item in context on creation.

# __slots__ = ['____attributes__',
Expand Down Expand Up @@ -5150,6 +5153,14 @@ def __init__(self, *args, autoplay: bool = None, controls=None, loop=None, muted

class HTMLBRElement(HTMLElement):
name = 'br'
__isempty = True
def __str__(self):
if DOMConfig.RENDER_OPTIONAL_CLOSING_SLASH:
if DOMConfig.SPACE_BEFORE_OPTIONAL_CLOSING_SLASH:
return f"<{self.name}{self.__attributes__} />"
else:
return f"<{self.name}{self.__attributes__}/>"
return f"<{self.name}{self.__attributes__} >"


class HTMLBaseElement(HTMLElement):
Expand Down Expand Up @@ -5422,6 +5433,7 @@ def __init__(self, *args, src=None, name=None, sandbox=None, allowfullscreen=Non

class HTMLImageElement(HTMLElement):
name = 'img'
__isempty = True

def __init__(self, *args, alt=None, src=None, crossorigin=None, height=None, ismap=None, longdesc=None, sizes=None, srcset=None, usemap=None, width=None, **kwargs):
"""HTMLImageElement
Expand Down Expand Up @@ -5463,6 +5475,7 @@ def __init__(self, *args, alt=None, src=None, crossorigin=None, height=None, ism

class HTMLInputElement(HTMLElement):
name = 'input'
__isempty = True

def __init__(self, *args, accept=None, alt=None, autocomplete=None, autofocus=None, checked=None, dirname=None, disabled=None, form=None, formaction=None, formenctype=None, formmethod=None, formnovalidate=None, formtarget=None, height=None, _list=None, _max=None, maxlength=None, _min=None, multiple=None, name=None, pattern=None, placeholder=None, readonly=None, required=None, size=None, src=None, step=None, type=None, value=None, width=None, **kwargs):
"""HTMLInputElement
Expand Down Expand Up @@ -5568,6 +5581,7 @@ class HTMLIsIndexElement(HTMLElement): # TODO - check

class HTMLKeygenElement(HTMLElement):
name = 'keygen'
__isempty = True


class HTMLLIElement(HTMLElement):
Expand Down Expand Up @@ -5606,6 +5620,7 @@ class HTMLMediaElement(HTMLElement): # TODO - check

class HTMLMetaElement(HTMLElement):
name = 'meta'
__isempty = True

def __init__(self, *args, charset=None, content=None, http_equiv=None, name=None, **kwargs):
"""HTMLMetaElement
Expand Down Expand Up @@ -5710,6 +5725,7 @@ class HTMLParagraphElement(HTMLElement):

class HTMLParamElement(HTMLElement): # TODO - check
name = 'param'
__isempty = True


class HTMLPictureElement(HTMLElement):
Expand Down Expand Up @@ -5792,6 +5808,7 @@ class HTMLShadowElement(HTMLElement): # TODO - check

class HTMLSourceElement(HTMLElement): # TODO - check
name = 'source'
__isempty = True


class HTMLSpanElement(HTMLElement):
Expand All @@ -5812,6 +5829,7 @@ class HTMLTableCellElement(HTMLElement): # TODO - check

class HTMLTableColElement(HTMLElement):
name = 'col'
__isempty = True


class HTMLTableDataCellElement(HTMLElement): # TODO - check
Expand Down Expand Up @@ -6009,3 +6027,24 @@ class HTMLPortalElement(HTMLElement):
'''
# self.screen = type('screen', (DOM,), {'name':'screen'})
'''


# https://developer.mozilla.org/en-US/docs/Glossary/Empty_element
# def is_empty(node):
# if its a class,
# if its an instance
# if its a string

# meta = HTMLMetaElement
# br = HTMLBRElement
# img = HTMLImageElement
# input = HTMLInputElement
# param = HTMLParamElement
# source = HTMLSourceElement
# track = HTMLTrackElement
# col = HTMLTableColElement
# keygen = HTMLKeygenElement

# hr = type("hr", (closed_tag, Element), {"name": "hr"})
# wbr = type("wbr", (closed_tag, Element), {"name": "wbr"})
# command = type("command", (closed_tag, Element), {"name": "command"})
27 changes: 16 additions & 11 deletions domonic/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Generate HTML using python.
"""
from domonic.dom import (Comment, Document, # HTMLOptionsCollection,
from domonic.dom import (DOMConfig, Comment, Document, # HTMLOptionsCollection,
DocumentType, Element, HTMLAnchorElement,
HTMLAreaElement, HTMLAudioElement, HTMLBaseElement,
HTMLBaseFontElement, HTMLBodyElement, HTMLBRElement,
Expand Down Expand Up @@ -420,7 +420,12 @@ def __init__(self, error, message="TemplateError: "):

class closed_tag(Node):
def __str__(self):
return f"<{self.name}{self.__attributes__}/>"
if DOMConfig.RENDER_OPTIONAL_CLOSING_SLASH:
if DOMConfig.SPACE_BEFORE_OPTIONAL_CLOSING_SLASH:
return f"<{self.name}{self.__attributes__} />"
else:
return f"<{self.name}{self.__attributes__}/>"
return f"<{self.name}{self.__attributes__}>"


html = HTMLDocument
Expand Down Expand Up @@ -597,19 +602,19 @@ def elements(self):
samp = type("samp", (Element,), {"name": "samp"})

base = HTMLBaseElement
link = type("link", (closed_tag, Element), {"name": "link"}) # HTMLLinkElement TODO - closed tags
meta = type("meta", (closed_tag, Element), {"name": "meta"}) # HTMLMetaElement TODO - closed tags
link = type("link", (closed_tag, HTMLLinkElement), {"name": "link"}) # HTMLLinkElement TODO - closed tags
meta = type("meta", (closed_tag, HTMLMetaElement), {"name": "meta"}) # HTMLMetaElement TODO - closed tags
hr = type("hr", (closed_tag, Element), {"name": "hr"})
br = HTMLBRElement # type("br", (closed_tag, Element), {"name": "br"})
wbr = type("wbr", (closed_tag, Element), {"name": "wbr"})
img = type("img", (closed_tag, Element), {"name": "img"}) # HTMLImageElement TODO - closed tags
param = type("param", (closed_tag, Element), {"name": "param"})
source = type("source", (closed_tag, Element), {"name": "source"})
track = type("track", (closed_tag, Element), {"name": "track"})
img = type("img", (closed_tag, HTMLImageElement), {"name": "img"}) # HTMLImageElement TODO - closed tags
param = type("param", (closed_tag, HTMLParamElement), {"name": "param"}) # HTMLParamElement
source = type("source", (closed_tag, HTMLSourceElement), {"name": "source"}) # HTMLSourceElement
track = type("track", (closed_tag, HTMLTrackElement), {"name": "track"}) # HTMLTrackElement
area = HTMLAreaElement
col = type("col", (closed_tag, Element), {"name": "col"})
input = type("input", (closed_tag, Element), {"name": "input"})
keygen = type("keygen", (closed_tag, Element), {"name": "keygen"})
col = type("col", (closed_tag, HTMLTableColElement), {"name": "col"}) # HTMLTableColElement
input = type("input", (closed_tag, HTMLInputElement), {"name": "input"}) # HTMLInputElement # TODO - closed tags
keygen = type("keygen", (closed_tag, HTMLKeygenElement), {"name": "keygen"}) # HTMLKeygenElement
command = type("command", (closed_tag, Element), {"name": "command"})

main = type("main", (Element,), {"name": "main"})
Expand Down

0 comments on commit 489d665

Please sign in to comment.