Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SI-9047: Extract attributes from multiple elements #43

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions jvm/src/test/scala/scala/xml/XMLTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,24 @@ class XMLTestJVM {
XML.loadString(broken)
}

@UnitTest
def issueSI9047AttributeFromSingleChildElementWorks: Unit = {
val x = <x><a b='1'/></x>

val b = x \ "a" \ "@b"

assertEquals(List("1"), b map (_.text))
}

@UnitTest
def issueSI9047AttributeMultipleChildElementsWorks: Unit = {
val x = <x><a b='1'/><a b='2'/></x>

val b = x \ "a" \ "@b"

assertEquals(List("1", "2"), b map (_.text))
}

@UnitTest
def nodeSeqNs: Unit = {
val x = {
Expand Down
12 changes: 5 additions & 7 deletions shared/src/main/scala/scala/xml/NodeSeq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ abstract class NodeSeq extends AbstractSeq[Node] with immutable.Seq[Node] with S
def \(that: String): NodeSeq = {
def fail = throw new IllegalArgumentException(that)
def atResult = {
lazy val y = this(0)
val attr =
this flatMap (y => (
if (that.length == 1) fail
else if (that(1) == '{') {
val i = that indexOf '}'
Expand All @@ -105,10 +104,9 @@ abstract class NodeSeq extends AbstractSeq[Node] with immutable.Seq[Node] with S
if (uri == "" || key == "") fail
else y.attribute(uri, key)
} else y.attribute(that drop 1)

attr match {
case Some(x) => Group(x)
case _ => NodeSeq.Empty
).getOrElse(Nil)) match {
case NodeSeq.Empty => NodeSeq.Empty
case x => Group(x)
}
}

Expand All @@ -118,7 +116,7 @@ abstract class NodeSeq extends AbstractSeq[Node] with immutable.Seq[Node] with S
that match {
case "" => fail
case "_" => makeSeq(!_.isAtom)
case _ if (that(0) == '@' && this.length == 1) => atResult
case _ if that(0) == '@' => atResult
case _ => makeSeq(_.label == that)
}
}
Expand Down
4 changes: 2 additions & 2 deletions shared/src/test/scala/scala/xml/AttributeTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ class AttributeTest {
val b = xml \ "b"
assertEquals(2, b.length)
assertEquals(NodeSeq.fromSeq(Seq(<b bar="1"/>, <b bar="2"/>)), b)
val barFail = b \ "@bar"
val barAttributesDirect = b \ "@bar"
val barList = b.map(_ \ "@bar")
assertEquals(NodeSeq.Empty, barFail)
assertEquals(Group(Seq(Text("1"), Text("2"))), barAttributesDirect)
assertEquals(List(Group(Seq(Text("1"))), Group(Seq(Text("2")))), barList)
}

Expand Down