def count_expandable_spaces(box):
    """Count expandable spaces (space and nbsp) for justification."""
    if isinstance(box, boxes.TextBox):
        # TODO: remove trailing spaces correctly
        return box.text.count(' ') + box.text.count('\u00a0')
    elif isinstance(box, (boxes.LineBox, boxes.InlineBox)):
        return sum(count_expandable_spaces(child) for child in box.children)
    else:
        return 0
Count expandable spaces (space and nbsp) for justification.
weasyprint/layout/inline.py