Skip to content

Commit 3691228

Browse files
rtyleyadriaanm
authored andcommitted
Add convenience attribute operator to NodeSeq
Compared to the current method of reading the string text of an attribute: (x \ "@bar").text ...the new operator removes the need for a pair of parenthesis and shortens the overall expression by 7 chars : x \@ "bar" Discussion on scala-internals: https://groups.google.com/d/topic/scala-internals/BZ-tfbebDqE/discussion
1 parent 9c22bc1 commit 3691228

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/library/scala/xml/NodeSeq.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ abstract class NodeSeq extends AbstractSeq[Node] with immutable.Seq[Node] with S
145145
}
146146
}
147147

148+
/** Convenience method which returns string text of the named attribute. Use:
149+
* - `that \@ "foo"` to get the string text of attribute `"foo"`;
150+
*/
151+
def \@(attributeName: String): String = (this \ ("@" + attributeName)).text
152+
148153
override def toString(): String = theSeq.mkString
149154

150155
def text: String = (this map (_.text)).mkString

test/files/jvm/xmlattr.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ object Test {
66
UnprefixedAttributeTest()
77
AttributeWithOptionTest()
88
AttributeOutputTest()
9+
AttributeOperatorTest()
910
}
1011

1112
object UnprefixedAttributeTest {
@@ -60,4 +61,10 @@ object Test {
6061
}
6162
}
6263

64+
object AttributeOperatorTest {
65+
def apply() {
66+
val xml = <foo bar="apple" />
67+
assert(xml \@ "bar" == "apple")
68+
}
69+
}
6370
}

0 commit comments

Comments
 (0)