Skip to content

Commit 7c62d79

Browse files
khernyoadriaanm
authored andcommitted
SI-7074 Fix xml attribute sorting
Sorting the attributes of an xml element could drop some of the attributes. It was caused by the incorrect use of MetaData#copy() to concatenate "smaller" with the rest of the attributes. The MetaData#copy() method is similar to the following hypothetical method on a List: def copy(other: List): List = head :: other The fix prepends all elements of "smaller" to the rest of the attributes in the proper order.
1 parent 34aaf0d commit 7c62d79

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/library/scala/xml/Utility.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ object Utility extends AnyRef with parsing.TokenTests {
6464
val key = md.key
6565
val smaller = sort(md.filter { m => m.key < key })
6666
val greater = sort(md.filter { m => m.key > key })
67-
smaller.copy(md.copy ( greater ))
67+
smaller.foldRight (md copy greater) ((x, xs) => x copy xs)
6868
}
6969

7070
/** Return the node with its attribute list sorted alphabetically

test/files/run/t7074.check

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<a/>
2+
<a b="2" c="3" d="1"/>
3+
<a b="2" c="4" d="1" e="3" f="5"/>
4+
<a b="5" c="4" d="3" e="2" f="1"/>
5+
<a b="1" c="2" d="3" e="4" f="5"/>
6+
<a a:b="2" a:c="3" a:d="1"/>
7+
<a a:b="2" a:c="4" a:d="1" a:e="3" a:f="5"/>
8+
<a a:b="5" a:c="4" a:d="3" a:e="2" a:f="1"/>
9+
<a a:b="1" a:c="2" a:d="3" a:e="4" a:f="5"/>

test/files/run/t7074.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.xml.Utility.sort
2+
3+
object Test extends App {
4+
println(sort(<a/>))
5+
println(sort(<a d="1" b="2" c="3"/>))
6+
println(sort(<a d="1" b="2" e="3" c="4" f="5"/>))
7+
println(sort(<a f="1" e="2" d="3" c="4" b="5"/>))
8+
println(sort(<a b="1" c="2" d="3" e="4" f="5"/>))
9+
10+
println(sort(<a a:d="1" a:b="2" a:c="3"/>))
11+
println(sort(<a a:d="1" a:b="2" a:e="3" a:c="4" a:f="5"/>))
12+
println(sort(<a a:f="1" a:e="2" a:d="3" a:c="4" a:b="5"/>))
13+
println(sort(<a a:b="1" a:c="2" a:d="3" a:e="4" a:f="5"/>))
14+
}
15+

0 commit comments

Comments
 (0)