Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
Preserve all white spaces before end of tag.
Browse files Browse the repository at this point in the history
  • Loading branch information
sallner committed Aug 29, 2018
1 parent c498270 commit 0028376
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/gocept/template_rewrite/pagetemplates.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

DOUBLE_SEMICOLON_REPLACEMENT = '_)_replacement_☃_(_'

# This prevents collision with attribute names.
ENDTAG = '>endtag<'


class PTParseError(SyntaxError):
"""Error while parsing a page template.
Expand Down Expand Up @@ -92,7 +95,7 @@ def startElement(self, name, attrs, ws_dict, is_short_tag, lineno):
rewrite_expression = self._rewrite_single_expression
value = value.replace(';;', DOUBLE_SEMICOLON_REPLACEMENT)

tag = join_element(name, attrs, ws_dict, is_short_tag)
tag = join_element(name, attrs, ws_dict)
rewrite_expression = functools.partial(
rewrite_expression, lineno=lineno, tag=tag,
filename=self.filename)
Expand All @@ -116,6 +119,8 @@ def parse(self, source):


leading_whitespace = '(\s*{})'
# This tag end matches whitespaces and shorttag
tag_end = '(\s*/?>$)'


class HTMLGenerator(html.parser.HTMLParser):
Expand All @@ -141,10 +146,8 @@ def handle_starttag(self, tag, attrs, is_short_tag=False):
flags=re.IGNORECASE)
ws_dict[attr] = mo.group(1)

if is_short_tag:
short_tag = '/>'
mo = re.search(leading_whitespace.format(short_tag), full_tag)
ws_dict[short_tag] = mo.group()
mo = re.search(tag_end, full_tag)
ws_dict[ENDTAG] = mo.group()

# XXX We are deeply coupling to our generator here, as we change the
# signature wrt the base class.
Expand Down Expand Up @@ -204,7 +207,7 @@ def quoteattr(data, entities={}):
return data


def join_element(name, attrs, ws_dict, is_short_tag):
def join_element(name, attrs, ws_dict):
"""Synthesize the element from parsed parts."""
content = []
content.append(u'<' + name)
Expand All @@ -219,19 +222,15 @@ def join_element(name, attrs, ws_dict, is_short_tag):
else:
content.append(u'%s=%s' % (ws_dict[attr],
saxutils.quoteattr(value)))
if is_short_tag:
# if we have a short_tag we want to render it with whitespaces.
content.append(ws_dict['/>'])
else:
content.append(u'>')
content.append(ws_dict[ENDTAG])
return ''.join(content)


class CustomXMLGenerator(saxutils.XMLGenerator):
"""XMLGenerator with escape tweaks."""

def startElement(self, name, attrs, ws_dict, is_short_tag):
self._write(join_element(name, attrs, ws_dict, is_short_tag))
self._write(join_element(name, attrs, ws_dict))


class PTParserRewriter(object):
Expand Down
2 changes: 2 additions & 0 deletions src/gocept/template_rewrite/tests/test_pagetemplates.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ def test_pagetemplates__PTParserRewriter____call____2(
'''<span tal:content="python: 'abd\\';;adb'" />''',
# Broken HTML without TAL statements
'''<count y;not any nw in x$y;1b];0b]}\n\nread:>''',
# Whitespaces at the end of a tag
'''<td tal:attributes="class item/class" >asdf</td>''',
])
def test_pagetemplates__PTParserRewriter____call____3(
input):
Expand Down

0 comments on commit 0028376

Please sign in to comment.