def _insert(self, element, new_element, before):
        """
        Insert a element before or after other element.

        :param element: The reference element.
        :type element: hatemile.util.html.htmldomelement.HTMLDOMElement
        :param new_element: The element that be inserted.
        :type new_element: hatemile.util.html.htmldomelement.HTMLDOMElement
        :param before: To insert the element before the other element.
        :type before: bool
        """

        tag_name = element.get_tag_name()
        append_tags = [
            'BODY',
            'A',
            'FIGCAPTION',
            'LI',
            'DT',
            'DD',
            'LABEL',
            'OPTION',
            'TD',
            'TH'
        ]
        controls = ['INPUT', 'SELECT', 'TEXTAREA']
        if tag_name == 'HTML':
            body = self.parser.find('body').first_result()
            if body is not None:
                self._insert(body, new_element, before)
        elif tag_name in append_tags:
            if before:
                element.prepend_element(new_element)
            else:
                element.append_element(new_element)
        elif tag_name in controls:
            labels = []
            if element.has_attribute('id'):
                labels = self.parser.find(
                    'label[for="'
                    + element.get_attribute('id')
                    + '"]'
                ).list_results()
            if not labels:
                labels = self.parser.find(element).find_ancestors(
                    'label'
                ).list_results()
            for label in labels:
                self._insert(label, new_element, before)
        elif before:
            element.insert_before(new_element)
        else:
            element.insert_after(new_element)
def _insert ( self , element , new_element , before ) : tag_name = element . get_tag_name ( ) append_tags = [ 'BODY' , 'A' , 'FIGCAPTION' , 'LI' , 'DT' , 'DD' , 'LABEL' , 'OPTION' , 'TD' , 'TH' ] controls = [ 'INPUT' , 'SELECT' , 'TEXTAREA' ] if tag_name == 'HTML' : body = self . parser . find ( 'body' ) . first_result ( ) if body is not None : self . _insert ( body , new_element , before ) elif tag_name in append_tags : if before : element . prepend_element ( new_element ) else : element . append_element ( new_element ) elif tag_name in controls : labels = [ ] if element . has_attribute ( 'id' ) : labels = self . parser . find ( 'label[for="' + element . get_attribute ( 'id' ) + '"]' ) . list_results ( ) if not labels : labels = self . parser . find ( element ) . find_ancestors ( 'label' ) . list_results ( ) for label in labels : self . _insert ( label , new_element , before ) elif before : element . insert_before ( new_element ) else : element . insert_after ( new_element )
Insert a element before or after other element.

        :param element: The reference element.
        :type element: hatemile.util.html.htmldomelement.HTMLDOMElement
        :param new_element: The element that be inserted.
        :type new_element: hatemile.util.html.htmldomelement.HTMLDOMElement
        :param before: To insert the element before the other element.
        :type before: bool