@@ -300,12 +300,28 @@ def _in_document(node):
300300 node = node .parentNode
301301 return False
302302
303- def _write_data (writer , data ):
303+ def _write_data (writer , text , attr ):
304304 "Writes datachars to writer."
305- if data :
306- data = data .replace ("&" , "&" ).replace ("<" , "<" ). \
307- replace ("\" " , """ ).replace (">" , ">" )
308- writer .write (data )
305+ if not text :
306+ return
307+ # See the comments in ElementTree.py for behavior and
308+ # implementation details.
309+ if "&" in text :
310+ text = text .replace ("&" , "&" )
311+ if "<" in text :
312+ text = text .replace ("<" , "<" )
313+ if ">" in text :
314+ text = text .replace (">" , ">" )
315+ if attr :
316+ if '"' in text :
317+ text = text .replace ('"' , """ )
318+ if "\r " in text :
319+ text = text .replace ("\r " , " " )
320+ if "\n " in text :
321+ text = text .replace ("\n " , " " )
322+ if "\t " in text :
323+ text = text .replace ("\t " , "	" )
324+ writer .write (text )
309325
310326def _get_elements_by_tagName_helper (parent , name , rc ):
311327 for node in parent .childNodes :
@@ -883,7 +899,7 @@ def writexml(self, writer, indent="", addindent="", newl=""):
883899
884900 for a_name in attrs .keys ():
885901 writer .write (" %s=\" " % a_name )
886- _write_data (writer , attrs [a_name ].value )
902+ _write_data (writer , attrs [a_name ].value , True )
887903 writer .write ("\" " )
888904 if self .childNodes :
889905 writer .write (">" )
@@ -1112,7 +1128,7 @@ def splitText(self, offset):
11121128 return newText
11131129
11141130 def writexml (self , writer , indent = "" , addindent = "" , newl = "" ):
1115- _write_data (writer , "%s%s%s" % (indent , self .data , newl ))
1131+ _write_data (writer , "%s%s%s" % (indent , self .data , newl ), False )
11161132
11171133 # DOM Level 3 (WD 9 April 2002)
11181134
0 commit comments