]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#13273: fix a bug that prevented HTMLParser to properly detect some tags when strict...
authorEzio Melotti <ezio.melotti@gmail.com>
Fri, 28 Oct 2011 10:21:09 +0000 (13:21 +0300)
committerEzio Melotti <ezio.melotti@gmail.com>
Fri, 28 Oct 2011 10:21:09 +0000 (13:21 +0300)
Lib/html/parser.py
Lib/test/test_htmlparser.py
Misc/NEWS

index a6d5be94fa335866831ffb408696b8855d197082..affaf7344fa4f90d69b065efd936345bc63144f3 100644 (file)
@@ -30,7 +30,7 @@ attrfind = re.compile(
     r'\s*([a-zA-Z_][-.:a-zA-Z_0-9]*)(\s*=\s*'
     r'(\'[^\']*\'|"[^"]*"|[^\s"\'=<>`]*))?')
 attrfind_tolerant = re.compile(
-    r'\s*([a-zA-Z_][-.:a-zA-Z_0-9]*)(\s*=\s*'
+    r',?\s*([a-zA-Z_][-.:a-zA-Z_0-9]*)(\s*=\s*'
     r'(\'[^\']*\'|"[^"]*"|[^>\s]*))?')
 locatestarttagend = re.compile(r"""
   <[a-zA-Z][-.a-zA-Z0-9:_]*          # tag name
@@ -277,12 +277,11 @@ class HTMLParser(_markupbase.ParserBase):
         assert match, 'unexpected call to parse_starttag()'
         k = match.end()
         self.lasttag = tag = rawdata[i+1:k].lower()
-
         while k < endpos:
             if self.strict:
                 m = attrfind.match(rawdata, k)
             else:
-                m = attrfind_tolerant.search(rawdata, k)
+                m = attrfind_tolerant.match(rawdata, k)
             if not m:
                 break
             attrname, rest, attrvalue = m.group(1, 2, 3)
index d45e45327fe82fe2421ed9834b23e2c42fa82742..966448551313b519ba134f37741810bb50d90717 100644 (file)
@@ -373,6 +373,39 @@ class HTMLParserTolerantTestCase(TestCaseBase):
                                 [('action', 'bogus|&#()value')])],
                         collector = self.collector)
 
+    def test_issue13273(self):
+        html = ('<div style=""    ><b>The <a href="some_url">rain</a> '
+                '<br /> in <span>Spain</span></b></div>')
+        expected = [
+            ('starttag', 'div', [('style', '')]),
+            ('starttag', 'b', []),
+            ('data', 'The '),
+            ('starttag', 'a', [('href', 'some_url')]),
+            ('data', 'rain'),
+            ('endtag', 'a'),
+            ('data', ' '),
+            ('startendtag', 'br', []),
+            ('data', ' in '),
+            ('starttag', 'span', []),
+            ('data', 'Spain'),
+            ('endtag', 'span'),
+            ('endtag', 'b'),
+            ('endtag', 'div')
+        ]
+        self._run_check(html, expected, collector=self.collector)
+
+    def test_issue13273_2(self):
+        html = '<div style="", foo = "bar" ><b>The <a href="some_url">rain</a>'
+        expected = [
+            ('starttag', 'div', [('style', ''), ('foo', 'bar')]),
+            ('starttag', 'b', []),
+            ('data', 'The '),
+            ('starttag', 'a', [('href', 'some_url')]),
+            ('data', 'rain'),
+            ('endtag', 'a'),
+        ]
+        self._run_check(html, expected, collector=self.collector)
+
     def test_unescape_function(self):
         p = html.parser.HTMLParser()
         self.assertEqual(p.unescape('&#bad;'),'&#bad;')
index cd8c9bfc5516cef272ec2bc32d2bd424216faeb5..6be52fbff95263f0d7646dd951a77045cc7927dd 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -61,6 +61,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #13273: fix a bug that prevented HTMLParser to properly detect some
+  tags when strict=False.
+
 - Issue #10332: multiprocessing: fix a race condition when a Pool is closed
   before all tasks have completed.