author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Tue, 30 Nov 2010 15:05:07 +0100 | |
changeset 6712 | 3e6cd6048be8 |
parent 5424 | 8ecbcbff9777 |
child 6771 | da71f1ad1721 |
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/>. |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1945
diff
changeset
|
18 |
"""defines a validating HTML parser used in web application tests |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1945
diff
changeset
|
19 |
|
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1945
diff
changeset
|
20 |
""" |
0 | 21 |
|
22 |
import re |
|
3325
44caeccd2db9
fix sys import
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
3151
diff
changeset
|
23 |
import sys |
0 | 24 |
|
25 |
from lxml import etree |
|
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 |
1485 | 28 |
STRICT_DOCTYPE = str(STRICT_DOCTYPE) |
29 |
TRANSITIONAL_DOCTYPE = str(TRANSITIONAL_DOCTYPE) |
|
0 | 30 |
|
31 |
ERR_COUNT = 0 |
|
32 |
||
33 |
class Validator(object): |
|
1485 | 34 |
|
0 | 35 |
def parse_string(self, data, sysid=None): |
36 |
try: |
|
37 |
data = self.preprocess_data(data) |
|
38 |
return PageInfo(data, etree.fromstring(data, self.parser)) |
|
39 |
except etree.XMLSyntaxError, exc: |
|
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 |
54 |
validate = True |
|
55 |
if sys.platform == 'win32': |
|
56 |
validate = False |
|
57 |
self.parser = etree.XMLParser(dtd_validation=validate) |
|
0 | 58 |
|
59 |
def preprocess_data(self, data): |
|
60 |
"""used to fix potential blockquote mess generated by docutils""" |
|
1485 | 61 |
if STRICT_DOCTYPE not in data: |
0 | 62 |
return data |
63 |
# parse using transitional DTD |
|
1485 | 64 |
data = data.replace(STRICT_DOCTYPE, TRANSITIONAL_DOCTYPE) |
0 | 65 |
tree = etree.fromstring(data, self.parser) |
66 |
namespace = tree.nsmap.get(None) |
|
67 |
# this is the list of authorized child tags for <blockquote> nodes |
|
68 |
expected = 'p h1 h2 h3 h4 h5 h6 div ul ol dl pre hr blockquote address ' \ |
|
69 |
'fieldset table form noscript ins del script'.split() |
|
70 |
if namespace: |
|
71 |
blockquotes = tree.findall('.//{%s}blockquote' % namespace) |
|
72 |
expected = ['{%s}%s' % (namespace, tag) for tag in expected] |
|
73 |
else: |
|
74 |
blockquotes = tree.findall('.//blockquote') |
|
75 |
# quick and dirty approach: remove all blockquotes |
|
76 |
for blockquote in blockquotes: |
|
77 |
parent = blockquote.getparent() |
|
78 |
parent.remove(blockquote) |
|
79 |
data = etree.tostring(tree) |
|
1485 | 80 |
return '<?xml version="1.0" encoding="UTF-8"?>%s\n%s' % ( |
81 |
STRICT_DOCTYPE, data) |
|
0 | 82 |
|
1485 | 83 |
|
0 | 84 |
class SaxOnlyValidator(Validator): |
85 |
||
86 |
def __init__(self): |
|
87 |
Validator.__init__(self) |
|
88 |
self.parser = etree.XMLParser() |
|
89 |
||
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
|
90 |
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
|
91 |
""" 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
|
92 |
|
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 |
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
|
94 |
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
|
95 |
""" |
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 |
|
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
|
130 |
def find_tag(self, tag, gettext=True): |
0 | 131 |
"""return a list which contains text of all "tag" elements """ |
132 |
if self.default_ns is None: |
|
133 |
iterstr = ".//%s" % tag |
|
134 |
else: |
|
135 |
iterstr = ".//{%s}%s" % (self.default_ns, 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
|
136 |
if not gettext or tag in ('a', 'input'): |
0 | 137 |
return [(elt.text, elt.attrib) for elt in self.etree.iterfind(iterstr)] |
138 |
return [u''.join(elt.xpath('.//text()')) for elt in self.etree.iterfind(iterstr)] |
|
1485 | 139 |
|
0 | 140 |
def appears(self, text): |
141 |
"""returns True if <text> appears in the page""" |
|
142 |
return text in self.raw_text |
|
143 |
||
144 |
def __contains__(self, text): |
|
145 |
return text in self.source |
|
1485 | 146 |
|
0 | 147 |
def has_title(self, text, level=None): |
148 |
"""returns True if <h?>text</h?> |
|
149 |
||
150 |
:param level: the title's level (1 for h1, 2 for h2, etc.) |
|
151 |
""" |
|
152 |
if level is None: |
|
153 |
for hlist in self.title_tags: |
|
154 |
if text in hlist: |
|
155 |
return True |
|
156 |
return False |
|
157 |
else: |
|
158 |
hlist = self.title_tags[level - 1] |
|
159 |
return text in hlist |
|
160 |
||
161 |
def has_title_regexp(self, pattern, level=None): |
|
162 |
"""returns True if <h?>pattern</h?>""" |
|
163 |
sre = re.compile(pattern) |
|
164 |
if level is None: |
|
165 |
for hlist in self.title_tags: |
|
166 |
for title in hlist: |
|
167 |
if sre.match(title): |
|
168 |
return True |
|
169 |
return False |
|
170 |
else: |
|
171 |
hlist = self.title_tags[level - 1] |
|
172 |
for title in hlist: |
|
173 |
if sre.match(title): |
|
174 |
return True |
|
175 |
return False |
|
1485 | 176 |
|
0 | 177 |
def has_link(self, text, url=None): |
178 |
"""returns True if <a href=url>text</a> was found in the page""" |
|
179 |
for link_text, attrs in self.a_tags: |
|
180 |
if text == link_text: |
|
181 |
if url is None: |
|
182 |
return True |
|
183 |
try: |
|
184 |
href = attrs['href'] |
|
185 |
if href == url: |
|
186 |
return True |
|
187 |
except KeyError: |
|
188 |
continue |
|
189 |
return False |
|
1485 | 190 |
|
0 | 191 |
def has_link_regexp(self, pattern, url=None): |
192 |
"""returns True if <a href=url>pattern</a> was found in the page""" |
|
193 |
sre = re.compile(pattern) |
|
194 |
for link_text, attrs in self.a_tags: |
|
195 |
if sre.match(link_text): |
|
196 |
if url is None: |
|
197 |
return True |
|
198 |
try: |
|
199 |
href = attrs['href'] |
|
200 |
if href == url: |
|
201 |
return True |
|
202 |
except KeyError: |
|
203 |
continue |
|
204 |
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
|
205 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
206 |
VALMAP = {None: None, 'dtd': DTDValidator, 'xml': SaxOnlyValidator} |