def trim(tree, limit, killwords, end):
    if tree.text:  # Root node's text.
        tree.text, limit = truncate_text(tree.text, limit, killwords, end)
    for child in tree:  # Immediate children.
        if limit <= 0:
            # We reached the limit, remove all remaining children.
            tree.remove(child)
        else:
            # Recurse on the current child.
            _parsed_tree, limit = trim(child, limit, killwords, end)
    if tree.tail:  # Root node's tail text.
        if limit <= 0:
            tree.tail = ''
        else:
            tree.tail, limit = truncate_text(tree.tail, limit, killwords, end)
    return tree, limit
Truncate the text of an html5lib tree.
dataset/ETHPy150Open mozilla/addons-server/src/olympia/translations/utils.py/trim