Dhia Shalabi
Dhia Shalabi

Reputation: 1530

Validate HTML tags if they are valid HTML tag or not python?

I am using Django to build website and I want to check if I am using a valid HTML tag or not. This is my code.

class Actions:
    view_class = None
    action_name = None
    action_code = None
    action_icon = None
    action_attr = None
    action_title = "icon"  # icon,text ,both
    with_url = False
    actions_template = "core/core/actions/action.html"
    action_css = []
    action_js = []
    action_class = "btn btn-secondary hide action-item"
    action_position = "above_form"  # above_form, above_grid, both ,on_form , on_grid
    action_tag = "a"

    def render_actions(self, request):
        return render_to_string(
            self.actions_template,
            {
                "action_name": self.action_name,
                "action_code": self.action_code,
                "action_icon": self.action_icon,
                "action_class": self.action_class,
                "action_title": self.action_title,
                "action_tag": self.action_tag,
                "with_url": self.with_url,
                "action_attr": self.action_attr,
                "with_modal": self.with_modal,
                "form_name": form_name,
                "action_url": reverse_lazy(self._related.__class__.__name__.lower()) if self.with_url else "",
            },
        )

For example if action_tag value is buttun the code should raise error, if action_tag value is button or any valid HTML tag there is no error raised. How can I do this offline not online.

Upvotes: 0

Views: 550

Answers (1)

Mickaël Martinez
Mickaël Martinez

Reputation: 1843

First of all, you can store a list of all the html tags in a constants.py file:

html_tags = ['<html>', '<base>', '<head>', '<link>', '<meta>', '<style>', '<title>', '<body>', '<address>', '<article>', '<aside>', '<footer>', '<header>', '<h1>', '<h2>', '<h3>', '<h4>', '<h5>', '<h6>', '<main>', '<nav>', '<section>', '<blockquote>', '<dd>', '<div>', '<dl>', '<dt>', '<figcaption>', '<figure>', '<hr>', '<li>', '<ol>', '<p>', '<pre>', '<ul>', '<a>', '<abbr>', '<b>', '<bdi>', '<bdo>', '<br>', '<cite>', '<code>', '<data>', '<dfn>', '<em>', '<i>', '<kbd>', '<mark>', '<q>', '<rp>', '<rt>', '<ruby>', '<s>', '<samp>', '<small>', '<span>', '<strong>', '<sub>', '<sup>', '<time>', '<u>', '<var>', '<wbr>', '<area>', '<audio>', '<img>', '<map>', '<track>', '<video>', '<embed>', '<iframe>', '<object>', '<param>', '<picture>', '<portal>', '<source>', '<svg>', '<math>', '<canvas>', '<noscript>', '<script>', '<del>', '<ins>', '<caption>', '<col>', '<colgroup>', '<table>', '<tbody>', '<td>', '<tfoot>', '<th>', '<thead>', '<tr>', '<button>', '<datalist>', '<fieldset>', '<form>', '<input>', '<label>', '<legend>', '<meter>', '<optgroup>', '<option>', '<output>', '<progress>', '<select>', '<textarea>', '<details>', '<dialog>', '<menu>', '<summary>', '<slot>', '<template>']

I wrote this one by using this page (without the deprecated elements, but you can add them).

Then you can easily raise an exception if your action tag is not in that list:

if action_tag not in html_tags:
    raise ValueError('Not a valid html tag.')

Upvotes: 1

Related Questions