Add helper function contains_class.

This commit is contained in:
Ethan Dalool 2019-12-29 13:03:44 -08:00
parent 45ba419f16
commit 4c5f416e2c

View file

@ -19,15 +19,20 @@ def raise_children_and_delete(element):
element.insert_after(children.pop(-1)) element.insert_after(children.pop(-1))
element.decompose() element.decompose()
def remove_class(element, cls): def contains_class(element, cls):
if not hasattr(element, 'class') or element['class'] is None: try:
return element['class']
except (AttributeError, KeyError):
return False
if isinstance(element['class'], str): if isinstance(element['class'], str):
if element['class'] == cls: element['class'] = element['class'].split()
del element['class']
return return cls in element['class']
else:
element['class'] = element['class'].split() def remove_class(element, cls):
if not contains_class(element, cls):
return
try: try:
element['class'].remove(cls) element['class'].remove(cls)
except IndexError: except IndexError: