ó
Ð²Ebc           @` su  d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l Z y d d l m	 Z	 Wn! e
 k
 r} d d l m	 Z	 n Xd d l m Z d d l m Z d d	 l m Z d d
 l m Z d d l m Z d d l j Z d d l m Z m Z e Z e j d ƒ Z e j d ƒ j Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ  d „  Z! d „  Z" d e j# f d „  ƒ  YZ# d S(   uÃ  Module for supporting the lxml.etree library. The idea here is to use as much
of the native library as possible, without using fragile hacks like custom element
names that break between releases. The downside of this is that we cannot represent
all possible trees; specifically the following are known to cause problems:

Text or comments as siblings of the root element
Docypes with no name

When any of these things occur, we emit a DataLossWarning
i    (   t   absolute_importt   divisiont   unicode_literalsN(   t   MutableMappingi   (   t   basei   (   t   DataLossWarning(   t	   constants(   t   etree(   t	   _ihatexml(   t   PY3t   binary_typeu   {([^}]*)}(.*)u   asdt   DocumentTypec           B` s   e  Z d  „  Z RS(   c         C` s   | |  _  | |  _ | |  _ d  S(   N(   t   namet   publicIdt   systemId(   t   selfR   R   R   (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt   __init__)   s    		(   t   __name__t
   __module__R   (    (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyR   (   s   t   Documentc           B` s/   e  Z d  „  Z d „  Z d „  Z e e ƒ Z RS(   c         C` s   d  |  _ g  |  _ d  S(   N(   t   Nonet   _elementTreet   _childNodes(   R   (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyR   0   s    	c         C` sC   |  j  j ƒ  } x |  j  j ƒ  j ƒ  D] } q% W| j | j ƒ d  S(   N(   R   t   getroott   itersiblingst   addnextt   _element(   R   t   elementt   last(    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt   appendChild4   s    c         C` s   |  j  S(   N(   R   (   R   (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt   _getChildNodes;   s    (   R   R   R   R   R   t   propertyt
   childNodes(    (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyR   /   s   			c         ` sJ   g  ‰ t  j d t ƒ ‰  d ‡  ‡ ‡ f d † ‰ ˆ |  d ƒ d j ˆ ƒ S(   Nt   preventDoubleDashCommentsi    c         ` sM  t  |  d ƒ s£t  |  d ƒ rˆ j d ƒ |  j j r© |  j j pL |  j j sb d |  j j } n% d |  j j |  j j |  j j f } ˆ j d d | d | f ƒ n  |  j ƒ  } x" | j ƒ  d  k	 rÙ | j ƒ  } q¸ WxÃ | d  k	 r	ˆ | | d ƒ | j
 ƒ  } qÝ WqIt |  t ƒ s+t |  t ƒ rqt |  t ƒ sSt j d	 d k sSt ‚ ˆ j d
 d | |  f ƒ qIˆ j d ƒ xÈ|  D] } ˆ | | d ƒ q…Wn¦|  j t k rˆ j d d | |  j f ƒ t  |  d ƒ rI|  j rIˆ j d
 d | |  j f ƒ qIn=t |  t j ƒ s$t ‚ t j j |  j ƒ } | d  k	 rš| j d ƒ } | j d ƒ } t j | } ˆ j d d | | ˆ  j | ƒ f ƒ n' ˆ j d d | ˆ  j |  j ƒ f ƒ t  |  d ƒ r¶g  } x› |  j j ƒ  D]Š \ }	 }
 t j |	 ƒ } | d  k	 rN| j  ƒ  \ } }	 ˆ  j |	 ƒ }	 t j | } d | |	 f } n ˆ  j |	 ƒ } | j | |
 f ƒ qæWx? t! | ƒ D]. \ }	 }
 ˆ j d d | d |	 |
 f ƒ qWn  |  j räˆ j d
 d | d |  j f ƒ n  | d 7} x |  D] } ˆ | | ƒ qõWt  |  d ƒ rI|  j rIˆ j d
 d | d |  j f ƒ n  d  S(   Nu   tagu   getrootu	   #documentu   <!DOCTYPE %s>u   <!DOCTYPE %s "%s" "%s">u   |%s%su    i   i    u   |%s"%s"u   #document-fragmentu   |%s<!-- %s -->u   taili   u
   |%s<%s %s>u   |%s<%s>u   attribu   %s %su
   |%s%s="%s"("   t   hasattrt   appendt   docinfot   internalDTDt	   public_idt
   system_urlt	   root_nameR   t   getpreviousR   t   getnextt
   isinstancet   strt   bytest   syst   version_infot   AssertionErrort   tagt   comment_typet   textt   tailR   t   _Elementt   etree_builderst
   tag_regexpt   matcht   groupR   t   prefixest   fromXmlNamet   attribt   itemst   groupst   sorted(   R   t   indentt   dtd_strt   next_elementt   nsmatcht   nsR1   t   prefixt
   attributesR   t   valuet   attr_stringt   child(   t   infosetFiltert   rvt   serializeElement(    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyRL   E   st    		"($)	%
u   
(   R   t   InfosetFiltert   Truet   join(   R   (    (   RJ   RK   RL   sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt   testSerializerA   s
    Fc         ` s/   g  ‰  ‡  ‡ f d †  ‰ ˆ |  ƒ d j  ˆ  ƒ S(   u4   Serialize an element and its child nodes to a stringc         ` s‡  t  |  d ƒ si |  j j rV |  j j r6 |  j j } n d |  j j } ˆ  j | ƒ n  ˆ |  j ƒ  ƒ nï |  j t k r’ ˆ  j d |  j	 f ƒ nÆ |  j
 sµ ˆ  j d |  j f ƒ nU d j g  |  j
 j ƒ  D] \ } } d | | f ^ qË ƒ } ˆ  j d |  j | f ƒ |  j	 r&ˆ  j |  j	 ƒ n  x |  D] } ˆ | ƒ q-Wˆ  j d |  j f ƒ t  |  d	 ƒ rƒ|  j rƒˆ  j |  j ƒ n  d  S(
   Nu   tagu   <!DOCTYPE %s>u	   <!--%s-->u   <%s>u    u   %s="%s"u   <%s %s>u   </%s>u   tail(   R"   R$   R%   t   doctypeR(   R#   R   R1   R2   R3   R<   RO   R=   R4   (   R   RA   R   RG   t   attrRI   (   RK   RL   (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyRL   ”   s*    		2	u    (   RO   (   R   (    (   RK   RL   sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt   tostring   s     
t   TreeBuilderc           B` s†   e  Z e Z e Z d	 Z d	 Z e Z	 e
 Z e d  „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d	 d „ Z d	 d „ Z d „  Z RS(
   c         ` sÃ   t  j t d | ƒ‰ t j d t ƒ ‰ |  _ | |  _ d t f ‡ f d †  ƒ  Y‰  d ˆ j	 f ‡  ‡ ‡ f d †  ƒ  Y} d ˆ j
 f ‡ ‡ f d †  ƒ  Y} | |  _ | |  _ t j j |  | ƒ d  S(	   Nt   fullTreeR!   t
   Attributesc           ` sV   e  Z d  „  Z ‡  f d †  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   c         S` s   | |  _  d  S(   N(   R   (   R   R   (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyR   Ç   s    c         ` sF   t  | t ƒ r3 d | d ˆ  j | d ƒ f } n ˆ  j | ƒ } | S(   Nu   {%s}%si   i   (   R+   t   tuplet   coerceAttribute(   R   t   keyR   (   RJ   (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt
   _coerceKeyÊ   s    $c         S` sH   |  j  j  j |  j | ƒ } t rD t | t ƒ rD | j d ƒ } n  | S(   Nu   ascii(   R   R<   RZ   R	   R+   R
   t   decode(   R   RY   RG   (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt   __getitem__Ñ   s    c         S` s    | |  j  j  j |  j | ƒ <d  S(   N(   R   R<   RZ   (   R   RY   RG   (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt   __setitem__×   s    c         S` s   |  j  j  j |  j | ƒ =d  S(   N(   R   R<   RZ   (   R   RY   (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt   __delitem__Ú   s    c         S` s   t  |  j j j ƒ S(   N(   t   iterR   R<   (   R   (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt   __iter__Ý   s    c         S` s   t  |  j j j ƒ S(   N(   t   lenR   R<   (   R   (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt   __len__à   s    c         S` s   |  j  j  j j ƒ  S(   N(   R   R<   t   clear(   R   (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyRc   ã   s    (
   R   R   R   RZ   R\   R]   R^   R`   Rb   Rc   (    (   RJ   (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyRV   Æ   s   						t   Elementc           ` s‰   e  Z ‡  ‡ ‡ f d  †  Z ‡ f d †  Z ‡ f d †  Z e e e ƒ Z d „  Z d „  Z e e e ƒ Z	 d ‡ ‡ f d † Z d „  Z RS(   c         ` s;   ˆ j  | ƒ } ˆ j j |  | d | ƒˆ  |  ƒ |  _ d  S(   Nt	   namespace(   t   coerceElementRd   R   t   _attributes(   R   R   Re   (   RV   t   builderRJ   (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyR   ç   s    c         ` s4   ˆ  j  | ƒ |  _ |  j |  j |  j ƒ |  j _ d  S(   N(   Rf   t   _namet   _getETreeTagt
   _namespaceR   R1   (   R   R   (   RJ   (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt   _setNameì   s    c         ` s   ˆ  j  |  j ƒ S(   N(   R;   Ri   (   R   (   RJ   (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt   _getNameñ   s    c         S` s   |  j  S(   N(   Rg   (   R   (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt   _getAttributesö   s    c         S` s$   |  j  } | j ƒ  | j | ƒ d  S(   N(   RF   Rc   t   update(   R   RG   RF   (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt   _setAttributesù   s    	
c         ` s)   ˆ j  | ƒ } ˆ  j j |  | | ƒ d  S(   N(   t   coerceCharactersRd   t
   insertText(   R   t   datat   insertBefore(   Rh   RJ   (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyRr      s    c         S` sG   t  |  ƒ |  j |  j ƒ } |  j j rC | j j j |  j j ƒ n  | S(   N(   t   typeR   Re   R   R<   Ro   (   R   R   (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt	   cloneNode  s    N(   R   R   R   Rl   Rm   R   R   Rn   Rp   RF   R   Rr   Rv   (    (   RV   Rh   RJ   (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyRd   æ   s   		t   Commentc           ` sA   e  Z ‡  ‡ f d  †  Z ‡ f d †  Z d „  Z e e e ƒ Z RS(   c         ` s&   ˆ j  | ƒ } ˆ  j j |  | ƒ d  S(   N(   t   coerceCommentRw   R   (   R   Rs   (   Rh   RJ   (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyR     s    c         ` s   ˆ  j  | ƒ } | |  j _ d  S(   N(   Rx   R   R3   (   R   Rs   (   RJ   (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt   _setData  s    c         S` s
   |  j  j S(   N(   R   R3   (   R   (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt   _getData  s    (   R   R   R   Ry   Rz   R   Rs   (    (   Rh   RJ   (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyRw   
  s   	(   R6   t   getETreeModuleR   R   RM   RN   RJ   t   namespaceHTMLElementsR   Rd   Rw   t   elementClasst   commentClassR   RT   R   (   R   R|   RU   Rd   Rw   (    (   RV   Rh   RJ   sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyR   Á   s    	 %$"		c         C` s2   t  j j |  ƒ |  j |  _ g  |  _ d  |  _ d  S(   N(   R   RT   t   resett   insertCommentInitialt   insertCommentt   initial_commentsR   RQ   (   R   (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyR     s    	c         C` s
   t  | ƒ S(   N(   RP   (   R   R   (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyRP   #  s    c         C` s$   t  r |  j j S|  j j j ƒ  Sd  S(   N(   RU   t   documentR   R   (   R   (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt   getDocument&  s    
c         C` se   g  } |  j  d j } | j r2 | j | j ƒ n  | j t | ƒ ƒ | j ra | j | j ƒ n  | S(   Ni    (   t   openElementsR   R3   R#   t   extendt   listR4   (   R   t   fragmentR   (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt   getFragment,  s    		c         C` s“   | d } | d } | d } | s@ t  j d t ƒ d  |  _ nO |  j j | ƒ } | | k rq t  j d t ƒ n  |  j | | | ƒ } | |  _ d  S(   Nu   nameu   publicIdu   systemIdu#   lxml cannot represent empty doctypeu%   lxml cannot represent non-xml doctype(   t   warningst   warnR   R   RQ   RJ   Rf   t   doctypeClass(   R   t   tokenR   R   R   t   coercedNameRQ   (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt   insertDoctype6  s    


c         C` sM   | d  k s! | |  j k s! t ‚ |  j j d  k s9 t ‚ |  j j | ƒ d  S(   N(   R   Rƒ   R0   R   R‚   R#   (   R   Rs   t   parent(    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyR€   F  s    !c         C` s^   | |  j  k rA |  j  j j ƒ  d j t k rA t j d t ƒ n  t t	 |  ƒ j
 | | ƒ d  S(   Niÿÿÿÿu@   lxml cannot represent adjacent comments beyond the root elements(   Rƒ   R   R   R1   R2   RŠ   R‹   R   t   superRT   R   (   R   Rs   R   (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt   insertCommentMainK  s    c         C` sl  d } |  j  r`|  j  j s! t ‚ | d |  j  j 7} |  j  j d  k	 sY |  j  j d  k	 r*| d |  j j |  j  j pw d ƒ 7} |  j  j r|  j  j } | j d ƒ d k ræ | j d ƒ d k ræ t	 j
 d t ƒ | j d d ƒ } n  | j d ƒ d k r| d	 | 7} q'| d
 | 7} q*| d 7} n  | d 7} |  j  j | d k r`t	 j
 d t ƒ q`n  | d 7} t j | ƒ } x4 |  j D]) } |  j | d ƒ } | j | j ƒ qƒW|  j ƒ  |  _ | j ƒ  |  j _ | d } | j d |  j ƒ } | d  k r| }	 n d | | f }	 |	 | _ |  j | | ƒ }
 | |
 _ |  j j j |
 ƒ |  j j |
 ƒ |  j |  _ d  S(   Nu    u   <!DOCTYPE %su    PUBLIC "%s" u   'i    u   "u6   DOCTYPE system cannot contain single and double quotesu   U00027u   "%s"u   '%s'u   ''u   >u   nameuG   lxml cannot represent doctype with a different name to the root elementu$   <THIS_SHOULD_NEVER_APPEAR_PUBLICLY/>u   datau	   namespaceu   {%s}%s(    RQ   R   R0   R   R   R   RJ   t   coercePubidt   findRŠ   R‹   R   t   replaceR   t
   fromstringR‚   R~   t   addpreviousR   t   documentClassRƒ   t   getroottreeR   t   gett   defaultNamespaceR1   R}   R   R#   R…   R’   R   (   R   R   t   docStrt   sysidt   roott   comment_tokent   commentR   Re   t	   etree_tagt   root_element(    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt
   insertRootQ  sL    	 *


			N(   R   R   R   R˜   R   RŒ   R   R}   R~   t   fragmentClassR   t   implementationt   FalseR   R   RP   R„   R‰   R   R€   R’   R£   (    (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyRT   ¹   s   \				
	($   t   __doc__t
   __future__R    R   R   RŠ   t   reR.   t   collections.abcR   t   ImportErrort   collectionst    R   R   R   R   R6   R   t
   lxml.etreet   pip._vendor.sixR	   R
   RN   RU   t   compileR7   Rw   R1   R2   t   objectR   R   RP   RS   RT   (    (    (    sI   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/treebuilders/etree_lxml.pyt   <module>
   s.   	O	)