3
T]4              	   @   s   d dl mZmZmZ d dlmZ ddlmZmZm	Z	 ddddd	d
dddg	Z
ejZejZejZejZejZejZdZdje	Z	G dd deZG dd deZdS )    )absolute_importdivisionunicode_literals)Node   )
namespacesvoidElementsspaceCharactersDOCUMENTDOCTYPETEXTELEMENTCOMMENTENTITYUNKNOWN
TreeWalkerNonRecursiveTreeWalkerz<#UNKNOWN#> c               @   sl   e Zd ZdZdd Zdd Zdd Zdd	d
Zdd Zdd Z	dd Z
dd ZdddZdd Zdd ZdS )r   z}Walks a tree yielding tokens

    Tokens are dicts that all have a ``type`` field specifying the type of the
    token.

    c             C   s
   || _ dS )zCCreates a TreeWalker

        :arg tree: the tree to walk

        N)tree)selfr    r   F/tmp/pip-install-_1y4nff5/pip/pip/_vendor/html5lib/treewalkers/base.py__init__   s    zTreeWalker.__init__c             C   s   t d S )N)NotImplementedError)r   r   r   r   __iter__#   s    zTreeWalker.__iter__c             C   s
   d|dS )zGenerates an error token with the given message

        :arg msg: the error message

        :returns: SerializeError token

        SerializeError)typedatar   )r   msgr   r   r   error&   s    zTreeWalker.errorFc             c   s$   d|||dV  |r | j dV  dS )ar  Generates an EmptyTag token

        :arg namespace: the namespace of the token--can be ``None``

        :arg name: the name of the element

        :arg attrs: the attributes of the element as a dict

        :arg hasChildren: whether or not to yield a SerializationError because
            this tag shouldn't have children

        :returns: EmptyTag token

        EmptyTag)r   name	namespacer   zVoid element has childrenN)r   )r   r"   r!   attrshasChildrenr   r   r   emptyTag0   s
    
zTreeWalker.emptyTagc             C   s   d|||dS )zGenerates a StartTag token

        :arg namespace: the namespace of the token--can be ``None``

        :arg name: the name of the element

        :arg attrs: the attributes of the element as a dict

        :returns: StartTag token

        StartTag)r   r!   r"   r   r   )r   r"   r!   r#   r   r   r   startTagE   s    zTreeWalker.startTagc             C   s   d||dS )zGenerates an EndTag token

        :arg namespace: the namespace of the token--can be ``None``

        :arg name: the name of the element

        :returns: EndTag token

        EndTag)r   r!   r"   r   )r   r"   r!   r   r   r   endTagV   s    
zTreeWalker.endTagc             c   sx   |}|j t}|dt|t|  }|r6d|dV  |}|jt}|t|d }|rdd|dV  |rtd|dV  dS )at  Generates SpaceCharacters and Characters tokens

        Depending on what's in the data, this generates one or more
        ``SpaceCharacters`` and ``Characters`` tokens.

        For example:

            >>> from html5lib.treewalkers.base import TreeWalker
            >>> # Give it an empty tree just so it instantiates
            >>> walker = TreeWalker([])
            >>> list(walker.text(''))
            []
            >>> list(walker.text('  '))
            [{u'data': '  ', u'type': u'SpaceCharacters'}]
            >>> list(walker.text(' abc '))  # doctest: +NORMALIZE_WHITESPACE
            [{u'data': ' ', u'type': u'SpaceCharacters'},
            {u'data': u'abc', u'type': u'Characters'},
            {u'data': u' ', u'type': u'SpaceCharacters'}]

        :arg data: the text data

        :returns: one or more ``SpaceCharacters`` and ``Characters`` tokens

        NSpaceCharacters)r   r   
Characters)lstripr	   lenrstrip)r   r   Zmiddleleftrightr   r   r   textd   s    

zTreeWalker.textc             C   s
   d|dS )zdGenerates a Comment token

        :arg data: the comment

        :returns: Comment token

        Comment)r   r   r   )r   r   r   r   r   comment   s    zTreeWalker.commentNc             C   s   d|||dS )zGenerates a Doctype token

        :arg name:

        :arg publicId:

        :arg systemId:

        :returns: the Doctype token

        Doctype)r   r!   publicIdsystemIdr   )r   r!   r5   r6   r   r   r   doctype   s    zTreeWalker.doctypec             C   s
   d|dS )zjGenerates an Entity token

        :arg name: the entity name

        :returns: an Entity token

        Entity)r   r!   r   )r   r!   r   r   r   entity   s    zTreeWalker.entityc             C   s   | j d| S )zHandles unknown node typeszUnknown node type: )r   )r   ZnodeTyper   r   r   unknown   s    zTreeWalker.unknown)F)NN)__name__
__module____qualname____doc__r   r   r   r%   r'   r)   r1   r3   r7   r9   r:   r   r   r   r   r      s   

&


c               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )r   c             C   s   t d S )N)r   )r   noder   r   r   getNodeDetails   s    z%NonRecursiveTreeWalker.getNodeDetailsc             C   s   t d S )N)r   )r   r?   r   r   r   getFirstChild   s    z$NonRecursiveTreeWalker.getFirstChildc             C   s   t d S )N)r   )r   r?   r   r   r   getNextSibling   s    z%NonRecursiveTreeWalker.getNextSiblingc             C   s   t d S )N)r   )r   r?   r   r   r   getParentNode   s    z$NonRecursiveTreeWalker.getParentNodec             c   s  | j }x|d k	r| j|}|d |dd   }}d}|tkrN| j| V  n|tkrrx| j| D ]
}|V  qbW n|tkr|\}}}}| s|td kr|tkrx| j	||||D ]
}|V  qW d}n| j
|||V  nV|tkr| j|d V  n<|tkr| j|d V  n |tkrd}n| j|d V  |r@| j|}	nd }	|	d k	rT|	}q
x|d k	r| j|}|d |dd   }}|tkr|\}}}}|r|td ks|tkr| j||V  | j |krd }P | j|}
|
d k	r|
}P n
| j|}qVW q
W d S )Nr      FhtmlT)r   r@   r   r7   r   r1   r   r   r   r%   r'   r   r3   r   r9   r
   r:   rA   r)   rB   rC   )r   currentNodedetailsr   r$   tokenr"   r!   
attributesZ
firstChildZnextSiblingr   r   r   r      sZ    









zNonRecursiveTreeWalker.__iter__N)r;   r<   r=   r@   rA   rB   rC   r   r   r   r   r   r      s
   N)
__future__r   r   r   xml.domr   	constantsr   r   r	   __all__ZDOCUMENT_NODEr
   ZDOCUMENT_TYPE_NODEr   Z	TEXT_NODEr   ZELEMENT_NODEr   ZCOMMENT_NODEr   ZENTITY_NODEr   r   joinobjectr   r   r   r   r   r   <module>   s   
 !