
    \dZ                        d Z ddlZddlmZmZ ddlmZ ddlmZ ddl	m
Z
mZ d Zd Zd	 Zd
Z ee          Ze                                 edz   Z ee          Ze                                 efdZefdZ G d de          Z G d d          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Zd Z  e             Z! G d d e          Z" G d! d"e          Z#d# Z$d$ Z% G d% d&e          Z&d' Z'd( Z(d) Z)d* Z* G d+ d,          Z+dS )-a  
Micro Document Object Model: a partial DOM implementation with SUX.

This is an implementation of what we consider to be the useful subset of the
DOM.  The chief advantage of this library is that, not being burdened with
standards compliance, it can remain very stable between versions.  We can also
implement utility 'pythonic' ways to access and mutate the XML tree.

Since this has not subjected to a serious trial by fire, it is not recommended
to use this outside of Twisted applications.  However, it seems to work just
fine for the documentation generator, which parses a fairly representative
sample of XML.

Microdom mainly focuses on working with HTML and XHTML.
    N)BytesIOStringIO)ioType)InsensitiveDict)
ParseError	XMLParserc                     g }|j         }| g}t          |          dk    rJ|                    d          }|j        |k    r ||           |j        |dd<   t          |          dk    J|S )a3  
    Return a list of all child elements of C{iNode} with a name matching
    C{name}.

    Note that this implementation does not conform to the DOM Level 1 Core
    specification because it may return C{iNode}.

    @param iNode: An element at which to begin searching.  If C{iNode} has a
        name matching C{name}, it will be included in the result.

    @param name: A C{str} giving the name of the elements to return.

    @return: A C{list} of direct or indirect child elements of C{iNode} with
        the name C{name}.  This may include C{iNode}.
    r   N)appendlenpopnodeName
childNodesiNodenamematchesmatches_appendslicecs         4lib/python3.11/site-packages/twisted/web/microdom.pygetElementsByTagNamer      sy      G^NGE
e**q..IIaLL:N1Lbqb		 e**q..
 N    c                 $   |                                 }g }|j        }| g}t          |          dk    r\|                    d          }|j                                         |k    r ||           |j        |d d<   t          |          dk    \|S Nr   )lowerr
   r   r   r   r   r   s         r   getElementsByTagNameNoCaser   :   s    ::<<DG^NGE
e**q..IIaLL:%%N1Lbqb		 e**q..
 Nr   c                 N     t                     t          k    r fd}n fd}|S )Nc                     t          | t                    r|                     d          }                     |            d S Nzutf-8)
isinstancestrencodewritesstreams    r   wz_streamWriteWrapper.<locals>.wJ   s9    !S!! &HHW%%LLOOOOOr   c                     t          | t                    r|                     d          }                     |            d S r   )r    bytesdecoder#   r$   s    r   r'   z_streamWriteWrapper.<locals>.wQ   s9    !U## &HHW%%LLOOOOOr   )r   r)   )r&   r'   s   ` r   _streamWriteWrapperr+   G   sR    f~~	 	 	 	 	 		 	 	 	 	
 Hr   ))&z&amp;)<z&lt;)>z&gt;)"z&quot;))'z&apos;c                 B    |D ]\  }}|                      ||          } | S )z1
    Perform the exact opposite of 'escape'.
    replacetextcharsr%   hs       r   unescaper8   h   1      " "1||Aq!!Kr   c                 B    |D ]\  }}|                      ||          } | S )z;
    Escape a few XML special chars with XML entities.
    r2   r4   s       r   escaper;   q   r9   r   c                        e Zd Zd ZdefdZdS )MismatchedTagsc                 n    |||||||f\  | _         | _        | _        | _        | _        | _        | _        d S N)filenameexpectgotbegLinebegColendLineendCol)selfr@   rA   rB   rE   rF   rC   rD   s           r   __init__zMismatchedTags.__init__{   sB     vsGVWfE	
MKHLKLKKKr   returnc                 f    d| j         d| j        d| j        d| j        d| j        d| j        S )Nzexpected </z	>, got </z> line: z col: z, began line: )rA   rB   rE   rF   rC   rD   rG   s    r   __str__zMismatchedTags.__str__   sD     
 		
r   N)__name__
__module____qualname__rH   r!   rL    r   r   r=   r=   z   sA        	F 	F 	F
 
 
 
 
 
 
r   r=   c                       e Zd Zd ZddZd Zddddi dfdZddddi dfdZdd
ZddZ	ddZ
d Zd Zd Zd Zd Zd Zd ZdS )NodeNc                 "    || _         g | _        d S r?   )
parentNoder   )rG   rT   s     r   rH   zNode.__init__   s    $r   c                     t          | j                  t          |j                  k    rdS t          | j        |j                  D ]\  }}|                    |          s dS dS )a  
        Compare this node to C{other}.  If the nodes have the same number of
        children and corresponding children are equal to each other, return
        C{True}, otherwise return C{False}.

        @type other: L{Node}
        @rtype: C{bool}
        FT)r   r   zipisEqualToNode)rG   otherabs       r   rW   zNode.isEqualToNode   sp     t3u'7#8#8885)9:: 	 	DAq??1%% uutr    r   c                     t                      r?   NotImplementedError)rG   r&   indent	addindentnewlstrip
nsprefixes	namespaces           r   writexmlzNode.writexml   s     "###r   c           	          t                      }|                     |||||||           |                                }|S r?   )r   re   getvalue)	rG   r_   r`   ra   rb   rc   rd   r%   rvs	            r   toxmlz
Node.toxml   s=     JJaD%YOOOZZ\\	r    
c                 4    |                      |||||          S r?   )re   )rG   r&   r_   r`   ra   rb   s         r   writeprettyxmlzNode.writeprettyxml   s    }}VVYeDDDr   c                 2    |                      ||||          S r?   )ri   )rG   r_   r`   ra   rb   s        r   toprettyxmlzNode.toprettyxml   s    zz&)T5999r   c                     t                      r?   r]   rG   deepparents      r   	cloneNodezNode.cloneNode   s    !###r   c                     | j         rdS dS )N   r   r   rK   s    r   hasChildNodeszNode.hasChildNodes   s    ? 	11r   c                     t          |t                    st          d          | j                            |           | |_        dS )z
        Make the given L{Node} the last child of this node.

        @param child: The L{Node} which will become a child of this node.

        @raise TypeError: If C{child} is not a C{Node} instance.
        expected Node instanceN)r    rR   	TypeErrorr   r
   rT   rG   childs     r   appendChildzNode.appendChild   sI     %&& 	64555u%%%r   c                     t          |t                    rt          |t                    st          d          | j                            |          }| |_        | j                            ||           |S )a  
        Make the given L{Node} C{new} a child of this node which comes before
        the L{Node} C{ref}.

        @param new: A L{Node} which will become a child of this node.

        @param ref: A L{Node} which is already a child of this node which
            C{new} will be inserted before.

        @raise TypeError: If C{new} or C{ref} is not a C{Node} instance.

        @return: C{new}
        rz   )r    rR   r{   r   indexrT   insert)rG   newrefis       r   insertBeforezNode.insertBefore   so     #t$$ 	6JsD,A,A 	64555O!!#&&q#&&&
r   c                     t          |t                    st          d          || j        v r!| j                            |           d|_        |S )a7  
        Remove the given L{Node} from this node's children.

        @param child: A L{Node} which is a child of this node which will no
            longer be a child of this node after this method is called.

        @raise TypeError: If C{child} is not a C{Node} instance.

        @return: C{child}
        rz   N)r    rR   r{   r   removerT   r|   s     r   removeChildzNode.removeChild   sU     %&& 	64555DO##O""5)))#Er   c                    t          |t                    rt          |t                    st          d          |j        | urt	          d          || j        | j                            |          <   d|_        | |_        dS )a  
        Replace a L{Node} which is already a child of this node with a
        different node.

        @param newChild: A L{Node} which will be made a child of this node.

        @param oldChild: A L{Node} which is a child of this node which will
            give up its position to C{newChild}.

        @raise TypeError: If C{newChild} or C{oldChild} is not a C{Node}
            instance.

        @raise ValueError: If C{oldChild} is not a child of this C{Node}.
        rz   z$oldChild is not a child of this nodeN)r    rR   r{   rT   
ValueErrorr   r   )rG   newChildoldChilds      r   replaceChildzNode.replaceChild  s     (D)) 	6Hd1K1K 	64555d**CDDD;C--h778""r   c                     | j         d         S Nrw   rK   s    r   	lastChildzNode.lastChild  s    r""r   c                 H    t          | j                  r| j        d         S d S r   )r   r   rK   s    r   
firstChildzNode.firstChild  s&    t 	&?1%%tr   r?   )r[   rj   rk   r   r   N)rM   rN   rO   r   rH   rW   re   ri   rm   ro   rt   rx   r~   r   r   r   r   r   rP   r   r   rR   rR      s1       H     & 
$ 
$ 
$ 
$ 2BaBRT   E E E E: : : :$ $ $ $         *  $# # #.# # #    r   rR   c                   z    e Zd ZddZddZdZd ZeZed             Z	d Z
ddddi dfd	Zd
 Zd Zd Zd Zd ZdS )DocumentNc                 l    t                               |            |r|                     |           d S d S r?   )rR   rH   r~   )rG   documentElements     r   rH   zDocument.__init__0  s@    d 	._-----	. 	.r   r   c                     t                      }| j        |_        |r| j                            d|           }n| j        }|                    |           |S Nrv   )r   doctyper   rt   r~   )rG   rr   rs   dnewEls        r   rt   zDocument.cloneNode5  sU    JJL	 	)(221d;;EE(E	er   c                 X    | j         |j         k    ot                              | |          S r?   )r   rR   rW   rG   ns     r   isEqualToDocumentzDocument.isEqualToDocumentA  s&    	)Jt/A/A$/J/JJr   c                     | j         d         S r   rw   rK   s    r   r   zDocument.documentElementF  s    q!!r   c                 h    | j         rt          d          t                              | |           dS )a  
        Make the given L{Node} the I{document element} of this L{Document}.

        @param child: The L{Node} to make into this L{Document}'s document
            element.

        @raise ValueError: If this document already has a document element.
        zOnly one element per document.N)r   r   rR   r~   r|   s     r   r~   zDocument.appendChildJ  s9     ? 	?=>>>u%%%%%r   r[   c           	          t          |          } |d|z              | j        r |d| j         d|            | j                            |||||||           d S )Nz<?xml version="1.0"?>z
<!DOCTYPE r.   )r+   r   r   re   	rG   r&   r_   r`   ra   rb   rc   rd   r'   s	            r   re   zDocument.writexmlW  s      ''	
!D
()))< 	2A04<00$00111%%FItUJ		
 	
 	
 	
 	
r   c                     t          |fi |S r?   )Element)rG   r   kws      r   createElementzDocument.createElementk  s    t""r"""r   c                      t          |          S r?   )TextrG   r5   s     r   createTextNodezDocument.createTextNoden  s    Dzzr   c                      t          |          S r?   )Commentr   s     r   createCommentzDocument.createCommentq  s    t}}r   c                 Z    | j         j        rt          | |          S t          | |          S r?   )r   caseInsensitiver   r   rG   r   s     r   r   zDocument.getElementsByTagNamet  s0    / 	:-dD999#D$///r   c                     | j         d d          }|re|                    d          }|j         r|                    |j                    t          |d          r|                    d          |k    r|S |cd S d S )Nr   getAttributeid)r   r   extendhasattrr   )rG   r   r   nodes       r   getElementByIdzDocument.getElementByIdy  s    _QQQ'
 	>>!$$D 3!!$/222t^,, 1B1B41H1HB1N1N  	 	 	 	 	r   r?   r   )rM   rN   rO   rH   rt   r   r   rW   propertyr   r~   re   r   r   r   r   r   rP   r   r   r   r   /  s        . . . .
    GK K K &M" " X"& & &  
 
 
 
(# # #    0 0 0
    r   r   c                   <    e Zd ZddZd ZeZddddi dfdZd	dZdS )
EntityReferenceNc                 r    t                               | |           || _        d|z   dz   x| _        | _        d S )Nr,   ;)rR   rH   eref	nodeValuedata)rG   r   rT   s      r   rH   zEntityReference.__init__  s8    dJ'''	%(4Z#%55r   c                 p    t          |t                    sdS | j        |j        k    o| j        |j        k    S r   )r    r   r   r   r   s     r   isEqualToEntityReferencez(EntityReference.isEqualToEntityReference  s6    !_-- 	1	QV#H$.AK*GHr   r[   r   c                 J    t          |          } |d| j        z              d S )Nr[   r+   r   r   s	            r   re   zEntityReference.writexml  s.      ''	"t~
r   c                 ,    t          | j        |          S r?   )r   r   rq   s      r   rt   zEntityReference.cloneNode  s    ty&111r   r?   r   )rM   rN   rO   rH   r   rW   re   rt   rP   r   r   r   r     s}        6 6 6 6
I I I
 -M
    2 2 2 2 2 2r   r   c                        e Zd ZddZd ZeZdS )CharacterDataNc                 f    t                               | |           |x| _        x| _        | _        d S r?   )rR   rH   valuer   r   )rG   r   rT   s      r   rH   zCharacterData.__init__  s/    dJ'''266
6TYr   c                 "    | j         |j         k    S r?   )r   r   s     r   isEqualToCharacterDataz$CharacterData.isEqualToCharacterData  s    zQW$$r   r?   )rM   rN   rO   rH   r   rW   rP   r   r   r   r     s8        7 7 7 7% % % +MMMr   r   c                   .    e Zd ZdZddddi dfdZddZdS )r   z
    A comment node.
    r[   r   c                 P    t          |          }| j        }	 |d|	 d           d S )Nz<!--z-->)r+   r   )
rG   r&   r_   r`   ra   rb   rc   rd   r'   vals
             r   re   zComment.writexml  s6      ''i	////r   Nc                 ,    t          | j        |          S r?   )r   r   rq   s      r   rt   zComment.cloneNode  s    t~v...r   r   )rM   rN   rO   __doc__re   rt   rP   r   r   r   r     sZ             / / / / / /r   r   c                   D    e Zd Zd
dZd ZddZddddi dfdZdefd	ZdS )r   Nr   c                 L    t                               | ||           || _        d S r?   )r   rH   raw)rG   r   rT   r   s       r   rH   zText.__init__  s%    tT:666r   c                 X    t                               | |          o| j        |j        k    S )z
        Compare this text to C{text}.  If the underlying values and the C{raw}
        flag are the same, return C{True}, otherwise return C{False}.
        )r   rW   r   rG   rX   s     r   rW   zText.isEqualToNode  s(    
 **477QDH	<QQr   c                 8    t          | j        || j                  S r?   )r   r   r   rq   s      r   rt   zText.cloneNode  s    DNFDH555r   r[   c                 p   t          |          }| j        r1| j        }	t          |	t                    st	          | j                  }	nc| j        }
t          |
t                    st	          |
          }
|r'd                    |
                                          }
t          |
          }	 ||	           d S )Nrj   )r+   r   r   r    r!   joinsplitr;   )rG   r&   r_   r`   ra   rb   rc   rd   r'   r   vs              r   re   zText.writexml  s      ''8 
	.Cc3'' *$.))Aa%% FF (HHQWWYY''))C	#r   rI   c                 6    dt          | j                  z  dz   S )NzText(%s))reprr   rK   s    r   __repr__zText.__repr__  s    4///#55r   r   r   )	rM   rN   rO   rH   rW   rt   re   r!   r   rP   r   r   r   r     s           R R R6 6 6 6    06# 6 6 6 6 6 6r   r   c                   *    e Zd ZddZddddi dfdZdS )CDATASectionr   Nc                 ,    t          | j        |          S r?   )r   r   rq   s      r   rt   zCDATASection.cloneNode  s    DNF333r   r[   c                 v    t          |          } |d            |d| j        z               |d           d S )Nz	<![CDATA[r[   z]]>r   r   s	            r   re   zCDATASection.writexml  sF      ''	+	"t~
	%r   r   )rM   rN   rO   rt   re   rP   r   r   r   r     sP        4 4 4 4      r   r   c               #   @   K   d} 	 dt          |           z   V  | dz   } )Nr   Tprv   )r!   )r   s    r   
_genprefixr     s4      	ACFFlEr   c                       e Zd ZdZdS )_Attrz#Support class for getAttributeNode.N)rM   rN   rO   r   rP   r   r   r   r     s        ))))r   r   c                       e Zd ZdZdZdZ	 	 	 	 	 	 	 ddZd Zd Zd Z	d Z
dd	Zd
 Zd ZddZddZd Zd Zd Zd Zddddi dfdZdefdZdefdZdS )r   r   rv   Nc	                    t                               | |           |p| | _        || _        |s|                                }|i | _        n=|| _        | j                                        D ]\  }	}
t          |
          | j        |	<   |rt          | j        |          | _        |x| _	        x| _
        | _        || _        || _        || _        d S )N)preserve)rR   rH   preserveCaser   r   
attributesitemsr8   r   
endTagNamer   tagName	_filename_markposrd   )rG   r   r   rT   r@   markposr   r   rd   kr   s              r   rH   zElement.__init__  s     	dJ'''(?,?. 	&mmooG DOO(DO--// 1 11%-a[["" 	V-doUUUDO9@@@$-$,!"r   c                 Z    | j         	|| _         d S | j                             |           d S r?   )rc   update)rG   pfxss     r   addPrefixeszElement.addPrefixes5  s1    ?""DOOOO""4(((((r   c                 J    | j         s|                                }|| _        d S r?   )r   r   r   )rG   r   s     r   endTagzElement.endTag;  s)      	,#))++J$r   c                     | j         rD| j        |j        k    o3| j                                        |j                                        k    S | j        |j        k    o| j        |j        k    S r?   )r   r   r   r   r   s     r   isEqualToElementzElement.isEqualToElement@  sf     	Oq|3 ##%%)9)9););; 1</Rdmqz6QRr   c                     | j                                         |j                                         k    o:| j        |j        k    o*| j        |j        k    ot                              | |          S )z
        Compare this element to C{other}.  If the C{nodeName}, C{namespace},
        C{attributes}, and C{childNodes} are all the same, return C{True},
        otherwise return C{False}.
        )r   r   rd   r   rR   rW   r   s     r   rW   zElement.isEqualToNodeG  sj     M!!U^%9%9%;%;; 0%/105#330 ""4//		
r   c                     t          | j        || j        | j        | j                  j                            | j                   |rfd| j        D             _        ng _        S )N)rT   rd   r   r   c                 <    g | ]}|                     d           S )rv   )rt   ).0r}   clones     r   
<listcomp>z%Element.cloneNode.<locals>.<listcomp>^  s'    WWWe5 9 9WWWr   )r   r   rd   r   r   r   r   r   )rG   rr   rs   r  s      @r   rt   zElement.cloneNodeT  s    Ln* 0
 
 
 	000 	"WWWWtWWWE!Er   c                 P    | j         rt          | |          S t          | |          S r?   )r   r   r   r   s     r   r   zElement.getElementsByTagNamec  s-     	:-dD999#D$///r   c                     dS r   rP   rK   s    r   hasAttributeszElement.hasAttributesh  s    qr   c                 8    | j                             ||          S r?   )r   get)rG   r   defaults      r   r   zElement.getAttributek  s    ""4111r   c                     ||f}|| j         v r| j         |         S || j        k    r| j                             ||          S |S r?   )r   rd   r  )rG   nsr   r	  nsks        r   getAttributeNSzElement.getAttributeNSn  sO    4j$/!!?3''?&&tW555r   c                 H    t          |                     |          |           S r?   )r   r   r   s     r   getAttributeNodezElement.getAttributeNodev  s     T&&t,,d333r   c                     || j         |<   d S r?   r   )rG   r   attrs      r   setAttributezElement.setAttributey  s     $r   c                 ,    || j         v r
| j         |= d S d S r?   r  r   s     r   removeAttributezElement.removeAttribute|  s'    4?""%%% #"r   c                     || j         v S r?   r  r   s     r   hasAttributezElement.hasAttribute  s    t&&r   r[   c           
         d}d}	d}
| j         s| j        | _        t          |          }| j        r8| j                                        }|                                D ]	}||v r||= 
ni }dg}| j        |	v r||g|z   }|j        fd}| j        }|| j        k    rj| j        c| j        |v r1|| j                 } |dz   | j        z              |dz   | j        z   }n9 | j                    |d| j                   | j        }n | j                   d	j	        }t          | j                                                  D ]l\  }}t          |t                    rB|\  }}||v r	||         }nt          t                     }|||<   |J  ||dz   |z   |           \|J  |||           m|rC|                                D ]\  }}|r |d
|z   |           |                    |           |}n|} | ||                     | j        r |d           ||z   }| j        D ]E}| j        |	v r| j        |
v r | |||f                     |                    |||||||           F| j        |	v r | |||f                      | |d|df                     dS | j                                        |vr | |d|df                     dS  |d           dS )a*  
        Serialize this L{Element} to the given stream.

        @param stream: A file-like object to which this L{Element} will be
            written.

        @param nsprefixes: A C{dict} mapping namespace URIs as C{str} to
            prefixes as C{str}.  This defines the prefixes which are already in
            scope in the document at the point at which this L{Element} exists.
            This is essentially an implementation detail for namespace support.
            Applications should not try to use it.

        @param namespace: The namespace URI as a C{str} which is the default at
            the point in the document at which this L{Element} exists.  This is
            essentially an implementation detail for namespace support.
            Applications should not try to use it.
        )imgbrhrbasemetalinkparamareainputcolbasefontisindexframe)htmlheadbodynoscriptinsdelh1h2h3h4h5h6scriptuloldlprer  
blockquoteaddressr   divfieldsettabletrformobjectr:  appletmap)r<  r3  r4  r'  r-   c                 >     d| dt          |          df          S )Nrj   z="r/   )r;   )_atr_valbexts     r   <lambda>z"Element.writexml.<locals>.<lambda>  s"    ttS$fTllC,P'Q'Q r   N:xmlnsr[   zxmlns:r.   z</z></ />)r   r   r   r+   rc   copykeysr   rd   r   sortedr   r   r    tuplenext	genprefixr   r   re   r   )rG   r&   r_   r`   ra   rb   rc   rd   ALLOWSINGLETONBLOCKELEMENTSFORMATNICELYr'   newprefixesr  begin	writeattrr   prefixjr  r   keydownprefixes	newindentr}   rD  s                            @r   re   zElement.writexml  s   8

@ 2   	+"lDO''? 	/..00K oo'' ( ($$#B( K<=((6NU*E|QQQQ	
 _
&&4>+E ~++ $DN3Vc\DL0111#c\DO;

 T\"""	'4>222 !N		 DG 5 5 7 788 	% 	%ID#$&& %C##'^FF!)__F&,KO	&3,,c2222	$$$$$ 	&)//11 5 5
F 5Ih/444z***&LL%L	!!E((? 	AcFFF*I  <=00T\\5Q5QAaay)**+++Iy$|Y    |},,!!T6N##$$$Aaaz3'(()))))\!!77Aaa
C())*****AeHHHHHr   rI   c                     dt          | j                  z  }| j        r|d| j        z  }| j        r|d| j        z  }| j        r|d| j        z  }|dz   S )Nz
Element(%sz, attributes=z, filename=z
, markpos=r   )r   r   r   r   r   )rG   reps     r   r   zElement.__repr__,  s}    T$-000? 	764?666C> 	43333C= 	21111CSyr   c                 `   d| j         z   }| j        s| j        r|dz  }| j        r|t          | j                  z  }| j        r|d| j        z  z  }| j        s| j        r|dz  }| j                                        D ]
}|d|z  z  }|                                 r|d| j         z  z  }n|dz  }|S )Nr-   z (z line %s column %sr   z %s=%rz
 >...</%s>rH  )r   r   r   r   r   r   rx   )rG   r[  items      r   rL   zElement.__str__6  s    DM!> 	T] 	4KC> 	(4'''C= 	8'$-77C> 	T] 	3JCO))++ 	# 	#D8d?"CC 	<$-//CC5LC
r   )NNNNrv   r   Nr   r?   )rM   rN   rO   r   r   rc   rH   r   r   r   rW   rt   r   r  r   r  r  r  r  r  re   r!   r   rL   rP   r   r   r   r     s       LOJ
 # # # #>) ) )% % %
S S S
 
 
   0 0 0
  2 2 2 2   4 4 4% % %& & &' ' ' g g g gR#          r   r   c                 b    i }|                                  D ]\  }}t          |          ||<   |S r?   )r   r8   r   ddr   r   s       r   _unescapeDictra  I  s8    	B		  11Ir   c                 H    i }|                                  D ]
\  }}|||<   |S r?   )r   r_  s       r   _reverseDictrc  P  s2    	B		  11Ir   c                       e Zd Zd                                Zddgddgddgdgg dg dg ddgdgd	gd
gdgdgddgdgdZdddeefdZd Zd Z e	j
        d          Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd S )!MicroDOMParserz#area link br img hr input base metar   dtr`  li)theadtfoottbodycolgroupr"  r<  tdthr(  r'  option)r   rf  r`  rg  rj  rh  ri  rk  r"  r<  rl  rm  r'  titlern  r   rv   c                     g | _         dd d}t          |          }|d |fg| _        g | _        d | _        || _        || _        |p| | _        || _        || _	        d S )NrG  )rG  r[   )
elementstackrc  nsstack	documents
_mddoctypebeExtremelyLenientr   r   soonCloserslaterClosers)rG   ru  r   r   rv  rw  r   drs           r   rH   zMicroDOMParser.__init__p  sw     4((!__D""4.(?,?&(r   c                     t          t          | j                            D ]8}| j        |          }|j        dk    s|                    dd          dk    r dS 9dS )Nr6  z	xml:spacer[   r   rv   r   )ranger   rq  r   r   )rG   edxels      r   shouldPreserveSpacez"MicroDOMParser.shouldPreserveSpace  sj    T.//00 	 	C"C4(BzU""book2&F&F*&T&Tqq 'Uqr   c                 .    | j         r| j         d         S d S r   )rq  rK   s    r   
_getparentzMicroDOMParser._getparent  s      	$R((4r   z\s*/[/*]\s*c                    | j         rt          |j                  dk    sd S |                                }t	          |t
                    rd}|j        }| j                            |          }|r+|	                                }|t          |          d          }	 t          d|z            j        d         }n# t          t          f$ r Y d S w xY wt          |j                  dk    rd S |                                }t	          |t          t          f          rNg |_        |r'|j                            t          |                     |j                            |           d S d S d S )Nrv   r[   z	<a>%s</a>r   )ru  r   r   r   r    r   r   COMMENTmatchgroupparseStringr   r=   r   r   r
   )rG   r|  r   rU  oldvaluer  es          r   _fixScriptElementz MicroDOMParser._fixScriptElement  st    & 	c"-.@.@A.E.EFMMOOa 	( FwHL&&x00E 3#CKKMM2h 677B1E/   1<  A%%A!lG455 ( " 7M((f666$$Q'''''3	( 	(*( (s   B; ;CCc                     || _         d S r?   )rt  )rG   r   s     r   
gotDoctypezMicroDOMParser.gotDoctype  s    !r   c                 d   |                                  }| j        rt          |t                    r|j        }|}| j        r(|                                }|                                }|| j                            |g           v r.| 	                    |j                   |                                  }t          |          }| j        d         d         }i }g }|                                D ]i\  }	}
|	                    d          rO|	                    dd          }t          |          dk    r|
||d         <   n|
|d<   |                    |	           j|D ]}	||	= |r)|                                }|                    |           g }|                                D ]_\  }	}
|	                    dd          }t          |          dk    r1|\  }}|dk    r&||v r"|
|||         |f<   |                    |	           `|D ]}	||	= t          |||| j        |                                 | j        | j        |                    d          	          }t-          |          }|                    |           |rW| j        d         d                                         }|                    |           | j                            |||f           | j                            |           |r|                    |           | j        r%|j        | j        v r| 	                    |           d S d S d S )
Nr   r   rG  rF  rv      r[   xml)r   r   rd   )r  ru  r    r   r   r   r   rw  r  	gotTagEndra  rr  r   
startswithr   r   r
   rI  r   r@   saveMarkr   rc  r   rq  r~   rv  )rG   r   r   rs   
parentNamemyName
namespaces	newspaceskeysToDeleter   r   
spacenamesksplitpfxtvr|  	revspacesrscopys                     r   gotTagStartzMicroDOMParser.gotTagStart  sh    """ 	+z&''B'B 	+JF# ('--//
*..z2>>>>v~...**":..
\"%a(
	$$&& 	' 	'DAq||G$$ 'WWS!__
z??a''/0Ijm,,$%IbM##A&&& 	 	A1 	)#**Ji((($$&& 	+ 	+DAqWWS!__F6{{a R%<<C:$5$567Jz#23 ''*** 	 	A1MMMOO 0* nnR((	
 	
 	
 !++	
y!!! 	:\"%a(--//FMM)$$$LR 8999  $$$ 	#r"""" 	!rzT5E'E'ENN4     	! 	!'E'Er   c                     |                                  } |||          }|r|                    |           d S | j        r| j                            |           d S d S r?   )r  r~   ru  rs  r
   )rG   factoryr   rs   tes        r   _gotStandalonezMicroDOMParser._gotStandalone  su    ""WT6"" 	&r"""""$ 	&N!!"%%%%%	& 	&r   c                     |                                 s|                                 r|                     t          |           d S d S r?   )rb   r}  r  r   rG   r   s     r   gotTextzMicroDOMParser.gotText  sK    ::<< 	,43355 	,d+++++	, 	,r   c                 <    |                      t          |           d S r?   )r  r   r  s     r   
gotCommentzMicroDOMParser.gotComment  s    GT*****r   c                 <    |                      t          |           d S r?   )r  r   )rG   	entityRefs     r   gotEntityReferencez!MicroDOMParser.gotEntityReference  s    OY77777r   c                 <    |                      t          |           d S r?   )r  r   )rG   cdatas     r   gotCDatazMicroDOMParser.gotCData	  s    L%00000r   c                    | j         s2| j        rd S t          | j        d|f|                                 z   dz    | j                                         }| j        d         d         }| j        d         d         |u r| j                                        }nd }| j        r.|j        	                                }|	                                }n	|j        }|}|
                    dd          }t          |          dk    rb|\  }}	|                    |d           }
|
E|j        |
k    r:| j        s3t          | j        |j        |f|                                 z   |j        z    ||k    s| j        r| j         r| j         d         }t          t          | j                             D ]B}| j         |dz             j        |k    r&| j         |dz                                 |            n9C| j                             |           || j                            |           d S | j         |dz    d = | j         s| j                            |           d S n3t          | j        |j        |f|                                 z   |j        z    |                    |           | j         s| j                            |           | j        r"|j        dk    r|                     |           d S d S d S )	NNOTHING)r   r   r   r  rv   rF  r   r2  )rq  ru  r=   r@   r  r   rr  r   r   r   r   r   r  rd   r   rz  r   r
   rs  r  )rG   r   r|  pfxdixnstupletncnamensplitr  newnamer  lastElidxs                r   r  zMicroDOMParser.gotTagEnd  s      	&  =)T2T]]__DvM  ""$$b!!$<A"$$l&&((GGG 	!!##BJJLLEEBEC##v;;!!LCC&&B~<2%%2 ,!%
D A"&--//!2"$+!.  e& $ !.q1F$S):%;%;<< 	 	,sQwZ8@EII -aj9@@FFF!E J
 )00444". L//888)C!G*,,7, --f555$}bj$7$--//IBKW  			$  	&N!!"%%%" 	'rzX'='=""2&&&&&	' 	''='=r   c                 "   t          j        | |           | j        rr| j        r'| j                            | j        d                    d S t          | j        | j        d         df|                                 z   | j        d         j	        z    d S )Nr   r   END_OF_FILE)
r   connectionLostrq  ru  rs  r
   r=   r@   r  r   )rG   reasons     r   r  zMicroDOMParser.connectionLostL  s     v... 
	& 	%%d&7&:;;;;;$(9"(=}M--//*+B/89 	
	 
	r   N)rM   rN   rO   r   rv  rw  rH   r}  r  recompiler  r  r  r  r  r  r  r  r  r  r  rP   r   r   re  re  W  s        8==??K4[TlTlf,,,,,,,,,Lwfff&!* L( !) ) ) )*     bj((G( ( (B" " "=! =! =!~& & &, , ,+ + +8 8 81 1 1>' >' >'@    r   re  c                    t          | d          st          | d          } t          |i |}t          | dd          |_        |                    d           t          | d          r(|                    |                                            nC|                     d          }|r,|                    |           |                     d          }|,|	                    d           |j
        st          |j        ddd	          |j        rt          |j
                  d
k    rI|j
        d         }t          |t                    s&t          d          }|                    |           |}n<t          d          }|j
        D ]}|                    |           n|j
        d         }t#          |          }|j        |_        |S )z%
    Parse HTML or XML readable.
    readrbr   z<xmlfile />Nrg   i   r   zNo top-level Nodes in documentrv   r&  )r   openre  getattrr@   makeConnectiondataReceivedrg   r  r  rs  r   ru  r   r    r   r~   r   rt  r   )	readableargskwargsmdprr   r|  r}   docs	            r   parser  [  s    8V$$ ($''
$
)&
)
)C8V];;CLtx$$ $**,,----MM$ 	$Qd##A  	$ t= Oq!-MNNN
 s}""a Aa)) V__q!!!A % %e$$$$% M!
1++C.CKJr   c                     t          | t                    r0t          t          |                     d                    g|R i |S t          t          |           g|R i |S )NzUTF-16)r    r!   r  r   r"   )str  r   s      r   r  r    sk    "c @WRYYx0011?D???B???*t***r***r   c                 &    t          | dd          S z'
    Parse an XML readable object.
    r   rv   r   r   )r  )r  s    r   parseXMLr    s     11====r   c                 &    t          | dd          S r  )r  )r  s    r   parseXMLStringr    s     r11====r   c                   :    e Zd ZdZddZd Zd Zd ZddZd	 Z	d
S )lmxz
    Easy creation of XML.
    r9  c                 \    t          |t                    rt          |          }|| _        d S r?   )r    r!   r   r   )rG   r   s     r   rH   zlmx.__init__  s*    dC   	!4==D			r   c                 H     d         dk    rt          d           fdS )Nr   _zno private attrsc                        j         fi | S r?   )add)r   r   rG   s    r   rE  z!lmx.__getattr__.<locals>.<lambda>  s    HDHT00R00 r   )AttributeErrorr   s   ``r   __getattr__zlmx.__getattr__  s2    7c>> !3444000000r   c                 <    | j                             ||           d S r?   )r   r  )rG   rW  r   s      r   __setitem__zlmx.__setitem__  s     	sC(((((r   c                 6    | j                             |          S r?   )r   r   )rG   rW  s     r   __getitem__zlmx.__getitem__  s    y%%c***r   r   c                 \    t          ||          }| j                            |           | S )N)r   )r   r   r~   )rG   txtr   nns       r   r5   zlmx.text  s/    #3	b!!!r   c                     t          |dd          }| j                            |           t          |          }|                                D ] \  }}|d         dk    r
|dd          }|||<   !|S )Nr   r  r  rv   )r   r   r~   r  r   )rG   r   r   newNodexfr   r   s          r   r  zlmx.add  s|    '11EEE	g&&&\\HHJJ 	 	DAqts{{abbEBqEE	r   N)r9  )r   )
rM   rN   rO   r   rH   r  r  r  r5   r  rP   r   r   r  r    s            
1 1 1
) ) )+ + +   
    r   r  ),r   r  ior   r   twisted.python.compatr   twisted.python.utilr   twisted.web.suxr   r   r   r   r+   HTML_ESCAPE_CHARSlistREV_HTML_ESCAPE_CHARSreverseXML_ESCAPE_CHARSREV_XML_ESCAPE_CHARSr8   r;   	Exceptionr=   rR   r   r   r   r   r   r   r   rN  r   r   ra  rc  re  r  r  r  r  r  rP   r   r   <module>r     s  
 " 
			                 ) ( ( ( ( ( / / / / / / 1 1 1 1 1 1 1 1  6
 
 
  &  .//       $'99 t,--        /     )    
 
 
 
 
Y 
 
 
6K K K K K K K KtQ Q Q Q Qt Q Q Qh2 2 2 2 2d 2 2 2<+ + + + +D + + +/ / / / /m / / /.(6 (6 (6 (6 (6= (6 (6 (6V    =   (   JLL	* * * * *M * * *v v v v vd v v vr	    A A A A AY A A AH$ $ $N+ + +> > >> > >" " " " " " " " " "r   