Skip to content

Commit 9a0db27

Browse files
committed
Fix failure for empty attribute match
Running the following invalid attribute search, properly throws an IllegalArgumentException. scala> <x/> \ "@" java.lang.IllegalArgumentException: @ scala> <x/> \@ "" java.lang.IllegalArgumentException: @ There's no such thing as an empty attribute. However, when the improper matching value is used against more than just one element, no error is thrown, just an empty NodeSeq is returned: scala> <x><y/><z/></x>.child \ "@" res1: scala.xml.NodeSeq = NodeSeq() It should be a failure. Similarly, the attribute search method, is similarly affected. scala> <x><y/><z/></x>.child \@ "" res1: scala.xml.NodeSeq = NodeSeq() This was identified while writing ScalaCheck property tests.
1 parent 5bb1af0 commit 9a0db27

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Diff for: shared/src/main/scala/scala/xml/NodeSeq.scala

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ abstract class NodeSeq extends AbstractSeq[Node] with immutable.Seq[Node] with S
116116
that match {
117117
case "" => fail
118118
case "_" => makeSeq(!_.isAtom)
119+
case "@" => fail
119120
case _ if (that(0) == '@' && this.length == 1) => atResult
120121
case _ => makeSeq(_.label == that)
121122
}

Diff for: shared/src/test/scala/scala/xml/AttributeTest.scala

+19
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,23 @@ class AttributeTest {
154154
assertEquals(List(Group(Seq(Text("1"))), Group(Seq(Text("2")))), barList)
155155
}
156156

157+
@Test(expected=classOf[IllegalArgumentException])
158+
def invalidAttributeFailForOne: Unit = {
159+
<x/> \ "@"
160+
}
161+
162+
@Test(expected=classOf[IllegalArgumentException])
163+
def invalidAttributeFailForMany: Unit = {
164+
<x><y/><z/></x>.child \ "@"
165+
}
166+
167+
@Test(expected=classOf[IllegalArgumentException])
168+
def invalidEmptyAttributeFailForOne: Unit = {
169+
<x/> \@ ""
170+
}
171+
172+
@Test(expected=classOf[IllegalArgumentException])
173+
def invalidEmptyAttributeFailForMany: Unit = {
174+
<x><y/><z/></x>.child \@ ""
175+
}
157176
}

0 commit comments

Comments
 (0)