<s> from __future__ import absolute_import <EOL> from __future__ import unicode_literals <EOL> from . import util <EOL> ElementTree = util . etree . ElementTree <EOL> QName = util . etree . QName <EOL> if hasattr ( util . etree , '<STR_LIT>' ) : <EOL> Comment = util . etree . test_comment <EOL> else : <EOL> Comment = util . etree . Comment <EOL> PI = util . etree . PI <EOL> ProcessingInstruction = util . etree . ProcessingInstruction <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> HTML_EMPTY = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT:input>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" "<STR_LIT>" ) <EOL> try : <EOL> HTML_EMPTY = set ( HTML_EMPTY ) <EOL> except NameError : <EOL> pass <EOL> _namespace_map = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:html>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> def _raise_serialization_error ( text ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" % ( text , type ( text ) . __name__ ) <EOL> ) <EOL> def _encode ( text , encoding ) : <EOL> try : <EOL> return text . encode ( encoding , "<STR_LIT>" ) <EOL> except ( TypeError , AttributeError ) : <EOL> _raise_serialization_error ( text ) <EOL> def _escape_cdata ( text ) : <EOL> try : <EOL> if "<STR_LIT:&>" in text : <EOL> text = text . replace ( "<STR_LIT:&>" , "<STR_LIT>" ) <EOL> if "<STR_LIT:<>" in text : <EOL> text = text . replace ( "<STR_LIT:<>" , "<STR_LIT>" ) <EOL> if "<STR_LIT:>>" in text : <EOL> text = text . replace ( "<STR_LIT:>>" , "<STR_LIT>" ) <EOL> return text <EOL> except ( TypeError , AttributeError ) : <EOL> _raise_serialization_error ( text ) <EOL> def _escape_attrib ( text ) : <EOL> try : <EOL> if "<STR_LIT:&>" in text : <EOL> text = text . replace ( "<STR_LIT:&>" , "<STR_LIT>" ) <EOL> if "<STR_LIT:<>" in text : <EOL> text = text . replace ( "<STR_LIT:<>" , "<STR_LIT>" ) <EOL> if "<STR_LIT:>>" in text : <EOL> text = text . replace ( "<STR_LIT:>>" , "<STR_LIT>" ) <EOL> if "<STR_LIT>" in text : <EOL> text = text . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if "<STR_LIT:\n>" in text : <EOL> text = text . replace ( "<STR_LIT:\n>" , "<STR_LIT>" ) <EOL> return text <EOL> except ( TypeError , AttributeError ) : <EOL> _raise_serialization_error ( text ) <EOL> def _escape_attrib_html ( text ) : <EOL> try : <EOL> if "<STR_LIT:&>" in text : <EOL> text = text . replace ( "<STR_LIT:&>" , "<STR_LIT>" ) <EOL> if "<STR_LIT:<>" in text : <EOL> text = text . replace ( "<STR_LIT:<>" , "<STR_LIT>" ) <EOL> if "<STR_LIT:>>" in text : <EOL> text = text . replace ( "<STR_LIT:>>" , "<STR_LIT>" ) <EOL> if "<STR_LIT>" in text : <EOL> text = text . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> return text <EOL> except ( TypeError , AttributeError ) : <EOL> _raise_serialization_error ( text ) <EOL> def _serialize_html ( write , elem , qnames , namespaces , format ) : <EOL> tag = elem . tag <EOL> text = elem . text <EOL> if tag is Comment : <EOL> write ( "<STR_LIT>" % _escape_cdata ( text ) ) <EOL> elif tag is ProcessingInstruction : <EOL> write ( "<STR_LIT>" % _escape_cdata ( text ) ) <EOL> else : <EOL> tag = qnames [ tag ] <EOL> if tag is None : <EOL> if text : <EOL> write ( _escape_cdata ( text ) ) <EOL> for e in elem : <EOL> _serialize_html ( write , e , qnames , None , format ) <EOL> else : <EOL> write ( "<STR_LIT:<>" + tag ) <EOL> items = elem . items ( ) <EOL> if items or namespaces : <EOL> items . sort ( ) <EOL> for k , v in items : <EOL> if isinstance ( k , QName ) : <EOL> k = k . text <EOL> if isinstance ( v , QName ) : <EOL> v = qnames [ v . text ] <EOL> else : <EOL> v = _escape_attrib_html ( v ) <EOL> if qnames [ k ] == v and format == '<STR_LIT:html>' : <EOL> write ( "<STR_LIT>" % v ) <EOL> else : <EOL> write ( "<STR_LIT>" % ( qnames [ k ] , v ) ) <EOL> if namespaces : <EOL> items = namespaces . items ( ) <EOL> items . sort ( key = lambda x : x [ <NUM_LIT:1> ] ) <EOL> for v , k in items : <EOL> if k : <EOL> k = "<STR_LIT::>" + k <EOL> write ( "<STR_LIT>" % ( k , _escape_attrib ( v ) ) ) <EOL> if format == "<STR_LIT>" and tag in HTML_EMPTY : <EOL> write ( "<STR_LIT>" ) <EOL> else : <EOL> write ( "<STR_LIT:>>" ) <EOL> tag = tag . lower ( ) <EOL> if text : <EOL> if tag == "<STR_LIT>" or tag == "<STR_LIT>" : <EOL> write ( text ) <EOL> else : <EOL> write ( _escape_cdata ( text ) ) <EOL> for e in elem : <EOL> _serialize_html ( write , e , qnames , None , format ) <EOL> if tag not in HTML_EMPTY : <EOL> write ( "<STR_LIT>" + tag + "<STR_LIT:>>" ) <EOL> if elem . tail : <EOL> write ( _escape_cdata ( elem . tail ) ) <EOL> def _write_html ( root , <EOL> encoding = None , <EOL> default_namespace = None , <EOL> format = "<STR_LIT:html>" ) : <EOL> assert root is not None <EOL> data = [ ] <EOL> write = data . append <EOL> qnames , namespaces = _namespaces ( root , default_namespace ) <EOL> _serialize_html ( write , root , qnames , namespaces , format ) <EOL> if encoding is None : <EOL> return "<STR_LIT>" . join ( data ) <EOL> else : <EOL> return _encode ( "<STR_LIT>" . join ( data ) ) <EOL> def _namespaces ( elem , default_namespace = None ) : <EOL> qnames = { None : None } <EOL> namespaces = { } <EOL> if default_namespace : <EOL> namespaces [ default_namespace ] = "<STR_LIT>" <EOL> def add_qname ( qname ) : <EOL> try : <EOL> if qname [ : <NUM_LIT:1> ] == "<STR_LIT:{>" : <EOL> uri , tag = qname [ <NUM_LIT:1> : ] . split ( "<STR_LIT:}>" , <NUM_LIT:1> ) <EOL> prefix = namespaces . get ( uri ) <EOL> if prefix is None : <EOL> prefix = _namespace_map . get ( uri ) <EOL> if prefix is None : <EOL> prefix = "<STR_LIT>" % len ( namespaces ) <EOL> if prefix != "<STR_LIT>" : <EOL> namespaces [ uri ] = prefix <EOL> if prefix : <EOL> qnames [ qname ] = "<STR_LIT>" % ( prefix , tag ) <EOL> else : <EOL> qnames [ qname ] = tag <EOL> else : <EOL> if default_namespace : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> qnames [ qname ] = qname <EOL> except TypeError : <EOL> _raise_serialization_error ( qname ) <EOL> try : <EOL> iterate = elem . iter <EOL> except AttributeError : <EOL> iterate = elem . getiterator <EOL> for elem in iterate ( ) : <EOL> tag = elem . tag <EOL> if isinstance ( tag , QName ) and tag . text not in qnames : <EOL> add_qname ( tag . text ) <EOL> elif isinstance ( tag , util . string_type ) : <EOL> if tag not in qnames : <EOL> add_qname ( tag ) <EOL> elif tag is not None and tag is not Comment and tag is not PI : <EOL> _raise_serialization_error ( tag ) <EOL> for key , value in elem . items ( ) : <EOL> if isinstance ( key , QName ) : <EOL> key = key . text <EOL> if key not in qnames : <EOL> add_qname ( key ) <EOL> if isinstance ( value , QName ) and value . text not in qnames : <EOL> add_qname ( value . text ) <EOL> text = elem . text <EOL> if isinstance ( text , QName ) and text . text not in qnames : <EOL> add_qname ( text . text ) <EOL> return qnames , namespaces <EOL> def to_html_string ( element ) : <EOL> return _write_html ( ElementTree ( element ) . getroot ( ) , format = "<STR_LIT:html>" ) <EOL> def to_xhtml_string ( element ) : <EOL> return _write_html ( ElementTree ( element ) . getroot ( ) , format = "<STR_LIT>" ) </s>
