o ,]:M@sdZddlmZmZmZmZddlTddlmZddl Z ddl Z e dZ e dZ e dZe d Ze d Ze d Ze d Ze d Ze dZe dZe dZe de jZe de jZe d Ze dZGdddeZGdddejZdS)zLA parser for HTML and XHTML. Backported for python-future from Python 3.3. )absolute_importdivisionprint_functionunicode_literals)*) _markupbaseNz[&<]z &[a-zA-Z#]z%&([a-zA-Z][-.a-zA-Z0-9]*)[^a-zA-Z0-9]z)&#(?:[0-9]+|[xX][0-9a-fA-F]+)[^0-9a-fA-F]z <[a-zA-Z]>z--\s*>z(([a-zA-Z][-.a-zA-Z0-9:_]*)(?:\s|/(?!>))*z[a-zA-Z][^ />]*zJ\s*([a-zA-Z_][-.:a-zA-Z_0-9]*)(\s*=\s*(\'[^\']*\'|"[^"]*"|[^\s"\'=<>`]*))?z]((?<=[\'"\s/])[^\s/>][^\s/=>]*)(\s*=+\s*(\'[^\']*\'|"[^"]*"|(?![\'"])[^>\s]*))?(?:\s|/(?!>))*a <[a-zA-Z][-.a-zA-Z0-9:_]* # tag name (?:\s+ # whitespace before attribute name (?:[a-zA-Z_][-.:a-zA-Z0-9_]* # attribute name (?:\s*=\s* # value indicator (?:'[^']*' # LITA-enclosed value |\"[^\"]*\" # LIT-enclosed value |[^'\">\s]+ # bare value ) )? ) )* \s* # trailing whitespace aF <[a-zA-Z][-.a-zA-Z0-9:_]* # tag name (?:[\s/]* # optional whitespace before attribute name (?:(?<=['"\s/])[^\s/>][^\s/=>]* # attribute name (?:\s*=+\s* # value indicator (?:'[^']*' # LITA-enclosed value |"[^"]*" # LIT-enclosed value |(?!['"])[^>\s]* # bare value ) (?:\s*,)* # possibly followed by a comma )?(?:\s|/(?!>))* )* )? \s* # trailing whitespace z#c@s"eZdZdZdddZddZdS) HTMLParseErrorz&Exception raised for all parse errors.NNcCs&|sJ||_|d|_|d|_dS)Nrmsglinenooffset)selfr positionr>/usr/lib/python3/dist-packages/future/backports/html/parser.py__init__Us zHTMLParseError.__init__cCs>|j}|jdur|d|j}|jdur|d|jd}|S)Nz , at line %dz , column %dr r )rresultrrr__str__[s   zHTMLParseError.__str__N)r )__name__ __module__ __qualname____doc__rrrrrrr Rs  r c@seZdZdZdZd:ddZddZdd Zd d Zd d Z dZ ddZ ddZ ddZ ddZddZd;ddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5Zd6d7Zd8d9ZdS)< HTMLParseraFind tags and other markup and call handler functions. Usage: p = HTMLParser() p.feed(data) ... p.close() Start tags are handled by calling self.handle_starttag() or self.handle_startendtag(); end tags by self.handle_endtag(). The data between tags is passed from the parser to the derived class by calling self.handle_data() with the data as argument (the data may be split up in arbitrary chunks). Entity references are passed by calling self.handle_entityref() with the entity reference as the argument. Numeric character references are passed to self.handle_charref() with the string containing the reference as the argument. )scriptstyleFcCs&|r tjdtdd||_|dS)zInitialize and reset this instance. If strict is set to False (the default) the parser will parse invalid markup, otherwise it will raise an error. Note that the strict mode is deprecated. zThe strict mode is deprecated.) stacklevelN)warningswarnDeprecationWarningstrictreset)rr#rrrrzs  zHTMLParser.__init__cCs(d|_d|_t|_d|_tj|dS)z1Reset this instance. Loses all unprocessed data.z???N)rawdatalasttaginteresting_normal interesting cdata_elemr ParserBaser$rrrrr$s zHTMLParser.resetcCs|j||_|ddS)zFeed data to the parser. Call this as often as you want, with as little or as much text as you want (may include '\n'). rN)r&goaheadrdatarrrfeeds zHTMLParser.feedcCs|ddS)zHandle any buffered data.r N)r-r,rrrcloseszHTMLParser.closecCst||N)r getpos)rmessagerrrerrorszHTMLParser.errorNcCs|jS)z)Return full source of start tag: '<...>'.)_HTMLParser__starttag_textr,rrrget_starttag_textszHTMLParser.get_starttag_textcCs$||_td|jtj|_dS)Nz )lowerr*recompileIr))relemrrrset_cdata_modes zHTMLParser.set_cdata_modecCst|_d|_dSr2)r(r)r*r,rrrclear_cdata_modes zHTMLParser.clear_cdata_modec Cs|j}d}t|}||kr|j||}|r|}n|jr!n|}||kr0|||||||}||kr<ng|j}|d|rt ||rP| |}nG|d|r[| |}n<|d|rf| |}n1|d|rq||}n&|d|r|jr||}n||}n|d|kr|d|d}nn |dkr|sn|jr|d|d |d}|dkr|d|d}|dkr|d}n|d7}|||||||}n|d |r$t ||}|r |d d } || |}|d |ds|d}|||}q d ||dvr#||dd ||d }n|d|rt ||}|rT|d} || |}|d |dsM|d}|||}q t ||}|r|r|||dkr|jru|dn||kr||}|||d}n|d|kr|d|||d}nnJd||ks|r||kr|js|||||||}||d|_dS)Nr< z junk characters in start tag: %rrq)r6check_for_whole_start_tagr&tagfindrOrZrXr8r'r#attrfindattrfind_tolerantunescapeappendstripr3countrHrfindr5rKendswithhandle_startendtaghandle_starttagCDATA_CONTENT_ELEMENTSr=)rr^endposr&attrsrOratagmattrnamerest attrvaluerZrrrrrrPEsd   &(         zHTMLParser.parse_starttagcCs|j}|jr t||}nt||}|r}|}|||d}|dkr)|dS|dkrY|d|r7|dS|d|r?dS|jrO|||d|d||krU|S|dS|dkr_dS|d vredS|jrs||||d ||kry|S|dSt d ) Nr r/rqrrEzmalformed empty start tagr%z6abcdefghijklmnopqrstuvwxyz=/ABCDEFGHIJKLMNOPQRSTUVWXYZzmalformed start tagzwe should not get here!) r&r#locatestarttagendrOlocatestarttagend_tolerantrZrMrLr5AssertionError)rr^r&rr`nextrrrrt~s>      z$HTMLParser.check_for_whole_start_tagcCsJ|j}|||ddksJdt||d}|sdS|}t||}|s||jdur9|||||S|jrH| d|||ft ||d}|se|||ddkr`|dS| |S| }|d |}|||dS| d }|jdur||jkr|||||S|| ||S) Nrr@zunexpected call to parse_endtagr rEzbad end tag: %rrdzr)r& endendtagrIrZ endtagfindrOr*rKr#r5tagfind_tolerantrhrXr8rV handle_endtagr>)rr^r&rOri namematchtagnamer<rrrrQs<       zHTMLParser.parse_endtagcCs|||||dSr2)rrrrrrrrr~s zHTMLParser.handle_startendtagcCdSr2rrrrrrzHTMLParser.handle_starttagcCrr2r)rrrrrrrzHTMLParser.handle_endtagcCrr2rrrbrrrrYrzHTMLParser.handle_charrefcCrr2rrrrrr\rzHTMLParser.handle_entityrefcCrr2rr.rrrrKrzHTMLParser.handle_datacCrr2rr.rrrrjrzHTMLParser.handle_commentcCrr2r)rdeclrrrrgrzHTMLParser.handle_declcCrr2rr.rrrrnrzHTMLParser.handle_picCs|jr |d|fdSdS)Nzunknown declaration: %r)r#r5r.rrr unknown_declszHTMLParser.unknown_declcCs"d|vr|Sdd}td||S)NrGcSs|d}z-|ddkr2|dd}|ddvr&t|dddd}nt|d}t|WSWn ty@d|YSwddlm}||vrO||S|drXd |Std t |D]}|d||vry||d|||dSq_d |S) Nr#r )xXrFrD)html5rGr) groupsintrstripchr ValueErrorfuture.backports.html.entitiesrr}rangerH)scrrrrrreplaceEntitiess,          z,HTMLParser.unescape..replaceEntitiesz&&(#?[xX]?(?:[0-9a-fA-F]+;|\w{1,32};?)))r9sub)rrrrrrrxs zHTMLParser.unescape)F)r ) rrrrrrr$r0r1r5r6r7r=r>r-rUrhrSrPrtrQr~rrrYr\rKrjrgrnrrxrrrrrds<   h   9+* r) r __future__rrrrfuture.builtinsfuture.backportsrr9r r:r(r]r[rWrNrm commentcloserurrvrwVERBOSErrrr Exceptionr r+rrrrrs>