U
    {hy                     @   s   d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	 e
dZe
dZe
dZeed	d
dZG dd dZG dd dZG dd deZG dd deZG dd dZdS )z=
Utility classes for rewriting elements of HTML-like strings
    N)defaultdict)CallableDictList)cached_propertyz<a(\b[^>]*)>z<embed(\b[^>]*)/>z([\w-]+)\="([^"]*)")attr_stringreturnc                 C   sH   i }t | D ]4\}}|dddddddd}|||< q|S )	zR
    helper method to extract tag attributes, as a dict of un-escaped strings
    z&lt;<z&gt;>z&quot;"z&amp;&)
FIND_ATTRSfindallreplace)r   
attributesnameval r   c/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/rich_text/rewriters.pyextract_attrs   s       
r   c                   @   s<   e Zd ZdZdd Zedd Zedd Zedd	 Z	d
S )TagMatchz5Represents a single matched tag in a rich text stringc                 C   s   || _ d | _d S N)matchreplacement)selfr   r   r   r   __init__#   s    zTagMatch.__init__c                 C   s   t | jdS )N   )r   r   groupr   r   r   r   attrs'   s    zTagMatch.attrsc                 C   s
   | j  S r   )r   startr   r   r   r   r    +   s    zTagMatch.startc                 C   s
   | j  S r   )r   endr   r   r   r   r!   /   s    zTagMatch.endN)
__name__
__module____qualname____doc__r   r   r   propertyr    r!   r   r   r   r   r       s   

r   c                   @   sr   e Zd ZdddZdd Zdd Zdd	 Zeed
ddZee	ee
e f d
ddZeedddZdd ZdS )TagRewriterNc                 C   s"   |pi | _ |pi | _|pi | _d S r   )rules
bulk_rulesreference_extractors)r   r(   r)   r*   r   r   r   r   5   s    

zTagRewriter.__init__c                 C   s   t d S r   NotImplementedErrorr   r   r   r   get_opening_tag_regex:   s    z!TagRewriter.get_opening_tag_regexc                 C   s   t dS )z;Given a dict of attributes from a tag, return the tag type.Nr+   r   r   r   r   r   get_tag_type_from_attrs=   s    z#TagRewriter.get_tag_type_from_attrsc                 C   s   t dS )zGiven a list of attribute dicts, all taken from tags of the same type, return a
        corresponding list of replacement strings to replace the tags with.

        Return an empty list for cases when you don't want any replacements made.
        Nr+   )r   tag_type
attrs_listr   r   r   get_tag_replacementsA   s    z TagRewriter.get_tag_replacements)htmlr   c                 C   s   |  |}g }| D ]L\}}dd |D }| ||}|s>qt||D ]\}}	|	|_|| qHq|jdd d d}
|D ]H}|d |j|
  |j ||j|
 d   }|
t	|j|j |j 7 }
q||S )Nc                 S   s   g | ]
}|j qS r   )r   ).0r   r   r   r   
<listcomp>O   s     z(TagRewriter.__call__.<locals>.<listcomp>c                 S   s   | j S r   )r    )r   r   r   r   <lambda>Z       z&TagRewriter.__call__.<locals>.<lambda>)keyr   )
extract_tagsitemsr2   zipr   appendsortr    r!   len)r   r3   matches_by_tag_typeZmatches_to_replacer0   Ztag_matchesZ
attr_dictsZreplacementsr   r   offsetr   r   r   __call__I   s*    
zTagRewriter.__call__c                 C   sF   t t}|  }||D ]&}t|}| |j}|| | q|S )zHelper method to extract and group HTML tags and their attributes.

        Returns a dict of TagMatch objects, mapping tag types to a list of all TagMatch objects of that tag type.
        )r   listr-   finditerr   r/   r   r<   )r   r3   r?   
re_patternZre_matchZ	tag_matchr0   r   r   r   r9   h   s    zTagRewriter.extract_tags)ruler   c                    s    fdd}|S )Nc                    s   t t | S r   )rB   map)argsrE   r   r   	bulk_rule{   s    z8TagRewriter.convert_rule_to_bulk_rule.<locals>.bulk_ruler   )r   rE   rI   r   rH   r   convert_rule_to_bulk_rulez   s    z%TagRewriter.convert_rule_to_bulk_rulec                 c   sN   |   }||D ]6}t|}| |}|| jkr4q| j| |E d H  qg S r   )r-   r   r   r/   r*   )r   r3   rD   r   r   r0   r   r   r   extract_references   s    

zTagRewriter.extract_references)NNN)r"   r#   r$   r   r-   r/   r2   strrA   r   r   r   r9   r   rJ   rK   r   r   r   r   r'   4   s   
r'   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	EmbedRewriterz
    Rewrites <embed embedtype="foo" /> tags within rich text into the HTML
    fragment given by the embed rule for 'foo'. Each embed rule is a function
    that takes a dict of attributes and returns the HTML fragment.
    c                 C   s   t S r   )FIND_EMBED_TAGr   r   r   r   r-      s    z#EmbedRewriter.get_opening_tag_regexc                 C   s
   | dS )NZ	embedtype)getr.   r   r   r   r/      s    z%EmbedRewriter.get_tag_type_from_attrsc                 C   st   z| j | }W n tk
r&   d }Y nX |sZz| j| }W n tk
rN   Y nX | |}|rf||S dgt| S )N r)   KeyErrorr(   rJ   r>   r   r0   r1   rE   r   r   r   r2      s    

z"EmbedRewriter.get_tag_replacementsNr"   r#   r$   r%   r-   r/   r2   r   r   r   r   rM      s   rM   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	LinkRewriterz
    Rewrites <a linktype="foo"> tags within rich text into the HTML fragment
    given by the rule for 'foo'. Each link rule is a function that takes a dict
    of attributes and returns the HTML fragment for the opening tag (only).
    c                 C   s   t S r   )
FIND_A_TAGr   r   r   r   r-      s    z"LinkRewriter.get_opening_tag_regexc                 C   sd   z
|d W S  t k
r^   |dd }|rZ|dr:Y dS |drJY dS |drZY dS Y nX d S )	Nlinktypehref)zhttp:zhttps:externalzmailto:email#anchor)rR   rO   
startswith)r   r   rX   r   r   r   r/      s    



z$LinkRewriter.get_tag_type_from_attrsc                 C   s   |sg S z| j | }W n tk
r.   d }Y nX |srz| j| }W n$ tk
rf   |dkrbg  Y S Y nX | |}|r~||S dgt| S )N)rZ   rY   r\   z<a>rQ   rS   r   r   r   r2      s    

z!LinkRewriter.get_tag_replacementsNrT   r   r   r   r   rU      s   rU   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	MultiRuleRewriterz:Rewrites HTML by applying a sequence of rewriter functionsc                 C   s
   || _ d S r   	rewriters)r   r`   r   r   r   r      s    zMultiRuleRewriter.__init__c                 C   s   | j D ]}||}q|S r   r_   )r   r3   Zrewriter   r   r   rA      s    

zMultiRuleRewriter.__call__c                 c   s    | j D ]}||E d H  qd S r   )r`   rK   )r   r3   Zrewriterr   r   r   rK      s    
z$MultiRuleRewriter.extract_referencesN)r"   r#   r$   r%   r   rA   rK   r   r   r   r   r^      s   r^   )r%   recollectionsr   typingr   r   r   Zdjango.utils.functionalr   compilerV   rN   r   rL   dictr   r   r'   rM   rU   r^   r   r   r   r   <module>   s   


Z8