author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 06 May 2013 17:13:43 +0200 | |
changeset 8973 | 6711f78c18be |
parent 8972 | 771337c3a754 |
child 8977 | 57e564c0118e |
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): |
|
8973
6711f78c18be
[testlib] unspaghettify Validator / PageInfo api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8972
diff
changeset
|
35 |
parser = None |
1485 | 36 |
|
8973
6711f78c18be
[testlib] unspaghettify Validator / PageInfo api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8972
diff
changeset
|
37 |
def parse_string(self, source): |
6711f78c18be
[testlib] unspaghettify Validator / PageInfo api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8972
diff
changeset
|
38 |
etree = self._parse(self.preprocess_data(source)) |
6711f78c18be
[testlib] unspaghettify Validator / PageInfo api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8972
diff
changeset
|
39 |
return PageInfo(source, etree) |
6711f78c18be
[testlib] unspaghettify Validator / PageInfo api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8972
diff
changeset
|
40 |
|
6711f78c18be
[testlib] unspaghettify Validator / PageInfo api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8972
diff
changeset
|
41 |
def preprocess_data(self, data): |
6711f78c18be
[testlib] unspaghettify Validator / PageInfo api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8972
diff
changeset
|
42 |
return data |
6711f78c18be
[testlib] unspaghettify Validator / PageInfo api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8972
diff
changeset
|
43 |
|
6711f78c18be
[testlib] unspaghettify Validator / PageInfo api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8972
diff
changeset
|
44 |
def _parse(self, pdata): |
0 | 45 |
try: |
8973
6711f78c18be
[testlib] unspaghettify Validator / PageInfo api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8972
diff
changeset
|
46 |
return etree.fromstring(pdata, self.parser) |
8695
358d8bed9626
[toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7014
diff
changeset
|
47 |
except etree.XMLSyntaxError as exc: |
0 | 48 |
def save_in(fname=''): |
49 |
file(fname, 'w').write(data) |
|
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8940
diff
changeset
|
50 |
new_exc = AssertionError(u'invalid document: %s' % exc) |
0 | 51 |
new_exc.position = exc.position |
52 |
raise new_exc |
|
53 |
||
54 |
||
55 |
class DTDValidator(Validator): |
|
56 |
def __init__(self): |
|
57 |
Validator.__init__(self) |
|
3151 | 58 |
# XXX understand what's happening under windows |
6771
da71f1ad1721
minor code cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
59 |
self.parser = etree.XMLParser(dtd_validation=sys.platform != 'win32') |
0 | 60 |
|
61 |
def preprocess_data(self, data): |
|
62 |
"""used to fix potential blockquote mess generated by docutils""" |
|
1485 | 63 |
if STRICT_DOCTYPE not in data: |
0 | 64 |
return data |
65 |
# parse using transitional DTD |
|
1485 | 66 |
data = data.replace(STRICT_DOCTYPE, TRANSITIONAL_DOCTYPE) |
0 | 67 |
tree = etree.fromstring(data, self.parser) |
68 |
namespace = tree.nsmap.get(None) |
|
69 |
# this is the list of authorized child tags for <blockquote> nodes |
|
70 |
expected = 'p h1 h2 h3 h4 h5 h6 div ul ol dl pre hr blockquote address ' \ |
|
71 |
'fieldset table form noscript ins del script'.split() |
|
72 |
if namespace: |
|
73 |
blockquotes = tree.findall('.//{%s}blockquote' % namespace) |
|
74 |
expected = ['{%s}%s' % (namespace, tag) for tag in expected] |
|
75 |
else: |
|
76 |
blockquotes = tree.findall('.//blockquote') |
|
77 |
# quick and dirty approach: remove all blockquotes |
|
78 |
for blockquote in blockquotes: |
|
79 |
parent = blockquote.getparent() |
|
80 |
parent.remove(blockquote) |
|
81 |
data = etree.tostring(tree) |
|
1485 | 82 |
return '<?xml version="1.0" encoding="UTF-8"?>%s\n%s' % ( |
83 |
STRICT_DOCTYPE, data) |
|
0 | 84 |
|
1485 | 85 |
|
8938
198fdadafed6
[htmlparser] rename SaxOnlyValidator to XMLValidator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8937
diff
changeset
|
86 |
class XMLValidator(Validator): |
198fdadafed6
[htmlparser] rename SaxOnlyValidator to XMLValidator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8937
diff
changeset
|
87 |
""" A fully compliant XML parser """ |
0 | 88 |
|
89 |
def __init__(self): |
|
90 |
Validator.__init__(self) |
|
91 |
self.parser = etree.XMLParser() |
|
92 |
||
8938
198fdadafed6
[htmlparser] rename SaxOnlyValidator to XMLValidator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8937
diff
changeset
|
93 |
SaxOnlyValidator = class_renamed('SaxOnlyValidator', |
198fdadafed6
[htmlparser] rename SaxOnlyValidator to XMLValidator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8937
diff
changeset
|
94 |
XMLValidator, |
198fdadafed6
[htmlparser] rename SaxOnlyValidator to XMLValidator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8937
diff
changeset
|
95 |
'[3.17] you should use the ' |
198fdadafed6
[htmlparser] rename SaxOnlyValidator to XMLValidator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8937
diff
changeset
|
96 |
'XMLValidator class instead') |
198fdadafed6
[htmlparser] rename SaxOnlyValidator to XMLValidator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8937
diff
changeset
|
97 |
|
8951
5cfc0387d63f
[devtools] Use XMLValidator instead deprecated SaxOnlyValidator
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
98 |
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
|
99 |
""" 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
|
100 |
|
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 |
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
|
102 |
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
|
103 |
""" |
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
|
104 |
__metaclass__ = class_deprecated |
8937
8a1809c9a043
[htmlparser] add missing deprecation message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8695
diff
changeset
|
105 |
__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
|
106 |
|
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 |
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
|
108 |
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
|
109 |
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
|
110 |
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
|
111 |
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
|
112 |
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
|
113 |
|
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
|
114 |
|
8940
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
115 |
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
|
116 |
|
0 | 117 |
class HTMLValidator(Validator): |
118 |
||
119 |
def __init__(self): |
|
120 |
Validator.__init__(self) |
|
8940
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
121 |
self.parser = etree.HTMLParser(recover=False) |
0 | 122 |
|
8940
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
123 |
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
|
124 |
""" 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
|
125 |
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
|
126 |
""" |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
127 |
# 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
|
128 |
# |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
129 |
# cleaner = Cleaner(scripts=True, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
130 |
# javascript=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
131 |
# comments=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
132 |
# style=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
133 |
# links=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
134 |
# meta=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
135 |
# page_structure=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
136 |
# processing_instructions=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
137 |
# embedded=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
138 |
# frames=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
139 |
# forms=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
140 |
# annoying_tags=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
141 |
# remove_tags=(), |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
142 |
# remove_unknown_tags=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
143 |
# safe_attrs_only=False, |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
144 |
# add_nofollow=False) |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
145 |
# >>> 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
|
146 |
# '<span></span>' |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
147 |
# >>> 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
|
148 |
# '<html><body></body></html>' |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
149 |
# >>> 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
|
150 |
# '<div></div>' |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
151 |
# >>> 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
|
152 |
# '<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
|
153 |
# >>> 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
|
154 |
# '<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
|
155 |
# |
ae898a084da2
[htmlparser] exclude <script> tag from html source
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8939
diff
changeset
|
156 |
# 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
|
157 |
# 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
|
158 |
return REM_SCRIPT_RGX.sub('', data) |
1485 | 159 |
|
0 | 160 |
|
161 |
class PageInfo(object): |
|
162 |
"""holds various informations on the view's output""" |
|
8973
6711f78c18be
[testlib] unspaghettify Validator / PageInfo api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8972
diff
changeset
|
163 |
def __init__(self, source, root): |
0 | 164 |
self.source = source |
165 |
self.etree = root |
|
166 |
self.raw_text = u''.join(root.xpath('//text()')) |
|
167 |
self.namespace = self.etree.nsmap |
|
168 |
self.default_ns = self.namespace.get(None) |
|
169 |
self.a_tags = self.find_tag('a') |
|
170 |
self.h1_tags = self.find_tag('h1') |
|
171 |
self.h2_tags = self.find_tag('h2') |
|
172 |
self.h3_tags = self.find_tag('h3') |
|
173 |
self.h4_tags = self.find_tag('h4') |
|
174 |
self.input_tags = self.find_tag('input') |
|
175 |
self.title_tags = [self.h1_tags, self.h2_tags, self.h3_tags, self.h4_tags] |
|
1485 | 176 |
|
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
|
177 |
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
|
178 |
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
|
179 |
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
|
180 |
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
|
181 |
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
|
182 |
|
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
|
183 |
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
|
184 |
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
|
185 |
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
|
186 |
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
|
187 |
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
|
188 |
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
|
189 |
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
|
190 |
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
|
191 |
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
|
192 |
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
|
193 |
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
|
194 |
|
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 |
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
|
196 |
"""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
|
197 |
`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
|
198 |
""" |
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 |
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
|
200 |
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
|
201 |
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
|
202 |
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
|
203 |
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
|
204 |
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
|
205 |
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
|
206 |
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
|
207 |
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
|
208 |
|
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 |
def find_tag(self, tag, gettext=True): |
0 | 210 |
"""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
|
211 |
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
|
212 |
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
|
213 |
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
|
214 |
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
|
215 |
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
|
216 |
for elt in self.etree.iterfind(iterstr)] |
1485 | 217 |
|
0 | 218 |
def appears(self, text): |
219 |
"""returns True if <text> appears in the page""" |
|
220 |
return text in self.raw_text |
|
221 |
||
222 |
def __contains__(self, text): |
|
223 |
return text in self.source |
|
1485 | 224 |
|
0 | 225 |
def has_title(self, text, level=None): |
226 |
"""returns True if <h?>text</h?> |
|
227 |
||
228 |
:param level: the title's level (1 for h1, 2 for h2, etc.) |
|
229 |
""" |
|
230 |
if level is None: |
|
231 |
for hlist in self.title_tags: |
|
232 |
if text in hlist: |
|
233 |
return True |
|
234 |
return False |
|
235 |
else: |
|
236 |
hlist = self.title_tags[level - 1] |
|
237 |
return text in hlist |
|
238 |
||
239 |
def has_title_regexp(self, pattern, level=None): |
|
240 |
"""returns True if <h?>pattern</h?>""" |
|
241 |
sre = re.compile(pattern) |
|
242 |
if level is None: |
|
243 |
for hlist in self.title_tags: |
|
244 |
for title in hlist: |
|
245 |
if sre.match(title): |
|
246 |
return True |
|
247 |
return False |
|
248 |
else: |
|
249 |
hlist = self.title_tags[level - 1] |
|
250 |
for title in hlist: |
|
251 |
if sre.match(title): |
|
252 |
return True |
|
253 |
return False |
|
1485 | 254 |
|
0 | 255 |
def has_link(self, text, url=None): |
256 |
"""returns True if <a href=url>text</a> was found in the page""" |
|
257 |
for link_text, attrs in self.a_tags: |
|
258 |
if text == link_text: |
|
259 |
if url is None: |
|
260 |
return True |
|
261 |
try: |
|
262 |
href = attrs['href'] |
|
263 |
if href == url: |
|
264 |
return True |
|
265 |
except KeyError: |
|
266 |
continue |
|
267 |
return False |
|
1485 | 268 |
|
0 | 269 |
def has_link_regexp(self, pattern, url=None): |
270 |
"""returns True if <a href=url>pattern</a> was found in the page""" |
|
271 |
sre = re.compile(pattern) |
|
272 |
for link_text, attrs in self.a_tags: |
|
273 |
if sre.match(link_text): |
|
274 |
if url is None: |
|
275 |
return True |
|
276 |
try: |
|
277 |
href = attrs['href'] |
|
278 |
if href == url: |
|
279 |
return True |
|
280 |
except KeyError: |
|
281 |
continue |
|
282 |
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
|
283 |
|
8972
771337c3a754
[testlib] update htmlparsers.VALMAP: stop using SaxOnlyValidator and add an entry for html
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8951
diff
changeset
|
284 |
VALMAP = {None: None, |
771337c3a754
[testlib] update htmlparsers.VALMAP: stop using SaxOnlyValidator and add an entry for html
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8951
diff
changeset
|
285 |
'dtd': DTDValidator, |
771337c3a754
[testlib] update htmlparsers.VALMAP: stop using SaxOnlyValidator and add an entry for html
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8951
diff
changeset
|
286 |
'xml': XMLValidator, |
771337c3a754
[testlib] update htmlparsers.VALMAP: stop using SaxOnlyValidator and add an entry for html
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8951
diff
changeset
|
287 |
'html': HTMLValidator, |
771337c3a754
[testlib] update htmlparsers.VALMAP: stop using SaxOnlyValidator and add an entry for html
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8951
diff
changeset
|
288 |
} |