ó
Y&=[c           @` s'  d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l Z d 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 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_literalsNi   (   t   basei   (   t   DataLossWarning(   t	   constants(   t   etree(   t	   _ihatexmlu   {([^}]*)}(.*)u   asdt   DocumentTypec           B` s   e  Z d  „  Z RS(   c         C` s   | |  _  | |  _ | |  _ d  S(   N(   t   namet   publicIdt   systemId(   t   selfR	   R
   R   (    (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyt   __init__#   s    		(   t   __name__t
   __module__R   (    (    (    s?   lib/python2.7/site-packages/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   (    (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyR   *   s    	c         C` s   |  j  j ƒ  j | j ƒ d  S(   N(   R   t   getroott   addnextt   _element(   R   t   element(    (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyt   appendChild.   s    c         C` s   |  j  S(   N(   R   (   R   (    (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyt   _getChildNodes1   s    (   R   R   R   R   R   t   propertyt
   childNodes(    (    (    s?   lib/python2.7/site-packages/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   nsR,   t   prefixt
   attributesR	   t   valuet   attr_stringt   child(   t   infosetFiltert   rvt   serializeElement(    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyRG   ;   st    		"($)	%
u   
(   R   t   InfosetFiltert   Truet   join(   R   (    (   RE   RF   RG   s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyt   testSerializer7   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   R,   R-   R.   R7   RJ   R8   R/   (   R   R<   R	   RB   t   attrRD   (   RF   RG   (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyRG   Š   s*    		2	u    (   RJ   (   R   (    (   RF   RG   s?   lib/python2.7/site-packages/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           ` s)   e  Z d ‡  f d  † Z ‡  f d †  Z RS(   c         ` s¤   | d  k r i  } n  | |  _ t j |  | ƒ xo |  j ƒ  D]a \ } } t | t ƒ rz d | d ˆ  j | d ƒ f } n ˆ  j | ƒ } | |  j j j | <q; Wd  S(   Nu   {%s}%si   i   (	   R   R   t   dictR   R8   R&   t   tuplet   coerceAttributeR7   (   R   R   RB   t   keyR	   (   RE   (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyR   ½   s    		$c         ` sl   t  j |  | | ƒ t | t ƒ rF d | d ˆ  j | d ƒ f } n ˆ  j | ƒ } | |  j j j | <d  S(   Nu   {%s}%si   i   (   RR   t   __setitem__R&   RS   RT   R   R7   (   R   RU   RB   R	   (   RE   (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyRV   É   s
    $N(   R   R   R   R   RV   (    (   RE   (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyRQ   ¼   s   t   Elementc           ` s•   e  Z ‡  ‡ ‡ f d  †  Z ‡ f d †  Z ‡ f d †  Z e e e ƒ Z d „  Z ‡  f d †  Z e e e ƒ Z	 d ‡ ‡ f d † Z ‡ f d †  Z RS(   c         ` s;   ˆ j  | ƒ } ˆ j j |  | d | ƒˆ  |  ƒ |  _ d  S(   Nt	   namespace(   t   coerceElementRW   R   t   _attributes(   R   R	   RX   (   RQ   t   builderRE   (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyR   Ò   s    c         ` s4   ˆ  j  | ƒ |  _ |  j |  j |  j ƒ |  j _ d  S(   N(   RY   t   _namet   _getETreeTagt
   _namespaceR   R,   (   R   R	   (   RE   (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyt   _setName×   s    c         ` s   ˆ  j  |  j ƒ S(   N(   R6   R\   (   R   (   RE   (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyt   _getNameÜ   s    c         S` s   |  j  S(   N(   RZ   (   R   (    (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyt   _getAttributesá   s    c         ` s   ˆ  |  | ƒ |  _  d  S(   N(   RZ   (   R   RA   (   RQ   (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyt   _setAttributesä   s    c         ` s)   ˆ j  | ƒ } ˆ  j j |  | | ƒ d  S(   N(   t   coerceCharactersRW   t
   insertText(   R   t   datat   insertBefore(   R[   RE   (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyRd   é   s    c         ` s   ˆ  j  j |  | ƒ d  S(   N(   RW   R   (   R   RD   (   R[   (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyR   í   s    N(   R   R   R   R_   R`   R   R	   Ra   Rb   RA   R   Rd   R   (    (   RQ   R[   RE   (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyRW   Ñ   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   coerceCommentRg   R   (   R   Re   (   R[   RE   (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyR   ñ   s    c         ` s   ˆ  j  | ƒ } | |  j _ d  S(   N(   Rh   R   R.   (   R   Re   (   RE   (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyt   _setDataõ   s    c         S` s
   |  j  j S(   N(   R   R.   (   R   (    (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyt   _getDataù   s    (   R   R   R   Ri   Rj   R   Re   (    (   R[   RE   (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyRg   ð   s   	(   R1   t   getETreeModuleR   R   RH   RI   RE   t   namespaceHTMLElementsRR   RW   Rg   t   elementClasst   commentClassR   RO   R   (   R   Rl   RP   RW   Rg   (    (   RQ   R[   RE   s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyR   ·   s    	%"		c         C` s2   t  j j |  ƒ |  j |  _ g  |  _ d  |  _ d  S(   N(   R   RO   t   resett   insertCommentInitialt   insertCommentt   initial_commentsR   RL   (   R   (    (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyRo     s    	c         C` s
   t  | ƒ S(   N(   RK   (   R   R   (    (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyRK   	  s    c         C` s$   t  r |  j j S|  j j j ƒ  Sd  S(   N(   RP   t   documentR   R   (   R   (    (    s?   lib/python2.7/site-packages/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   R.   R   t   extendt   listR/   (   R   t   fragmentR   (    (    s?   lib/python2.7/site-packages/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   RL   RE   RY   t   doctypeClass(   R   t   tokenR	   R
   R   t   coercedNameRL   (    (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyt   insertDoctype  s    


c         C` sM   | d  k s! | |  j k s! t ‚ |  j j d  k s9 t ‚ |  j j | ƒ d  S(   N(   R   Rs   R+   R   Rr   R   (   R   Re   t   parent(    (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyRp   ,  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(   Rs   R   R   R,   R-   Rz   R{   R   t   superRO   Rq   (   R   Re   R€   (    (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyt   insertCommentMain1  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(    RL   R	   R+   R
   R   R   RE   t   coercePubidt   findRz   R{   R   t   replaceR   t
   fromstringRr   Rn   t   addpreviousR   t   documentClassRs   t   getroottreeR   t   gett   defaultNamespaceR,   Rm   R   R   Ru   R‚   Rq   (   R   R}   t   docStrt   sysidt   roott   comment_tokent   commentR	   RX   t	   etree_tagt   root_element(    (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyt
   insertRoot7  sL    	 *


			N(   R   R   R   Rˆ   R   R|   R   Rm   Rn   t   fragmentClassR   t   implementationt   FalseR   Ro   RK   Rt   Ry   R   Rp   R‚   R“   (    (    (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyRO   ¯   s   L				
	(   t   __doc__t
   __future__R    R   R   Rz   t   reR)   t    R   R   R   R   R1   R   t
   lxml.etreeRI   RP   t   compileR2   Rg   R,   R-   t   objectR   R   RK   RN   RO   (    (    (    s?   lib/python2.7/site-packages/html5lib/treebuilders/etree_lxml.pyt   <module>
   s$   	O	)