o
    V=ñ^ƒ=  ã                   @   sv  d dl mZmZmZ d dlmZ d dlZd dlmZm	Z	 ddl
mZmZmZ ddl
mZmZmZ ddlmZmZ d d	lmZ d
 e¡d Ze de d ¡Ze de d ¡Zi ZedƒdkZee ¡ ƒD ]5\Z Z!erqee!ƒdksyeszee!ƒdkrzqee!dkršee!ƒdkrŠe "e!¡Z!ne#e!ƒZ!e!evs–e  $¡ rše ee!< qedd„ Z%ede%ƒ ddd„Z&G dd„ de'ƒZ(G dd„ de)ƒZ*dS )é    )Úabsolute_importÚdivisionÚunicode_literals)Ú	text_typeN)Úregister_errorÚxmlcharrefreplace_errorsé   )ÚvoidElementsÚbooleanAttributesÚspaceCharacters)ÚrcdataElementsÚentitiesÚxmlEntities)ÚtreewalkersÚ_utils)ÚescapeÚ z"'=<>`ú[ú]u_    	
 /`Â áš€á Žá â€€â€â€‚â€ƒâ€„â€…â€†â€‡â€ˆâ€‰â€Šâ€¨â€©â€¯âŸã€€]u   ô¿¿é   ú&c           
   	   C   s  t | ttfƒr†g }g }d}t| j| j| j… ƒD ]7\}}|r!d}q|| j }t | j|t	| j|d gƒ… ¡rFt 
| j||d … ¡}d}nt|ƒ}| |¡ q|D ]+}t |¡}	|	rp| d¡ | |	¡ |	 d¡so| d¡ qR| dt|ƒdd …  ¡ qRd |¡| jfS t| ƒS )NFr   Tr   ú;z&#x%s;r   )Ú
isinstanceÚUnicodeEncodeErrorÚUnicodeTranslateErrorÚ	enumerateÚobjectÚstartÚendr   ZisSurrogatePairÚminÚsurrogatePairToCodepointÚordÚappendÚ_encode_entity_mapÚgetÚendswithÚhexÚjoinr   )
ÚexcÚresZ
codepointsÚskipÚiÚcÚindexZ	codepointÚcpÚe© r0   ú5/usr/lib/python3/dist-packages/html5lib/serializer.pyÚhtmlentityreplace_errors*   s2   
"




€r2   ÚhtmlentityreplaceÚetreec                 K   s(   t  |¡}tdi |¤Ž}| || ƒ|¡S )a˜  Serializes the input token stream using the specified treewalker

    :arg input: the token stream to serialize

    :arg tree: the treewalker to use

    :arg encoding: the encoding to use

    :arg serializer_opts: any options to pass to the
        :py:class:`html5lib.serializer.HTMLSerializer` that gets created

    :returns: the tree serialized as a string

    Example:

    >>> from html5lib.html5parser import parse
    >>> from html5lib.serializer import serialize
    >>> token_stream = parse('<html><body><p>Hi!</p></body></html>')
    >>> serialize(token_stream, omit_optional_tags=False)
    '<html><head></head><body><p>Hi!</p></body></html>'

    Nr0   )r   ZgetTreeWalkerÚHTMLSerializerÚrender)ÚinputZtreeÚencodingZserializer_optsZwalkerÚsr0   r0   r1   Ú	serializeK   s   
r:   c                   @   s~   e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZdZdZdZdZdZdd„ Zdd	„ Zd
d„ Zddd„Zddd„Zddd„ZdS )r5   Úlegacyú"TF)Úquote_attr_valuesÚ
quote_charÚuse_best_quote_charÚomit_optional_tagsÚminimize_boolean_attributesÚuse_trailing_solidusÚspace_before_trailing_solidusÚescape_lt_in_attrsÚescape_rcdataÚresolve_entitiesÚalphabetical_attributesÚinject_meta_charsetÚstrip_whitespaceÚsanitizec              
   K   sv   t |ƒt | jƒ }t|ƒdkrtdtt|ƒƒ ƒ‚d|v r d| _| jD ]}t| || |t	| |ƒ¡ƒ q#g | _
d| _dS )aB
  Initialize HTMLSerializer

        :arg inject_meta_charset: Whether or not to inject the meta charset.

            Defaults to ``True``.

        :arg quote_attr_values: Whether to quote attribute values that don't
            require quoting per legacy browser behavior (``"legacy"``), when
            required by the standard (``"spec"``), or always (``"always"``).

            Defaults to ``"legacy"``.

        :arg quote_char: Use given quote character for attribute quoting.

            Defaults to ``"`` which will use double quotes unless attribute
            value contains a double quote, in which case single quotes are
            used.

        :arg escape_lt_in_attrs: Whether or not to escape ``<`` in attribute
            values.

            Defaults to ``False``.

        :arg escape_rcdata: Whether to escape characters that need to be
            escaped within normal elements within rcdata elements such as
            style.

            Defaults to ``False``.

        :arg resolve_entities: Whether to resolve named character entities that
            appear in the source tree. The XML predefined entities &lt; &gt;
            &amp; &quot; &apos; are unaffected by this setting.

            Defaults to ``True``.

        :arg strip_whitespace: Whether to remove semantically meaningless
            whitespace. (This compresses all whitespace to a single space
            except within ``pre``.)

            Defaults to ``False``.

        :arg minimize_boolean_attributes: Shortens boolean attributes to give
            just the attribute value, for example::

              <input disabled="disabled">

            becomes::

              <input disabled>

            Defaults to ``True``.

        :arg use_trailing_solidus: Includes a close-tag slash at the end of the
            start tag of void elements (empty elements whose end tag is
            forbidden). E.g. ``<hr/>``.

            Defaults to ``False``.

        :arg space_before_trailing_solidus: Places a space immediately before
            the closing slash in a tag using a trailing solidus. E.g.
            ``<hr />``. Requires ``use_trailing_solidus=True``.

            Defaults to ``True``.

        :arg sanitize: Strip all unsafe or unknown constructs from output.
            See :py:class:`html5lib.filters.sanitizer.Filter`.

            Defaults to ``False``.

        :arg omit_optional_tags: Omit start/end tags that are optional.

            Defaults to ``True``.

        :arg alphabetical_attributes: Reorder attributes to be in alphabetical order.

            Defaults to ``False``.

        r   z2__init__() got an unexpected keyword argument '%s'r>   FN)Ú	frozensetÚoptionsÚlenÚ	TypeErrorÚnextÚiterr?   Úsetattrr$   ÚgetattrÚerrorsÚstrict)ÚselfÚkwargsZunexpected_argsÚattrr0   r0   r1   Ú__init__‡   s   O

zHTMLSerializer.__init__c                 C   ó&   t |tƒsJ ‚| jr| | jd¡S |S )Nr3   ©r   r   r8   Úencode©rU   Ústringr0   r0   r1   r[   à   ó   zHTMLSerializer.encodec                 C   rY   )NrT   rZ   r\   r0   r0   r1   ÚencodeStrictç   r^   zHTMLSerializer.encodeStrictNc                 c   sÌ   || _ d}g | _|r| jrddlm} |||ƒ}| jr&ddlm} ||ƒ}| jr3ddlm} ||ƒ}| j	r@ddl
m} ||ƒ}| jrMddlm} ||ƒ}|D ]}|d }|dkr«d|d  }|d rm|d	|d  7 }n|d
 ru|d7 }|d
 r |d
  d¡dkr“|d
  d¡dkr|  d¡ d}nd}|d||d
 |f 7 }|d7 }|  |¡V  qO|dv rÙ|dksµ|rÎ|rÅ|d  d¡dkrÅ|  d¡ |  |d ¡V  qO|  t|d ƒ¡V  qO|dv rç|d }	|  d|	 ¡V  |	tv rô| jsôd}n|rû|  d¡ |d  ¡ D ]Ä\\}
}}|}|}|  d¡V  |  |¡V  | jr/|t |	tƒ ¡vrÄ|t dtƒ ¡vrÄ|  d¡V  | jdksBt|ƒdkrEd}n | jdkrSt |¡d u}n| jd krat |¡d u}ntd!ƒ‚| d"d#¡}| j ru| d$d%¡}|r¾| j!}| j"r˜d|v rŒd|vrŒd}nd|v r˜d|vr˜d}|dkr¤| dd&¡}n| dd'¡}|  |¡V  |  |¡V  |  |¡V  q|  |¡V  q|	t#v rà| j$rà| j%rÚ|  d(¡V  n|  d)¡V  |  d¡V  qO|d*kr	|d }	|	tv rød}n|r |  d¡ |  d+|	 ¡V  qO|d,kr*|d }| d-¡dkr|  d.¡ |  d/|d  ¡V  qO|d0kr\|d }	|	d1 }|t&vrC|  d2|	 ¡ | j'rQ|t(vrQt&| }nd3|	 }|  |¡V  qO|  |d ¡ qOd S )4NFr   )ÚFilterÚtypeZDoctypez<!DOCTYPE %sÚnameZpublicIdz PUBLIC "%s"ZsystemIdz SYSTEMr<   r   ú'zBSystem identifier contains both single and double quote charactersz %s%s%sú>)Z
CharactersÚSpaceCharactersre   Údataz</zUnexpected </ in CDATA)ZStartTagZEmptyTagz<%sTz+Unexpected child element of a CDATA elementú r   ú=ÚalwaysÚspecr;   z?quote_attr_values must be one of: 'always', 'spec', or 'legacy'r   z&amp;ú<z&lt;z&#39;z&quot;z /ú/ZEndTagz</%s>ÚCommentz--zComment contains --z	<!--%s-->ZEntityr   zEntity %s not recognizedz&%s;))r8   rS   rH   Zfilters.inject_meta_charsetr`   rG   Zfilters.alphabeticalattributesrI   Zfilters.whitespacerJ   Zfilters.sanitizerr@   Zfilters.optionaltagsÚfindÚserializeErrorr_   r[   r   r   rE   ÚitemsrA   r
   r$   Útupler=   rM   Ú_quoteAttributeSpecÚsearchÚ_quoteAttributeLegacyÚ
ValueErrorÚreplacerD   r>   r?   r	   rB   rC   r   rF   r   )rU   Ú
treewalkerr8   Zin_cdatar`   Útokenra   Zdoctyper>   rb   Ú_Z	attr_nameZ
attr_valueÚkÚvZ
quote_attrrf   Úkeyr0   r0   r1   r:   î   sâ   €






ÿþ
€







”zHTMLSerializer.serializec                 C   s.   |rd  t|  ||¡ƒ¡S d  t|  |¡ƒ¡S )an  Serializes the stream from the treewalker into a string

        :arg treewalker: the treewalker to serialize

        :arg encoding: the string encoding to use

        :returns: the serialized tree

        Example:

        >>> from html5lib import parse, getTreeWalker
        >>> from html5lib.serializer import HTMLSerializer
        >>> token_stream = parse('<html><body>Hi!</body></html>')
        >>> walker = getTreeWalker('etree')
        >>> serializer = HTMLSerializer(omit_optional_tags=False)
        >>> serializer.render(walker(token_stream))
        '<html><head></head><body>Hi!</body></html>'

        ó    r   )r'   Úlistr:   )rU   rw   r8   r0   r0   r1   r6   w  s   zHTMLSerializer.renderúXXX ERROR MESSAGE NEEDEDc                 C   s   | j  |¡ | jrt‚d S ©N)rS   r"   rT   ÚSerializeError)rU   rf   r0   r0   r1   ro     s   ÿzHTMLSerializer.serializeErrorr€   )r   )Ú__name__Ú
__module__Ú__qualname__r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rL   rX   r[   r_   r:   r6   ro   r0   r0   r0   r1   r5   h   s.    Y
 

r5   c                   @   s   e Zd ZdZdS )r   zError in serialized treeN)r‚   rƒ   r„   Ú__doc__r0   r0   r0   r1   r   —  s    r   )r4   N)+Z
__future__r   r   r   Zsixr   ÚreÚcodecsr   r   Z	constantsr	   r
   r   r   r   r   r   r   r   Zxml.sax.saxutilsr   r'   Z_quoteAttributeSpecCharsÚcompilerr   rt   r#   rM   Z_is_ucs4r~   rp   rz   r{   r    r!   Úislowerr2   r:   r   r5   Ú	Exceptionr   r0   r0   r0   r1   Ú<module>   sH    
ÿ
ÿ
ÿ€

  1