扩展lift CssSelector为使用jQuery CSS Selector 形式

啥也不说了,直接上代码

注意:lift 2.5 RC1 +, Scala 2.10 +


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>

你可能感兴趣的:(jquery,scala,cssSelector,lift,CssSel)