3636# explode, so don't do it.
3737# see http://www.w3.org/TR/html5/tokenization.html#tag-open-state
3838# and http://www.w3.org/TR/html5/tokenization.html#tag-name-state
39- tagfind_tolerant = re .compile (r'([a-zA-Z][^\t\n\r\f />\x00]*)(?:\s|/(?!>))*' )
40- attrfind_tolerant = re .compile (
41- r'((?<=[\'"\s/])[^\s/>][^\s/=>]*)(\s*=+\s*'
42- r'(\'[^\']*\'|"[^"]*"|(?![\'"])[^>\s]*))?(?:\s|/(?!>))*' )
43- locatestarttagend_tolerant = re .compile (r"""
44- <[a-zA-Z][^\t\n\r\f />\x00]* # tag name
45- (?:[\s/]* # optional whitespace before attribute name
46- (?:(?<=['"\s/])[^\s/>][^\s/=>]* # attribute name
47- (?:\s*=+\s* # value indicator
48- (?:'[^']*' # LITA-enclosed value
49- |"[^"]*" # LIT-enclosed value
50- |(?!['"])[^>\s]* # bare value
51- )
52- \s* # possibly followed by a space
53- )?(?:\s|/(?!>))*
54- )*
39+ tagfind_tolerant = re .compile (r'([a-zA-Z][^\t\n\r\f />]*)(?:[\t\n\r\f ]|/(?!>))*' )
40+ attrfind_tolerant = re .compile (r"""
41+ (
42+ (?<=['"\t\n\r\f /])[^\t\n\r\f />][^\t\n\r\f /=>]* # attribute name
43+ )
44+ (= # value indicator
45+ ('[^']*' # LITA-enclosed value
46+ |"[^"]*" # LIT-enclosed value
47+ |(?!['"])[^>\t\n\r\f ]* # bare value
48+ )
5549 )?
56- \s* # trailing whitespace
50+ (?:[\t\n\r\f ]|/(?!>))* # possibly followed by a space
51+ """ , re .VERBOSE )
52+ locatetagend_tolerant = re .compile (r"""
53+ [a-zA-Z][^\t\n\r\f />]* # tag name
54+ [\t\n\r\f /]* # optional whitespace before attribute name
55+ (?:(?<=['"\t\n\r\f /])[^\t\n\r\f />][^\t\n\r\f /=>]* # attribute name
56+ (?:= # value indicator
57+ (?:'[^']*' # LITA-enclosed value
58+ |"[^"]*" # LIT-enclosed value
59+ |(?!['"])[^>\t\n\r\f ]* # bare value
60+ )
61+ )?
62+ [\t\n\r\f /]* # possibly followed by a space
63+ )*
64+ >?
5765""" , re .VERBOSE )
58- endendtag = re .compile ('>' )
59- # the HTML 5 spec, section 8.1.2.2, doesn't allow spaces between
60- # </ and the tag name, so maybe this should be fixed
61- endtagfind = re .compile (r'</\s*([a-zA-Z][-.a-zA-Z0-9:_]*)\s*>' )
6266
6367# Character reference processing logic specific to attribute values
6468# See: https://html.spec.whatwg.org/multipage/parsing.html#named-character-reference-state
@@ -141,7 +145,8 @@ def get_starttag_text(self):
141145
142146 def set_cdata_mode (self , elem ):
143147 self .cdata_elem = elem .lower ()
144- self .interesting = re .compile (r'</\s*%s\s*>' % self .cdata_elem , re .I )
148+ self .interesting = re .compile (r'</%s(?=[\t\n\r\f />])' % self .cdata_elem ,
149+ re .IGNORECASE | re .ASCII )
145150
146151 def clear_cdata_mode (self ):
147152 self .interesting = interesting_normal
@@ -166,7 +171,7 @@ def goahead(self, end):
166171 # & near the end and see if it's followed by a space or ;.
167172 amppos = rawdata .rfind ('&' , max (i , n - 34 ))
168173 if (amppos >= 0 and
169- not re .compile (r'[\s ;]' ).search (rawdata , amppos )):
174+ not re .compile (r'[\t\n\r\f ;]' ).search (rawdata , amppos )):
170175 break # wait till we get all the text
171176 j = n
172177 else :
@@ -381,76 +386,39 @@ def parse_starttag(self, i):
381386 # or -1 if incomplete.
382387 def check_for_whole_start_tag (self , i ):
383388 rawdata = self .rawdata
384- m = locatestarttagend_tolerant .match (rawdata , i )
385- if m :
386- j = m .end ()
387- next = rawdata [j :j + 1 ]
388- if next == ">" :
389- return j + 1
390- if next == "/" :
391- if rawdata .startswith ("/>" , j ):
392- return j + 2
393- if rawdata .startswith ("/" , j ):
394- # buffer boundary
395- return - 1
396- # else bogus input
397- if j > i :
398- return j
399- else :
400- return i + 1
401- if next == "" :
402- # end of input
403- return - 1
404- if next in ("abcdefghijklmnopqrstuvwxyz=/"
405- "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ):
406- # end of input in or before attribute value, or we have the
407- # '/' from a '/>' ending
408- return - 1
409- if j > i :
410- return j
411- else :
412- return i + 1
413- raise AssertionError ("we should not get here!" )
389+ match = locatetagend_tolerant .match (rawdata , i + 1 )
390+ assert match
391+ j = match .end ()
392+ if rawdata [j - 1 ] != ">" :
393+ return - 1
394+ return j
414395
415396 # Internal -- parse endtag, return end or -1 if incomplete
416397 def parse_endtag (self , i ):
417398 rawdata = self .rawdata
418399 assert rawdata [i :i + 2 ] == "</" , "unexpected call to parse_endtag"
419- match = endendtag .search (rawdata , i + 1 ) # >
420- if not match :
400+ if rawdata .find ('>' , i + 2 ) < 0 :
421401 return - 1
422- gtpos = match .end ()
423- match = endtagfind .match (rawdata , i ) # </ + tag + >
424- if not match :
425- if self .cdata_elem is not None :
426- self .handle_data (rawdata [i :gtpos ])
427- return gtpos
428- # find the name: w3.org/TR/html5/tokenization.html#tag-name-state
429- namematch = tagfind_tolerant .match (rawdata , i + 2 )
430- if not namematch :
431- # w3.org/TR/html5/tokenization.html#end-tag-open-state
432- if rawdata [i :i + 3 ] == '</>' :
433- return i + 3
434- else :
435- return self .parse_bogus_comment (i )
436- tagname = namematch .group (1 ).lower ()
437- # consume and ignore other stuff between the name and the >
438- # Note: this is not 100% correct, since we might have things like
439- # </tag attr=">">, but looking for > after the name should cover
440- # most of the cases and is much simpler
441- gtpos = rawdata .find ('>' , namematch .end ())
442- self .handle_endtag (tagname )
443- return gtpos + 1
402+ if not endtagopen .match (rawdata , i ): # </ + letter
403+ # w3.org/TR/html5/tokenization.html#end-tag-open-state
404+ if rawdata [i + 2 :i + 3 ] == '>' :
405+ return i + 3
406+ else :
407+ return self .parse_bogus_comment (i )
444408
445- elem = match . group ( 1 ). lower () # script or style
446- if self . cdata_elem is not None :
447- if elem != self . cdata_elem :
448- self . handle_data ( rawdata [i : gtpos ])
449- return gtpos
409+ match = locatetagend_tolerant . match ( rawdata , i + 2 )
410+ assert match
411+ j = match . end ()
412+ if rawdata [j - 1 ] != ">" :
413+ return - 1
450414
451- self .handle_endtag (elem )
415+ # find the name: w3.org/TR/html5/tokenization.html#tag-name-state
416+ match = tagfind_tolerant .match (rawdata , i + 2 )
417+ assert match
418+ tag = match .group (1 ).lower ()
419+ self .handle_endtag (tag )
452420 self .clear_cdata_mode ()
453- return gtpos
421+ return j
454422
455423 # Overridable -- finish processing of start+end tag: <tag.../>
456424 def handle_startendtag (self , tag , attrs ):
0 commit comments