/
CssSelector.scala
224 lines (176 loc) · 6.71 KB
/
CssSelector.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* Copyright 2011 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.liftweb
package util
import scala.util.parsing.combinator.{Parsers, ImplicitConversions}
import scala.xml.NodeSeq
import net.liftweb.common._
sealed trait CssSelector {
def subNodes: Box[SubNode]
}
final case class ElemSelector(elem: String, subNodes: Box[SubNode]) extends
CssSelector
final case class StarSelector(subNodes: Box[SubNode]) extends CssSelector
final case class IdSelector(id: String, subNodes: Box[SubNode]) extends
CssSelector
final case class ClassSelector(clss: String, subNodes: Box[SubNode]) extends
CssSelector
final case class NameSelector(name: String, subNodes: Box[SubNode]) extends
CssSelector
final case class AttrSelector(name: String, value: String,
subNodes: Box[SubNode]) extends CssSelector
sealed trait SubNode
object SubNode {
def unapply(bind: CssBind): Option[Box[SubNode]] =
Some(bind.css.flatMap(_.subNodes))
}
sealed trait WithKids {
def transform(original: NodeSeq, newNs: NodeSeq): NodeSeq
}
final case class KidsSubNode() extends SubNode with WithKids {
def transform(original: NodeSeq, newNs: NodeSeq): NodeSeq = newNs
}
final case class PrependKidsSubNode() extends SubNode with WithKids {
def transform(original: NodeSeq, newNs: NodeSeq): NodeSeq = newNs ++ original
}
final case class AppendKidsSubNode() extends SubNode with WithKids {
def transform(original: NodeSeq, newNs: NodeSeq): NodeSeq = original ++ newNs
}
sealed trait AttributeRule
final case class AttrSubNode(attr: String) extends SubNode with AttributeRule
final case class AttrAppendSubNode(attr: String) extends SubNode with AttributeRule
final case class SelectThisNode(kids: Boolean) extends SubNode
/**
* Parse a subset of CSS into the appropriate selector objects
*/
object CssSelectorParser extends Parsers with ImplicitConversions {
private val cache = new LRUMap[String, CssSelector](25000)
/**
* Parse a String into a CSS Selector
*/
def parse(_toParse: String): Box[CssSelector] = synchronized {
// trim off leading and trailing spaces
val toParse = _toParse.trim
// this method is synchronized because the Parser combinator is not
// thread safe, so we'll only parse one at a time, but given that most
// of the selectors will be cached, it's not really a performance hit
cache.get(toParse) or {
internalParse(toParse).map {
sel => {
// cache the result
cache(toParse) = sel
sel
}
}
}
}
import scala.util.parsing.input.CharSequenceReader
type Elem = Char
type UnitParser=Parser[Unit]
private def internalParse(toParse: String): Box[CssSelector] = {
val reader: Input = new CharSequenceReader(toParse, 0)
topParser(reader) match {
case Success(v, _) => Full(v)
case x => Empty
}
}
private implicit def str2chars(s: String): List[Char] = new scala.collection.immutable.WrappedString(s).toList
private lazy val topParser: Parser[CssSelector] = {
phrase(idMatch |
nameMatch |
classMatch |
attrMatch |
elemMatch |
starMatch |
colonMatch)
}
private lazy val colonMatch: Parser[CssSelector] =
':' ~> id ~ opt(subNode) ^? {
case "button" ~ sn => AttrSelector("type", "button", sn)
case "checkbox" ~ sn => AttrSelector("type", "checkbox", sn)
case "file" ~ sn => AttrSelector("type", "file", sn)
case "password" ~ sn => AttrSelector("type", "password", sn)
case "radio" ~ sn => AttrSelector("type", "radio", sn)
case "reset" ~ sn => AttrSelector("type", "reset", sn)
case "submit" ~ sn => AttrSelector("type", "submit", sn)
case "text" ~ sn => AttrSelector("type", "text", sn)
}
private lazy val idMatch: Parser[CssSelector] = '#' ~> id ~ opt(subNode) ^^ {
case id ~ sn => IdSelector(id, sn)
}
private lazy val nameMatch: Parser[CssSelector] = '@' ~> id ~ opt(subNode) ^^ {
case name ~ sn => NameSelector(name, sn)
}
private lazy val elemMatch: Parser[CssSelector] = id ~ opt(subNode) ^^ {
case elem ~ sn => ElemSelector(elem, sn)
}
private lazy val starMatch: Parser[CssSelector] = '*' ~> opt(subNode) ^^ {
case sn => StarSelector(sn)
}
private lazy val id: Parser[String] = letter ~
rep(letter | number | '-' | '_' | ':' | '.') ^^ {
case first ~ rest => (first :: rest).mkString
}
private def isLetter(c: Char): Boolean = c.isLetter
private def isNumber(c: Char): Boolean = c.isDigit
private lazy val letter: Parser[Char] = elem("letter", isLetter)
private lazy val number: Parser[Char] = elem("number", isNumber)
private lazy val classMatch: Parser[CssSelector] =
'.' ~> attrName ~ opt(subNode) ^^ {
case cls ~ sn => ClassSelector(cls, sn)
}
private lazy val attrMatch: Parser[CssSelector] =
attrName ~ '=' ~ attrConst ~ opt(subNode) ^^ {
case "id" ~ _ ~ const ~ sn => IdSelector(const, sn)
case "name" ~ _ ~ const ~ sn => NameSelector(const, sn)
case n ~ _ ~ v ~ sn => AttrSelector(n, v, sn)
}
private lazy val subNode: Parser[SubNode] = rep1(' ') ~>
((opt('*') ~ '[' ~> attrName <~ '+' ~ ']' ^^ {
name => AttrAppendSubNode(name)
}) |
(opt('*') ~ '[' ~> attrName <~ ']' ^^ {
name => AttrSubNode(name)
}) |
('-' ~ '*' ^^ (a => PrependKidsSubNode())) |
('>' ~ '*' ^^ (a => PrependKidsSubNode())) |
('*' ~ '+' ^^ (a => AppendKidsSubNode())) |
('*' ~ '<' ^^ (a => AppendKidsSubNode())) |
'*' ^^ (a => KidsSubNode()) |
'^' ~ '*' ^^ (a => SelectThisNode(true)) |
'^' ~ '^' ^^ (a => SelectThisNode(false)))
private lazy val attrName: Parser[String] = (letter | '_' | ':') ~
rep(letter | number | '-' | '_' | ':' | '.') ^^ {
case first ~ rest => (first :: rest).mkString
}
private lazy val attrConst: Parser[String] = {
(('\'' ~> rep(elem("isValid", (c: Char) => {
c != '\'' && c >= ' '
})) <~ '\'') ^^ {
case s => s.mkString
}) |
(('"' ~> rep(elem("isValid", (c: Char) => {
c != '"' && c >= ' '
})) <~ '"') ^^ {
case s => s.mkString
}) |
(rep1(elem("isValid", (c: Char) => {
c != '\'' && c != '"' && c > ' '
})) ^^ {
case s => s.mkString
})
}
}