Completed
Pull Request — master (#45)
by Oleg
02:04
created

Html.generate_void_element()   A

Complexity

Conditions 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
crap 2
1
"""
2
SDoc
3
4
Copyright 2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
9
10 1
class Html:
11
    """
12
    Utility class with functions for generating HTML code.
13
    """
14
15
    # ------------------------------------------------------------------------------------------------------------------
16 1
    @staticmethod
17 1
    def escape(text, quote=True):
18
        """
19
        Returns a string with special characters converted to HTML entities.
20
21
        :param str text: The string with optionally special characters.
22
        :param bool quote: If true the quotation mark characters, both double quote (") and single quote (')
23
                           characters are also translated.
24
25
        :rtype: str
26
        """
27 1
        text = text.replace("&", "&")  # Must be done first!
28 1
        text = text.replace("<", "&lt;")
29 1
        text = text.replace(">", "&gt;")
30 1
        if quote:
31 1
            text = text.replace('"', "&quot;")
32 1
            text = text.replace('\'', "&#x27;")
33
34 1
        return text
35
36
    # ------------------------------------------------------------------------------------------------------------------
37 1
    @staticmethod
38
    def generate_attribute(name, value):
39
        """
40
        Returns a string proper conversion of special characters to HTML entities of an attribute of a HTML tag.
41
42
        :param str name: The name of the attribute.
43
        :param str value: The value of the attribute.
44
45
        :rtype: str
46
        """
47 1
        html = ''
48
49
        # Boolean attributes.
50 1
        if name in ('autofocus',
51
                    'checked',
52
                    'disabled',
53
                    'hidden',
54
                    'ismap',
55
                    'multiple',
56
                    'novalidate',
57
                    'readonly',
58
                    'required',
59
                    'selected',
60
                    'spellcheck'):
61
            if value:
62
                html += ' '
63
                html += name
64
                html += '="'
65
                html += name
66
                html += '"'
67
68
        # Annoying boolean attribute exceptions.
69 1
        elif name in ('draggable', 'contenteditable'):
70
            if value is not None:
71
                html += ' '
72
                html += name
73
                html += '="true"' if value else '="false"'
74
75 1
        elif name == 'autocomplete':
76
            if value is not None:
77
                html += ' '
78
                html += name
79
                html += '="on"' if value else '="off"'
80
81 1
        elif name == 'translate':
82
            if value is not None:
83
                html += ' '
84
                html += name
85
                html += '="yes"' if value else '="no"'
86
87
        else:
88 1
            if value is not None and value != '':
89 1
                html += ' '
90 1
                html += Html.escape(name)
91 1
                html += '="'
92 1
                html += Html.escape(value)
93 1
                html += '"'
94
95 1
        return html
96
97
    # ------------------------------------------------------------------------------------------------------------------
98 1
    @staticmethod
99 1
    def generate_element(tag_name, attributes=None, inner_text='', is_html=False):
100
        """
101
        Generates HTML code for an element.
102
103
        Note: tags for void elements such as '<br/>' are not supported.
104
105
        :param str tag_name: The name of the tag, e.g. a, form.
106
        :param dict[str,str] attributes: The attributes of the tag. Special characters in the attributes will be
107
                                         replaced with HTML entities.
108
        :param str inner_text: The inner text of the tag.
109
        :param bool is_html: If set the inner text is a HTML snippet, otherwise special characters in the inner text
110
                             will be replaced with HTML entities.
111
112
        :rtype: str
113
        """
114 1
        html = Html.generate_tag(tag_name, attributes)
115 1
        html += inner_text if is_html else Html.escape(inner_text)
116 1
        html += '</'
117 1
        html += tag_name
118 1
        html += '>'
119
120 1
        return html
121
122
    # ------------------------------------------------------------------------------------------------------------------
123 1
    @staticmethod
124 1
    def generate_tag(tag_name, attributes=None):
125
        """
126
        Generates HTML code for a start tag of an element.
127
128
        :param str tag_name: The name of the tag, e.g. a, form.
129
        :param dict[str,str] attributes: The attributes of the tag. Special characters in the attributes will be
130
                                         replaced with HTML entities.
131
132
        :rtype: str
133
        """
134 1
        html = '<'
135 1
        html += tag_name
136 1
        for key in sorted(attributes):
137 1
            html += Html.generate_attribute(key, attributes[key])
138 1
        html += '>'
139
140 1
        return html
141
142
    # ------------------------------------------------------------------------------------------------------------------
143 1
    @staticmethod
144 1
    def generate_void_element(tag_name, attributes=None):
145
        """
146
        Generates HTML code for an element.
147
148
        Note: tags for void elements such as '<br/>' are not supported.
149
150
        :param str tag_name: The name of the tag, e.g. a, form.
151
        :param dict[str,str] attributes: The attributes of the tag. Special characters in the attributes will be
152
                                         replaced with HTML entities.
153
154
        :rtype: str
155
        """
156 1
        html = '<'
157 1
        html += tag_name
158 1
        for key in sorted(attributes):
159 1
            html += Html.generate_attribute(key, attributes[key])
160 1
        html += '/>'
161
162 1
        return html
163
164
# ----------------------------------------------------------------------------------------------------------------------
165