Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefilters: fix crash due to class being a string not list #173

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions wagtail_wordpress_import/prefilters/transform_styles_defaults.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
from django.conf import settings

def __append_or_set(tag, key, value):
if tag.attrs.get(key):
# The key class can be a string or list
# so copy the mode that is being used currently
# https://beautiful-soup-4.readthedocs.io/en/latest/index.html?highlight=find_all#multi-valued-attributes
if type(tag.attrs[key]) is str:
tag.attrs[key] += " " + value
else:
tag.attrs[key].append(value)
else:
tag.attrs[key] = value

def transform_style_bold(soup, tag):
"""
Expand Down Expand Up @@ -34,50 +45,35 @@ def transform_style_center(soup, tag):
"""
apply a new css class to any existing classes
"""
if tag.attrs.get("class"):
tag.attrs["class"].append("align-center")
else:
tag.attrs["class"] = "align-center"
__append_or_set(tag, "class", "align-center")


def transform_style_left(soup, tag):
"""
apply a new css class to any existing classes
"""
if tag.attrs.get("class"):
tag.attrs["class"].append("align-left")
else:
tag.attrs["class"] = "align-left"
__append_or_set(tag, "class", "align-left")


def transform_style_right(soup, tag):
"""
apply a new css class to any existing classes
"""
if tag.attrs.get("class"):
tag.attrs["class"].append("align-right")
else:
tag.attrs["class"] = "align-right"
__append_or_set(tag, "class", "align-right")


def transform_float_left(soup, tag):
"""
apply a new css class to any existing classes
"""
if tag.attrs.get("class"):
tag.attrs["class"].append("float-left")
else:
tag.attrs["class"] = "float-left"
__append_or_set(tag, "class", "float-left")


def transform_float_right(soup, tag):
"""
apply a new css class to any existing classes
"""
if tag.attrs.get("class"):
tag.attrs["class"].append("float-right")
else:
tag.attrs["class"] = "float-right"
__append_or_set(tag, "class", "float-right")


def transform_html_tag_strong(soup, tag):
Expand Down