Skip to content

Commit 0303321

Browse files
khernyoadriaanm
authored andcommitted
Fixed equality and string representation of xml attributes with null value
Prior to this patch <t a={ null: String }/> was not equal to <t/> and it's string representation was "<t ></t>" instead of "<t></t>" This includes changing MetaData.normalize() so that it doesn't reverse the chain. On the downside, the iterate function in MetaData.normalize() is not tail-recursive now.
1 parent 0029cbd commit 0303321

File tree

6 files changed

+22
-6
lines changed

6 files changed

+22
-6
lines changed

src/library/scala/xml/Elem.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,16 @@ object Elem {
4141
class Elem(
4242
override val prefix: String,
4343
val label: String,
44-
override val attributes: MetaData,
44+
attributes1: MetaData,
4545
override val scope: NamespaceBinding,
4646
val child: Node*)
4747
extends Node with Serializable
4848
{
4949
final override def doCollectNamespaces = true
5050
final override def doTransform = true
5151

52+
override val attributes = MetaData.normalize(attributes1, scope)
53+
5254
if (prefix == "")
5355
throw new IllegalArgumentException("prefix of zero length, use null instead")
5456

src/library/scala/xml/MetaData.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ object MetaData {
3838
def iterate(md: MetaData, normalized_attribs: MetaData, set: Set[String]): MetaData = {
3939
lazy val key = getUniversalKey(md, scope)
4040
if (md eq Null) normalized_attribs
41-
else if (set(key)) iterate(md.next, normalized_attribs, set)
42-
else iterate(md.next, md copy normalized_attribs, set + key)
41+
else if ((md.value eq null) || set(key)) iterate(md.next, normalized_attribs, set)
42+
else md copy iterate(md.next, normalized_attribs, set + key)
4343
}
4444
iterate(attribs, Null, Set())
4545
}

src/library/scala/xml/UnprefixedAttribute.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extends Attribute
2222
final val pre = null
2323
val next = if (value ne null) next1 else next1.remove(key)
2424

25-
/** same as this(key, Text(value), next) */
25+
/** same as this(key, Text(value), next), or no attribute if value is null */
2626
def this(key: String, value: String, next: MetaData) =
2727
this(key, if (value ne null) Text(value) else null: NodeSeq, next)
2828

src/library/scala/xml/Utility.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ object Utility extends AnyRef with parsing.TokenTests {
6161
val key = md.key
6262
val smaller = sort(md.filter { m => m.key < key })
6363
val greater = sort(md.filter { m => m.key > key })
64-
smaller.append( Null ).append(md.copy ( greater ))
64+
smaller.copy(md.copy ( greater ))
6565
}
6666

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

test/files/jvm/xml03syntax.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ true
2323
4
2424

2525
node=<elem key="<b>hello</b>"></elem>, key=Some(<b>hello</b>)
26-
node=<elem ></elem>, key=None
26+
node=<elem></elem>, key=None

test/files/run/xml-attribute.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import xml.Node
2+
3+
object Test {
4+
def main(args: Array[String]): Unit = {
5+
val noAttr = <t/>
6+
val attrNull = <t a={ null: String }/>
7+
val attrNone = <t a={ None: Option[Seq[Node]] }/>
8+
assert(noAttr == attrNull)
9+
assert(noAttr == attrNone)
10+
assert(noAttr.toString() == "<t></t>")
11+
assert(attrNull.toString() == "<t></t>")
12+
assert(attrNone.toString() == "<t></t>")
13+
}
14+
}

0 commit comments

Comments
 (0)