o
    %e4                     @   s   d dl mZmZmZ d dlmZ ddlmZmZm	Z	 g dZ
ejZejZejZejZejZejZdZd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   L/var/www/html/venv/lib/python3.10/site-packages/html5lib/treewalkers/base.py__init__   s   
zTreeWalker.__init__c                 C      t NNotImplementedError)r   r   r   r   __iter__#      zTreeWalker.__iter__c                 C   
   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&      
zTreeWalker.errorFc                 c   s*    d|||dV  |r|  dV  dS 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

        StartTagr(   r   )r   r*   r)   r,   r   r   r   startTagE   
   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   s~    |}| t}|dt|t|  }|rd|dV  |}|t}|t|d }|r3d|dV  |r=d|dV  dS 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SpaceCharactersr!   
Characters)lstripr	   lenrstrip)r   r#   middleleftrightr   r   r   textd   s   

zTreeWalker.textc                 C   r   )zdGenerates a Comment token

        :arg data: the comment

        :returns: Comment token

        Commentr!   r   )r   r#   r   r   r   comment   r&   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)   r@   rA   r   r   r   doctype   r1   zTreeWalker.doctypec                 C   r   )zjGenerates an Entity token

        :arg name: the entity name

        :returns: an Entity token

        Entity)r"   r)   r   )r   r)   r   r   r   entity   r&   zTreeWalker.entityc                 C   s   |  d| S )zHandles unknown node typeszUnknown node type: r+   )r   nodeTyper   r   r   unknown   s   zTreeWalker.unknown)F)NN)__name__
__module____qualname____doc__r   r   r%   r.   r0   r3   r<   r>   rB   rD   rF   r   r   r   r   r      s    

&


r   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   r   r   r   r   noder   r   r   getNodeDetails   r   z%NonRecursiveTreeWalker.getNodeDetailsc                 C   r   r   r   rK   r   r   r   getFirstChild   r   z$NonRecursiveTreeWalker.getFirstChildc                 C   r   r   r   rK   r   r   r   getNextSibling   r   z%NonRecursiveTreeWalker.getNextSiblingc                 C   r   r   r   rK   r   r   r   getParentNode   r   z$NonRecursiveTreeWalker.getParentNodec                 c   s   | j }|d ur| |}|d |dd  }}d}|tkr%| j| V  ni|tkr5| j| D ]}|V  q.nY|tkre|\}}}}|rG|td kr\|tv r\| 	||||D ]}|V  qSd}n2| 
|||V  n)|tkrr| |d V  n|tkr| |d V  n|tkrd}n| |d V  |r| |}	nd }	|	d ur|	}nN|d ur| |}|d |dd  }}|tkr|\}}}}|r|td ks|tvr| ||V  | j |u rd }n| |}
|
d ur|
}n	| |}|d us|d usd S d S )Nr      FhtmlT)r   rM   r   rB   r   r<   r   r   r   r.   r0   r   r>   r   rD   r
   rF   rN   r3   rO   rP   )r   currentNodedetailsr"   r-   tokenr*   r)   
attributes
firstChildnextSiblingr   r   r   r      sd   





zNonRecursiveTreeWalker.__iter__N)rG   rH   rI   rM   rN   rO   rP   r   r   r   r   r   r      s    r   N)
__future__r   r   r   xml.domr   	constantsr   r   r	   __all__DOCUMENT_NODEr
   DOCUMENT_TYPE_NODEr   	TEXT_NODEr   ELEMENT_NODEr   COMMENT_NODEr   ENTITY_NODEr   r   joinobjectr   r   r   r   r   r   <module>   s    
 !