-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCSS.swift
executable file
·364 lines (320 loc) · 13.2 KB
/
CSS.swift
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**@file CSS.swift
Kanna
Copyright (c) 2015 Atsushi Kiwaki (@_tid_)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import Foundation
import libxml2
typealias AKRegularExpression = NSRegularExpression
#if os(Linux) && swift(>=4)
typealias AKTextCheckingResult = NSTextCheckingResult
#elseif os(Linux) && swift(>=3)
typealias AKTextCheckingResult = TextCheckingResult
#else
typealias AKTextCheckingResult = NSTextCheckingResult
#endif
public enum CSSError: Error {
case UnsupportSyntax(String)
}
/**
CSS
*/
public enum CSS {
/**
CSS3 selector to XPath
@param selector CSS3 selector
@return XPath
*/
public static func toXPath(_ css: String, isRoot: Bool = true) throws -> String {
let selectorGroups = css.components(separatedBy: ",")
let prefix = isRoot ? "" : "."
return try selectorGroups
.map { prefix.appending(try toXPath(selector: $0)) }
.joined(separator: " | ")
}
private static func toXPath(selector: String) throws -> String {
var xpath = "//"
var str = selector
var prev = str
while !str.isEmpty {
var attributes: [String] = []
var combinator: String = ""
str = str.trimmingCharacters(in: .whitespaces)
// element
let element = getElement(&str)
// class / id
while let attr = getClassId(&str) {
attributes.append(attr)
}
// attribute
while let attr = getAttribute(&str) {
attributes.append(attr)
}
// matchCombinator
if let combi = genCombinator(&str) {
combinator = combi
}
// generate xpath phrase
let attr = attributes.joined(separator: " and ")
if attr.isEmpty {
xpath += "\(element)\(combinator)"
} else {
xpath += "\(element)[\(attr)]\(combinator)"
}
if str == prev {
throw CSSError.UnsupportSyntax(selector)
}
prev = str
}
return xpath
}
}
private func firstMatch(_ pattern: String) -> (String) -> AKTextCheckingResult? {
return { str in
let length = str.utf16.count
guard let regex = try? AKRegularExpression(pattern: pattern, options: .caseInsensitive) else {
return nil
}
if let result = regex.firstMatch(in: str, options: .reportProgress, range: NSRange(location: 0, length: length)) {
return result
}
return nil
}
}
private func nth(prefix: String, a: Int, b: Int) -> String {
let sibling = "\(prefix)-sibling::*"
if a == 0 {
return "count(\(sibling)) = \(b-1)"
} else if a > 0 {
if b != 0 {
return "((count(\(sibling)) + 1) >= \(b)) and ((((count(\(sibling)) + 1)-\(b)) mod \(a)) = 0)"
}
return "((count(\(sibling)) + 1) mod \(a)) = 0"
}
let a = abs(a)
return "(count(\(sibling)) + 1) <= \(b)" + ((a != 1) ? " and ((((count(\(sibling)) + 1)-\(b)) mod \(a) = 0)" : "")
}
// a(n) + b | a(n) - b
private func nth_child(a: Int, b: Int) -> String {
nth(prefix: "preceding", a: a, b: b)
}
private func nth_last_child(a: Int, b: Int) -> String {
nth(prefix: "following", a: a, b: b)
}
private let escapePattern = "(?:\\\\([!\"#\\$%&\'\\(\\)\\*\\+,\\./:;<=>\\?@\\[\\\\\\]\\^`\\{\\|\\}~]))"
private let escapeRepeatPattern = "\(escapePattern)*"
private let matchElement = firstMatch("^((?:[a-z0-9\\*_-]+\(escapeRepeatPattern))+)((\\|)((?:[a-z0-9\\*_-]+\(escapeRepeatPattern))+))?")
private let matchClassId = firstMatch("^([#.])((?:[a-z0-9\\*_-]+\(escapeRepeatPattern))+)")
private let matchAttr1 = firstMatch("^\\[([^\\]]*)\\]")
private let matchAttr2 = firstMatch("^\\[\\s*([^~\\|\\^\\$\\*=\\s]+)\\s*([~\\|\\^\\$\\*]?=)\\s*(.*)\\s*\\]")
private let matchAttrN = firstMatch("^:not\\((.*?\\)?)\\)")
private let matchPseudo = firstMatch("^:([\'\"()a-z0-9_+-]+)")
private let matchCombinator = firstMatch("^\\s*([\\s>+~,])\\s*")
private let matchSubNthChild = firstMatch("^(nth-child|nth-last-child)\\(\\s*(odd|even|\\d+)\\s*\\)")
private let matchSubNthChildN = firstMatch("^(nth-child|nth-last-child)\\(\\s*(-?\\d*)n(\\+\\d+)?\\s*\\)")
private let matchSubNthOfType = firstMatch("nth-of-type\\((odd|even|\\d+)\\)")
private let matchSubContains = firstMatch("contains\\([\"\'](.*?)[\"\']\\)")
private func substringWithRangeAtIndex(_ result: AKTextCheckingResult, str: String, at: Int) -> String {
if result.numberOfRanges > at {
#if swift(>=4.0) || os(Linux)
let range = result.range(at: at)
#else
let range = result.rangeAt(at)
#endif
if range.length > 0 {
let startIndex = str.index(str.startIndex, offsetBy: range.location)
let endIndex = str.index(startIndex, offsetBy: range.length)
return String(str[startIndex..<endIndex])
}
}
return ""
}
private func escapeCSS(_ text: String) -> String {
text.replacingOccurrences(of: escapePattern, with: "$1", options: .regularExpression, range: nil)
}
private func getElement(_ str: inout String, skip: Bool = true) -> String {
if let result = matchElement(str) {
let (text, text2) = (escapeCSS(substringWithRangeAtIndex(result, str: str, at: 1)),
escapeCSS(substringWithRangeAtIndex(result, str: str, at: 5)))
if skip {
str = String(str[str.index(str.startIndex, offsetBy: result.range.length)..<str.endIndex])
}
// tag with namespace
if !text.isEmpty && !text2.isEmpty {
return "\(text):\(text2)"
}
// tag
if !text.isEmpty {
return text
}
}
return "*"
}
private func getClassId(_ str: inout String, skip: Bool = true) -> String? {
if let result = matchClassId(str) {
let (attr, text) = (escapeCSS(substringWithRangeAtIndex(result, str: str, at: 1)),
escapeCSS(substringWithRangeAtIndex(result, str: str, at: 2)))
if skip {
str = String(str[str.index(str.startIndex, offsetBy: result.range.length)..<str.endIndex])
}
if attr.hasPrefix("#") {
return "@id = '\(text)'"
} else if attr.hasPrefix(".") {
return "contains(concat(' ', normalize-space(@class), ' '), ' \(text) ')"
}
}
return nil
}
private func getAttribute(_ str: inout String, skip: Bool = true) -> String? {
if let result = matchAttr2(str) {
let (attr, expr, text) = (escapeCSS(substringWithRangeAtIndex(result, str: str, at: 1)),
substringWithRangeAtIndex(result, str: str, at: 2),
escapeCSS(substringWithRangeAtIndex(result, str: str, at: 3).replacingOccurrences(of: "[\'\"](.*)[\'\"]", with: "$1", options: .regularExpression, range: nil)))
if skip {
str = String(str[str.index(str.startIndex, offsetBy: result.range.length)..<str.endIndex])
}
switch expr {
case "!=":
return "@\(attr) != \(text)"
case "~=":
return "contains(concat(' ', @\(attr), ' '),concat(' ', '\(text)', ' '))"
case "|=":
return "@\(attr) = '\(text)' or starts-with(@\(attr),concat('\(text)', '-'))"
case "^=":
return "starts-with(@\(attr), '\(text)')"
case "$=":
return "substring(@\(attr), string-length(@\(attr)) - string-length('\(text)') + 1, string-length('\(text)')) = '\(text)'"
case "*=":
return "contains(@\(attr), '\(text)')"
default:
return "@\(attr) = '\(text)'"
}
} else if let result = matchAttr1(str) {
let atr = substringWithRangeAtIndex(result, str: str, at: 1)
if skip {
str = String(str[str.index(str.startIndex, offsetBy: result.range.length)..<str.endIndex])
}
return "@\(atr)"
} else if str.hasPrefix("[") {
// bad syntax attribute
return nil
} else if let attr = getAttrNot(&str) {
return "not(\(attr))"
} else if let result = matchPseudo(str) {
let one = substringWithRangeAtIndex(result, str: str, at: 1)
if skip {
str = String(str[str.index(str.startIndex, offsetBy: result.range.length)..<str.endIndex])
}
switch one {
case "first-child":
return "count(preceding-sibling::*) = 0"
case "last-child":
return "count(following-sibling::*) = 0"
case "only-child":
return "count(preceding-sibling::*) = 0 and count(following-sibling::*) = 0"
case "first-of-type":
return "position() = 1"
case "last-of-type":
return "position() = last()"
case "only-of-type":
return "last() = 1"
case "empty":
return "not(node())"
case "root":
return "not(parent::*)"
default:
if let sub = matchSubNthChild(one) {
let (nth, arg1) = (substringWithRangeAtIndex(sub, str: one, at: 1),
substringWithRangeAtIndex(sub, str: one, at: 2))
let nthFunc = (nth == "nth-child") ? nth_child : nth_last_child
if arg1 == "odd" {
return nthFunc(2, 1)
} else if arg1 == "even" {
return nthFunc(2, 0)
} else {
return nthFunc(0, Int(arg1)!)
}
} else if let sub = matchSubNthChildN(one) {
let (nth, arg1, arg2) = (substringWithRangeAtIndex(sub, str: one, at: 1),
substringWithRangeAtIndex(sub, str: one, at: 2),
substringWithRangeAtIndex(sub, str: one, at: 3))
let nthFunc = (nth == "nth-child") ? nth_child : nth_last_child
let a: Int = (arg1 == "-") ? -1 : Int(arg1)!
let b: Int = (arg2.isEmpty) ? 0 : Int(arg2)!
return nthFunc(a, b)
} else if let sub = matchSubNthOfType(one) {
let arg1 = substringWithRangeAtIndex(sub, str: one, at: 1)
if arg1 == "odd" {
return "(position() >= 1) and (((position()-1) mod 2) = 0)"
} else if arg1 == "even" {
return "(position() mod 2) = 0"
} else {
return "position() = \(arg1)"
}
} else if let sub = matchSubContains(one) {
let text = substringWithRangeAtIndex(sub, str: one, at: 1)
return "contains(., '\(text)')"
} else {
return nil
}
}
}
return nil
}
private func getAttrNot(_ str: inout String, skip: Bool = true) -> String? {
if let result = matchAttrN(str) {
var one = substringWithRangeAtIndex(result, str: str, at: 1)
if skip {
str = String(str[str.index(str.startIndex, offsetBy: result.range.length)..<str.endIndex])
}
if let attr = getAttribute(&one, skip: false) {
return attr
} else if let sub = matchElement(one) {
#if swift(>=4.0) || os(Linux)
let range = sub.range(at: 1)
#else
let range = sub.rangeAt(1)
#endif
let startIndex = one.index(one.startIndex, offsetBy: range.location)
let endIndex = one.index(startIndex, offsetBy: range.length)
let elem = one[startIndex ..< endIndex]
return "self::\(elem)"
} else if let attr = getClassId(&one) {
return attr
}
}
return nil
}
private func genCombinator(_ str: inout String, skip: Bool = true) -> String? {
if let result = matchCombinator(str) {
let one = substringWithRangeAtIndex(result, str: str, at: 1)
if skip {
str = String(str[str.index(str.startIndex, offsetBy: result.range.length)..<str.endIndex])
}
switch one {
case ">":
return "/"
case "+":
return "/following-sibling::*[1]/self::"
case "~":
return "/following-sibling::"
default:
return "//"
}
}
return nil
}