Skip to content

Commit

Permalink
fix build, update utils/document
Browse files Browse the repository at this point in the history
  • Loading branch information
Knio committed Jul 25, 2022
1 parent 5f72ba4 commit 1c9fa09
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -18,7 +18,7 @@ install:

# command to run tests, e.g. python setup.py test
script:
- python setup.py sdist --format=zip
- python setup.py sdist --formats=zip
- pip install dist/dominate*.zip
- py.test

Expand Down
2 changes: 1 addition & 1 deletion dominate/_version.py
@@ -1 +1 @@
__version__ = '2.6.0'
__version__ = '2.6.1'
28 changes: 14 additions & 14 deletions dominate/document.py
Expand Up @@ -17,6 +17,7 @@
'''

from . import tags
from . import util

try:
basestring = basestring
Expand All @@ -26,16 +27,20 @@

class document(tags.html):
tagname = 'html'
def __init__(self, title='Dominate', doctype='<!DOCTYPE html>', request=None):
def __init__(self, title='Dominate', doctype='<!DOCTYPE html>'):
'''
Creates a new document instance. Accepts `title`, `doctype`, and `request` keyword arguments.
Creates a new document instance. Accepts `title` and `doctype`
'''
super(document, self).__init__()
self.doctype = doctype
self.head = super(document, self).add(tags.head())
self.body = super(document, self).add(tags.body())
self.title_node = self.head.add(tags.title(title))
self._entry = self.body
with self.body:
self.header = util.container()
self.main = util.container()
self.footer = util.container()
self._entry = self.main

def get_title(self):
return self.title_node.text
Expand All @@ -56,20 +61,15 @@ def add(self, *args):
'''
return self._entry.add(*args)

def render(self, *args, **kwargs):
def _render(self, sb, *args, **kwargs):
'''
Creates a <title> tag if not present and renders the DOCTYPE and tag tree.
Renders the DOCTYPE and tag tree.
'''
r = []

#Validates the tag tree and adds the doctype if one was set
# adds the doctype if one was set
if self.doctype:
r.append(self.doctype)
r.append('\n')
r.append(super(document, self).render(*args, **kwargs))

return u''.join(r)
__str__ = __unicode__ = render
sb.append(self.doctype)
sb.append('\n')
return super(document, self)._render(sb, *args, **kwargs)

def __repr__(self):
return '<dominate.document "%s">' % self.title
28 changes: 14 additions & 14 deletions dominate/dom_tag.py
Expand Up @@ -199,7 +199,7 @@ def add(self, *args):
obj = str(obj)

if isinstance(obj, basestring):
obj = escape(obj)
obj = util.escape(obj)
self.children.append(obj)

elif isinstance(obj, dom_tag):
Expand Down Expand Up @@ -356,29 +356,30 @@ def _render(self, sb, indent_level, indent_str, pretty, xhtml):

for attribute, value in sorted(self.attributes.items()):
if value is not False: # False values must be omitted completely
sb.append(' %s="%s"' % (attribute, escape(unicode(value), True)))
sb.append(' %s="%s"' % (attribute, util.escape(unicode(value), True)))

sb.append(' />' if self.is_single and xhtml else '>')

if not self.is_single:
inline = self._render_children(sb, indent_level + 1, indent_str, pretty, xhtml)
if self.is_single:
return sb

if pretty and not inline:
sb.append('\n')
sb.append(indent_str * indent_level)
inline = self._render_children(sb, indent_level + 1, indent_str, pretty, xhtml)
if pretty and not inline:
sb.append('\n')
sb.append(indent_str * indent_level)

# close tag
sb.append('</')
sb.append(name)
sb.append('>')
# close tag
sb.append('</')
sb.append(name)
sb.append('>')

return sb

def _render_children(self, sb, indent_level, indent_str, pretty, xhtml):
inline = True
for child in self.children:
if isinstance(child, dom_tag):
if pretty and not child.is_inline:
if pretty and not child.is_inline:# and not isinstance(self, util.container):
inline = False
sb.append('\n')
sb.append(indent_str * indent_level)
Expand Down Expand Up @@ -482,5 +483,4 @@ def attr(*args, **kwargs):
c.set_attribute(*dom_tag.clean_pair(attr, value))


# escape() is used in render
from .util import escape
from . import util
18 changes: 15 additions & 3 deletions dominate/util.py
Expand Up @@ -120,6 +120,19 @@ def url_unescape(data):
lambda m: unichr(int(m.group(1), 16)), data)


class container(dom_tag):
'''
Contains multiple elements, but does not add a level
'''
is_inline = True
def _render(self, sb, indent_level, indent_str, pretty, xhtml):
inline = self._render_children(sb, indent_level, indent_str, pretty, xhtml)
if pretty and not inline:
sb.append('\n')
sb.append(indent_str * (indent_level - 1))
return sb


class lazy(dom_tag):
'''
delays function execution until rendered
Expand All @@ -146,10 +159,9 @@ def _render(self, sb, *a, **kw):
sb.append(str(r))


# TODO rename this to raw?
class text(dom_tag):
'''
Just a string. useful for inside context managers
Just a string. Useful for inside context managers
'''
is_pretty = False
is_inline = True
Expand All @@ -168,6 +180,6 @@ def _render(self, sb, *a, **kw):

def raw(s):
'''
Inserts a raw string into the DOM. Unsafe.
Inserts a raw string into the DOM. Unsafe. Alias for text(x, escape=False)
'''
return text(s, escape=False)
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -51,6 +51,7 @@
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: Implementation :: PyPy',
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
'Topic :: Software Development :: Libraries :: Python Modules',
Expand Down
30 changes: 30 additions & 0 deletions tests/test_document.py
Expand Up @@ -70,6 +70,36 @@ def test_title():
<body></body>
</html>'''

def test_containers():
d = document()
with d.footer:
div('footer')
with d:
div('main1')
with d.main:
div('main2')
print(d.header)
print(d)
print(d.body.children)
with d.header:
div('header1')
div('header2')
assert d.render() == \
'''<!DOCTYPE html>
<html>
<head>
<title>Dominate</title>
</head>
<body>
<div>header1</div>
<div>header2</div>
''''''
<div>main1</div>
<div>main2</div>
''''''
<div>footer</div>
</body>
</html>'''

if __name__ == '__main__':
# test_doc()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_dominate.py
Expand Up @@ -6,7 +6,7 @@

def test_version():
import dominate
version = '2.6.0'
version = '2.6.1'
assert dominate.version == version
assert dominate.__version__ == version

Expand Down
19 changes: 19 additions & 0 deletions tests/test_utils.py
Expand Up @@ -35,3 +35,22 @@ def test_unescape():
def test_url():
assert util.url_escape('hi there?') == 'hi%20there%3F'
assert util.url_unescape('hi%20there%3f') == 'hi there?'


def test_container():
d = div()
with d:
with util.container():
pass
assert d.render() == '<div></div>'


d = div()
with d:
with util.container():
h1('a')
assert d.render() == \
'''<div>
<h1>a</h1>
</div>'''

0 comments on commit 1c9fa09

Please sign in to comment.