Skip to content

Commit 2ffd24b

Browse files
khernyoadriaanm
authored andcommitted
Accept prefixed xml attributes with null value
This changes makes PrefixedAttribute work the same way as UnprefixedAttribute with respect to null values: <t p:a={ null: String }/> is accepted and results in <t/>
1 parent 0303321 commit 2ffd24b

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

src/library/scala/xml/PrefixedAttribute.scala

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,25 @@ package scala.xml
1313
*
1414
* @param pre ...
1515
* @param key ...
16-
* @param value the attribute value, which may not be null
16+
* @param value the attribute value
1717
* @param next ...
1818
*/
1919
class PrefixedAttribute(
2020
val pre: String,
2121
val key: String,
2222
val value: Seq[Node],
23-
val next: MetaData)
23+
val next1: MetaData)
2424
extends Attribute
2525
{
26-
if (value eq null)
27-
throw new UnsupportedOperationException("value is null")
26+
val next = if (value ne null) next1 else next1.remove(key)
2827

29-
/** same as this(key, Utility.parseAttributeValue(value), next) */
28+
/** same as this(pre, key, Text(value), next), or no attribute if value is null */
3029
def this(pre: String, key: String, value: String, next: MetaData) =
31-
this(pre, key, Text(value), next)
30+
this(pre, key, if (value ne null) Text(value) else null: NodeSeq, next)
31+
32+
/** same as this(pre, key, value.get, next), or no attribute if value is None */
33+
def this(pre: String, key: String, value: Option[Seq[Node]], next: MetaData) =
34+
this(pre, key, value.orNull, next)
3235

3336
/** Returns a copy of this unprefixed attribute with the given
3437
* next field.

test/files/run/xml-attribute.scala

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,29 @@ object Test {
55
val noAttr = <t/>
66
val attrNull = <t a={ null: String }/>
77
val attrNone = <t a={ None: Option[Seq[Node]] }/>
8+
val preAttrNull = <t p:a={ null: String }/>
9+
val preAttrNone = <t p:a={ None: Option[Seq[Node]] }/>
810
assert(noAttr == attrNull)
911
assert(noAttr == attrNone)
10-
assert(noAttr.toString() == "<t></t>")
11-
assert(attrNull.toString() == "<t></t>")
12-
assert(attrNone.toString() == "<t></t>")
12+
assert(noAttr == preAttrNull)
13+
assert(noAttr == preAttrNone)
14+
15+
val noAttrStr = "<t></t>"
16+
assert(noAttr.toString() == noAttrStr)
17+
assert(attrNull.toString() == noAttrStr)
18+
assert(attrNone.toString() == noAttrStr)
19+
assert(preAttrNull.toString() == noAttrStr)
20+
assert(preAttrNone.toString() == noAttrStr)
21+
22+
val xml1 = <t b="1" d="2"/>
23+
val xml2 = <t a={ null: String } p:a={ null: String } b="1" c={ null: String } d="2"/>
24+
val xml3 = <t b="1" c={ null: String } d="2" a={ null: String } p:a={ null: String }/>
25+
assert(xml1 == xml2)
26+
assert(xml1 == xml3)
27+
28+
val xml1Str = "<t d=\"2\" b=\"1\"></t>"
29+
assert(xml1.toString() == xml1Str)
30+
assert(xml2.toString() == xml1Str)
31+
assert(xml3.toString() == xml1Str)
1332
}
1433
}

0 commit comments

Comments
 (0)