Permalink
Browse files

Added some convenience methods for working with Elem

  • Loading branch information...
1 parent e176e33 commit 324c4b14e696d057a5beeba7ba9bed7be659d6b1 @hamnis hamnis committed Feb 15, 2012
Showing with 23 additions and 1 deletion.
  1. +23 −1 src/main/scala/com/codecommit/antixml/node.scala
@@ -168,9 +168,31 @@ case class Elem(prefix: Option[String], name: String, attrs: Attributes, scope:
}
def toGroup = Group(this)
+
+ /**
+ * Convenience method to allow adding attributes in a chaining fashion.
+ */
+ def withAttribute(name: QName, value: String) = copy(attrs = attrs + (name -> value))
+
+ /**
+ * Convenience method to allow adding a single child in a chaining fashion.
+ */
+ def addChild(node: Node) = copy(children = children :+ node)
+
+ /**
+ * Convenience method to allow adding children in a chaining fashion.
+ */
+ def addChildren(children: Group[Node]) = copy(children = children ++ children)
@kaja47

kaja47 May 21, 2012

This looks like a bug. It inserts children passed via argument twice. It should be something like this:

def addChildren(newChildren: Group[Node]) = copy(children = children ++ newChildren)
@hamnis

hamnis May 21, 2012

Contributor

this is indeed a bug.

@hamnis

hamnis via email May 21, 2012

Contributor
@hamnis

hamnis via email May 21, 2012

Contributor
+
+ /**
+ * Convenience method to allow replacing all children in a chaining fashion.
+ */
+ def withChildren(children: Group[Node]) = copy(children = children)
}
-object Elem extends ((Option[String], String, Attributes, Map[String, String], Group[Node]) => Elem) {
+object Elem extends ((Option[String], String, Attributes, Map[String, String], Group[Node]) => Elem) {
+ def apply(name: String, attrs: Attributes = Attributes()): Elem = apply(None, name, attrs, Map.empty, Group.empty)
+
private[this] val NameRegex = {
val nameStartChar = """:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD"""
"[" + nameStartChar + "][" + nameStartChar + """\-\.0-9\u00B7\u0300-\u036F\u203F-\u2040]*"""r

0 comments on commit 324c4b1

Please sign in to comment.