def insert_tag(tag, before, root):
"""
Insert `tag` before `before` tag if present. If not, insert it into `root`.
Args:
tag (obj): HTMLElement instance.
before (obj): HTMLElement instance.
root (obj): HTMLElement instance.
"""
if not before:
root.childs.append(tag)
tag.parent = root
return
before = before[0] if type(before) in [tuple, list] else before
# put it before first existing identifier
parent = before.parent
iop = parent.childs.index(before)
parent.childs.insert(
iop,
tag
)