<s> from __future__ import absolute_import , division , unicode_literals <EOL> from xml . dom . pulldom import START_ELEMENT , END_ELEMENT , COMMENT , IGNORABLE_WHITESPACE , CHARACTERS <EOL> from . import _base <EOL> from . . constants import voidElements <EOL> class TreeWalker ( _base . TreeWalker ) : <EOL> def __iter__ ( self ) : <EOL> ignore_until = None <EOL> previous = None <EOL> for event in self . tree : <EOL> if previous is not None and ( ignore_until is None or previous [ <NUM_LIT:1> ] is ignore_until ) : <EOL> if previous [ <NUM_LIT:1> ] is ignore_until : <EOL> ignore_until = None <EOL> for token in self . tokens ( previous , event ) : <EOL> yield token <EOL> if token [ "<STR_LIT:type>" ] == "<STR_LIT>" : <EOL> ignore_until = previous [ <NUM_LIT:1> ] <EOL> previous = event <EOL> if ignore_until is None or previous [ <NUM_LIT:1> ] is ignore_until : <EOL> for token in self . tokens ( previous , None ) : <EOL> yield token <EOL> elif ignore_until is not None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def tokens ( self , event , next ) : <EOL> type , node = event <EOL> if type == START_ELEMENT : <EOL> name = node . nodeName <EOL> namespace = node . namespaceURI <EOL> attrs = { } <EOL> for attr in list ( node . attributes . keys ( ) ) : <EOL> attr = node . getAttributeNode ( attr ) <EOL> attrs [ ( attr . namespaceURI , attr . localName ) ] = attr . value <EOL> if name in voidElements : <EOL> for token in self . emptyTag ( namespace , <EOL> name , <EOL> attrs , <EOL> not next or next [ <NUM_LIT:1> ] is not node ) : <EOL> yield token <EOL> else : <EOL> yield self . startTag ( namespace , name , attrs ) <EOL> elif type == END_ELEMENT : <EOL> name = node . nodeName <EOL> namespace = node . namespaceURI <EOL> if name not in voidElements : <EOL> yield self . endTag ( namespace , name ) <EOL> elif type == COMMENT : <EOL> yield self . comment ( node . nodeValue ) <EOL> elif type in ( IGNORABLE_WHITESPACE , CHARACTERS ) : <EOL> for token in self . text ( node . nodeValue ) : <EOL> yield token <EOL> else : <EOL> yield self . unknown ( type ) </s>
