Skip to content

Commit

Permalink
Fixes (#160)
Browse files Browse the repository at this point in the history
* fix #153

* fix #144

* fix #139

* bump version

* remove dead code
  • Loading branch information
Knio committed Jul 25, 2022
1 parent fc81c80 commit 842759b
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 30 deletions.
2 changes: 1 addition & 1 deletion dominate/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.6.1'
__version__ = '2.7.0'
7 changes: 4 additions & 3 deletions dominate/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@

class document(tags.html):
tagname = 'html'
def __init__(self, title='Dominate', doctype='<!DOCTYPE html>'):
def __init__(self, title='Dominate', doctype='<!DOCTYPE html>', *a, **kw):
'''
Creates a new document instance. Accepts `title` and `doctype`
'''
super(document, self).__init__()
super(document, self).__init__(*a, **kw)
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))
if title is not None:
self.title_node = self.head.add(tags.title(title))
with self.body:
self.header = util.container()
self.main = util.container()
Expand Down
8 changes: 2 additions & 6 deletions dominate/dom1core.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,19 @@ def getElementById(self, id):
raise ValueError('Multiple tags with id "%s".' % id)
elif results:
return results[0]
else:
return None
return None

def getElementsByTagName(self, name):
'''
DOM API: Returns all tags that match name.
'''
if isinstance(name, basestring):
return self.get(name.lower())
else:
return None
return None

def appendChild(self, obj):
'''
DOM API: Add an item to the end of the children list.
'''
self.add(obj)
return self


17 changes: 2 additions & 15 deletions dominate/dom_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ def __init__(self, *args, **kwargs):
self.attributes = {}
self.children = []
self.parent = None
self.document = None

# Does not insert newlines on all children if True (recursive attribute)
self.is_inline = kwargs.pop('__inline', self.is_inline)
Expand Down Expand Up @@ -169,25 +168,14 @@ def set_attribute(self, key, value):
'child tags and attributes, respectively.')
__setitem__ = set_attribute


def delete_attribute(self, key):
if isinstance(key, int):
del self.children[key:key+1]
else:
del self.attributes[key]
__delitem__ = delete_attribute

def setdocument(self, doc):
'''
Creates a reference to the parent document to allow for partial-tree
validation.
'''
# assume that a document is correct in the subtree
if self.document != doc:
self.document = doc
for i in self.children:
if not isinstance(i, dom_tag): return
i.setdocument(doc)


def add(self, *args):
'''
Expand All @@ -208,7 +196,6 @@ def add(self, *args):
stack[-1].used.add(obj)
self.children.append(obj)
obj.parent = self
obj.setdocument(self.document)

elif isinstance(obj, dict):
for attr, value in obj.items():
Expand Down Expand Up @@ -355,7 +342,7 @@ def _render(self, sb, indent_level, indent_str, pretty, xhtml):
sb.append(name)

for attribute, value in sorted(self.attributes.items()):
if value is False:
if value in (False, None):
continue
val = unicode(value) if isinstance(value, util.text) and not value.escape else util.escape(unicode(value), True)
sb.append(' %s="%s"' % (attribute, val))
Expand Down
1 change: 1 addition & 0 deletions dominate/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ class sup(html_tag):


class i(html_tag):
is_inline = True
'''
The i element represents a span of text in an alternate voice or mood, or
otherwise offset from the normal prose in a manner indicating a different
Expand Down
10 changes: 10 additions & 0 deletions tests/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ def test_containers():
</body>
</html>'''


def test_attributes():
d = document(title=None, doctype=None, lang='en')
assert d.render() == \
'''<html lang="en">
<head></head>
<body></body>
</html>'''


if __name__ == '__main__':
# test_doc()
test_decorator()
17 changes: 14 additions & 3 deletions tests/test_dom1core.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import pytest

from dominate.tags import *

def test_dom():
container = div()
with container.add(div(id='base')) as dom:
s1 = span('Hello', id='span1')
s2 = span('World', id='span2')

s3 = span('foobar', id='span3')
dom.appendChild(s3)

assert container.getElementById('base') is dom
assert container.getElementById('base') is dom
assert container.getElementById('span1') is s1
assert container.getElementById('span3') is s3
assert container.getElementsByTagName('span') == [s1, s2, s3]
assert container.getElementsByTagName('SPAN') == [s1, s2, s3]


def test_element():
d = div(
span(id='a'),
span(id='a'),
)
with pytest.raises(ValueError):
d.getElementById('a')

2 changes: 1 addition & 1 deletion tests/test_dominate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

Expand Down
11 changes: 10 additions & 1 deletion tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,15 @@ def test_attributes():
assert d['data-test']

with pytest.raises(ValueError):
# not in a tag context
attr(id='moo')


def test_attribute_none():
d = div(foo=1, bar=None)
assert d.render() == '<div foo="1"></div>'


def test_attribute_dashes():
# fix issue #118 (https://github.com/Knio/dominate/issues/118)
expected = '<div aria-foo="bar" data-a-b-c="foo" data-page-size="123"></div>'
Expand Down Expand Up @@ -321,6 +327,9 @@ def test_pretty():
assert span('hi', br(__inline=False), 'there').render() == \
'''<span>hi\n <br>there\n</span>'''

assert p('goodbye ', i('cruel'), ' world').render() == \
'''<p>goodbye <i>cruel</i> world</p>'''


def test_xhtml():
assert head(script('foo'), style('bar')).render(xhtml=True) == '''<head>
Expand All @@ -337,7 +346,7 @@ def test_xhtml():
assert span('hi', br(), 'there').render(xhtml=False) == \
'''<span>hi<br>there</span>'''


def test_verbatim_attributes():
assert div(attr = '{<div></div>}').render() == \
'''<div attr="{&lt;div&gt;&lt;/div&gt;}"></div>'''
Expand Down

0 comments on commit 842759b

Please sign in to comment.