author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 05 Feb 2010 11:56:57 +0100 | |
changeset 4467 | 0e73d299730a |
parent 4252 | 6c4f109c2b03 |
child 5276 | 5037d891e207 |
permissions | -rw-r--r-- |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1945
diff
changeset
|
1 |
"""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
|
2 |
|
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1945
diff
changeset
|
3 |
:organization: Logilab |
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3325
diff
changeset
|
4 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1945
diff
changeset
|
5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1945
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1945
diff
changeset
|
7 |
""" |
0 | 8 |
|
9 |
import re |
|
3325
44caeccd2db9
fix sys import
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
3151
diff
changeset
|
10 |
import sys |
0 | 11 |
|
12 |
from lxml import etree |
|
13 |
||
1421
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
14 |
from cubicweb.view import STRICT_DOCTYPE, TRANSITIONAL_DOCTYPE |
1485 | 15 |
STRICT_DOCTYPE = str(STRICT_DOCTYPE) |
16 |
TRANSITIONAL_DOCTYPE = str(TRANSITIONAL_DOCTYPE) |
|
0 | 17 |
|
18 |
ERR_COUNT = 0 |
|
19 |
||
20 |
class Validator(object): |
|
1485 | 21 |
|
0 | 22 |
def parse_string(self, data, sysid=None): |
23 |
try: |
|
24 |
data = self.preprocess_data(data) |
|
25 |
return PageInfo(data, etree.fromstring(data, self.parser)) |
|
26 |
except etree.XMLSyntaxError, exc: |
|
27 |
def save_in(fname=''): |
|
28 |
file(fname, 'w').write(data) |
|
29 |
new_exc = AssertionError(u'invalid xml %s' % exc) |
|
30 |
new_exc.position = exc.position |
|
31 |
raise new_exc |
|
32 |
||
33 |
def preprocess_data(self, data): |
|
34 |
return data |
|
35 |
||
36 |
||
37 |
class DTDValidator(Validator): |
|
38 |
def __init__(self): |
|
39 |
Validator.__init__(self) |
|
3151 | 40 |
# XXX understand what's happening under windows |
41 |
validate = True |
|
42 |
if sys.platform == 'win32': |
|
43 |
validate = False |
|
44 |
self.parser = etree.XMLParser(dtd_validation=validate) |
|
0 | 45 |
|
46 |
def preprocess_data(self, data): |
|
47 |
"""used to fix potential blockquote mess generated by docutils""" |
|
1485 | 48 |
if STRICT_DOCTYPE not in data: |
0 | 49 |
return data |
50 |
# parse using transitional DTD |
|
1485 | 51 |
data = data.replace(STRICT_DOCTYPE, TRANSITIONAL_DOCTYPE) |
0 | 52 |
tree = etree.fromstring(data, self.parser) |
53 |
namespace = tree.nsmap.get(None) |
|
54 |
# this is the list of authorized child tags for <blockquote> nodes |
|
55 |
expected = 'p h1 h2 h3 h4 h5 h6 div ul ol dl pre hr blockquote address ' \ |
|
56 |
'fieldset table form noscript ins del script'.split() |
|
57 |
if namespace: |
|
58 |
blockquotes = tree.findall('.//{%s}blockquote' % namespace) |
|
59 |
expected = ['{%s}%s' % (namespace, tag) for tag in expected] |
|
60 |
else: |
|
61 |
blockquotes = tree.findall('.//blockquote') |
|
62 |
# quick and dirty approach: remove all blockquotes |
|
63 |
for blockquote in blockquotes: |
|
64 |
parent = blockquote.getparent() |
|
65 |
parent.remove(blockquote) |
|
66 |
data = etree.tostring(tree) |
|
1485 | 67 |
return '<?xml version="1.0" encoding="UTF-8"?>%s\n%s' % ( |
68 |
STRICT_DOCTYPE, data) |
|
0 | 69 |
|
1485 | 70 |
|
0 | 71 |
class SaxOnlyValidator(Validator): |
72 |
||
73 |
def __init__(self): |
|
74 |
Validator.__init__(self) |
|
75 |
self.parser = etree.XMLParser() |
|
76 |
||
77 |
class HTMLValidator(Validator): |
|
78 |
||
79 |
def __init__(self): |
|
80 |
Validator.__init__(self) |
|
81 |
self.parser = etree.HTMLParser() |
|
82 |
||
1485 | 83 |
|
0 | 84 |
|
85 |
class PageInfo(object): |
|
86 |
"""holds various informations on the view's output""" |
|
87 |
def __init__(self, source, root): |
|
88 |
self.source = source |
|
89 |
self.etree = root |
|
90 |
self.source = source |
|
91 |
self.raw_text = u''.join(root.xpath('//text()')) |
|
92 |
self.namespace = self.etree.nsmap |
|
93 |
self.default_ns = self.namespace.get(None) |
|
94 |
self.a_tags = self.find_tag('a') |
|
95 |
self.h1_tags = self.find_tag('h1') |
|
96 |
self.h2_tags = self.find_tag('h2') |
|
97 |
self.h3_tags = self.find_tag('h3') |
|
98 |
self.h4_tags = self.find_tag('h4') |
|
99 |
self.input_tags = self.find_tag('input') |
|
100 |
self.title_tags = [self.h1_tags, self.h2_tags, self.h3_tags, self.h4_tags] |
|
1485 | 101 |
|
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
|
102 |
def find_tag(self, tag, gettext=True): |
0 | 103 |
"""return a list which contains text of all "tag" elements """ |
104 |
if self.default_ns is None: |
|
105 |
iterstr = ".//%s" % tag |
|
106 |
else: |
|
107 |
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
|
108 |
if not gettext or tag in ('a', 'input'): |
0 | 109 |
return [(elt.text, elt.attrib) for elt in self.etree.iterfind(iterstr)] |
110 |
return [u''.join(elt.xpath('.//text()')) for elt in self.etree.iterfind(iterstr)] |
|
1485 | 111 |
|
0 | 112 |
def appears(self, text): |
113 |
"""returns True if <text> appears in the page""" |
|
114 |
return text in self.raw_text |
|
115 |
||
116 |
def __contains__(self, text): |
|
117 |
return text in self.source |
|
1485 | 118 |
|
0 | 119 |
def has_title(self, text, level=None): |
120 |
"""returns True if <h?>text</h?> |
|
121 |
||
122 |
:param level: the title's level (1 for h1, 2 for h2, etc.) |
|
123 |
""" |
|
124 |
if level is None: |
|
125 |
for hlist in self.title_tags: |
|
126 |
if text in hlist: |
|
127 |
return True |
|
128 |
return False |
|
129 |
else: |
|
130 |
hlist = self.title_tags[level - 1] |
|
131 |
return text in hlist |
|
132 |
||
133 |
def has_title_regexp(self, pattern, level=None): |
|
134 |
"""returns True if <h?>pattern</h?>""" |
|
135 |
sre = re.compile(pattern) |
|
136 |
if level is None: |
|
137 |
for hlist in self.title_tags: |
|
138 |
for title in hlist: |
|
139 |
if sre.match(title): |
|
140 |
return True |
|
141 |
return False |
|
142 |
else: |
|
143 |
hlist = self.title_tags[level - 1] |
|
144 |
for title in hlist: |
|
145 |
if sre.match(title): |
|
146 |
return True |
|
147 |
return False |
|
1485 | 148 |
|
0 | 149 |
def has_link(self, text, url=None): |
150 |
"""returns True if <a href=url>text</a> was found in the page""" |
|
151 |
for link_text, attrs in self.a_tags: |
|
152 |
if text == link_text: |
|
153 |
if url is None: |
|
154 |
return True |
|
155 |
try: |
|
156 |
href = attrs['href'] |
|
157 |
if href == url: |
|
158 |
return True |
|
159 |
except KeyError: |
|
160 |
continue |
|
161 |
return False |
|
1485 | 162 |
|
0 | 163 |
def has_link_regexp(self, pattern, url=None): |
164 |
"""returns True if <a href=url>pattern</a> was found in the page""" |
|
165 |
sre = re.compile(pattern) |
|
166 |
for link_text, attrs in self.a_tags: |
|
167 |
if sre.match(link_text): |
|
168 |
if url is None: |
|
169 |
return True |
|
170 |
try: |
|
171 |
href = attrs['href'] |
|
172 |
if href == url: |
|
173 |
return True |
|
174 |
except KeyError: |
|
175 |
continue |
|
176 |
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
|
177 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
178 |
VALMAP = {None: None, 'dtd': DTDValidator, 'xml': SaxOnlyValidator} |