Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stop removal of spaces that causes display changes #150

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Comment on lines +189 to +191
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function always returns the same value. Why this is necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Essentially, this is an optimization. Prior to this, I had something like this:

prev_flow = (
    is_text_tag(soup.previous_sibling)
    or is_text_tag(soup.parent)
)

next_flow = (
    is_text_tag(soup.next_sibling)
    or is_text_tag(soup.parent)
)

Then I noticed that I was essentially running is_text_tag(soup.parent) twice and that when True it forces both prev_flow and next_flow to True.

I'm open to suggestions on a code comment to make it more understandable.


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))