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
ab8a6bba
Commit
ab8a6bba
authored
Mar 06, 2007
by
Martin v. Löwis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Patch #912410: Replace HTML entity references for attribute values
in HTMLParser.
parent
ff432e6f
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
43 additions
and
13 deletions
+43
-13
Doc/lib/libhtmlparser.tex
Doc/lib/libhtmlparser.tex
+11
-7
Lib/HTMLParser.py
Lib/HTMLParser.py
+24
-6
Lib/test/test_htmlparser.py
Lib/test/test_htmlparser.py
+5
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Doc/lib/libhtmlparser.tex
View file @
ab8a6bba
...
@@ -75,14 +75,18 @@ This method is called to handle the start of a tag. It is intended to
...
@@ -75,14 +75,18 @@ This method is called to handle the start of a tag. It is intended to
be overridden by a derived class; the base class implementation does
be overridden by a derived class; the base class implementation does
nothing.
nothing.
The
\var
{
tag
}
argument is the name of the tag converted to
The
\var
{
tag
}
argument is the name of the tag converted to
lower case.
lower case. The
\var
{
attrs
}
argument is a list of
\code
{
(
\var
{
name
}
,
The
\var
{
attrs
}
argument is a list of
\code
{
(
\var
{
name
}
,
\var
{
value
}
)
}
\var
{
value
}
)
}
pairs containing the attributes found inside the tag's
pairs containing the attributes found inside the tag's
\code
{
<>
}
\code
{
<>
}
brackets. The
\var
{
name
}
will be translated to lower case
brackets. The
\var
{
name
}
will be translated to lower case, and quotes
and double quotes and backslashes in the
\var
{
value
}
have been
in the
\var
{
value
}
have been removed, and character and entity
interpret
ed. For instance, for the tag
\code
{
<A
references have been replac
ed. For instance, for the tag
\code
{
<A
HREF="http://www.cwi.nl/">
}
, this method would be called as
HREF="http://www.cwi.nl/">
}
, this method would be called as
\samp
{
handle
_
starttag('a', [('href', 'http://www.cwi.nl/')])
}
.
\samp
{
handle
_
starttag('a', [('href', 'http://www.cwi.nl/')])
}
.
\versionchanged
[All entity references from htmlentitydefs are now
replaced in the attribute values]
{
2.6
}
\end{methoddesc}
\end{methoddesc}
\begin{methoddesc}
{
handle
_
startendtag
}{
tag, attrs
}
\begin{methoddesc}
{
handle
_
startendtag
}{
tag, attrs
}
...
...
Lib/HTMLParser.py
View file @
ab8a6bba
...
@@ -358,12 +358,30 @@ class HTMLParser(markupbase.ParserBase):
...
@@ -358,12 +358,30 @@ class HTMLParser(markupbase.ParserBase):
self
.
error
(
"unknown declaration: %r"
%
(
data
,))
self
.
error
(
"unknown declaration: %r"
%
(
data
,))
# Internal -- helper to remove special character quoting
# Internal -- helper to remove special character quoting
entitydefs
=
None
def
unescape
(
self
,
s
):
def
unescape
(
self
,
s
):
if
'&'
not
in
s
:
if
'&'
not
in
s
:
return
s
return
s
s
=
s
.
replace
(
"<"
,
"<"
)
def
replaceEntities
(
s
):
s
=
s
.
replace
(
">"
,
">"
)
s
=
s
.
groups
()[
0
]
s
=
s
.
replace
(
"'"
,
"'"
)
if
s
[
0
]
==
"#"
:
s
=
s
.
replace
(
"""
,
'"'
)
s
=
s
[
1
:]
s
=
s
.
replace
(
"&"
,
"&"
)
# Must be last
if
s
[
0
]
in
[
'x'
,
'X'
]:
return
s
c
=
int
(
s
[
1
:],
16
)
else
:
c
=
int
(
s
)
return
unichr
(
c
)
else
:
# Cannot use name2codepoint directly, because HTMLParser supports apos,
# which is not part of HTML 4
import
htmlentitydefs
if
HTMLParser
.
entitydefs
is
None
:
entitydefs
=
HTMLParser
.
entitydefs
=
{
'apos'
:
u"'"
}
for
k
,
v
in
htmlentitydefs
.
name2codepoint
.
iteritems
():
entitydefs
[
k
]
=
unichr
(
v
)
try
:
return
self
.
entitydefs
[
s
]
except
KeyError
:
return
'&'
+
s
+
';'
return
re
.
sub
(
r"&(#?[xX]?(?:[0-9a-fA-F]+|\
w{
1,8}));"
,
replaceEntities
,
s
)
Lib/test/test_htmlparser.py
View file @
ab8a6bba
...
@@ -309,6 +309,11 @@ DOCTYPE html [
...
@@ -309,6 +309,11 @@ DOCTYPE html [
(
"endtag"
,
"script"
),
(
"endtag"
,
"script"
),
])
])
def
test_entityrefs_in_attributes
(
self
):
self
.
_run_check
(
"<html foo='€&aa&unsupported;'>"
,
[
(
"starttag"
,
"html"
,
[(
"foo"
,
u"
\
u20AC
&aa&unsupported;"
)])
])
def
test_main
():
def
test_main
():
test_support
.
run_unittest
(
HTMLParserTestCase
)
test_support
.
run_unittest
(
HTMLParserTestCase
)
...
...
Misc/NEWS
View file @
ab8a6bba
...
@@ -141,6 +141,9 @@ Core and builtins
...
@@ -141,6 +141,9 @@ Core and builtins
Library
Library
-------
-------
- Patch #912410: Replace HTML entity references for attribute values
in HTMLParser.
- Patch #1663234: you can now run doctest on test files and modules
- Patch #1663234: you can now run doctest on test files and modules
using "python -m doctest [-v] filename ...".
using "python -m doctest [-v] filename ...".
...
...
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