Skip to content

Instantly share code, notes, and snippets.

@yangbajing
Last active December 14, 2015 01:59
Show Gist options
  • Save yangbajing/5010139 to your computer and use it in GitHub Desktop.
Save yangbajing/5010139 to your computer and use it in GitHub Desktop.
扩展lift Css Selector为使用jQuery CSS selector 形式。(scala 2.10 +, lift 2.5 RC1 +)
import scala.xml.{ NodeSeq, Text }
import net.liftweb.http.js._
import net.liftweb.util.{ CssSel, CanBind }
import net.liftweb.util.BindHelpers._
object Implicits {
private val _selRegex = new scala.util.matching.Regex("""(\S)+""")
private val _selMatch = Set('*', '-', '[', '^')
implicit final class YbjStringToCssSel(exp: String) {
def #>>[T](content: => T)(implicit computer: CanBind[T]): CssSel = {
val selects = _selRegex.findAllMatchIn(exp).map(_.matched).toList
val (init, last) =
if (_selMatch.contains(selects.last.head))
selects.dropRight(2) -> selects.takeRight(2).mkString(" ")
else
selects.dropRight(1) -> selects.takeRight(1).mkString
init.foldRight(last #> content)((s, c) => s #> c)
}
}
}
// Test
import Imports._
"#id .class span *+" #>> "羊八井!" apply <div id="id"><a class="class"><span>您好,</span></a></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment