Skip to content
Browse files

Allow unique transform created id attributes

When post-processing a transform created list, keep id attributes as
long as they are unique over the list.

Closes #1110
  • Loading branch information...
1 parent 300e701 commit fafa49bd2576ec7088f45ac7c9146f7d6ee2b4b7 @pbrant pbrant committed Sep 21, 2011
View
29 core/util/src/main/scala/net/liftweb/util/BindHelpers.scala
@@ -2201,19 +2201,24 @@ private class SelectorMap(binds: List[CssBind]) extends Function1[NodeSeq, NodeS
}
case n => {
- calced.toList.zipWithIndex.flatMap {
- case (Group(g), _) => g
- case (e: Elem, 0) =>
- new Elem(e.prefix,
- e.label, mergeAll(e.attributes, false),
- e.scope, e.child :_*)
- case (e: Elem, _) =>
- new Elem(e.prefix,
- e.label, mergeAll(e.attributes, true),
- e.scope, e.child :_*)
- case (x, _) =>
- x
+ val calcedList = calced.toList
+ val availableIds = (attrs.get("id").toList ++
+ calcedList.collect({ case e:Elem => e.attribute("id") }).flatten.map(_.toString)).toSet
+ val merged = calcedList.foldLeft((availableIds, Nil: List[Seq[xml.Node]])) { (idsAndResult, a) =>
+ val (ids, result) = idsAndResult
+ a match {
+ case Group(g) => (ids, g :: result)
+ case e:Elem => {
+ val targetId = e.attribute("id").map(_.toString) orElse (attrs.get("id"))
+ val keepId = targetId map { id => ids.contains(id) } getOrElse (false)
+ val newIds = targetId filter (_ => keepId) map (i => ids - i) getOrElse (ids)
+ val newElem = new Elem(e.prefix, e.label, mergeAll(e.attributes, ! keepId), e.scope, e.child: _*)
+ (newIds, newElem :: result)
+ }
+ case x => (ids, x :: result)
+ }
}
+ merged._2.reverse.flatten
}
}
}
View
12 core/util/src/test/scala/net/liftweb/util/BindHelpersSpec.scala
@@ -737,7 +737,17 @@ object CssBindHelpersSpec extends Specification {
answer must ==/ (<span><div id="horse">frog<span class="item">1</span></div><div>frog<span class="item">2</span></div><div>frog<span class="item">3</span></div></span>)
}
-
+
+ "maintain unique id attributes provided by transform" in {
+ val func = ".thinglist *" #>
+ (".thing" #> List("xx1", "xx2", "xx2", "xx2", "xx4").map(t => {
+ ".thing [id]" #> t
+ })
+ )
+ val answer = func(<ul class="thinglist"><li id="other" class="thing" /></ul>)
+
+ answer must ==/ (<ul class="thinglist"><li class="thing" id="xx1"></li><li class="thing" id="xx2"></li><li id="other" class="thing"></li><li class="thing"></li><li class="thing" id="xx4"></li></ul>)
+ }
"merge classes" in {
val answer = ("cute=moose" #> <input class="a" name="goof" value="8" id="88"/>).apply (

0 comments on commit fafa49b

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