Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
b6f11b68
Commit
b6f11b68
authored
Nov 14, 2011
by
Ezio Melotti
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
parent
02500e76
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
87 additions
and
12 deletions
+87
-12
Lib/HTMLParser.py
Lib/HTMLParser.py
+11
-9
Lib/test/test_htmlparser.py
Lib/test/test_htmlparser.py
+73
-3
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/HTMLParser.py
View file @
b6f11b68
...
...
@@ -24,22 +24,23 @@ starttagopen = re.compile('<[a-zA-Z]')
piclose
=
re
.
compile
(
'>'
)
commentclose
=
re
.
compile
(
r'--\
s*>
')
tagfind = re.compile('
[
a
-
zA
-
Z
][
-
.
a
-
zA
-
Z0
-
9
:
_
]
*
')
attrfind = re.compile(
r'
\
s
*
(
[
a
-
zA
-
Z_
][
-
.:
a
-
zA
-
Z_0
-
9
]
*
)(
\
s
*=
\
s
*
'
r'
(
\
'[^
\
'
]*
\
'
|"[^"]*"|
[^
\
s
"
\
'
=<>`
]*))?'
)
r'
\
s
*
(
(
?
<=
[
\
'"
\
s])[^
\
s/>][^
\
s/=>]*)(
\
s*=+
\
s*
'
r'
(
\
'[^
\
'
]*
\
'
|"[^"]*"|
(?![
\
'
"])[^>
\
s
]*))?
'
)
locatestarttagend = re.compile(r"""
<[a-zA-Z][-.a-zA-Z0-9:_]* # tag name
(?:
\
s+ # whi
t
espace before attribute name
(?:
[a-zA-Z_][-.:a-zA-Z0-9_]*
# attribute name
(?:\
s*=
\s*
# value indicator
(?:
(?<=['"
\
s])[^
\
s/>][^
\
s/=>]*
#
a
ttribute name
(?:
\
s*=
+
\
s*
# value indicator
(?:'[^']*' # LITA-enclosed value
|
\"[^\"]*\"
# LIT-enclosed value
|
[^'\">\
s]+
#
bare value
|
"
[
^
"]*"
# LIT-enclosed value
|
(
?!
[
'"])[^>
\
s]*
#
b
are value
)
)?
)
)
*
)?
\
s*
)
*
)
?
\
s* #
t
railing whitespace
""", re.VERBOSE)
endendtag = re.compile('
>
')
...
...
@@ -254,6 +255,7 @@ class HTMLParser(markupbase.ParserBase):
elif
attrvalue
[:
1
]
==
'
\
'
'
==
attrvalue
[
-
1
:]
or
\
attrvalue
[:
1
]
==
'"'
==
attrvalue
[
-
1
:]:
attrvalue
=
attrvalue
[
1
:
-
1
]
if
attrvalue
:
attrvalue
=
self
.
unescape
(
attrvalue
)
attrs
.
append
((
attrname
.
lower
(),
attrvalue
))
k
=
m
.
end
()
...
...
Lib/test/test_htmlparser.py
View file @
b6f11b68
...
...
@@ -226,13 +226,11 @@ DOCTYPE html [
self
.
_parse_error
(
"<a<a>"
)
self
.
_parse_error
(
"</a<a>"
)
self
.
_parse_error
(
"<!"
)
self
.
_parse_error
(
"<a $>"
)
self
.
_parse_error
(
"<a"
)
self
.
_parse_error
(
"<a foo='bar'"
)
self
.
_parse_error
(
"<a foo='bar"
)
self
.
_parse_error
(
"<a foo='>'"
)
self
.
_parse_error
(
"<a foo='>"
)
self
.
_parse_error
(
"<a foo=>"
)
def
test_declaration_junk_chars
(
self
):
self
.
_parse_error
(
"<!DOCTYPE foo $ >"
)
...
...
@@ -352,13 +350,85 @@ class AttributesTestCase(TestCaseBase):
self
.
_run_check
(
"<a a.b='v' c:d=v e-f=v>"
,
[(
"starttag"
,
"a"
,
[(
"a.b"
,
"v"
),
(
"c:d"
,
"v"
),
(
"e-f"
,
"v"
)])])
self
.
_run_check
(
"<a $><b $=%><c
\
=/>
"
,
[("
starttag
", "
a
", [("
$
", None)]),
("
starttag
", "b", [("
$
", "
%
")]),
("
starttag
", "
c
", [("
\\
", "
/
")])])
def test_entityrefs_in_attributes(self):
self._run_check(
"
<
html
foo
=
'€&aa&unsupported;'
>
",
[("
starttag
", "
html
", [("
foo
", u"
\
u20AC
&
aa
&
unsupported
;
")])])
def test_entities_in_attribute_value(self):
# see #1200313
for entity in ['&', '&', '&', '&']:
self._run_check('<a href="
%
s
">' % entity,
[("
starttag
", "
a
", [("
href
", "
&
")])])
self._run_check("
<
a
href
=
'%s'
>
" % entity,
[("
starttag
", "
a
", [("
href
", "
&
")])])
self._run_check("
<
a
href
=%
s
>
" % entity,
[("
starttag
", "
a
", [("
href
", "
&
")])])
def test_malformed_attributes(self):
# see #13357
html = (
"
<
a
href
=
test
'style='
color
:
red
;
bad1
'>test - bad1</a>"
"<a href=test'
+
style
=
'color:red;ba2'
>
test
-
bad2
</
a
>
"
"
<
a
href
=
test
' style='
color
:
red
;
bad3
'>test - bad3</a>"
"<a href = test'
&
nbsp
;
style
=
'color:red;bad4'
>
test
-
bad4
</
a
>
"
)
expected = [
('starttag', 'a', [('href', "
test
'style='
color
:
red
;
bad1
'")]),
('
data
', '
test
-
bad1
'), ('
endtag
', '
a
'),
('
starttag
', '
a
', [('
href
', "test'
+
style
=
'color:red;ba2'")]),
('data', 'test - bad2'), ('endtag', 'a'),
('starttag', 'a', [('href', u"
test
'
\
xa0
style='
color
:
red
;
bad3
'")]),
('
data
', '
test
-
bad3
'), ('
endtag
', '
a
'),
('
starttag
', '
a
', [('
href
', u"test'
\
xa0style
=
'color:red;bad4'")]),
('data', 'test - bad4'), ('endtag', 'a')
]
self._run_check(html, expected)
def test_malformed_adjacent_attributes(self):
# see #12629
self._run_check('<x><y z=""o"" /></x>',
[('starttag', 'x', []),
('startendtag', 'y', [('z', ''), ('o""', None)]),
('endtag', 'x')])
self._run_check('<x><y z=""""
/></
x
>
',
[('
starttag
', '
x
', []),
('
startendtag
', '
y
', [('
z
', ''), ('""', None)]),
('
endtag
', '
x
')])
# see #755670 for the following 3 tests
def test_adjacent_attributes(self):
self._run_check('
<
a
width
=
"100%"
cellspacing
=
0
>
',
[("starttag", "a",
[("width", "100%"), ("cellspacing","0")])])
self._run_check('
<
a
id
=
"foo"
class
=
"bar"
>
',
[("starttag", "a",
[("id", "foo"), ("class","bar")])])
def test_missing_attribute_value(self):
self._run_check('
<
a
v
=>
',
[("starttag", "a", [("v", "")])])
def test_javascript_attribute_value(self):
self._run_check("<a href=javascript:popup('
/
popup
/
help
.
html
')>",
[("starttag", "a",
[("href", "javascript:popup('
/
popup
/
help
.
html
')")])])
def test_end_tag_in_attribute_value(self):
# see #1745761
self._run_check("<a href='
http
:
//
www
.
example
.
org
/
\
">;'>spam</a>"
,
[(
"starttag"
,
"a"
,
[(
"href"
,
"http://www.example.org/
\
"
>;"
)]),
(
"data"
,
"spam"
),
(
"endtag"
,
"a"
)])
def
test_main
():
test_support
.
run_unittest
(
HTMLParserTestCase
,
AttributesTestCase
)
...
...
Misc/NEWS
View file @
b6f11b68
...
...
@@ -76,6 +76,9 @@ Core and Builtins
Library
-------
-
Issues
#
1745761
,
#
755670
,
#
13357
,
#
12629
,
#
1200313
:
HTMLParser
now
correctly
handles
non
-
valid
attributes
,
including
adjacent
and
unquoted
attributes
.
-
Issue
#
13193
:
Fix
distutils
.
filelist
.
FileList
under
Windows
.
The
"recursive-include"
directive
now
recognizes
both
legal
path
separators
.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment