Skip to content

Commit

Permalink
Fix incorrect context handling when a tag is used 2+ levels deeper th…
Browse files Browse the repository at this point in the history
…an it was created. Fixes #173
  • Loading branch information
Knio committed May 20, 2023
1 parent 9573b28 commit f43a9ca
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion dominate/_version.py
@@ -1 +1 @@
__version__ = '2.7.0'
__version__ = '2.8.0'
6 changes: 3 additions & 3 deletions dominate/dom_tag.py
Expand Up @@ -191,9 +191,9 @@ def add(self, *args):
self.children.append(obj)

elif isinstance(obj, dom_tag):
stack = dom_tag._with_contexts.get(_get_thread_context())
if stack:
stack[-1].used.add(obj)
stack = dom_tag._with_contexts.get(_get_thread_context(), [])
for s in stack:
s.used.add(obj)
self.children.append(obj)
obj.parent = self

Expand Down
28 changes: 20 additions & 8 deletions tests/test_dom_tag.py
@@ -1,5 +1,8 @@
import mock
import pytest
try:
import mock
except ImportError:
import unittest.mock as mock

from dominate.tags import *

Expand All @@ -21,15 +24,12 @@ def test___get_thread_context(monkeypatch):

def test_add_raw_string():
container = div()

container.add_raw_string('foo')

assert container.children == ['foo']

def test_clear():
container = div()
child = div()

container.add(child)

assert container.children == [child]
Expand All @@ -42,15 +42,13 @@ def test_clear():

def test_set_attribute():
container = div()

container.add_raw_string('foo')
container.set_attribute(0, 'bar')

assert container.children == ['bar']

def test_set_attribute_error():
container = div()\

container = div()
with pytest.raises(TypeError, match=(
'Only integer and string types are valid for assigning '
'child tags and attributes, respectively.'
Expand All @@ -65,5 +63,19 @@ def test___get_item___child_index_error():
def test___contains__():
container = div()
container.add(div())

assert 'div' in container

def test_nested_context():
def sub(*args):
with div('B') as B:
B.add(*args)

with div('A') as A:
sub(div('C'))

assert str(A) == \
'''<div>A
<div>B
<div>C</div>
</div>
</div>'''
2 changes: 1 addition & 1 deletion tests/test_dominate.py
Expand Up @@ -6,7 +6,7 @@

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

Expand Down

0 comments on commit f43a9ca

Please sign in to comment.