Skip to content

Commit

Permalink
Merge pull request #150 from tisdall/keep_needed_inline_text_spaces
Browse files Browse the repository at this point in the history
stop removal of spaces that causes display changes
  • Loading branch information
andrewsmedina authored Jan 20, 2021
2 parents c3b4478 + 25b0332 commit afcb214
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 16 deletions.
26 changes: 11 additions & 15 deletions htmlmin/minify.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,23 +175,19 @@ def is_cond_comment(soup):


def is_inflow(soup):
"""test whether an element belongs to a text flow, returns a tuple
"""Test whether a NavigableString belongs to a text flow, returns a tuple
of two booleans describing the flow around the element. The first
boolean represents the flow before the element, the second boolean
represents the flow after the element.
:param soup: a BeautifulSoup of the code to reduce
:type soup: bs4.BeautifulSoup
:param soup: a NavigableString of the code to reduce
:type soup: bs4.element.NavigableString
"""
if soup.previous_sibling is not None and \
soup.previous_sibling.name in TEXT_FLOW:
prev_flow = True
else:
prev_flow = False
if soup.next_sibling is not None and \
soup.next_sibling.name in TEXT_FLOW:
next_flow = True
else:
next_flow = False

return (prev_flow, next_flow)
def is_text_tag(soup):
return soup and soup.name in TEXT_FLOW

if is_text_tag(soup.parent):
# NavigableString is within a text tag element
return (True, True)

return (is_text_tag(soup.previous_sibling), is_text_tag(soup.next_sibling))
2 changes: 1 addition & 1 deletion htmlmin/tests/resources/inline_whitespace_minified.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<html><head><title>Inline Whitespace</title></head><body><article><h1>Main Title<sup>TM</sup></h1><section><h2>Abstract</h2><p><span class="s1">a</span> <span class="s2">b</span> <span class="s1">c</span></p></section><section><h2>Premise</h2><p><span>a</span> <span>space</span></p><p><span>no</span><span>space</span></p><p>This is<a href="http://example.com">a link</a>.</p></section></article></body></html>
<html><head><title>Inline Whitespace</title></head><body><article><h1>Main Title<sup> TM</sup></h1><section><h2>Abstract</h2><p><span class="s1">a</span> <span class="s2">b</span> <span class="s1"> c </span></p></section><section><h2>Premise</h2><p><span>a</span> <span>space</span></p><p><span>no</span><span> space</span></p><p>This is<a href="http://example.com"> a link</a>.</p></section></article></body></html>
84 changes: 84 additions & 0 deletions htmlmin/tests/test_minify.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,87 @@ def test_conservative_whitespace(self):
self.maxDiff = None
html, minified = self._normal_and_minified('inline_whitespace', variant='conservative')
self.assertEqual(minified, html_minify(html))

def test_inline_text_spacing(self):
self.maxDiff = None
html = '''<html><head></head><body>
<p>a <u> b</u></p>
<p>c<u> d</u></p>
<p>e <u>f</u></p>
<p><u>g </u> h</p>
<p><u>i</u> j</p>
<p><u>k </u>l</p>
<p><u>m </u> <u> n</u></p>
<p><u>o</u> <u> p</u></p>
<p><u>q </u> <u>r</u></p>
<p><u>s</u> <u>t</u></p>
<p><u>u</u><u> v</u></p>
<p><u>w </u><u>x</u></p>
<p> <u> y </u> </p>
<p> <s> <u> z </u> </s> </p>
</body></html>'''
minified = (
'<html><head></head><body>'
'<p>a <u> b</u></p>'
'<p>c<u> d</u></p>'
'<p>e <u>f</u></p>'
'<p><u>g </u> h</p>'
'<p><u>i</u> j</p>'
'<p><u>k </u>l</p>'
'<p><u>m </u> <u> n</u></p>'
'<p><u>o</u> <u> p</u></p>'
'<p><u>q </u> <u>r</u></p>'
'<p><u>s</u> <u>t</u></p>'
'<p><u>u</u><u> v</u></p>'
'<p><u>w </u><u>x</u></p>'
'<p><u> y </u></p>'
'<p><s> <u> z </u> </s></p>'
'</body></html>'
)
self.assertEqual(minified, html_minify(html))

def test_inline_text_spacing_with_nontext_prev_siblings(self):
self.maxDiff = None
html = '''<html><head></head><body>
<div><div>m </div> <u> n</u></div>
<div><div>o</div> <u> p</u></div>
<div><div>q </div> <u>r</u></div>
<div><div>s</div> <u>t</u></div>
<div><div>u</div><u> v</u></div>
<div><div>w </div><u>x</u></div>
<div> <div> y </div> </div>
</body></html>'''
minified = (
'<html><head></head><body>'
'<div><div>m</div><u> n</u></div>'
'<div><div>o</div><u> p</u></div>'
'<div><div>q</div><u>r</u></div>'
'<div><div>s</div><u>t</u></div>'
'<div><div>u</div><u> v</u></div>'
'<div><div>w</div><u>x</u></div>'
'<div><div>y</div></div>'
'</body></html>'
)
self.assertEqual(minified, html_minify(html))

def test_inline_text_spacing_with_nontext_next_siblings(self):
self.maxDiff = None
html = '''<html><head></head><body>
<div><u>m </u> <div> n</div></div>
<div><u>o</u> <div> p</div></div>
<div><u>q </u> <div>r</div></div>
<div><u>s</u> <div>t</div></div>
<div><u>u</u><div> v</div></div>
<div><u>w </u><div>x</div></div>
</body></html>'''
minified = (
'<html><head></head><body>'
'<div><u>m </u><div>n</div></div>'
'<div><u>o</u><div>p</div></div>'
'<div><u>q </u><div>r</div></div>'
'<div><u>s</u><div>t</div></div>'
'<div><u>u</u><div>v</div></div>'
'<div><u>w </u><div>x</div></div>'
'</body></html>'
)
self.assertEqual(minified, html_minify(html))

0 comments on commit afcb214

Please sign in to comment.