From 2e3607c1e758865519b28066b8925f37203e2197 Mon Sep 17 00:00:00 2001 From: Ezio Melotti Date: Thu, 7 Apr 2011 22:03:31 +0300 Subject: [PATCH] #7311: fix html.parser to accept non-ASCII attribute values. --- Lib/html/parser.py | 2 +- Lib/test/test_htmlparser.py | 17 +++++++++++++++++ Misc/NEWS | 2 ++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Lib/html/parser.py b/Lib/html/parser.py index 21ebbc3eaf0..a3586ebf516 100644 --- a/Lib/html/parser.py +++ b/Lib/html/parser.py @@ -28,7 +28,7 @@ tagfind = re.compile('[a-zA-Z][-.a-zA-Z0-9:_]*') # make it correctly strict without breaking backward compatibility. attrfind = re.compile( r'\s*([a-zA-Z_][-.:a-zA-Z_0-9]*)(\s*=\s*' - r'(\'[^\']*\'|"[^"]*"|[-a-zA-Z0-9./,:;+*%?!&$\(\)_#=~@]*))?') + r'(\'[^\']*\'|"[^"]*"|[^\s"\'=<>`]*))?') attrfind_tolerant = re.compile( r'\s*([a-zA-Z_][-.:a-zA-Z_0-9]*)(\s*=\s*' r'(\'[^\']*\'|"[^"]*"|[^>\s]*))?') diff --git a/Lib/test/test_htmlparser.py b/Lib/test/test_htmlparser.py index 5ecd016084d..637ab01f126 100644 --- a/Lib/test/test_htmlparser.py +++ b/Lib/test/test_htmlparser.py @@ -217,6 +217,23 @@ DOCTYPE html [ ("starttag", "a", [("href", "mailto:xyz@example.com")]), ]) + def test_attr_nonascii(self): + # see issue 7311 + self._run_check("\u4e2d\u6587", [ + ("starttag", "img", [("src", "/foo/bar.png"), + ("alt", "\u4e2d\u6587")]), + ]) + self._run_check("", [ + ("starttag", "a", [("title", "\u30c6\u30b9\u30c8"), + ("href", "\u30c6\u30b9\u30c8.html")]), + ]) + self._run_check('', [ + ("starttag", "a", [("title", "\u30c6\u30b9\u30c8"), + ("href", "\u30c6\u30b9\u30c8.html")]), + ]) + def test_attr_entity_replacement(self): self._run_check("""""", [ ("starttag", "a", [("b", "&><\"'")]), diff --git a/Misc/NEWS b/Misc/NEWS index 2ae7eaf657a..80d46ed117a 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -49,6 +49,8 @@ Core and Builtins Library ------- +- Issue #7311: fix html.parser to accept non-ASCII attribute values. + - Issue #11605: email.parser.BytesFeedParser was incorrectly converting multipart subpararts with an 8bit CTE into unicode instead of preserving the bytes.