a
    &b                     @   s   zd dl mZ W n ey.   d dlmZ Y n0 d dlmZ d dlmZ d dl	m
Z d dl	mZmZ d dlmZ G dd deZ
G d	d
 d
eZG dd deeZG dd deeZe Zdd ZdS )    )	lru_cache)GenericTranslator)HTMLTranslator)	XPathExpr)_unicode_safe_getattrExpressionError)FunctionalPseudoElementc                       s>   e Zd ZdZdZed	ddZ fddZ fddZ  Z	S )
r   FNc                 C   s$   | |j |j|jd}||_||_|S )N)pathelement	condition)r	   r
   r   textnode	attribute)clsxpathr   r   x r   3lib/python3.9/site-packages/parsel/csstranslator.py
from_xpath   s    zXPathExpr.from_xpathc                    sx   t t|  }| jrF|dkr"d}n$|dr>|d d d }n|d7 }| jd urt|drf|d d }|d| j 7 }|S )N*ztext()z::*/*z/text()z/@%s)superr   __str__r   endswithr   )selfr	   	__class__r   r   r      s    


zXPathExpr.__str__c                    s&   t t| || |j| _|j| _| S N)r   r   joinr   r   )r   Zcombinerotherr   r   r   r   *   s    zXPathExpr.join)FN)
__name__
__module____qualname__r   r   classmethodr   r   r   __classcell__r   r   r   r   r      s   r   c                       s8   e Zd ZdZ fddZdd Zdd Zdd	 Z  ZS )
TranslatorMixinzThis mixin adds support to CSS pseudo elements via dynamic dispatch.

    Currently supported pseudo-elements are ``::text`` and ``::attr(ATTR_NAME)``.
    c                    s   t t| |}t|S r   )r   r%   xpath_elementr   r   )r   Zselectorr   r   r   r   r&   7   s    zTranslatorMixin.xpath_elementc                 C   s~   t |trFd|jdd }t| |d}|s:td|j |||}n4d|dd }t| |d}|srtd| ||}|S )zQ
        Dispatch method that transforms XPath to support pseudo-element
        z"xpath_%s_functional_pseudo_element-_Nz/The functional pseudo-element ::%s() is unknownzxpath_%s_simple_pseudo_elementz"The pseudo-element ::%s is unknown)
isinstancer   namereplacer   r   )r   r   Zpseudo_elementmethodr   r   r   xpath_pseudo_element;   s0    

z$TranslatorMixin.xpath_pseudo_elementc                 C   s8   |  dgdgfvr"td|j tj||jd jdS )zISupport selecting attribute values using ::attr() pseudo-element
        STRINGZIDENTz6Expected a single string or ident for ::attr(), got %rr   )r   )Zargument_typesr   Z	argumentsr   r   value)r   r   Zfunctionr   r   r   $xpath_attr_functional_pseudo_elementS   s    
z4TranslatorMixin.xpath_attr_functional_pseudo_elementc                 C   s   t j|ddS )z8Support selecting text nodes using ::text pseudo-elementT)r   )r   r   )r   r   r   r   r    xpath_text_simple_pseudo_element]   s    z0TranslatorMixin.xpath_text_simple_pseudo_element)	r    r!   r"   __doc__r&   r-   r0   r1   r$   r   r   r   r   r%   1   s
   
r%   c                       s(   e Zd Zeddd fdd	Z  ZS )r      maxsizedescendant-or-self::c                    s   t t| ||S r   )r   r   css_to_xpathr   Zcssprefixr   r   r   r7   c   s    zGenericTranslator.css_to_xpath)r6   r    r!   r"   r   r7   r$   r   r   r   r   r   b   s   r   c                       s(   e Zd Zeddd fdd	Z  ZS )r   r3   r4   r6   c                    s   t t| ||S r   )r   r   r7   r8   r   r   r   r7   i   s    zHTMLTranslator.css_to_xpath)r6   r:   r   r   r   r   r   h   s   r   c                 C   s
   t | S )z4Return translated XPath version of a given CSS query)_translatorr7   )Zqueryr   r   r   	css2xpathq   s    r<   N)	functoolsr   ImportErrorZfunctools32Z	cssselectr   ZOriginalGenericTranslatorr   ZOriginalHTMLTranslatorZcssselect.xpathr   ZOriginalXPathExprr   r   Zcssselect.parserr   objectr%   r;   r<   r   r   r   r   <module>   s   $1