author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Mon, 03 Dec 2012 00:03:03 +0100 | |
changeset 8609 | 112a04c0473d |
parent 7014 | 7e3e80f4179a |
child 8695 | 358d8bed9626 |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5276
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5276
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5276
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5276
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5276
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5276
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5276
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5276
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5276
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5276
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5276
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5276
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5276
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5276
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5276
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5276
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
6771
da71f1ad1721
minor code cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
18 |
"""defines a validating HTML parser used in web application tests""" |
0 | 19 |
|
20 |
import re |
|
3325
44caeccd2db9
fix sys import
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
3151
diff
changeset
|
21 |
import sys |
0 | 22 |
|
23 |
from lxml import etree |
|
24 |
||
6772
68bb0943d192
[test, html validation] make validator selection somewhat smarter (at least it works properly when content is demoted from xhtml to html, making the XMLDemotingValidator class useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6771
diff
changeset
|
25 |
from logilab.common.deprecation import class_deprecated |
68bb0943d192
[test, html validation] make validator selection somewhat smarter (at least it works properly when content is demoted from xhtml to html, making the XMLDemotingValidator class useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6771
diff
changeset
|
26 |
|
1421
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
27 |
from cubicweb.view import STRICT_DOCTYPE, TRANSITIONAL_DOCTYPE |
6771
da71f1ad1721
minor code cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
28 |
|
1485 | 29 |
STRICT_DOCTYPE = str(STRICT_DOCTYPE) |
30 |
TRANSITIONAL_DOCTYPE = str(TRANSITIONAL_DOCTYPE) |
|
0 | 31 |
|
32 |
ERR_COUNT = 0 |
|
33 |
||
34 |
class Validator(object): |
|
1485 | 35 |
|
0 | 36 |
def parse_string(self, data, sysid=None): |
37 |
try: |
|
38 |
data = self.preprocess_data(data) |
|
39 |
return PageInfo(data, etree.fromstring(data, self.parser)) |
|
40 |
except etree.XMLSyntaxError, exc: |
|
41 |
def save_in(fname=''): |
|
42 |
file(fname, 'w').write(data) |
|
43 |
new_exc = AssertionError(u'invalid xml %s' % exc) |
|
44 |
new_exc.position = exc.position |
|
45 |
raise new_exc |
|
46 |
||
47 |
def preprocess_data(self, data): |
|
48 |
return data |
|
49 |
||
50 |
||
51 |
class DTDValidator(Validator): |
|
52 |
def __init__(self): |
|
53 |
Validator.__init__(self) |
|
3151 | 54 |
# XXX understand what's happening under windows |
6771
da71f1ad1721
minor code cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
55 |
self.parser = etree.XMLParser(dtd_validation=sys.platform != 'win32') |
0 | 56 |
|
57 |
def preprocess_data(self, data): |
|
58 |
"""used to fix potential blockquote mess generated by docutils""" |
|
1485 | 59 |
if STRICT_DOCTYPE not in data: |
0 | 60 |
return data |
61 |
# parse using transitional DTD |
|
1485 | 62 |
data = data.replace(STRICT_DOCTYPE, TRANSITIONAL_DOCTYPE) |
0 | 63 |
tree = etree.fromstring(data, self.parser) |
64 |
namespace = tree.nsmap.get(None) |
|
65 |
# this is the list of authorized child tags for <blockquote> nodes |
|
66 |
expected = 'p h1 h2 h3 h4 h5 h6 div ul ol dl pre hr blockquote address ' \ |
|
67 |
'fieldset table form noscript ins del script'.split() |
|
68 |
if namespace: |
|
69 |
blockquotes = tree.findall('.//{%s}blockquote' % namespace) |
|
70 |
expected = ['{%s}%s' % (namespace, tag) for tag in expected] |
|
71 |
else: |
|
72 |
blockquotes = tree.findall('.//blockquote') |
|
73 |
# quick and dirty approach: remove all blockquotes |
|
74 |
for blockquote in blockquotes: |
|
75 |
parent = blockquote.getparent() |
|
76 |
parent.remove(blockquote) |
|
77 |
data = etree.tostring(tree) |
|
1485 | 78 |
return '<?xml version="1.0" encoding="UTF-8"?>%s\n%s' % ( |
79 |
STRICT_DOCTYPE, data) |
|
0 | 80 |
|
1485 | 81 |
|
0 | 82 |
class SaxOnlyValidator(Validator): |
83 |
||
84 |
def __init__(self): |
|
85 |
Validator.__init__(self) |
|
86 |
self.parser = etree.XMLParser() |
|
87 |
||
6771
da71f1ad1721
minor code cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
88 |
|
5276
5037d891e207
[devtools/validators] add an Xml validator able to degrade to Html validation because of views perusing demote_to_html
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
89 |
class XMLDemotingValidator(SaxOnlyValidator): |
5037d891e207
[devtools/validators] add an Xml validator able to degrade to Html validation because of views perusing demote_to_html
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
90 |
""" some views produce html instead of xhtml, using demote_to_html |
5037d891e207
[devtools/validators] add an Xml validator able to degrade to Html validation because of views perusing demote_to_html
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
91 |
|
5037d891e207
[devtools/validators] add an Xml validator able to degrade to Html validation because of views perusing demote_to_html
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
92 |
this is typically related to the use of external dependencies |
5037d891e207
[devtools/validators] add an Xml validator able to degrade to Html validation because of views perusing demote_to_html
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
93 |
which do not produce valid xhtml (google maps, ...) |
5037d891e207
[devtools/validators] add an Xml validator able to degrade to Html validation because of views perusing demote_to_html
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
94 |
""" |
6772
68bb0943d192
[test, html validation] make validator selection somewhat smarter (at least it works properly when content is demoted from xhtml to html, making the XMLDemotingValidator class useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6771
diff
changeset
|
95 |
__metaclass__ = class_deprecated |
5276
5037d891e207
[devtools/validators] add an Xml validator able to degrade to Html validation because of views perusing demote_to_html
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
96 |
|
5037d891e207
[devtools/validators] add an Xml validator able to degrade to Html validation because of views perusing demote_to_html
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
97 |
def preprocess_data(self, data): |
5037d891e207
[devtools/validators] add an Xml validator able to degrade to Html validation because of views perusing demote_to_html
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
98 |
if data.startswith('<?xml'): |
5037d891e207
[devtools/validators] add an Xml validator able to degrade to Html validation because of views perusing demote_to_html
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
99 |
self.parser = etree.XMLParser() |
5037d891e207
[devtools/validators] add an Xml validator able to degrade to Html validation because of views perusing demote_to_html
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
100 |
else: |
5037d891e207
[devtools/validators] add an Xml validator able to degrade to Html validation because of views perusing demote_to_html
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
101 |
self.parser = etree.HTMLParser() |
5037d891e207
[devtools/validators] add an Xml validator able to degrade to Html validation because of views perusing demote_to_html
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
102 |
return data |
5037d891e207
[devtools/validators] add an Xml validator able to degrade to Html validation because of views perusing demote_to_html
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
103 |
|
5037d891e207
[devtools/validators] add an Xml validator able to degrade to Html validation because of views perusing demote_to_html
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
104 |
|
0 | 105 |
class HTMLValidator(Validator): |
106 |
||
107 |
def __init__(self): |
|
108 |
Validator.__init__(self) |
|
109 |
self.parser = etree.HTMLParser() |
|
110 |
||
1485 | 111 |
|
0 | 112 |
|
113 |
class PageInfo(object): |
|
114 |
"""holds various informations on the view's output""" |
|
115 |
def __init__(self, source, root): |
|
116 |
self.source = source |
|
117 |
self.etree = root |
|
118 |
self.source = source |
|
119 |
self.raw_text = u''.join(root.xpath('//text()')) |
|
120 |
self.namespace = self.etree.nsmap |
|
121 |
self.default_ns = self.namespace.get(None) |
|
122 |
self.a_tags = self.find_tag('a') |
|
123 |
self.h1_tags = self.find_tag('h1') |
|
124 |
self.h2_tags = self.find_tag('h2') |
|
125 |
self.h3_tags = self.find_tag('h3') |
|
126 |
self.h4_tags = self.find_tag('h4') |
|
127 |
self.input_tags = self.find_tag('input') |
|
128 |
self.title_tags = [self.h1_tags, self.h2_tags, self.h3_tags, self.h4_tags] |
|
1485 | 129 |
|
7014
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
130 |
def _iterstr(self, tag): |
6977
cb78108bf603
[testlib] new method on page info object to ensure some tag with arbitrary attributes is found
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6772
diff
changeset
|
131 |
if self.default_ns is None: |
cb78108bf603
[testlib] new method on page info object to ensure some tag with arbitrary attributes is found
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6772
diff
changeset
|
132 |
return ".//%s" % tag |
cb78108bf603
[testlib] new method on page info object to ensure some tag with arbitrary attributes is found
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6772
diff
changeset
|
133 |
else: |
cb78108bf603
[testlib] new method on page info object to ensure some tag with arbitrary attributes is found
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6772
diff
changeset
|
134 |
return ".//{%s}%s" % (self.default_ns, tag) |
cb78108bf603
[testlib] new method on page info object to ensure some tag with arbitrary attributes is found
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6772
diff
changeset
|
135 |
|
7014
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
136 |
def matching_nodes(self, tag, **attrs): |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
137 |
for elt in self.etree.iterfind(self._iterstr(tag)): |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
138 |
eltattrs = elt.attrib |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
139 |
for attr, value in attrs.iteritems(): |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
140 |
try: |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
141 |
if eltattrs[attr] != value: |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
142 |
break |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
143 |
except KeyError: |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
144 |
break |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
145 |
else: # all attributes match |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
146 |
yield elt |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
147 |
|
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
148 |
def has_tag(self, tag, nboccurs=1, **attrs): |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
149 |
"""returns True if tag with given attributes appears in the page |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
150 |
`nbtimes` (any if None) |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
151 |
""" |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
152 |
for elt in self.matching_nodes(tag, **attrs): |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
153 |
if nboccurs is None: # no need to check number of occurences |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
154 |
return True |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
155 |
if not nboccurs: # too much occurences |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
156 |
return False |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
157 |
nboccurs -= 1 |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
158 |
if nboccurs == 0: # correct number of occurences |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
159 |
return True |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
160 |
return False # no matching tag/attrs |
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
161 |
|
1945
2b59d9ae17ae
new argument telling if we want text or (text / attrs), keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1485
diff
changeset
|
162 |
def find_tag(self, tag, gettext=True): |
0 | 163 |
"""return a list which contains text of all "tag" elements """ |
7014
7e3e80f4179a
[testlib pageinfo] extract matching_node method from has_tag to ease looking for a node with a given set of attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6977
diff
changeset
|
164 |
iterstr = self._iterstr(tag) |
1945
2b59d9ae17ae
new argument telling if we want text or (text / attrs), keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1485
diff
changeset
|
165 |
if not gettext or tag in ('a', 'input'): |
6977
cb78108bf603
[testlib] new method on page info object to ensure some tag with arbitrary attributes is found
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6772
diff
changeset
|
166 |
return [(elt.text, elt.attrib) |
cb78108bf603
[testlib] new method on page info object to ensure some tag with arbitrary attributes is found
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6772
diff
changeset
|
167 |
for elt in self.etree.iterfind(iterstr)] |
cb78108bf603
[testlib] new method on page info object to ensure some tag with arbitrary attributes is found
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6772
diff
changeset
|
168 |
return [u''.join(elt.xpath('.//text()')) |
cb78108bf603
[testlib] new method on page info object to ensure some tag with arbitrary attributes is found
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6772
diff
changeset
|
169 |
for elt in self.etree.iterfind(iterstr)] |
1485 | 170 |
|
0 | 171 |
def appears(self, text): |
172 |
"""returns True if <text> appears in the page""" |
|
173 |
return text in self.raw_text |
|
174 |
||
175 |
def __contains__(self, text): |
|
176 |
return text in self.source |
|
1485 | 177 |
|
0 | 178 |
def has_title(self, text, level=None): |
179 |
"""returns True if <h?>text</h?> |
|
180 |
||
181 |
:param level: the title's level (1 for h1, 2 for h2, etc.) |
|
182 |
""" |
|
183 |
if level is None: |
|
184 |
for hlist in self.title_tags: |
|
185 |
if text in hlist: |
|
186 |
return True |
|
187 |
return False |
|
188 |
else: |
|
189 |
hlist = self.title_tags[level - 1] |
|
190 |
return text in hlist |
|
191 |
||
192 |
def has_title_regexp(self, pattern, level=None): |
|
193 |
"""returns True if <h?>pattern</h?>""" |
|
194 |
sre = re.compile(pattern) |
|
195 |
if level is None: |
|
196 |
for hlist in self.title_tags: |
|
197 |
for title in hlist: |
|
198 |
if sre.match(title): |
|
199 |
return True |
|
200 |
return False |
|
201 |
else: |
|
202 |
hlist = self.title_tags[level - 1] |
|
203 |
for title in hlist: |
|
204 |
if sre.match(title): |
|
205 |
return True |
|
206 |
return False |
|
1485 | 207 |
|
0 | 208 |
def has_link(self, text, url=None): |
209 |
"""returns True if <a href=url>text</a> was found in the page""" |
|
210 |
for link_text, attrs in self.a_tags: |
|
211 |
if text == link_text: |
|
212 |
if url is None: |
|
213 |
return True |
|
214 |
try: |
|
215 |
href = attrs['href'] |
|
216 |
if href == url: |
|
217 |
return True |
|
218 |
except KeyError: |
|
219 |
continue |
|
220 |
return False |
|
1485 | 221 |
|
0 | 222 |
def has_link_regexp(self, pattern, url=None): |
223 |
"""returns True if <a href=url>pattern</a> was found in the page""" |
|
224 |
sre = re.compile(pattern) |
|
225 |
for link_text, attrs in self.a_tags: |
|
226 |
if sre.match(link_text): |
|
227 |
if url is None: |
|
228 |
return True |
|
229 |
try: |
|
230 |
href = attrs['href'] |
|
231 |
if href == url: |
|
232 |
return True |
|
233 |
except KeyError: |
|
234 |
continue |
|
235 |
return False |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
236 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
237 |
VALMAP = {None: None, 'dtd': DTDValidator, 'xml': SaxOnlyValidator} |