Skip to content

Instantly share code, notes, and snippets.

@Centaur
Forked from yangbajing/gist:5010139
Created February 22, 2013 03:14
Show Gist options
  • Save Centaur/5010462 to your computer and use it in GitHub Desktop.
Save Centaur/5010462 to your computer and use it in GitHub Desktop.
private val _jqueryCssSelectRegex = """(^\S)+""".r
private val _matchChar = Set('*', '[', '^', '-')
def sel(exp: String, content: NodeSeq): CssSel = {
val selects = _jqueryCssSelectRegex.findAllMatchIn(exp).map(_.matched).toList
val (init, last) =
if (_matchChar.contains(selects.last.head))
selects.dropRight(2) -> selects.takeRight(2).mkString(" ")
else
selects.dropRight(1) -> selects.takeRight(1).mkString
init.foldRight(last #> content)((exp, cssSel) => exp #> cssSel)
}
implicit final class TsStringToCssSel(exp: String) {
def #>>(content: NodeSeq): CssSel =
H.sel(exp, content)
def #>>[V <: AnyVal](value: V): CssSel =
H.sel(exp, Text(value.toString))
def #>>(value: String): CssSel =
H.sel(exp, Text(value))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment