diff --git a/htmlmin/minify.py b/htmlmin/minify.py index f3b0a7b..eb34378 100644 --- a/htmlmin/minify.py +++ b/htmlmin/minify.py @@ -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)) diff --git a/htmlmin/tests/resources/inline_whitespace_minified.html b/htmlmin/tests/resources/inline_whitespace_minified.html index 24a24e5..c20d7fc 100644 --- a/htmlmin/tests/resources/inline_whitespace_minified.html +++ b/htmlmin/tests/resources/inline_whitespace_minified.html @@ -1 +1 @@ -Inline Whitespace

Main TitleTM

Abstract

a b c

Premise

a space

nospace

This isa link.

\ No newline at end of file +Inline Whitespace

Main Title TM

Abstract

a b c

Premise

a space

no space

This is a link.

\ No newline at end of file diff --git a/htmlmin/tests/test_minify.py b/htmlmin/tests/test_minify.py index faf0dbe..12af772 100644 --- a/htmlmin/tests/test_minify.py +++ b/htmlmin/tests/test_minify.py @@ -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 = ''' +

a b

+

c d

+

e f

+

g h

+

i j

+

k l

+

m n

+

o p

+

q r

+

s t

+

u v

+

w x

+

y

+

z

+ ''' + minified = ( + '' + '

a b

' + '

c d

' + '

e f

' + '

g h

' + '

i j

' + '

k l

' + '

m n

' + '

o p

' + '

q r

' + '

s t

' + '

u v

' + '

w x

' + '

y

' + '

z

' + '' + ) + self.assertEqual(minified, html_minify(html)) + + def test_inline_text_spacing_with_nontext_prev_siblings(self): + self.maxDiff = None + html = ''' +
m
n
+
o
p
+
q
r
+
s
t
+
u
v
+
w
x
+
y
+ ''' + minified = ( + '' + '
m
n
' + '
o
p
' + '
q
r
' + '
s
t
' + '
u
v
' + '
w
x
' + '
y
' + '' + ) + self.assertEqual(minified, html_minify(html)) + + def test_inline_text_spacing_with_nontext_next_siblings(self): + self.maxDiff = None + html = ''' +
m
n
+
o
p
+
q
r
+
s
t
+
u
v
+
w
x
+ ''' + minified = ( + '' + '
m
n
' + '
o
p
' + '
q
r
' + '
s
t
' + '
u
v
' + '
w
x
' + '' + ) + self.assertEqual(minified, html_minify(html))