author | Pierre-Yves David <pierre-yves.david@logilab.fr> |
Mon, 29 Apr 2013 11:06:28 +0200 | |
changeset 8951 | 5cfc0387d63f |
parent 8941 | 7b26fe71404f |
child 8972 | 771337c3a754 |
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) |
|
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8940
diff
changeset
|
42 |
new_exc = AssertionError(u'invalid document: %s' % exc) |
0 | 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 |
|
8951
5cfc0387d63f
[devtools] Use XMLValidator instead deprecated SaxOnlyValidator
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
93 |
class XMLDemotingValidator(XMLValidator): |
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 |
""" 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
|
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 |
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
|
97 |
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
|
98 |
""" |
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
|
99 |
__metaclass__ = class_deprecated |
8937
8a1809c9a043
[htmlparser] add missing deprecation message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8695
diff
changeset
|
100 |
__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
|
101 |
|
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 |
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
|
103 |
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
|
104 |
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
|
105 |
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
|
106 |
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
|
107 |
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
|
108 |
|
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 |
|
8940
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
110 |
REM_SCRIPT_RGX = re.compile(r"<script[^>]*>.*?</script>", re.U|re.M|re.I|re.S) |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
111 |
|
0 | 112 |
class HTMLValidator(Validator): |
113 |
||
114 |
def __init__(self): |
|
115 |
Validator.__init__(self) |
|
8940
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
116 |
self.parser = etree.HTMLParser(recover=False) |
0 | 117 |
|
8940
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
118 |
def preprocess_data(self, data): |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
119 |
""" Here we essentially wipe the javascript tags to help the HTMLParser |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
120 |
do its job. Without that, it chokes on tags embedded in JS strings. |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
121 |
""" |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
122 |
# Notice we may want to use lxml cleaner, but it's far too intrusive: |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
123 |
# |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
124 |
# cleaner = Cleaner(scripts=True, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
125 |
# javascript=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
126 |
# comments=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
127 |
# style=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
128 |
# links=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
129 |
# meta=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
130 |
# page_structure=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
131 |
# processing_instructions=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
132 |
# embedded=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
133 |
# frames=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
134 |
# forms=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
135 |
# annoying_tags=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
136 |
# remove_tags=(), |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
137 |
# remove_unknown_tags=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
138 |
# safe_attrs_only=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
139 |
# add_nofollow=False) |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
140 |
# >>> cleaner.clean_html('<body></body>') |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
141 |
# '<span></span>' |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
142 |
# >>> cleaner.clean_html('<!DOCTYPE html><body></body>') |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
143 |
# '<html><body></body></html>' |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
144 |
# >>> cleaner.clean_html('<body><div/></body>') |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
145 |
# '<div></div>' |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
146 |
# >>> cleaner.clean_html('<html><body><div/><br></body><html>') |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
147 |
# '<html><body><div></div><br></body></html>' |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
148 |
# >>> cleaner.clean_html('<html><body><div/><br><span></body><html>') |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
149 |
# '<html><body><div></div><br><span></span></body></html>' |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
150 |
# |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
151 |
# using that, we'll miss most actual validation error we want to |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
152 |
# catch. For now, use dumb regexp |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
153 |
return REM_SCRIPT_RGX.sub('', data) |
1485 | 154 |
|
0 | 155 |
|
156 |
class PageInfo(object): |
|
157 |
"""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
|
158 |
def __init__(self, validator, source): |
0 | 159 |
self.source = source |
8939
30ce8eccfe3f
[htmlparser] store unaltered source in pageinfo.source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8938
diff
changeset
|
160 |
root = etree.fromstring(validator.preprocess_data(source), validator.parser) |
0 | 161 |
self.etree = root |
162 |
self.source = source |
|
163 |
self.raw_text = u''.join(root.xpath('//text()')) |
|
164 |
self.namespace = self.etree.nsmap |
|
165 |
self.default_ns = self.namespace.get(None) |
|
166 |
self.a_tags = self.find_tag('a') |
|
167 |
self.h1_tags = self.find_tag('h1') |
|
168 |
self.h2_tags = self.find_tag('h2') |
|
169 |
self.h3_tags = self.find_tag('h3') |
|
170 |
self.h4_tags = self.find_tag('h4') |
|
171 |
self.input_tags = self.find_tag('input') |
|
172 |
self.title_tags = [self.h1_tags, self.h2_tags, self.h3_tags, self.h4_tags] |
|
1485 | 173 |
|
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
|
174 |
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
|
175 |
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
|
176 |
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
|
177 |
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
|
178 |
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
|
179 |
|
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
|
180 |
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
|
181 |
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
|
182 |
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
|
183 |
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
|
184 |
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
|
185 |
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
|
186 |
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
|
187 |
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
|
188 |
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
|
189 |
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
|
190 |
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
|
191 |
|
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
|
192 |
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
|
193 |
"""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
|
194 |
`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
|
195 |
""" |
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
|
196 |
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
|
197 |
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
|
198 |
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
|
199 |
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
|
200 |
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
|
201 |
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
|
202 |
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
|
203 |
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
|
204 |
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
|
205 |
|
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
|
206 |
def find_tag(self, tag, gettext=True): |
0 | 207 |
"""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
|
208 |
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
|
209 |
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
|
210 |
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
|
211 |
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
|
212 |
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
|
213 |
for elt in self.etree.iterfind(iterstr)] |
1485 | 214 |
|
0 | 215 |
def appears(self, text): |
216 |
"""returns True if <text> appears in the page""" |
|
217 |
return text in self.raw_text |
|
218 |
||
219 |
def __contains__(self, text): |
|
220 |
return text in self.source |
|
1485 | 221 |
|
0 | 222 |
def has_title(self, text, level=None): |
223 |
"""returns True if <h?>text</h?> |
|
224 |
||
225 |
:param level: the title's level (1 for h1, 2 for h2, etc.) |
|
226 |
""" |
|
227 |
if level is None: |
|
228 |
for hlist in self.title_tags: |
|
229 |
if text in hlist: |
|
230 |
return True |
|
231 |
return False |
|
232 |
else: |
|
233 |
hlist = self.title_tags[level - 1] |
|
234 |
return text in hlist |
|
235 |
||
236 |
def has_title_regexp(self, pattern, level=None): |
|
237 |
"""returns True if <h?>pattern</h?>""" |
|
238 |
sre = re.compile(pattern) |
|
239 |
if level is None: |
|
240 |
for hlist in self.title_tags: |
|
241 |
for title in hlist: |
|
242 |
if sre.match(title): |
|
243 |
return True |
|
244 |
return False |
|
245 |
else: |
|
246 |
hlist = self.title_tags[level - 1] |
|
247 |
for title in hlist: |
|
248 |
if sre.match(title): |
|
249 |
return True |
|
250 |
return False |
|
1485 | 251 |
|
0 | 252 |
def has_link(self, text, url=None): |
253 |
"""returns True if <a href=url>text</a> was found in the page""" |
|
254 |
for link_text, attrs in self.a_tags: |
|
255 |
if text == link_text: |
|
256 |
if url is None: |
|
257 |
return True |
|
258 |
try: |
|
259 |
href = attrs['href'] |
|
260 |
if href == url: |
|
261 |
return True |
|
262 |
except KeyError: |
|
263 |
continue |
|
264 |
return False |
|
1485 | 265 |
|
0 | 266 |
def has_link_regexp(self, pattern, url=None): |
267 |
"""returns True if <a href=url>pattern</a> was found in the page""" |
|
268 |
sre = re.compile(pattern) |
|
269 |
for link_text, attrs in self.a_tags: |
|
270 |
if sre.match(link_text): |
|
271 |
if url is None: |
|
272 |
return True |
|
273 |
try: |
|
274 |
href = attrs['href'] |
|
275 |
if href == url: |
|
276 |
return True |
|
277 |
except KeyError: |
|
278 |
continue |
|
279 |
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
|
280 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
281 |
VALMAP = {None: None, 'dtd': DTDValidator, 'xml': SaxOnlyValidator} |