Skip to content

Commit

Permalink
Allow unique transform created id attributes
Browse files Browse the repository at this point in the history
When post-processing a transform created list, keep id attributes as
long as they are unique over the list.

Closes lift#1110
  • Loading branch information
pbrant committed Sep 21, 2011
1 parent 300e701 commit fafa49b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
29 changes: 17 additions & 12 deletions core/util/src/main/scala/net/liftweb/util/BindHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion core/util/src/test/scala/net/liftweb/util/BindHelpersSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit fafa49b

Please sign in to comment.