Skip to content

Commit 389cdce

Browse files
committed
Fix failure for empty string match
Trying to match the empty string on descendants (or self) throws an unexpected error: scala> <x/> \\ "" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(String.java:646) at scala.collection.immutable.StringOps$.apply$extension(StringOps.scala:37) at scala.xml.NodeSeq.$bslash$bslash(NodeSeq.scala:147) ... The error should be consistent with what is thrown when trying to match the empty string on just children: scala> <x/> \ "" java.lang.IllegalArgumentException at scala.xml.NodeSeq.fail$1(NodeSeq.scala:97) at scala.xml.NodeSeq.$bslash(NodeSeq.scala:120) ... This was identified while writing ScalaCheck property tests.
1 parent f3841aa commit 389cdce

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

shared/src/main/scala/scala/xml/NodeSeq.scala

+2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,10 @@ abstract class NodeSeq extends AbstractSeq[Node] with immutable.Seq[Node] with S
140140
* The document order is preserved.
141141
*/
142142
def \\(that: String): NodeSeq = {
143+
def fail = throw new IllegalArgumentException(that)
143144
def filt(cond: (Node) => Boolean) = this flatMap (_.descendant_or_self) filter cond
144145
that match {
146+
case "" => fail
145147
case "_" => filt(!_.isAtom)
146148
case _ if that(0) == '@' => filt(!_.isAtom) flatMap (_ \ that)
147149
case _ => filt(x => !x.isAtom && x.label == that)

shared/src/test/scala/scala/xml/XMLTest.scala

+10
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@ class XMLTest {
145145
assertEquals(expected, actual)
146146
}
147147

148+
@UnitTest(expected=classOf[IllegalArgumentException])
149+
def failEmptyStringChildren: Unit = {
150+
<x/> \ ""
151+
}
152+
153+
@UnitTest(expected=classOf[IllegalArgumentException])
154+
def failEmptyStringDescendants: Unit = {
155+
<x/> \\ ""
156+
}
157+
148158
@UnitTest
149159
def namespaces: Unit = {
150160
val cuckoo = <cuckoo xmlns="http://cuckoo.com">

0 commit comments

Comments
 (0)