Skip to content

Commit 3c8af49

Browse files
authored
Merge pull request #823 from Tontyna/box-sizing
Shrink min-max-height and -width according to box-sizing
2 parents 83a2f25 + 9c9f1de commit 3c8af49

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

weasyprint/layout/percentages.py

+29-10
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,39 @@ def resolve_percentages(box, containing_block, main_flex_direction=None):
106106
prop = 'border_{0}_width'.format(side)
107107
setattr(box, prop, box.style[prop])
108108

109+
# Shrink *content* widths and heights according to box-sizing
110+
# Thanks heavens and the spec: Our validator rejects negative values
111+
# for padding and border-width
109112
if box.style['box_sizing'] == 'border-box':
110-
if box.width != 'auto':
111-
box.width -= (box.padding_left + box.padding_right +
112-
box.border_left_width + box.border_right_width)
113-
if box.height != 'auto':
114-
box.height -= (box.padding_top + box.padding_bottom +
115-
box.border_top_width + box.border_bottom_width)
113+
deltahorz = (box.padding_left + box.padding_right +
114+
box.border_left_width + box.border_right_width)
115+
deltavert = (box.padding_top + box.padding_bottom +
116+
box.border_top_width + box.border_bottom_width)
116117
elif box.style['box_sizing'] == 'padding-box':
117-
if box.width != 'auto':
118-
box.width -= box.padding_left + box.padding_right
119-
if box.height != 'auto':
120-
box.height -= box.padding_top + box.padding_bottom
118+
deltahorz = box.padding_left + box.padding_right
119+
deltavert = box.padding_top + box.padding_bottom
121120
else:
122121
assert box.style['box_sizing'] == 'content-box'
122+
deltahorz = 0
123+
deltavert = 0
124+
125+
# Keep at least min_* >= 0 to prevent funny output in case box.width or
126+
# box.height become negative.
127+
# Restricting max_* seems reasonable, too.
128+
if deltahorz > 0:
129+
if box.width != 'auto':
130+
box.width -= deltahorz
131+
if box.max_width != float('inf'):
132+
box.max_width = max(0, box.max_width-deltahorz)
133+
if box.min_width != 'auto':
134+
box.min_width = max(0, box.min_width-deltahorz)
135+
if deltavert > 0:
136+
if box.height != 'auto':
137+
box.height -= deltavert
138+
if box.max_height != float('inf'):
139+
box.max_height = max(0, box.max_height-deltahorz)
140+
if box.min_height != 'auto':
141+
box.min_height = max(0, box.min_height-deltahorz)
123142

124143

125144
def resolve_radii_percentages(box):

0 commit comments

Comments
 (0)