Add helper function contains_class.
This commit is contained in:
parent
45ba419f16
commit
4c5f416e2c
1 changed files with 13 additions and 8 deletions
|
@ -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:
|
|
||||||
del element['class']
|
|
||||||
return
|
|
||||||
else:
|
|
||||||
element['class'] = element['class'].split()
|
element['class'] = element['class'].split()
|
||||||
|
|
||||||
|
return cls in element['class']
|
||||||
|
|
||||||
|
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:
|
||||||
|
|
Loading…
Reference in a new issue