Skip to content

Commit

Permalink
Prevent space between merged tags of the same type (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-3 authored Feb 7, 2024
1 parent 17712cb commit b755b52
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 2 additions & 2 deletions html_sanitizer/sanitizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,12 @@ def sanitize(self, html):
# tags of a mergeable type.
if nx.text:
if len(element):
list(element)[-1].tail = "{} {}".format(
list(element)[-1].tail = "{}{}".format(
list(element)[-1].tail or "",
nx.text,
)
else:
element.text = "{} {}".format(element.text or "", nx.text)
element.text = "{}{}".format(element.text or "", nx.text)

for child in nx:
element.append(child)
Expand Down
10 changes: 7 additions & 3 deletions html_sanitizer/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,16 @@ def test_02_a_tag(self):

def test_03_merge(self):
entries = (
("<h2>foo</h2><h2>bar</h2>", "<h2>foo bar</h2>"),
("<h2>foo</h2><h2>bar</h2>", "<h2>foobar</h2>"),
("<h2>foo </h2> <h2> bar</h2>", "<h2>foo bar</h2> "),
)

self.run_tests(entries)

def test_no_space_between_same_tags(self):
entries = [("<strong>Hel</strong><strong>lo</strong>", "<strong>Hello</strong>")]
self.run_tests(entries)

def test_04_p_in_li(self):
entries = (
("<li><p>foo</p></li>", "<li> foo </li>"),
Expand Down Expand Up @@ -275,7 +279,7 @@ def test_14_classes(self):
(
'<p class="centered">Test <span class="bla">span</span>'
'<span class="blub">span</span></p>',
'<p class="centered">Test <span class="bla">span span</span></p>',
'<p class="centered">Test <span class="bla">spanspan</span></p>',
),
('<h1 class="centered">Test</h1>', '<h1 class="centered">Test</h1>'),
('<h2 class="centered">Test</h2>', "<h2>Test</h2>"),
Expand Down Expand Up @@ -311,7 +315,7 @@ def test_15_classes(self):
(
'<p class="centered">Test <span class="bla">span</span>'
'<span class="bla">span</span></p>',
'<p class="centered">Test <span class="bla">span span</span></p>',
'<p class="centered">Test <span class="bla">spanspan</span></p>',
),
],
sanitizer=sanitizer,
Expand Down

0 comments on commit b755b52

Please sign in to comment.