
    qh<              	          S r S/rSSKr\R                  \R                  -  \R
                  -  \R                  -  r\R                  " S\5      r	Sr
S r\" S5      r\" SS	5      r\R                  " S
\5      r\R                  " S\5      rSSSSS.r\R                  " SSR#                  \" \R'                  5       5      5      -  5      rS rSS jrS rS rS rS rg)a	  
This module implements a VERY limited parser that finds <link> tags in
the head of HTML or XHTML documents and parses out their attributes
according to the OpenID spec. It is a liberal parser, but it requires
these things from the data in order to work:

 - There must be an open <html> tag

 - There must be an open <head> tag inside of the <html> tag

 - Only <link>s that are found inside of the <head> tag are parsed
   (this is by design)

 - The parser follows the OpenID specification in resolving the
   attributes of the link tags. This means that the attributes DO NOT
   get resolved as they would by an XML or HTML parser. In particular,
   only certain entities get replaced, and href attributes do not get
   resolved relative to a base URL.

From http://openid.net/specs.bml#linkrel:

 - The openid.server URL MUST be an absolute URL. OpenID consumers
   MUST NOT attempt to resolve relative URLs.

 - The openid.server URL MUST NOT include entities other than &amp;,
   &lt;, &gt;, and &quot;.

The parser ignores SGML comments and <![CDATA[blocks]]>. Both kinds of
quoting are allowed for attributes.

The parser deals with invalid markup in these ways:

 - Tag names are not case-sensitive

 - The <html> tag is accepted even when it is not at the top level

 - The <head> tag is accepted even when it is not a direct child of
   the <html> tag, but a <html> tag must be an ancestor of the <head>
   tag

 - <link> tags are accepted even when they are not direct children of
   the <head> tag, but a <head> tag must be an ancestor of the <link>
   tag

 - If there is no closing tag for an open <html> or <head> tag, the
   remainder of the document is viewed as being inside of the tag. If
   there is no closing tag for a <link> tag, the link tag is treated
   as a short tag. Exceptions to this rule are that <html> closes
   <html> and <body> or <head> closes <head>

 - Attributes of the <link> tag are not required to be quoted.

 - In the case of duplicated attribute names, the attribute coming
   last in the tag will be the value returned.

 - Any text that does not parse as an attribute within a link tag will
   be ignored. (e.g. <link pumpkin rel='openid.server' /> will ignore
   pumpkin)

 - If there are more than one <html> or <head> tag, the parser only
   looks inside of the first one.

 - The contents of <script> tags are ignored entirely, except unclosed
   <script> tags. Unclosed <script> tags are ignored.

 - Any other invalid markup is ignored, including unclosed SGML
   comments and unclosed <![CDATA[blocks.
parseLinkAttrs    Nz
  # Comments
  <!--.*?-->

  # CDATA blocks
| <!\[CDATA\[.*?\]\]>

  # script blocks
| <script\b

  # make sure script is not an XML namespace
  (?!:)

  [^>]*>.*?</script>

a  
# Starts with the tag name at a word boundary, where the tag name is
# not a namespace
<%(tag_name)s\b(?!:)

# All of the stuff up to a ">", hopefully attributes.
(?P<attrs>[^>]*?)

(?: # Match a short tag
    />

|   # Match a full tag
    >

    (?P<contents>.*?)

    # Closed by
    (?: # One of the specified close tags
        </?%(closers)s\s*>

        # End of the string
    |   \Z

    )

)
c                     U(       a  SR                  U 4U-   5      nSU< S3nOU n[        [        5       -  n[        R                  " U[
        5      $ )N|z(?:))jointag_exprlocalsrecompileflags)tag_name
close_tagsoptionsclosersexprs        L/var/www/html/env/lib/python3.13/site-packages/openid/consumer/html_parse.py
tagMatcherr      sD    ((H<*45%(fhD::dE""    htmlheadbodyz<link\b(?!:)al  
# Must start with a sequence of word-characters, followed by an equals sign
(?P<attr_name>\w+)=

# Then either a quoted or unquoted attribute
(?:

 # Match everything that\'s between matching quote marks
 (?P<qopen>["\'])(?P<q_val>.*?)(?P=qopen)
|

 # If the value is not quoted, match up to whitespace
 (?P<unq_val>(?:[^\s<>/]|/(?!>))+)
)

|

(?P<end_link>[<>])
&<>")ampltgtquotz&(%s);r   c                 h    [         R                  U R                  S5      U R                  5       5      $ )z1Replace the entities that are specified by OpenID   )replacementsgetgroup)mos    r   
replaceEntr&      s#    BHHQK44r   c                    [        U [        5      (       a   U R                  S5      n [
        R                  SU 5      n[        R                  U5      nUb  UR                  S5      S:X  a  / $ UR                  S5      u  pE[        R                  X$U5      nUb  UR                  S5      S:X  a  / $ UR                  S5      u  pE[        R                  X&R                  5       UR                  5       5      n/ nU H  n	U	R                  5       S-   n0 n
[        R                  X$5       HR  nUR                   S:X  a    OAUR#                  S	S
S5      u  pn[$        R                  [&        U=(       d    U5      nXU'   MT     UR)                  U
5        M     U$ ! [         a<     U R                  S5      n  GN! [         a    U(       a  / s s $ [	        S5      ef = ff = f)ah  Find all link tags in a string representing a HTML document and
return a list of their attributes.

@param html: the text to parse
@type html: str or unicode

@param ignore_errors: whether to return despite e.g. parsing errors
@type ignore_errors: bool

@return: A list of dictionaries of attributes, one for each link tag
@rtype: [[(type(html), type(html))]]
zutf-8latin1zUnreadable HTML! contents   end_link	attr_nameq_valunq_val)
isinstancebytesdecodeUnicodeDecodeErrorAssertionError
removed_resub	html_findsearchstartspan	head_find	link_findfinditerend	attr_find	lastgroupr$   ent_replacer&   append)r   ignore_errorsstrippedhtml_mor:   r?   head_molink_mosmatcheslink_mo
link_attrsattr_mor.   r/   r0   attr_vals                   r   r   r      s    $	=;;w'D ~~b$'Hx(G'--
3r9	j)JEx4G'--
3r9	j)JE!!(MMOW[[]KHG!#
 ))(:G  J. )0k76?)A%Ig"z73CeDH$,y! ; 	z" " NU " 		=={{8,% =  I();<<=		=s)   F 
GF22GG	GGc                     U R                  5       R                  5       nU H  nUR                  5       nX1:X  d  M    g   g)z+Does this target_rel appear in the rel_str?r!   r   )stripsplitlower)rel_attr
target_relrelsrels       r   
relMatchesrV      s<     >>!!#Diik 
 r   c                 L    U R                  S5      nU=(       a    [        X!5      $ )z1Does this link have target_rel as a relationship?rU   )r#   rV   )rK   rS   rR   s      r   
linkHasRelrX      s"     ~~e$H8
888r   c                 8   ^ U4S jn[        [        X 5      5      $ )zRFilter the list of link attributes on whether it has target_rel
as a relationship.c                    > [        U T5      $ )N)rX   )attrsrS   s    r   <lambda>findLinksRel.<locals>.<lambda>
  s    *UJ"?r   )listfilter)link_attrs_listrS   matchesTargets    ` r   findLinksRelrb     s     @M}677r   c                 T    [        X5      nU(       d  gUS   nUR                  S5      $ )zpReturn the value of the href attribute for the first link tag
in the list that has target_rel as a relationship.Nr   href)rb   r#   )r`   rS   rI   firsts       r   findFirstHrefrf     s,     ?7GAJE99Vr   )F)__doc____all__r
   DOTALL
IGNORECASEVERBOSEUNICODEr   r   r6   r   r   r8   r<   r=   r@   r"   r   r^   keysrB   r&   r   rV   rX   rb   rf    r   r   <module>ro      s  CJ 
 	 IImmjj!jj  ZZ  
":# v	vv&	JJ.	JJ $ %	, 

	 jjSXXd<3D3D3F.G%HHI5
=@	98r   