| 1 |
-- Source: https://github.com/leafo/web_sanitize |
| 2 |
-- Author: Leaf Corcoran |
| 3 |
local R, S, V, P |
| 4 |
do |
| 5 |
local _obj_0 = require("lpeg") |
| 6 |
R, S, V, P = _obj_0.R, _obj_0.S, _obj_0.V, _obj_0.P |
| 7 |
end |
| 8 |
local C, Cs, Ct, Cmt, Cg, Cb, Cc, Cp |
| 9 |
do |
| 10 |
local _obj_0 = require("lpeg") |
| 11 |
C, Cs, Ct, Cmt, Cg, Cb, Cc, Cp = _obj_0.C, _obj_0.Cs, _obj_0.Ct, _obj_0.Cmt, _obj_0.Cg, _obj_0.Cb, _obj_0.Cc, _obj_0.Cp |
| 12 |
end |
| 13 |
local alphanum = R("az", "AZ", "09") |
| 14 |
local num = R("09") |
| 15 |
local quotes = S("'\"") ^ 1 |
| 16 |
local white = S(" \t\n") ^ 0 |
| 17 |
-- this is a deviation from the upstream, we allow "|" in the tag name, because |
| 18 |
-- luaxml doesn't support XML namespaces and elements must be queried using |
| 19 |
-- dom:query_selector("namespace|element") |
| 20 |
local word = (alphanum + S("_-") + S("|")) ^ 1 |
| 21 |
local attr_word = (alphanum + S("_-") + S("|:")) ^ 1 |
| 22 |
|
| 23 |
local combinators = S(">~+") |
| 24 |
|
| 25 |
local attr_name = (alphanum + S("_-:")) ^ 1 |
| 26 |
local attr_function = S("~|^$*") ^ 0 |
| 27 |
|
| 28 |
local attr_content = C((P(1) - quotes) ^ 1) |
| 29 |
local mark |
| 30 |
mark = function(name) |
| 31 |
return function(...) |
| 32 |
return { |
| 33 |
name, |
| 34 |
... |
| 35 |
} |
| 36 |
end |
| 37 |
end |
| 38 |
local parse_query |
| 39 |
parse_query = function(query) |
| 40 |
local tag = word / mark("tag") |
| 41 |
local cls = P(".") * (word / mark("class")) |
| 42 |
local id = P("#") * (word / mark("id")) |
| 43 |
local any = P("*") / mark("any") |
| 44 |
local nth = P(":nth-child(") * C(num ^ 1) * ")" / mark("nth-child") |
| 45 |
local first = P(":first-child") / mark("first-child") |
| 46 |
local first_of_type = P(":first-of-type") / mark("first-of-type") |
| 47 |
local last = P(":last-child") / mark("last-child") |
| 48 |
local last_of_type = P(":last-of-type") / mark("last-of-type") |
| 49 |
local attr = P("[") * C(attr_word) * P("]") / mark("attr") |
| 50 |
local attr_value = P("[") * C(attr_name ) * C(attr_function)* P("=") * quotes * attr_content * quotes * P("]") / mark("attr_value") |
| 51 |
local combinator = C(combinators) / mark("combinator") |
| 52 |
local selector = Ct((any + nth + first + first_of_type + last + last_of_type + tag + cls + id + attr + attr_value + combinator) ^ 1) |
| 53 |
local pq = Ct(selector * (white * selector) ^ 0) |
| 54 |
local pqs = Ct(pq * (white * P(",") * white * pq) ^ 0) |
| 55 |
pqs = pqs * (white * -1) |
| 56 |
return pqs:match(query) |
| 57 |
end |
| 58 |
return { |
| 59 |
parse_query = parse_query |
| 60 |
} |