File tree Expand file tree Collapse file tree 4 files changed +22
-9
lines changed Expand file tree Collapse file tree 4 files changed +22
-9
lines changed Original file line number Diff line number Diff line change @@ -412,6 +412,15 @@ customization consistently by always using the value specified by
412
412
case), and one used ``__VENV_NAME__ `` instead.
413
413
(Contributed by Brett Cannon in :issue: `37663 `.)
414
414
415
+ xml
416
+ ---
417
+
418
+ White space characters within attributes are now preserved when serializing
419
+ :mod: `xml.etree.ElementTree ` to XML file. EOLNs are no longer normalized
420
+ to "\n ". This is the result of discussion about how to interpret
421
+ section 2.11 of XML spec.
422
+ (Contributed by Mefistotelis in :issue: `39011 `.)
423
+
415
424
416
425
Optimizations
417
426
=============
Original file line number Diff line number Diff line change @@ -430,13 +430,14 @@ def test_attrib(self):
430
430
self .assertEqual (ET .tostring (elem ),
431
431
b'<test testa="testval" testb="test1" testc="test2">aa</test>' )
432
432
433
+ # Test preserving white space chars in attributes
433
434
elem = ET .Element ('test' )
434
435
elem .set ('a' , '\r ' )
435
436
elem .set ('b' , '\r \n ' )
436
437
elem .set ('c' , '\t \n \r ' )
437
- elem .set ('d' , '\n \n ' )
438
+ elem .set ('d' , '\n \n \r \r \t \t ' )
438
439
self .assertEqual (ET .tostring (elem ),
439
- b'<test a="
 ;" b=" " c="	 
 ; " d=" " />' )
440
+ b'<test a="
 ;" b=" &# 10;" c="	 
 ; " d=" 		 " />' )
440
441
441
442
def test_makeelement (self ):
442
443
# Test makeelement handling.
Original file line number Diff line number Diff line change @@ -1057,15 +1057,15 @@ def _escape_attrib(text):
1057
1057
text = text .replace (">" , ">" )
1058
1058
if "\" " in text :
1059
1059
text = text .replace ("\" " , """ )
1060
- # The following business with carriage returns is to satisfy
1061
- # Section 2.11 of the XML specification, stating that
1062
- # CR or CR LN should be replaced with just LN
1060
+ # Although section 2.11 of the XML specification states that CR or
1061
+ # CR LN should be replaced with just LN, it applies only to EOLNs
1062
+ # which take part of organizing file into lines. Within attributes,
1063
+ # we are replacing these with entity numbers, so they do not count.
1063
1064
# http://www.w3.org/TR/REC-xml/#sec-line-ends
1064
- if " \r \n " in text :
1065
- text = text . replace ( " \r \n " , " \n " )
1065
+ # The current solution, contained in following six lines, was
1066
+ # discussed in issue 17582 and 39011.
1066
1067
if "\r " in text :
1067
- text = text .replace ("\r " , "\n " )
1068
- #The following four lines are issue 17582
1068
+ text = text .replace ("\r " , " " )
1069
1069
if "\n " in text :
1070
1070
text = text .replace ("\n " , " " )
1071
1071
if "\t " in text :
Original file line number Diff line number Diff line change
1
+ Normalization of line endings in ElementTree attributes was removed, as line
2
+ endings which were replaced by entity numbers should be preserved in
3
+ original form.
You can’t perform that action at this time.
0 commit comments