Skip to content
This repository
Browse code

Adding unselectAll to Zipper.

  • Loading branch information...
commit e84221ba9c4329b466c96a161358bc0bc1711f9f 1 parent 191fd38
ncreep authored January 16, 2012
10  src/main/scala/com/codecommit/antixml/Zipper.scala
@@ -151,6 +151,16 @@ trait Zipper[+A <: Node] extends Group[A]
151 151
     val ctx = context.getOrElse(sys.error("Zipper does not have a valid context"))
152 152
     unselect(ctx, zms)
153 153
   }
  154
+  
  155
+  /** Recursively applying [[Zipper#unselect]] until reaching the root. 
  156
+   * Guaranteed not to fail upon root unselection. */
  157
+  def unselectAll(implicit zms: ZipperMergeStrategy): Group[Node] = {
  158
+    def unselectRecur(z: Zipper[Node]): Group[Node] = {
  159
+      if (z.context.isDefined) unselectRecur(z.unselect)
  160
+      else z.stripZipper
  161
+    }
  162
+    unselectRecur(this)
  163
+  }
154 164
 }
155 165
 
156 166
 object Zipper {
21  src/test/scala/com/codecommit/antixml/ZipperSpecs.scala
@@ -918,6 +918,27 @@ class ZipperSpecs extends SpecificationWithJUnit with ScalaCheck  with XMLGenera
918 918
     }
919 919
   }
920 920
   
  921
+  "Zipper.unselectAll" should {
  922
+    val xml = <top><a><b><c><d /></c></b></a></top>.convert
  923
+    
  924
+    "behave like unselect for single selection" in {
  925
+      val z = xml \\ *
  926
+      val z1 = z.updated(0, <c />.convert)
  927
+      z1.unselectAll mustEqual z1.unselect
  928
+    }
  929
+    
  930
+    "behave like reucrsive unselect for multiple selections" in {
  931
+      val z = xml \ 'a \ 'b \ 'c \ 'd
  932
+      val z1 = z.updated(0, <e />.convert)
  933
+      z1.unselectAll mustEqual z1.unselect.unselect.unselect.unselect
  934
+    }
  935
+    
  936
+    "return self group on a broken zipper" in {
  937
+      val group = xml.toGroup
  938
+      group.toZipper.unselectAll mustEqual group 
  939
+    }
  940
+  }
  941
+  
921 942
   "Zipper.flatMap" should {
922 943
     "increase update times front to back" in {
923 944
       val xml = <top><a><b /></a></top>.convert

0 notes on commit e84221b

Please sign in to comment.
Something went wrong with that request. Please try again.