Permalink
Browse files

Fix whitespace collapsing across not-in-normal-flow boxes.

  • Loading branch information...
1 parent 63196bb commit f65619f452d907a4de378b3c5ea3a45e4e8955ac @SimonSapin committed Jun 28, 2012
Showing with 22 additions and 15 deletions.
  1. +10 −11 weasyprint/formatting_structure/build.py
  2. +12 −4 weasyprint/tests/test_boxes.py
@@ -551,6 +551,7 @@ def process_whitespace(box, following_collapsible_space=False):
"""First part of "The 'white-space' processing model".
See http://www.w3.org/TR/CSS21/text.html#white-space-model
+ http://dev.w3.org/csswg/css3-text/#white-space-rules
"""
if isinstance(box, boxes.TextBox):
@@ -584,27 +585,25 @@ def process_whitespace(box, following_collapsible_space=False):
if handling in ('normal', 'nowrap', 'pre-line'):
text = text.replace('\t', ' ')
text = re.sub(' +', ' ', text)
+ previous_text = text
if following_collapsible_space and text.startswith(' '):
text = text[1:]
- following_collapsible_space = text.endswith(' ')
+ following_collapsible_space = previous_text.endswith(' ')
else:
following_collapsible_space = False
box.text = text
return following_collapsible_space
- # Don't collapse across starting tag
- if isinstance(box, boxes.AtomicInlineLevelBox):
- following_collapsible_space = False
-
if isinstance(box, boxes.ParentBox):
for child in box.children:
- following_collapsible_space = process_whitespace(
- child, following_collapsible_space)
-
- # Don't collapse across ending tag
- if isinstance(box, boxes.AtomicInlineLevelBox):
- following_collapsible_space = False
+ if isinstance(child, (boxes.TextBox, boxes.InlineBox)):
+ following_collapsible_space = process_whitespace(
+ child, following_collapsible_space)
+ else:
+ process_whitespace(child)
+ if child.is_in_normal_flow():
+ following_collapsible_space = False
return following_collapsible_space
@@ -160,7 +160,9 @@ def sanity_checks(box):
break
assert any(
- all(isinstance(child, acceptable_types) for child in box.children)
+ all(isinstance(child, acceptable_types)
+ or not child.is_in_normal_flow()
+ for child in box.children)
for acceptable_types in acceptable_types_lists
), (box, box.children)
@@ -384,7 +386,9 @@ def test_whitespace():
# http://www.w3.org/TR/CSS21/text.html#white-space-model
assert_tree(parse_all('''
<p>Lorem \t\r\n ipsum\t<strong> dolor
- <img src=pattern.png> sit</strong>.</p>
+ <img src=pattern.png> sit
+ <span style="position: absolute"></span> <em> amet </em>
+ consectetur</strong>.</p>
<pre>\t foo\n</pre>
<pre style="white-space: pre-wrap">\t foo\n</pre>
<pre style="white-space: pre-line">\t foo\n</pre>
@@ -395,7 +399,11 @@ def test_whitespace():
('strong', 'Inline', [
('strong', 'Text', 'dolor '),
('img', 'InlineReplaced', '<replaced>'),
- ('strong', 'Text', ' sit')]),
+ ('strong', 'Text', ' sit '),
+ ('span', 'Block', []),
+ ('em', 'Inline', [
+ ('em', 'Text', 'amet ')]),
+ ('strong', 'Text', 'consectetur')]),
('p', 'Text', '.')])]),
('pre', 'Block', [
('pre', 'Line', [
@@ -408,7 +416,7 @@ def test_whitespace():
('pre', 'Block', [
('pre', 'Line', [
# pre-line
- ('pre', 'Text', 'foo\n')])])])
+ ('pre', 'Text', ' foo\n')])])])
@assert_no_logs

0 comments on commit f65619f

Please sign in to comment.