def normalize_style_attrs(soup):
    """
    Normalize the style attrs on tags so we have a predictable format for parsing.

    e.g. font-style: italic becomes font-style:italic;
    e.g. FONT-WEIGHT:400; becomes font-weight:400;
    e.g. font-weight: bold;font-style:italic; becomes font-style:italic;font-weight:bold;
    The style rules are all lowercased, with `;` appended and have no spaces.
    Spaces are removed between multiple style rules.
    Style rules are sorted alphabetically

    The styles attrs won't exist in the final page content they are removed later on
    in filter_bleach_clean() method, they are kept around for any other processing needs.
    """

    elements = soup.findAll(recursive=True)

    for el in elements:
        if el.attrs and el.attrs.get("style"):
            styles_list = [
                style.strip().lower().replace(" ", "") + ";"
                for style in el.attrs["style"].split(";")
                if style != ""
            ]
            el.attrs["style"] = "".join(sorted(styles_list))

    return soup