dominate
dominate copied to clipboard
Repeatedly calling attr() on same attribute doesn't accumulate values
For example, I'd expect the following to generate <button class="bar baz">foo</button>
:
from dominate.tags import *
b = button("foo")
with b:
attr(className = "bar")
attr(className = "baz")
print(b)
<button class="baz">foo</button>
Accumulating is nice for proving a "base component" with a set of "base classes" that an end user could add to, for example:
def myButton(label):
b = button(label)
with b:
attr(className = "btn")
return b
b2 = myButton("foo")
with b2:
attr(className = "btn-primary")
print(b2)
Would ideally generate
<button class="btn btn-primary">foo</button>
So technically your ideal/expected result is actually unexpected behavior. The documentation on the attr
function literally says:
Set attributes on the current active tag context
So in essence what you're doing when you call attr(className = "bar")
you're quite literally setting className
(class) to "btn"
then when you call it again with attr(className = "baz")
it's literally setting className
(class) to "baz"
.
What you'd really want is the addition of a function along the lines of append_attribute
or something, which adds to a current attribute if it exists or falls back to just setting the attribute if it doesn't exist.