]> granicus.if.org Git - python/commitdiff
#14538: HTMLParser can now parse correctly start tags that contain a bare /.
authorEzio Melotti <ezio.melotti@gmail.com>
Thu, 19 Apr 2012 01:18:22 +0000 (19:18 -0600)
committerEzio Melotti <ezio.melotti@gmail.com>
Thu, 19 Apr 2012 01:18:22 +0000 (19:18 -0600)
Lib/html/parser.py
Lib/test/test_htmlparser.py
Misc/NEWS

index 2bfd187a892ea85528b0f6e3cbb41d9fb593157c..de504ab54409314352461fa39517e7ab411a0be6 100644 (file)
@@ -22,7 +22,7 @@ charref = re.compile('&#(?:[0-9]+|[xX][0-9a-fA-F]+)[^0-9a-fA-F]')
 starttagopen = re.compile('<[a-zA-Z]')
 piclose = re.compile('>')
 commentclose = re.compile(r'--\s*>')
-tagfind = re.compile('[a-zA-Z][-.a-zA-Z0-9:_]*')
+tagfind = re.compile('([a-zA-Z][-.a-zA-Z0-9:_]*)(?:\s|/(?!>))*')
 # see http://www.w3.org/TR/html5/tokenization.html#tag-open-state
 # and http://www.w3.org/TR/html5/tokenization.html#tag-name-state
 tagfind_tolerant = re.compile('[a-zA-Z][^\t\n\r\f />\x00]*')
@@ -36,7 +36,7 @@ attrfind = re.compile(
     r'\s*([a-zA-Z_][-.:a-zA-Z_0-9]*)(\s*=\s*'
     r'(\'[^\']*\'|"[^"]*"|[^\s"\'=<>`]*))?')
 attrfind_tolerant = re.compile(
-    r'[\s/]*((?<=[\'"\s/])[^\s/>][^\s/=>]*)(\s*=+\s*'
+    r'((?<=[\'"\s/])[^\s/>][^\s/=>]*)(\s*=+\s*'
     r'(\'[^\']*\'|"[^"]*"|(?![\'"])[^>\s]*))?(?:\s|/(?!>))*')
 locatestarttagend = re.compile(r"""
   <[a-zA-Z][-.a-zA-Z0-9:_]*          # tag name
@@ -327,7 +327,7 @@ class HTMLParser(_markupbase.ParserBase):
         match = tagfind.match(rawdata, i+1)
         assert match, 'unexpected call to parse_starttag()'
         k = match.end()
-        self.lasttag = tag = rawdata[i+1:k].lower()
+        self.lasttag = tag = match.group(1).lower()
         while k < endpos:
             if self.strict:
                 m = attrfind.match(rawdata, k)
index 3e2a59064e5bbf3e82b139048b895e1bfaa1a570..c4f80cca30e25d363448386226dccd838f83ef01 100644 (file)
@@ -409,6 +409,16 @@ class HTMLParserTolerantTestCase(HTMLParserStrictTestCase):
             ('starttag', 'a', [('foo', None), ('=', None), ('bar', None)])
         ]
         self._run_check(html, expected)
+        #see issue #14538
+        html = ('<meta><meta / ><meta // ><meta / / >'
+                '<meta/><meta /><meta //><meta//>')
+        expected = [
+            ('starttag', 'meta', []), ('starttag', 'meta', []),
+            ('starttag', 'meta', []), ('starttag', 'meta', []),
+            ('startendtag', 'meta', []), ('startendtag', 'meta', []),
+            ('startendtag', 'meta', []), ('startendtag', 'meta', []),
+        ]
+        self._run_check(html, expected)
 
     def test_declaration_junk_chars(self):
         self._run_check("<!DOCTYPE foo $ >", [('decl', 'DOCTYPE foo $ ')])
index bfae292e2ac80ea3095e4066ae7def3771cb5a6e..44cb4a10faa44494f2507161b2844b7e028c6267 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -47,6 +47,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #14538: HTMLParser can now parse correctly start tags that contain
+  a bare '/'.
+
 - Issue #14452: SysLogHandler no longer inserts a UTF-8 BOM into the message.
 
 - Issue #13496: Fix potential overflow in bisect.bisect algorithm when applied