<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def build_html_element(tag, text=None, icon_class=None, cls=None, **kwargs):
 """Builds an html element. :param tag: the html tag to build ('a', 'div', 'img', etc) :param icon_class: the class to apply to an icon element. This only applies to elements that allow a closing tag. :param cls: the css class to apply to the tag. This can also be passed in as a kwarg as "class". '<a href='someurl.com'>hello</a>' """

    if cls is not None:
        kwargs['class'] = cls

    tag_attrs = ' '.join(['{0}="{1}"'.format(k, v) for k, v in kwargs.items()])

    tag_content = '{tag} {tag_attrs}'.format(tag=tag, tag_attrs=tag_attrs)

    if tag in ('img', 'input', 'hr', 'br'):
        return mark_safe('<{tag_content} />'.format(tag_content=tag_content))

    icon = '<i class="{0}"></i> '.format(icon_class) if icon_class else ''

    if not text:
        text = ''
    elif not isinstance(text, SafeText):
        text = escape(text)

    return mark_safe('<{tag_content}>{icon}{text}</{tag}>'.format(
        tag_content=tag_content,
        icon=icon,
        tag=tag,
        text=text)
    )