B ǁaW�a�@s�dZddlZddlZddlmZmZmZejddkr@eZ e Z ne Z e Z ddd�Z Gdd�dee�ZGd d �d e�Ze�d �jZe�d �jZe�d �jZGdd�de�ZGdd�de�ZdS)a cssselect.xpath =============== Translation of parsed CSS selectors to XPath expressions. :copyright: (c) 2007-2012 Ian Bicking and contributors. See AUTHORS for more details. :license: BSD, see LICENSE for more details. �N)�parse� parse_series� SelectorError�cCs|�dd��d�}t|||�S)N�ascii�replace)�encode�decode�getattr)�obj�name�default�r�@/opt/alt/python37/lib/python3.7/site-packages/cssselect/xpath.py�_unicode_safe_getattrsrc@seZdZdZdS)�ExpressionErrorz3Unknown or unsupported selector (eg. pseudo-class).N)�__name__� __module__� __qualname__�__doc__rrrrr#src@sFeZdZddd�Zdd�Zdd �Zd d �Zd d �Zdd�Zdd�Z dS)� XPathExpr��*FcCs||_||_||_dS)N)�path�element� condition)�selfrrrZ star_prefixrrr�__init__+szXPathExpr.__init__cCs,t|j�t|j�}|jr(|d|j7}|S)Nz[%s])�_unicoderrr)rrrrr�__str__0szXPathExpr.__str__cCsd|jj|fS)Nz%s[%s])� __class__r)rrrr�__repr__6szXPathExpr.__repr__cCs"|jrd|j|f|_n||_|S)Nz %s and (%s))r)rrrrr� add_condition9szXPathExpr.add_conditioncCs.|jdkrdS|�dt�|j��d|_dS)Nrz name() = %s)rr"�GenericTranslator� xpath_literal)rrrr� add_name_test@s  zXPathExpr.add_name_testcCs|jd7_dS)ze Append '*/' to the path to keep the context constrained to a single parent. z*/N)r)rrrr�add_star_prefixHszXPathExpr.add_star_prefixcCs:t|�|}|jdkr ||j7}||_|j|_|j|_|S)Nz*/)rrrr)rZcombiner�otherrrrr�joinOs   zXPathExpr.joinN)rrrF) rrrrrr!r"r%r&r(rrrrr)s rz('+)z^[a-zA-Z_][a-zA-Z0-9_.-]*$z^[^ \t\r\n\f]+$c @s�eZdZdZddddd�Zddd d d d d dd�ZdZdZdZdZ dZ e Z dfdd�Z dgdd�Zdd�Zedd��Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�Zdhd7d8�Zd9d:�Z d;d<�Z!d=d>�Z"d?d@�Z#dAdB�Z$dCdD�Z%dEdF�Z&dGdH�Z'dIdJ�Z(dKdL�Z)dMdN�Z*dOdP�Z+dQdR�Z,dSdT�Z-e-Z.e-Z/e-Z0e-Z1e-Z2e-Z3e-Z4e-Z5e-Z6dUdV�Z7dWdX�Z8dYdZ�Z9d[d\�Z:d]d^�Z;d_d`�Z<dadb�Z=dcdd�Z>deS)ir#z� Translator for "generic" XML documents. Everything is case-sensitive, no assumption is made on the meaning of element names and attribute names. Z descendantZchildZdirect_adjacentZindirect_adjacent)� �>�+�~�existsZequalsZincludesZ dashmatchZ prefixmatchZ suffixmatchZsubstringmatchZ different)r-�=z~=z|=z^=z$=z*=z!=�idzxml:langF�descendant-or-self::csd���fdd�t|�D��S)u�Translate a *group of selectors* to XPath. Pseudo-elements are not supported here since XPath only knows about "real" elements. :param css: A *group of selectors* as an Unicode string. :param prefix: This string is prepended to the XPath expression for each selector. The default makes selectors scoped to the context node’s subtree. :raises: :class:`SelectorSyntaxError` on invalid selectors, :class:`ExpressionError` on unknown/unsupported selectors, including pseudo-elements. :returns: The equivalent XPath 1.0 expression as an Unicode string. z | c3s|]}�j|�dd�VqdS)T)�translate_pseudo_elementsN)�selector_to_xpath)�.0�selector)�prefixrrr� <genexpr>�sz1GenericTranslator.css_to_xpath.<locals>.<genexpr>)r(r)rZcssr5r)r5rr� css_to_xpath�szGenericTranslator.css_to_xpathcCs`t|dd�}|std|f��|�|�}t||j�s8t�|rP|jrP|�||j�}|pVdt|�S)u4Translate a parsed selector to XPath. :param selector: A parsed :class:`Selector` object. :param prefix: This string is prepended to the resulting XPath expression. The default makes selectors scoped to the context node’s subtree. :param translate_pseudo_elements: Unless this is set to ``True`` (as :meth:`css_to_xpath` does), the :attr:`~Selector.pseudo_element` attribute of the selector is ignored. It is the caller's responsibility to reject selectors with pseudo-elements, or to account for them somehow. :raises: :class:`ExpressionError` on unknown/unsupported selectors. :returns: The equivalent XPath 1.0 expression as an Unicode string. Z parsed_treeNz"Expected a parsed selector, got %rr) r � TypeError�xpath� isinstance� xpathexpr_cls�AssertionError�pseudo_element�xpath_pseudo_elementr)rr4r5r1Ztreer9rrrr2�s   z#GenericTranslator.selector_to_xpathcCs td��dS)z�Translate a pseudo-element. Defaults to not supporting pseudo-elements at all, but can be overridden by sub-classes. z"Pseudo-elements are not supported.N)r)rr9r=rrrr>�sz&GenericTranslator.xpath_pseudo_elementcCsLt|�}d|krd|}n.d|kr,d|}ndd�dd�t|�D��}|S) N�'z'%s'�"z"%s"z concat(%s)�,cSs$g|]}|rd|krdpd|�qS)r?z"%s"z'%s'r)r3�partrrr� <listcomp>�sz3GenericTranslator.xpath_literal.<locals>.<listcomp>)rr(�split_at_single_quotes)�srrrr$�s  zGenericTranslator.xpath_literalcCs:t|�j}t|d|��d�}|dkr2td|��||�S)z%Translate any parsed selector object.zxpath_%sNz%s is not supported.)�typerr �lowerr)rZparsed_selectorZ type_name�methodrrrr9�s   zGenericTranslator.xpathcCs4|j|j}t|d|�}||�|j�|�|j��S)zTranslate a combined selector.zxpath_%s_combinator)�combinator_mapping� combinatorr r9r4� subselector)rZcombinedrJrHrrr�xpath_combinedselectors  z(GenericTranslator.xpath_combinedselectorcCsD|�|j�}|�|j�}|��|jr6|�d|j�S|�d�SdS)Nznot(%s)�0)r9r4rKr%rr")rZnegationr9Z sub_xpathrrr�xpath_negation s   z GenericTranslator.xpath_negationcCsBd|j�dd�}t||d�}|s0td|j��||�|j�|�S)z$Translate a functional pseudo-class.zxpath_%s_function�-�_Nz!The pseudo-class :%s() is unknown)r rrrr9r4)r�functionrHrrr�xpath_functions   z GenericTranslator.xpath_functioncCs@d|j�dd�}t||d�}|s0td|j��||�|j��S)zTranslate a pseudo-class.zxpath_%s_pseudorOrPNzThe pseudo-class :%s is unknown)Zidentrrrr9r4)rZpseudorHrrr� xpath_pseudos   zGenericTranslator.xpath_pseudocCs�|j|j}t|d|�}|jr,|j��}n|j}t|�}|jr\d|j|f}|oZt|j�}|rjd|}nd|�|�}|j r�|j ��}n|j }||� |j �||�S)z Translate an attribute selector.zxpath_attrib_%sz%s:%s�@zattribute::*[name() = %s]) �attribute_operator_mapping�operatorr �lower_case_attribute_names�attribrG� is_safe_name� namespacer$�lower_case_attribute_values�valuer9r4)rr4rVrHr �saferXr\rrr� xpath_attrib's     zGenericTranslator.xpath_attribcCs|�|j�}|�|d|j�S)zTranslate a class selector.z@class)r9r4�xpath_attrib_includes� class_name)rZclass_selectorr9rrr� xpath_class=s zGenericTranslator.xpath_classcCs|�|j�}|�|d|j�S)zTranslate an ID selector.z@id)r9r4�xpath_attrib_equalsr/)rZ id_selectorr9rrr� xpath_hashDs zGenericTranslator.xpath_hashcCsh|j}|sd}d}nt|�}|jr*|��}|jrLd|j|f}|oJt|j�}|j|d�}|sd|��|S)z'Translate a type or universal selector.rTz%s:%s)r)rrY�lower_case_element_namesrGrZr;r%)rr4rr]r9rrr� xpath_elementIs zGenericTranslator.xpath_elementcCs |�d|�S)z;right is a child, grand-child or further descendant of leftz/descendant-or-self::*/)r()r�left�rightrrr�xpath_descendant_combinator`sz-GenericTranslator.xpath_descendant_combinatorcCs |�d|�S)z#right is an immediate child of left�/)r()rrfrgrrr�xpath_child_combinatordsz(GenericTranslator.xpath_child_combinatorcCs|�d|�}|��|�d�S)z)right is a sibling immediately after leftz/following-sibling::zposition() = 1)r(r%r")rrfrgr9rrr� xpath_direct_adjacent_combinatorhs z2GenericTranslator.xpath_direct_adjacent_combinatorcCs |�d|�S)z1right is a sibling after left, immediately or notz/following-sibling::)r()rrfrgrrr�"xpath_indirect_adjacent_combinatornsz4GenericTranslator.xpath_indirect_adjacent_combinatorTc Csyt|j�\}}Wn"tk r4td|j��YnX|rB|��|��|dkrl|r^d|}|�d|�S|r|| }| }|dkr�t| �}n d| }|dkr�d||fg}ng}|dkr�|�d|�n|dkr�|r�|�d |�d � |�}|�r|�|�|S) NzInvalid series: '%r'rz last() - %szposition() = %sz+%s�z(position() %s) mod %s = 0zposition() >= %szposition() < (last() %s)z and ) r� arguments� ValueErrorrr%r&r"�str�appendr() rr9rQ�lastr%�a�bZb_neg�exprrrr�xpath_nth_child_functionus8     z*GenericTranslator.xpath_nth_child_functioncCs|j||dd�S)NT)rr)rv)rr9rQrrr�xpath_nth_last_child_function�sz/GenericTranslator.xpath_nth_last_child_functioncCs"|jdkrtd��|j||dd�S)Nrz"*:nth-of-type() is not implementedF)r%)rrrv)rr9rQrrr�xpath_nth_of_type_function�s  z,GenericTranslator.xpath_nth_of_type_functioncCs$|jdkrtd��|j||ddd�S)Nrz"*:nth-of-type() is not implementedTF)rrr%)rrrv)rr9rQrrr�xpath_nth_last_of_type_function�s   z1GenericTranslator.xpath_nth_last_of_type_functioncCsB|��dgdgfkr"td|j��|jdj}|�d|�|��S)N�STRING�IDENTz9Expected a single string or ident for :contains(), got %rrzcontains(., %s))�argument_typesrrnr\r"r$)rr9rQr\rrr�xpath_contains_function�s  z)GenericTranslator.xpath_contains_functioncCsB|��dgdgfkr"td|j��|jdj}|�d|�|��S)Nrzr{z5Expected a single string or ident for :lang(), got %rrzlang(%s))r|rrnr\r"r$)rr9rQr\rrr�xpath_lang_function�s  z%GenericTranslator.xpath_lang_functioncCs |�d�S)Nznot(parent::*))r")rr9rrr�xpath_root_pseudo�sz#GenericTranslator.xpath_root_pseudocCs|��|��|�d�S)Nzposition() = 1)r&r%r")rr9rrr�xpath_first_child_pseudo�sz*GenericTranslator.xpath_first_child_pseudocCs|��|��|�d�S)Nzposition() = last())r&r%r")rr9rrr�xpath_last_child_pseudo�sz)GenericTranslator.xpath_last_child_pseudocCs$|jdkrtd��|��|�d�S)Nrz"*:first-of-type is not implementedzposition() = 1)rrr&r")rr9rrr�xpath_first_of_type_pseudo�s  z,GenericTranslator.xpath_first_of_type_pseudocCs$|jdkrtd��|��|�d�S)Nrz!*:last-of-type is not implementedzposition() = last())rrr&r")rr9rrr�xpath_last_of_type_pseudo�s  z+GenericTranslator.xpath_last_of_type_pseudocCs|��|��|�d�S)Nz last() = 1)r%r&r")rr9rrr�xpath_only_child_pseudo�sz)GenericTranslator.xpath_only_child_pseudocCs|jdkrtd��|�d�S)Nrz!*:only-of-type is not implementedz last() = 1)rrr")rr9rrr�xpath_only_of_type_pseudo�s z+GenericTranslator.xpath_only_of_type_pseudocCs |�d�S)Nznot(*) and not(string-length()))r")rr9rrr�xpath_empty_pseudo�sz$GenericTranslator.xpath_empty_pseudocCs |�d�S)z:Common implementation for pseudo-classes that never match.rM)r")rr9rrr�pseudo_never_matches�sz&GenericTranslator.pseudo_never_matchescCs|rt�|�|�|S)N)r<r")rr9r r\rrr�xpath_attrib_exists�s z%GenericTranslator.xpath_attrib_existscCs|�d||�|�f�|S)Nz%s = %s)r"r$)rr9r r\rrrrbsz%GenericTranslator.xpath_attrib_equalscCs<|r |�d|||�|�f�n|�d||�|�f�|S)Nznot(%s) or %s != %sz%s != %s)r"r$)rr9r r\rrr�xpath_attrib_differents z(GenericTranslator.xpath_attrib_differentc Cs:t|�r,|�d|||�d|d�f�n |�d�|S)Nz:%s and contains(concat(' ', normalize-space(%s), ' '), %s)r)rM)�is_non_whitespacer"r$)rr9r r\rrrr_s  z'GenericTranslator.xpath_attrib_includesc Cs,|�d|||�|�||�|d�f�|S)Nz'%s and (%s = %s or starts-with(%s, %s))rO)r"r$)rr9r r\rrr�xpath_attrib_dashmatchs  z(GenericTranslator.xpath_attrib_dashmatchcCs.|r |�d|||�|�f�n |�d�|S)Nz%s and starts-with(%s, %s)rM)r"r$)rr9r r\rrr�xpath_attrib_prefixmatch#s  z*GenericTranslator.xpath_attrib_prefixmatchc Cs:|r,|�d|||t|�d|�|�f�n |�d�|S)Nz/%s and substring(%s, string-length(%s)-%s) = %srmrM)r"�lenr$)rr9r r\rrr�xpath_attrib_suffixmatch+s " z*GenericTranslator.xpath_attrib_suffixmatchcCs.|r |�d|||�|�f�n |�d�|S)Nz%s and contains(%s, %s)rM)r"r$)rr9r r\rrr�xpath_attrib_substringmatch5s  z-GenericTranslator.xpath_attrib_substringmatchN)r0)r0F)FT)?rrrrrIrUZ id_attribute�lang_attributerdrWr[rr;r7r2r>� staticmethodr$r9rLrNrRrSr^rarcrerhrjrkrlrvrwrxryr}r~rr�r�r�r�r�r�r�r��xpath_link_pseudoZxpath_visited_pseudoZxpath_hover_pseudoZxpath_active_pseudoZxpath_focus_pseudoZxpath_target_pseudo�xpath_enabled_pseudo�xpath_disabled_pseudo�xpath_checked_pseudor�rbr�r_r�r�r�r�rrrrr#gs�        (     r#c@sFeZdZdZdZddd�Zdd�Zdd �Zd d �Zd d �Z dd�Z dS)�HTMLTranslatora� Translator for (X)HTML documents. Has a more useful implementation of some pseudo-classes based on HTML-specific element names and attribute names, as described in the `HTML5 specification`_. It assumes no-quirks mode. The API is the same as :class:`GenericTranslator`. .. _HTML5 specification: http://www.w3.org/TR/html5/links.html#selectors :param xhtml: If false (the default), element names and attribute names are case-insensitive. ZlangFcCs||_|sd|_d|_dS)NT)�xhtmlrdrW)rr�rrrrRszHTMLTranslator.__init__cCs |�d�S)Nz�(@selected and name(.) = 'option') or (@checked and (name(.) = 'input' or name(.) = 'command')and (@type = 'checkbox' or @type = 'radio')))r")rr9rrrr�Ysz#HTMLTranslator.xpath_checked_pseudocCsP|��dgdgfkr"td|j��|jdj}|�d|j|�|��d�f�S)Nrzr{z5Expected a single string or ident for :lang(), got %rrz�ancestor-or-self::*[@lang][1][starts-with(concat(translate(@%s, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '-'), %s)]rO)r|rrnr\r"r�r$rG)rr9rQr\rrrr~as  z"HTMLTranslator.xpath_lang_functioncCs |�d�S)NzA@href and (name(.) = 'a' or name(.) = 'link' or name(.) = 'area'))r")rr9rrrr�osz HTMLTranslator.xpath_link_pseudocCs |�d�S)Na� ( @disabled and ( (name(.) = 'input' and @type != 'hidden') or name(.) = 'button' or name(.) = 'select' or name(.) = 'textarea' or name(.) = 'command' or name(.) = 'fieldset' or name(.) = 'optgroup' or name(.) = 'option' ) ) or ( ( (name(.) = 'input' and @type != 'hidden') or name(.) = 'button' or name(.) = 'select' or name(.) = 'textarea' ) and ancestor::fieldset[@disabled] ) )r")rr9rrrr�vsz$HTMLTranslator.xpath_disabled_pseudocCs |�d�S)Na' ( @href and ( name(.) = 'a' or name(.) = 'link' or name(.) = 'area' ) ) or ( ( name(.) = 'command' or name(.) = 'fieldset' or name(.) = 'optgroup' ) and not(@disabled) ) or ( ( (name(.) = 'input' and @type != 'hidden') or name(.) = 'button' or name(.) = 'select' or name(.) = 'textarea' or name(.) = 'keygen' ) and not (@disabled or ancestor::fieldset[@disabled]) ) or ( name(.) = 'option' and not( @disabled or ancestor::optgroup[@disabled] ) ) )r")rr9rrrr��sz#HTMLTranslator.xpath_enabled_pseudoN)F) rrrrr�rr�r~r�r�r�rrrrr�?s r�)N)r�sys�reZcssselect.parserrrr� version_infoZ basestringZ _basestringZunicoderrpr� RuntimeErrorr�objectr�compile�splitrD�matchrYr�r#r�rrrr�<module> s& 1   [