U
    {h                     @   sX   d dl Z d dlmZ e dZe dZe dZe dZe dZG dd	 d	Z	dS )
    N)Mappingz
^([\w-]+)$z^([\w-]+)\[([\w-]+)\]$z^([\w-]+)\[([\w-]+)='(.*)'\]$z^([\w-]+)\[([\w-]+)="(.*)"\]$z^([\w-]+)\[([\w-]+)=([\w-]+)\]$c                   @   sJ   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dS )HTMLRuleseta  
    Maintains a set of rules for matching HTML elements.
    Each rule defines a mapping from a CSS-like selector to an arbitrary result object.

    The following forms of rule are currently supported:
    'a' = matches any <a> element
    'a[href]' = matches any <a> element with an 'href' attribute
    'a[linktype="page"]' = matches any <a> element with a 'linktype' attribute equal to 'page'
    Nc                 C   s   i | _ |r| | d S N)element_rules	add_rules)selfrules r	   w/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/admin/rich_text/converters/html_ruleset.py__init__   s    zHTMLRuleset.__init__c                 C   s0   t |tr| }|D ]\}}| || qd S r   )
isinstancer   itemsadd_rule)r   r   selectorresultr	   r	   r
   r   $   s    
zHTMLRuleset.add_rulesc                 C   s6   | j |g }|ddd |f |jdd d d S )N   c                 S   s   dS )NTr	   attrsr	   r	   r
   <lambda>0       z/HTMLRuleset._add_element_rule.<locals>.<lambda>c                 S   s   | d S Nr   r	   tr	   r	   r
   r   2   r   keyr   
setdefaultappendsort)r   namer   r   r	   r	   r
   _add_element_rule,   s    zHTMLRuleset._add_element_rulec                    s:   | j |g }|d fdd|f |jdd d d S )N   c                    s    | kS r   r	   r   attrr	   r
   r   8   r   z9HTMLRuleset._add_element_with_attr_rule.<locals>.<lambda>c                 S   s   | d S r   r	   r   r	   r	   r
   r   :   r   r   r   )r   r   r#   r   r   r	   r"   r
   _add_element_with_attr_rule4   s    z'HTMLRuleset._add_element_with_attr_rulec                    s<   | j |g }|d fdd|f |jdd d d S )Nr!   c                    s    | ko|   kS r   r	   r   r#   valuer	   r
   r   B   r   z?HTMLRuleset._add_element_with_attr_exact_rule.<locals>.<lambda>c                 S   s   | d S r   r	   r   r	   r	   r
   r   E   r   r   r   )r   r   r#   r&   r   r   r	   r%   r
   !_add_element_with_attr_exact_rule<   s
    z-HTMLRuleset._add_element_with_attr_exact_rulec                 C   s   t |}|r(|d}| || d S t|}|rT| \}}| ||| d S ttt	fD ]6}||}|r^| \}}}| 
||||  d S q^d S )Nr!   )ELEMENT_SELECTORmatchgroupr    ELEMENT_WITH_ATTR_SELECTORgroupsr$   -ELEMENT_WITH_ATTR_EXACT_SINGLE_QUOTE_SELECTOR-ELEMENT_WITH_ATTR_EXACT_DOUBLE_QUOTE_SELECTOR)ELEMENT_WITH_ATTR_EXACT_UNQUOTED_SELECTORr'   )r   r   r   r)   r   r#   regexr&   r	   r	   r
   r   G   s&    



zHTMLRuleset.add_rulec                 C   sJ   z| j | }W n tk
r$   Y dS X |D ]\}}}||r*|  S q*dS )z
        Look for a rule matching an HTML element with the given name and attribute dict,
        and return the corresponding result object. If no rule matches, return None.
        If multiple rules match, the one chosen is undetermined.
        N)r   KeyError)r   r   r   Zrules_to_test
precedenceZ
attr_checkr   r	   r	   r
   r)   _   s    zHTMLRuleset.match)N)__name__
__module____qualname____doc__r   r   r    r$   r'   r   r)   r	   r	   r	   r
   r      s   

r   )
recollections.abcr   compiler(   r+   r-   r.   r/   r   r	   r	   r	   r
   <module>   s   

