Skip to content

Commit

Permalink
raw() can now be used to prevent escaping of attributes (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpsievert committed Jul 25, 2022
1 parent 4a4340c commit fc81c80
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
6 changes: 4 additions & 2 deletions dominate/dom_tag.py
Expand Up @@ -355,8 +355,10 @@ def _render(self, sb, indent_level, indent_str, pretty, xhtml):
sb.append(name)

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

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

Expand Down
1 change: 1 addition & 0 deletions dominate/util.py
Expand Up @@ -168,6 +168,7 @@ class text(dom_tag):

def __init__(self, _text, escape=True):
super(text, self).__init__()
self.escape = escape
if escape:
self.text = str_escape(_text)
else:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_html.py
@@ -1,6 +1,7 @@
import dominate
from dominate.tags import *
import pytest
from dominate.util import raw

try:
xrange = xrange
Expand Down Expand Up @@ -335,3 +336,10 @@ 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>'''
assert div(attr = raw('{<div></div>}')).render() == \
'''<div attr="{<div></div>}"></div>'''

0 comments on commit fc81c80

Please sign in to comment.