author | Pierre-Yves David <pierre-yves.david@logilab.fr> |
Fri, 26 Apr 2013 11:52:35 +0200 | |
changeset 8939 | 30ce8eccfe3f |
parent 8938 | 198fdadafed6 |
child 8940 | ae898a084da2 |
permissions | -rw-r--r-- |
8937
8a1809c9a043
[htmlparser] add missing deprecation message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8695
diff
changeset
|
1 |
# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
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
|
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 |
||
8938
198fdadafed6
[htmlparser] rename SaxOnlyValidator to XMLValidator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8937
diff
changeset
|
25 |
from logilab.common.deprecation import class_deprecated, class_renamed |
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
|
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: |
|
8939
30ce8eccfe3f
[htmlparser] store unaltered source in pageinfo.source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8938
diff
changeset
|
38 |
return PageInfo(self, data) |
8695
358d8bed9626
[toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7014
diff
changeset
|
39 |
except etree.XMLSyntaxError as exc: |
0 | 40 |
def save_in(fname=''): |
41 |
file(fname, 'w').write(data) |
|
42 |
new_exc = AssertionError(u'invalid xml %s' % exc) |
|
43 |
new_exc.position = exc.position |
|
44 |
raise new_exc |
|
45 |
||
46 |
def preprocess_data(self, data): |
|
47 |
return data |
|
48 |
||
49 |
||
50 |
class DTDValidator(Validator): |
|
51 |
def __init__(self): |
|
52 |
Validator.__init__(self) |
|
3151 | 53 |
# XXX understand what's happening under windows |
6771
da71f1ad1721
minor code cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
54 |
self.parser = etree.XMLParser(dtd_validation=sys.platform != 'win32') |
0 | 55 |
|
56 |
def preprocess_data(self, data): |
|
57 |
"""used to fix potential blockquote mess generated by docutils""" |
|
1485 | 58 |
if STRICT_DOCTYPE not in data: |
0 | 59 |
return data |
60 |
# parse using transitional DTD |
|
1485 | 61 |
data = data.replace(STRICT_DOCTYPE, TRANSITIONAL_DOCTYPE) |
0 | 62 |
tree = etree.fromstring(data, self.parser) |
63 |
namespace = tree.nsmap.get(None) |
|
64 |
# this is the list of authorized child tags for <blockquote> nodes |
|
65 |
expected = 'p h1 h2 h3 h4 h5 h6 div ul ol dl pre hr blockquote address ' \ |
|
66 |
'fieldset table form noscript ins del script'.split() |
|
67 |
if namespace: |
|
68 |
blockquotes = tree.findall('.//{%s}blockquote' % namespace) |
|
69 |
expected = ['{%s}%s' % (namespace, tag) for tag in expected] |
|
70 |
else: |
|
71 |
blockquotes = tree.findall('.//blockquote') |
|
72 |
# quick and dirty approach: remove all blockquotes |
|
73 |
for blockquote in blockquotes: |
|
74 |
parent = blockquote.getparent() |
|
75 |
parent.remove(blockquote) |
|
76 |
data = etree.tostring(tree) |
|
1485 | 77 |
return '<?xml version="1.0" encoding="UTF-8"?>%s\n%s' % ( |
78 |
STRICT_DOCTYPE, data) |
|
0 | 79 |
|
1485 | 80 |
|
8938
198fdadafed6
[htmlparser] rename SaxOnlyValidator to XMLValidator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8937
diff
changeset
|
81 |
class XMLValidator(Validator): |
198fdadafed6
[htmlparser] rename SaxOnlyValidator to XMLValidator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8937
diff
changeset
|
82 |
""" A fully compliant XML parser """ |
0 | 83 |
|
84 |
def __init__(self): |
|
85 |
Validator.__init__(self) |
|
86 |
self.parser = etree.XMLParser() |
|
87 |
||
8938
198fdadafed6
[htmlparser] rename SaxOnlyValidator to XMLValidator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8937
diff
changeset
|
88 |
SaxOnlyValidator = class_renamed('SaxOnlyValidator', |
198fdadafed6
[htmlparser] rename SaxOnlyValidator to XMLValidator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8937
diff
changeset
|
89 |
XMLValidator, |
198fdadafed6
[htmlparser] rename SaxOnlyValidator to XMLValidator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8937
diff
changeset
|
90 |
'[3.17] you should use the ' |
198fdadafed6
[htmlparser] rename SaxOnlyValidator to XMLValidator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8937
diff
changeset
|
91 |
'XMLValidator class instead') |
198fdadafed6
[htmlparser] rename SaxOnlyValidator to XMLValidator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8937
diff
changeset
|
92 |
|
6771
da71f1ad1721
minor code cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
93 |
|
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
|
94 |
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
|
95 |
""" 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
|
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 |
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
|
98 |
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
|
99 |
""" |
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
|
100 |
__metaclass__ = class_deprecated |
8937
8a1809c9a043
[htmlparser] add missing deprecation message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8695
diff
changeset
|
101 |
__deprecation_warning__ = '[3.10] this is now handled in testlib.py' |
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
|
102 |
|
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 |
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
|
104 |
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
|
105 |
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
|
106 |
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
|
107 |
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
|
108 |
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
|
109 |
|
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
|
110 |
|
0 | 111 |
class HTMLValidator(Validator): |
112 |
||
113 |
def __init__(self): |
|
114 |
Validator.__init__(self) |
|
115 |
self.parser = etree.HTMLParser() |
|
116 |
||
1485 | 117 |
|
0 | 118 |
|
119 |
class PageInfo(object): |
|
120 |
"""holds various informations on the view's output""" |
|
8939
30ce8eccfe3f
[htmlparser] store unaltered source in pageinfo.source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8938
diff
changeset
|
121 |
def __init__(self, validator, source): |
0 | 122 |
self.source = source |
8939
30ce8eccfe3f
[htmlparser] store unaltered source in pageinfo.source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8938
diff
changeset
|
123 |
root = etree.fromstring(validator.preprocess_data(source), validator.parser) |
0 | 124 |
self.etree = root |
125 |
self.source = source |
|
126 |
self.raw_text = u''.join(root.xpath('//text()')) |
|
127 |
self.namespace = self.etree.nsmap |
|
128 |
self.default_ns = self.namespace.get(None) |
|
129 |
self.a_tags = self.find_tag('a') |
|
130 |
self.h1_tags = self.find_tag('h1') |
|
131 |
self.h2_tags = self.find_tag('h2') |
|
132 |
self.h3_tags = self.find_tag('h3') |
|
133 |
self.h4_tags = self.find_tag('h4') |
|
134 |
self.input_tags = self.find_tag('input') |
|
135 |
self.title_tags = [self.h1_tags, self.h2_tags, self.h3_tags, self.h4_tags] |
|
1485 | 136 |
|
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
|
137 |
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
|
138 |
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
|
139 |
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
|
140 |
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
|
141 |
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
|
142 |
|
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
|
143 |
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
|
144 |
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
|
145 |
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
|
146 |
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
|
147 |
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
|
148 |
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
|
149 |
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
|
150 |
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
|
151 |
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
|
152 |
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
|
153 |
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
|
154 |
|
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 |
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
|
156 |
"""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
|
157 |
`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
|
158 |
""" |
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 |
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
|
160 |
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
|
161 |
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
|
162 |
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
|
163 |
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
|
164 |
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
|
165 |
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
|
166 |
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
|
167 |
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
|
168 |
|
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
|
169 |
def find_tag(self, tag, gettext=True): |
0 | 170 |
"""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
|
171 |
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
|
172 |
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
|
173 |
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
|
174 |
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
|
175 |
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
|
176 |
for elt in self.etree.iterfind(iterstr)] |
1485 | 177 |
|
0 | 178 |
def appears(self, text): |
179 |
"""returns True if <text> appears in the page""" |
|
180 |
return text in self.raw_text |
|
181 |
||
182 |
def __contains__(self, text): |
|
183 |
return text in self.source |
|
1485 | 184 |
|
0 | 185 |
def has_title(self, text, level=None): |
186 |
"""returns True if <h?>text</h?> |
|
187 |
||
188 |
:param level: the title's level (1 for h1, 2 for h2, etc.) |
|
189 |
""" |
|
190 |
if level is None: |
|
191 |
for hlist in self.title_tags: |
|
192 |
if text in hlist: |
|
193 |
return True |
|
194 |
return False |
|
195 |
else: |
|
196 |
hlist = self.title_tags[level - 1] |
|
197 |
return text in hlist |
|
198 |
||
199 |
def has_title_regexp(self, pattern, level=None): |
|
200 |
"""returns True if <h?>pattern</h?>""" |
|
201 |
sre = re.compile(pattern) |
|
202 |
if level is None: |
|
203 |
for hlist in self.title_tags: |
|
204 |
for title in hlist: |
|
205 |
if sre.match(title): |
|
206 |
return True |
|
207 |
return False |
|
208 |
else: |
|
209 |
hlist = self.title_tags[level - 1] |
|
210 |
for title in hlist: |
|
211 |
if sre.match(title): |
|
212 |
return True |
|
213 |
return False |
|
1485 | 214 |
|
0 | 215 |
def has_link(self, text, url=None): |
216 |
"""returns True if <a href=url>text</a> was found in the page""" |
|
217 |
for link_text, attrs in self.a_tags: |
|
218 |
if text == link_text: |
|
219 |
if url is None: |
|
220 |
return True |
|
221 |
try: |
|
222 |
href = attrs['href'] |
|
223 |
if href == url: |
|
224 |
return True |
|
225 |
except KeyError: |
|
226 |
continue |
|
227 |
return False |
|
1485 | 228 |
|
0 | 229 |
def has_link_regexp(self, pattern, url=None): |
230 |
"""returns True if <a href=url>pattern</a> was found in the page""" |
|
231 |
sre = re.compile(pattern) |
|
232 |
for link_text, attrs in self.a_tags: |
|
233 |
if sre.match(link_text): |
|
234 |
if url is None: |
|
235 |
return True |
|
236 |
try: |
|
237 |
href = attrs['href'] |
|
238 |
if href == url: |
|
239 |
return True |
|
240 |
except KeyError: |
|
241 |
continue |
|
242 |
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
|
243 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
244 |
VALMAP = {None: None, 'dtd': DTDValidator, 'xml': SaxOnlyValidator} |