summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSzabolcs Berecz <szabolcs.berecz@gmail.com>2012-01-07 18:23:21 +0100
committerSzabolcs Berecz <szabolcs.berecz@gmail.com>2012-01-07 18:43:16 +0100
commit51089b34a7a535498dee42e6465d4d577d65b7d5 (patch)
tree5f403562fe66796c0962a69278c39e8c26a6cc20
parent4787f883604d1344257c0b40c15790c3dde477f2 (diff)
downloadscala-51089b34a7a535498dee42e6465d4d577d65b7d5.tar.gz
scala-51089b34a7a535498dee42e6465d4d577d65b7d5.tar.bz2
scala-51089b34a7a535498dee42e6465d4d577d65b7d5.zip
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/>
-rw-r--r--src/library/scala/xml/PrefixedAttribute.scala15
-rw-r--r--test/files/run/xml-attribute.scala25
2 files changed, 31 insertions, 9 deletions
diff --git a/src/library/scala/xml/PrefixedAttribute.scala b/src/library/scala/xml/PrefixedAttribute.scala
index 436dfcda43..b80d6a1c73 100644
--- a/src/library/scala/xml/PrefixedAttribute.scala
+++ b/src/library/scala/xml/PrefixedAttribute.scala
@@ -13,22 +13,25 @@ package scala.xml
*
* @param pre ...
* @param key ...
- * @param value the attribute value, which may not be null
+ * @param value the attribute value
* @param next ...
*/
class PrefixedAttribute(
val pre: String,
val key: String,
val value: Seq[Node],
- val next: MetaData)
+ val next1: MetaData)
extends Attribute
{
- if (value eq null)
- throw new UnsupportedOperationException("value is null")
+ val next = if (value ne null) next1 else next1.remove(key)
- /** same as this(key, Utility.parseAttributeValue(value), next) */
+ /** same as this(pre, key, Text(value), next), or no attribute if value is null */
def this(pre: String, key: String, value: String, next: MetaData) =
- this(pre, key, Text(value), next)
+ this(pre, key, if (value ne null) Text(value) else null: NodeSeq, next)
+
+ /** same as this(pre, key, value.get, next), or no attribute if value is None */
+ def this(pre: String, key: String, value: Option[Seq[Node]], next: MetaData) =
+ this(pre, key, value.orNull, next)
/** Returns a copy of this unprefixed attribute with the given
* next field.
diff --git a/test/files/run/xml-attribute.scala b/test/files/run/xml-attribute.scala
index 2b83f70b22..8b261acc94 100644
--- a/test/files/run/xml-attribute.scala
+++ b/test/files/run/xml-attribute.scala
@@ -5,10 +5,29 @@ object Test {
val noAttr = <t/>
val attrNull = <t a={ null: String }/>
val attrNone = <t a={ None: Option[Seq[Node]] }/>
+ val preAttrNull = <t p:a={ null: String }/>
+ val preAttrNone = <t p:a={ None: Option[Seq[Node]] }/>
assert(noAttr == attrNull)
assert(noAttr == attrNone)
- assert(noAttr.toString() == "<t></t>")
- assert(attrNull.toString() == "<t></t>")
- assert(attrNone.toString() == "<t></t>")
+ assert(noAttr == preAttrNull)
+ assert(noAttr == preAttrNone)
+
+ val noAttrStr = "<t></t>"
+ assert(noAttr.toString() == noAttrStr)
+ assert(attrNull.toString() == noAttrStr)
+ assert(attrNone.toString() == noAttrStr)
+ assert(preAttrNull.toString() == noAttrStr)
+ assert(preAttrNone.toString() == noAttrStr)
+
+ val xml1 = <t b="1" d="2"/>
+ val xml2 = <t a={ null: String } p:a={ null: String } b="1" c={ null: String } d="2"/>
+ val xml3 = <t b="1" c={ null: String } d="2" a={ null: String } p:a={ null: String }/>
+ assert(xml1 == xml2)
+ assert(xml1 == xml3)
+
+ val xml1Str = "<t d=\"2\" b=\"1\"></t>"
+ assert(xml1.toString() == xml1Str)
+ assert(xml2.toString() == xml1Str)
+ assert(xml3.toString() == xml1Str)
}
}