| 
 | 1 | +#!/usr/bin/python  | 
 | 2 | +# -*- coding: utf-8 -*-  | 
 | 3 | +#  | 
 | 4 | +# --- BEGIN_HEADER ---  | 
 | 5 | +#  | 
 | 6 | +# htmlsupp - test support library for HTML  | 
 | 7 | +# Copyright (C) 2003-2024  The MiG Project by the Science HPC Center at UCPH  | 
 | 8 | +#  | 
 | 9 | +# This file is part of MiG.  | 
 | 10 | +#  | 
 | 11 | +# MiG is free software: you can redistribute it and/or modify  | 
 | 12 | +# it under the terms of the GNU General Public License as published by  | 
 | 13 | +# the Free Software Foundation; either version 2 of the License, or  | 
 | 14 | +# (at your option) any later version.  | 
 | 15 | +#  | 
 | 16 | +# MiG is distributed in the hope that it will be useful,  | 
 | 17 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of  | 
 | 18 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  | 
 | 19 | +# GNU General Public License for more details.  | 
 | 20 | +#  | 
 | 21 | +# You should have received a copy of the GNU General Public License  | 
 | 22 | +# along with this program; if not, write to the Free Software  | 
 | 23 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.  | 
 | 24 | +#  | 
 | 25 | +# -- END_HEADER ---  | 
 | 26 | +#  | 
 | 27 | + | 
 | 28 | +"""Test support library for HTML."""  | 
 | 29 | + | 
 | 30 | +_POSSIBLE_DOCTYPE_OPENERS =[  | 
 | 31 | +    "<!DOCTYPE HTML",  | 
 | 32 | +    "<!DOCTYPE html",  | 
 | 33 | +    "<!doctype html",  | 
 | 34 | +]  | 
 | 35 | + | 
 | 36 | + | 
 | 37 | +class HtmlAssertMixin:  | 
 | 38 | +    """Custom assertions for HTML containing strings."""  | 
 | 39 | + | 
 | 40 | +    def assertHtmlElement(self, value, tag_name):  | 
 | 41 | +        """Check that an occurrence of the specifid tag within an HTML input  | 
 | 42 | +        string can be found. Returns the textual content of the first match.  | 
 | 43 | +        """  | 
 | 44 | + | 
 | 45 | +        self.assertIsValidHtmlDocument(value, permit_no_close=True)  | 
 | 46 | + | 
 | 47 | +        # TODO: this is a definitively stop-gap way of finding a tag within the HTML  | 
 | 48 | +        #       and is used purely to keep this initial change to a reasonable size.  | 
 | 49 | + | 
 | 50 | +        tag_open = ''.join(['<', tag_name, '>'])  | 
 | 51 | +        tag_open_index = value.index(tag_open)  | 
 | 52 | +        tag_open_index_after = tag_open_index + len(tag_open)  | 
 | 53 | + | 
 | 54 | +        tag_close = ''.join(['</', tag_name, '>'])  | 
 | 55 | +        tag_close_index = value.index(tag_close, tag_open_index_after)  | 
 | 56 | + | 
 | 57 | +        return value[tag_open_index_after:tag_close_index]  | 
 | 58 | + | 
 | 59 | +    def assertIsValidHtmlDocument(self, value, permit_no_close=False):  | 
 | 60 | +        """Check that the input string contains a valid HTML document.  | 
 | 61 | +        """  | 
 | 62 | + | 
 | 63 | +        assert isinstance(value, type(u""))  | 
 | 64 | + | 
 | 65 | +        error = None  | 
 | 66 | +        try:  | 
 | 67 | +            for opener in _POSSIBLE_DOCTYPE_OPENERS:  | 
 | 68 | +                has_doctype = value.startswith(opener)  | 
 | 69 | +                if has_doctype:  | 
 | 70 | +                    break  | 
 | 71 | +            assert has_doctype, "no valid document opener"  | 
 | 72 | +            end_html_tag_idx = value.rfind('</html>')  | 
 | 73 | +            if end_html_tag_idx == -1 and permit_no_close:  | 
 | 74 | +                return  | 
 | 75 | +            maybe_document_end = value[end_html_tag_idx:].rstrip()  | 
 | 76 | +            assert maybe_document_end == '</html>', "no valid document closer"  | 
 | 77 | +        except Exception as exc:  | 
 | 78 | +            error = exc  | 
 | 79 | +        if error:  | 
 | 80 | +            raise AssertionError("failed to verify input string as HTML: %s\ndocument:%s" % (str(error), value))  | 
0 commit comments