
    Tj V                        d Z ddlZddlZddlmZ dgZ ej                  d      Z ej                  d      Z ej                  d      Z	 ej                  d      Z
 ej                  d	      Z ej                  d
      Z ej                  d      Z ej                  d      Z ej                  d      Z ej                  d      Z ej                  dej"                        Z ej                  dej"                        Z ej                  dej"                        Z ej                  d      Z ej                  d      Z G d dej.                        Zy)zA parser for HTML and XHTML.    N)unescape
HTMLParserz[&<]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
</[a-zA-Z]>z--!?>z-?>z0([a-zA-Z][^\t\n\r\f />]*)(?:[\t\n\r\f ]|/(?!>))*a{  
  (
    (?<=['"\t\n\r\f /])[^\t\n\r\f />][^\t\n\r\f /=>]*  # attribute name
   )
  ([\t\n\r\f ]*=[\t\n\r\f ]*        # value indicator
    ('[^']*'                        # LITA-enclosed value
    |"[^"]*"                        # LIT-enclosed value
    |(?!['"])[^>\t\n\r\f ]*         # bare value
    )
   )?
  (?:[\t\n\r\f ]|/(?!>))*           # possibly followed by a space
a  
  [a-zA-Z][^\t\n\r\f />]*           # tag name
  [\t\n\r\f /]*                     # optional whitespace before attribute name
  (?:(?<=['"\t\n\r\f /])[^\t\n\r\f />][^\t\n\r\f /=>]*  # attribute name
    (?:[\t\n\r\f ]*=[\t\n\r\f ]*    # value indicator
      (?:'[^']*'                    # LITA-enclosed value
        |"[^"]*"                    # LIT-enclosed value
        |(?!['"])[^>\t\n\r\f ]*     # bare value
       )
     )?
    [\t\n\r\f /]*                   # possibly followed by a space
   )*
   >?
aF  
  <[a-zA-Z][^\t\n\r\f />\x00]*       # 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 space
       )?(?:\s|/(?!>))*
     )*
   )?
  \s*                                # trailing whitespace
z#</\s*([a-zA-Z][-.a-zA-Z0-9:_]*)\s*>c                        e Zd ZdZdZdZddd fd
Z fdZd	 Zd
 Z	dZ
d ZdddZd Zd#dZd Zd Zd#dZd$dZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd  Zd! Zd" Z  xZ!S )%r   aE  Find 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).  If convert_charrefs is
    True the character references are converted automatically to the
    corresponding Unicode character (and self.handle_data() is no
    longer split in chunks), otherwise they are passed by calling
    self.handle_entityref() or self.handle_charref() with the string
    containing respectively the named or numeric reference as the
    argument.
    )scriptstylexmpiframenoembednoframes)textareatitleTF)convert_charrefs	scriptingc                ^    t         |           || _        || _        | j	                          y)az  Initialize and reset this instance.

        If convert_charrefs is true (the default), all character references
        are automatically converted to the corresponding Unicode characters.

        If *scripting* is false (the default), the content of the
        ``noscript`` element is parsed normally; if it's true,
        it's returned as is without being parsed.
        N)super__init__r   r   reset)selfr   r   	__class__s      "/usr/lib/python3.12/html/parser.pyr   zHTMLParser.__init__v   s(     	 0"

    c                     d| _         d| _        t        | _        d| _        d| _        d| _        g | _        d| _        d| _	        t        | -          y)z1Reset this instance.  Loses all unprocessed data. z???NTr      )rawdatalasttaginteresting_normalinteresting
cdata_elem_support_cdata
_escapable_pending_pending_len_parse_thresholdr   r   )r   r   s    r   r   zHTMLParser.reset   sO    -" !r   c                    | xj                   t        |      z  c_         | j                   | j                  k  r| j                  j	                  |       y| j                  s| xj
                  |z  c_        nc| j                  j	                  |       | xj
                  dj                  | j                        z  c_        | j                  j                          d| _         t        | j
                        }| j                  d       t        | j
                        |k  rd| _        yt        | j
                        | _        y)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').
        r   r   r   N)	r$   lenr%   r#   appendr   joincleargoahead)r   datans      r   feedzHTMLParser.feed   s     	SY&t444MM  &==$$$T* 66##% !DDLL!ALLO4<< 1$()% ),DLL(9%r   c                     | j                   rO| xj                  dj                  | j                         z  c_        | j                   j                          d| _        | j                  d       y)zHandle any buffered data.r   r   r   N)r#   r   r)   r*   r$   r+   r   s    r   closezHTMLParser.close   sG    ==LLBGGDMM22LMM! !DQr   Nc                     | j                   S )z)Return full source of start tag: '<...>'.)_HTMLParser__starttag_textr0   s    r   get_starttag_textzHTMLParser.get_starttag_text   s    ###r   	escapablec                   |j                         | _        || _        | j                  dk(  rt        j                  d      | _        y |rT| j                  sHt        j                  d| j                  z  t        j                  t        j                  z        | _        y t        j                  d| j                  z  t        j                  t        j                  z        | _        y )N	plaintextz\Zz&|</%s(?=[\t\n\r\f />])z</%s(?=[\t\n\r\f />]))	lowerr    r"   recompiler   r   
IGNORECASEASCII)r   elemr6   s      r   set_cdata_modezHTMLParser.set_cdata_mode   s    **,#??k)!zz%0Dt44!zz*Dt*V*,--*@ BD  "zz*BT__*T*,--*@ BDr   c                 6    t         | _        d | _        d| _        y )NT)r   r   r    r"   r0   s    r   clear_cdata_modezHTMLParser.clear_cdata_mode   s    -r   c                     || _         y)a  Enable or disable support of the CDATA sections.
        If enabled, "<[CDATA[" starts a CDATA section which ends with "]]>".
        If disabled, "<[CDATA[" starts a bogus comments which ends with ">".

        This method is not called by default. Its purpose is to be called
        in custom handle_starttag() and handle_endtag() methods, with
        value that depends on the adjusted current node.
        See https://html.spec.whatwg.org/multipage/parsing.html#markup-declaration-open-state
        for details.
        N)r!   )r   flags     r   _set_support_cdatazHTMLParser._set_support_cdata   s     #r   c                    | j                   }d}t        |      }||k  r9| j                  rq| j                  se|j	                  d|      }|dk  r|j                  dt        ||dz
              }|dk\  r't        j                  d      j                  ||      sn|}n?| j                  j                  ||      }|r|j                         }n| j                  rn|}||k  rJ| j                  r*| j                  r| j                  t        |||              n| j                  |||        | j                  ||      }||k(  rn|j                   } |d|      r=t"        j%                  ||      r| j'                  |      }	n |d|      r| j)                  |      }	nt |d|      r| j+                  |      }	nY |d|      r| j-                  |      }	n> |d	|      r| j/                  |      }	n#|d
z   |k  s|r| j                  d       |d
z   }	nnH|	dk  rl|sn>t"        j%                  ||      rnN |d|      rK|dz   |k(  r| j                  d       n*t0        j%                  ||      rn| j3                  ||dz   d         n |d|      rF|}dD ]'  }
|j5                  |
|dz         s|t        |
      z  } n | j3                  ||dz   |        n |d|      r$| j6                  r| j9                  ||dz   d         n~|||dz    j;                         dk(  r| j=                  ||dz   d         nM |d	|      r| j3                  ||dz   d         n, |d|      r| j?                  ||dz   d         ntA        d      |}	| j                  ||	      }n |d|      rtB        j%                  ||      }|rY|jE                         dd }| jG                  |       |jI                         }	 |d|	d
z
        s|	d
z
  }	| j                  ||	      }d||d  v r,| j                  |||dz           | j                  ||dz         }n |d|      rtJ        j%                  ||      }|rW|jE                  d
      }| jM                  |       |jI                         }	 |d|	d
z
        s|	d
z
  }	| j                  ||	      }tN        j%                  ||      }|rE|rB|jE                         ||d  k(  r,|jI                         }	|	|k  r|}	| j                  ||d
z         }n>|d
z   |k  r'| j                  d       | j                  ||d
z         }nnJ d       ||k  r9|ra||k  r\| j                  r*| j                  r| j                  t        |||              n| j                  |||        | j                  ||      }||d  | _         y )Nr   <&"   z[\t\n\r\f ;]</<!--<?<!r      )z--!z---   	<![CDATA[   	   	<!doctypezwe should not get here!z&#;zinteresting.search() lied)(r   r'   r   r    findrfindmaxr:   r;   searchr   startr"   handle_datar   	updatepos
startswithstarttagopenmatchparse_starttagparse_endtagparse_commentparse_piparse_html_declaration
endtagopenhandle_commentendswithr!   unknown_declr9   handle_decl	handle_piAssertionErrorcharrefgrouphandle_charrefend	entityrefhandle_entityref
incomplete)r   ro   r   ir-   jampposr_   r]   ksuffixnames               r   r+   zHTMLParser.goahead   s   ,,L!e$$T__LLa(q5 %]]3Aqt=F!JJ7>>wOA((//;AA1u((T__$$Xgal%;<$$WQq\2q!$AAvu ++J#q!%%gq1++A.Aa())!,A***1-Aa(a(Aa(33A6A!eq[C$$S)AAq5#))'15#D!,q5A: ,,T2'--gq9  !//!>#FA.&8 &F&//!< !S[ 0 %& ++GAaCN;#K38K8K))'!A#$-8 1Q3--/;>((17#D!,++GAaCDM:#D!,wqst}5,-FGGANN1a(D!$gq1 ;;=2.D''-		A%c1Q3/Eq!,Agabk)((1Q38 NN1ac2C#!3 ;;q>D))$/		A%c1Q3/Eq!,A"((!4u{{};!IIK6 !A NN1a!e4!eq[ $$S)q!a%0A555qw !ez 1q5$$  '!A,!78  1.q!$Aqr{r   c                    | j                   }|||dz    dk(  sJ d       |||dz    dk(  r| j                  |      S |||dz    dk(  rC| j                  r7|j                  d|dz         }|d	k  ry
| j	                  ||dz   |        |dz   S |||dz    j                         dk(  r7|j                  d|dz         }|d
k(  ry
| j                  ||dz   |        |dz   S |||dz    dk(  ra|j                  d|dz         }|d	k  ry
||dz
     dk(  r| j	                  ||dz   |dz
          |dz   S | j                  ||dz   |        |dz   S | j                  |      S )NrM   rL   z+unexpected call to parse_html_declaration()rO   rJ   rR   rP   z]]>r   rT   rQ   rS   r   r   z<![])	r   rb   r!   rV   rh   r9   ri   rf   parse_bogus_comment)r   rs   r   rt   gtposs        r   rd   z!HTMLParser.parse_html_declarationk  s   ,,q1~% 	D )C 	D%1QqS>V#%%a((Qqs^{*t/B/BUAaC(A1ugac1o.q5LQqs^!!#{2LLac*E{WQqS/07NQqs^u$S!A#&A1uqs|s"!!'!A#qs"34 q5L ##GAaCO4q5L++A..r   c                 ,   | j                   }|j                  d|      sJ d       t        j                  ||dz         }|st        j                  ||dz         }|sy|r'|j                         }| j                  ||dz   |        |j                         S )NrJ   "unexpected call to parse_comment()rO   rT   )	r   r]   commentcloserY   commentabruptcloser_   rZ   rf   ro   )r   rs   reportr   r_   rt   s         r   rb   zHTMLParser.parse_comment  s    ,,!!&!,R.RR,##GQqS1&,,Wac:EA!Q0yy{r   c                     | j                   }|||dz    dv sJ d       |j                  d|dz         }|dk(  ry|r| j                  ||dz   |        |dz   S )NrM   )rL   rI   r~   r   rT   r   )r   rV   rf   )r   rs   r   r   poss        r   r{   zHTMLParser.parse_bogus_comment  su    ,,q1~- 	C 1B 	C-ll3!$"9!C 01Qwr   c                     | j                   }|||dz    dk(  sJ d       t        j                  ||dz         }|sy|j                         }| j	                  ||dz   |        |j                         }|S )NrM   rK   zunexpected call to parse_pi()rT   )r   picloserY   rZ   rj   ro   r   rs   r   r_   rt   s        r   rc   zHTMLParser.parse_pi  st    ,,q1~%F'FF%w!,KKMwqsA'IIKr   c                    d | _         | j                  |      }|dk  r|S | j                  }||| | _         g }t        j	                  ||dz         }|sJ d       |j                         }|j                  d      j                         x| _        }||k  rt        j	                  ||      }|sn|j                  ddd      \  }	}
}|
sd }n,|d d dcxk(  r|dd  k(  sn |d d dcxk(  r|dd  k(  rn n|dd }|rt        |      }|j                  |	j                         |f       |j                         }||k  r||| j                         }|d	vr| j                  |||        |S |j                  d
      r| j                  ||       |S | j!                  ||       || j"                  v s| j$                  r|dk(  s|dk(  r| j'                  |d       |S || j(                  v r| j'                  |d       |S )Nr   r   z#unexpected call to parse_starttag()rM   rQ   'rT   ")r   />r   noscriptr8   Fr5   T)r3   check_for_whole_start_tagr   tagfind_tolerantr_   ro   rm   r9   r   attrfind_tolerantr   r(   stripr[   rg   handle_startendtaghandle_starttagCDATA_CONTENT_ELEMENTSr   r?   RCDATA_CONTENT_ELEMENTS)r   rs   endposr   attrsr_   rv   tagmattrnamerest	attrvaluero   s                r   r`   zHTMLParser.parse_starttag  s     $//2A:M,,&q0  &&w!4;;;uIIK"[[^1133s&j!''3A()1a(8%HdI 	2A$8)BC.82A#7237%aO	$Y/	LL(..*I67A &j a%%'k!WQv./M<<##C/    e,t222C:$5{"##C5#9  444##C4#8r   c                     | j                   }t        j                  ||dz         }|sJ |j                         }||dz
     dk7  ry|S )Nr   r   rT   )r   locatetagendr_   ro   r   s        r   r   z$HTMLParser.check_for_whole_start_tag  sK    ,,""7AaC0uIIK1Q3<3r   c                    | j                   }|||dz    dk(  sJ d       |j                  d|dz         dk  ryt        j                  ||      s$||dz   |dz    dk(  r|dz   S | j	                  |      S t
        j                  ||dz         }|sJ |j                         }||dz
     dk7  ryt        j                  ||dz         }|sJ |j                  d      j                         }| j                  |       | j                          |S )	NrM   rI   zunexpected call to parse_endtagr   r   rT   rQ   r   )r   rV   re   r_   r{   r   ro   r   rm   r9   handle_endtagrA   )r   rs   r   r_   rt   r   s         r   ra   zHTMLParser.parse_endtag  s    ,,q1~%H'HH%<<QqS!A%+qs1Q33&s
//22""7AaC0uIIK1Q3<3 !&&w!4ukk!n""$3r   c                 J    | j                  ||       | j                  |       y N)r   r   r   r   r   s      r   r   zHTMLParser.handle_startendtag  s     S%(3r   c                      y r    r   s      r   r   zHTMLParser.handle_starttag      r   c                      y r   r   )r   r   s     r   r   zHTMLParser.handle_endtag  r   r   c                      y r   r   r   rx   s     r   rn   zHTMLParser.handle_charref  r   r   c                      y r   r   r   s     r   rq   zHTMLParser.handle_entityref  r   r   c                      y r   r   r   r,   s     r   r[   zHTMLParser.handle_data#  r   r   c                      y r   r   r   s     r   rf   zHTMLParser.handle_comment'  r   r   c                      y r   r   )r   decls     r   ri   zHTMLParser.handle_decl+  r   r   c                      y r   r   r   s     r   rj   zHTMLParser.handle_pi/  r   r   c                      y r   r   r   s     r   rh   zHTMLParser.unknown_decl2  r   r   )T)r   )"__name__
__module____qualname____doc__r   r   r   r   r.   r1   r3   r4   r?   rA   rD   r+   rd   rb   r{   rc   r`   r   ra   r   r   r   rn   rq   r[   rf   ri   rj   rh   __classcell__)r   s   @r   r   r   Z   s    0 Y3+/5 :< O$ 16 
B
# G#X/D		.d< 
r   )r   r:   _markupbasehtmlr   __all__r;   r   rr   rp   rl   r^   re   r   r   r   r   VERBOSEr   r   locatestarttagend_tolerant	endendtag
endtagfind
ParserBaser   r   r   r   <module>r      s[   " 
   .  RZZ' RZZ%
BJJ>?	
"**@
Arzz+&RZZ%

"**S/rzz(#RZZ'  2::QR BJJ   ZZ  rzz  ZZ  (RZZ ) ZZ  BJJsO	RZZ>?
Y'' Yr   