| 1 |
--- CSS query module for LuaXML |
| 2 |
-- @module luaxml-cssquery |
| 3 |
-- @author Michal Hoftich <[email protected] |
| 4 |
local parse_query |
| 5 |
if kpse then |
| 6 |
parse_query = require("luaxml-parse-query") |
| 7 |
else |
| 8 |
parse_query = require("luaxml.parse-query") |
| 9 |
end |
| 10 |
|
| 11 |
-- the string.explode function is provided by LuaTeX |
| 12 |
-- this is alternative for stock Lua |
| 13 |
-- source: http://lua-users.org/wiki/SplitJoin |
| 14 |
local function string_split(str, sep) |
| 15 |
local sep, fields = sep or ":", {} |
| 16 |
local pattern = string.format("([^%s]+)", sep) |
| 17 |
str:gsub(pattern, function(c) fields[#fields+1] = c end) |
| 18 |
return fields |
| 19 |
end |
| 20 |
|
| 21 |
string.explode = string.explode or string_split |
| 22 |
|
| 23 |
--- CssQuery constructor |
| 24 |
-- @function cssquery |
| 25 |
-- @return CssQuery object |
| 26 |
local function cssquery() |
| 27 |
--- @type CssQuery |
| 28 |
local CssQuery = {} |
| 29 |
CssQuery.__index = CssQuery |
| 30 |
CssQuery.__debug = false |
| 31 |
CssQuery.querylist = {} |
| 32 |
|
| 33 |
function CssQuery.debug(self) |
| 34 |
self.__debug = true |
| 35 |
end |
| 36 |
|
| 37 |
function CssQuery:debug_print(text) |
| 38 |
if self.__debug then |
| 39 |
print("[CSS Object]: " .. text) |
| 40 |
end |
| 41 |
end |
| 42 |
--- Calculate CSS specificity of the query |
| 43 |
-- @param query table created by CssQuery:prepare_selector() function |
| 44 |
-- @return integer specificity value |
| 45 |
function CssQuery:calculate_specificity(query) |
| 46 |
local query = query or {} |
| 47 |
local specificity = 0 |
| 48 |
for _, item in ipairs(query.query or {}) do |
| 49 |
for _, t in ipairs(item) do |
| 50 |
local key = t.key |
| 51 |
-- for key, value in pairs(item) do |
| 52 |
if key == "id" then |
| 53 |
specificity = specificity + 100 |
| 54 |
elseif key == "tag" then |
| 55 |
specificity = specificity + 1 |
| 56 |
elseif key == "any" then |
| 57 |
-- * has 0 specificity |
| 58 |
else |
| 59 |
specificity = specificity + 10 |
| 60 |
end |
| 61 |
end |
| 62 |
end |
| 63 |
return specificity |
| 64 |
end |
| 65 |
|
| 66 |
-- save element position in the current siblings list |
| 67 |
local function make_nth(curr_el) |
| 68 |
local pos = 0 |
| 69 |
local el_pos = 0 |
| 70 |
local type_pos = 0 |
| 71 |
-- get current node list |
| 72 |
local siblings = curr_el:get_siblings() |
| 73 |
if siblings then |
| 74 |
local parent = curr_el:get_parent() |
| 75 |
local element_types = {} |
| 76 |
for _, other_el in ipairs(siblings) do |
| 77 |
-- number the elements |
| 78 |
if other_el:is_element() then |
| 79 |
pos = pos + 1 |
| 80 |
other_el.nth = pos |
| 81 |
-- save also element type, for nth-of-type and similar queries |
| 82 |
local el_name = other_el:get_element_name() |
| 83 |
local counter = (element_types[el_name] or 0) + 1 |
| 84 |
other_el.type_nth = counter |
| 85 |
element_types[el_name] = counter |
| 86 |
-- save the current element position |
| 87 |
if other_el == curr_el then |
| 88 |
el_pos = pos |
| 89 |
type_pos = counter |
| 90 |
end |
| 91 |
end |
| 92 |
end |
| 93 |
-- save counter of element types |
| 94 |
parent.element_types = element_types |
| 95 |
-- save count of elements |
| 96 |
parent.child_elements = pos |
| 97 |
else |
| 98 |
return false |
| 99 |
end |
| 100 |
return el_pos, type_pos |
| 101 |
end |
| 102 |
|
| 103 |
local function test_first_child(el, nth) |
| 104 |
local el_pos = el.nth or make_nth(el) |
| 105 |
return el_pos == 1 |
| 106 |
end |
| 107 |
|
| 108 |
local function test_first_of_type(el, val) |
| 109 |
local type_pos = el.type_nth |
| 110 |
if not type_pos then _, type_pos = make_nth(el) end |
| 111 |
return type_pos == 1 |
| 112 |
end |
| 113 |
|
| 114 |
local function test_last_child(el, val) |
| 115 |
local el_pos = el.nth or make_nth(el) |
| 116 |
-- number of child elements is saved in the parent element |
| 117 |
-- by make_nth function |
| 118 |
local parent = el:get_parent() |
| 119 |
return el_pos == parent.child_elements |
| 120 |
end |
| 121 |
|
| 122 |
local function test_last_of_type(el, val) |
| 123 |
local type_pos = el.type_nth |
| 124 |
if not type_pos then _, type_pos = make_nth(el) end |
| 125 |
-- get table with type counts in this sibling list |
| 126 |
local parent = el:get_parent() |
| 127 |
local element_types = parent.element_types or {} |
| 128 |
local type_count = element_types[el:get_element_name()] |
| 129 |
return type_pos == type_count |
| 130 |
end |
| 131 |
|
| 132 |
-- test element for nth-child selector |
| 133 |
local function test_nth_child(el, nth) |
| 134 |
local el_pos = el.nth or make_nth(el) |
| 135 |
-- we support only the nth-child(number) form |
| 136 |
return el_pos == tonumber(nth) |
| 137 |
end |
| 138 |
|
| 139 |
-- test if element has attribute |
| 140 |
local function test_attr(el, attr) |
| 141 |
local result = el:get_attribute(attr) |
| 142 |
return result~=nil |
| 143 |
end |
| 144 |
|
| 145 |
local function test_any(el, value) |
| 146 |
-- * selector |
| 147 |
return true |
| 148 |
end |
| 149 |
|
| 150 |
-- test attribute values |
| 151 |
local function test_attr_value(el, parts) |
| 152 |
-- parts is a table: {attr_name, modifier, search value} |
| 153 |
local _, name, modifier, search = table.unpack(parts) |
| 154 |
local value = el:get_attribute(name) |
| 155 |
if value == nil then return false end |
| 156 |
-- make sure we deal with a string |
| 157 |
value = tostring(value) |
| 158 |
-- make the search string safe for pattern matching |
| 159 |
local escaped_search = search:gsub("([%(%)%.%%%+%-%#%*%?%[%^%$])", "%%%1") |
| 160 |
if modifier == "" then |
| 161 |
return value == search |
| 162 |
elseif modifier == "|" then |
| 163 |
-- attribute must be exactly the value or start with value + "-" |
| 164 |
return value == search or (value:match("^" .. escaped_search .. "%-")~=nil) |
| 165 |
elseif modifier == "~" then |
| 166 |
-- test any word |
| 167 |
for word in value:gmatch("(%S+)") do |
| 168 |
if word == search then return true end |
| 169 |
end |
| 170 |
return false |
| 171 |
elseif modifier == "^" then |
| 172 |
-- value start |
| 173 |
return value:match("^" .. escaped_search) ~= nil |
| 174 |
elseif modifier == "$" then |
| 175 |
-- value ends |
| 176 |
return value:match(escaped_search .. "$") ~= nil |
| 177 |
elseif modifier == "*" then |
| 178 |
-- value anywhere |
| 179 |
return value:match(escaped_search) ~= nil |
| 180 |
end |
| 181 |
end |
| 182 |
|
| 183 |
--- Test prepared querylist |
| 184 |
-- @param domobj DOM element to test |
| 185 |
-- @param querylist [optional] List of queries to test |
| 186 |
-- @return table with CSS queries, which match the selected DOM element |
| 187 |
function CssQuery:match_querylist(domobj, querylist) |
| 188 |
local matches = {} |
| 189 |
-- querylist can be explicit, saved queries can be used otherwise |
| 190 |
local querylist = querylist or self.querylist |
| 191 |
|
| 192 |
local function test_part(key, value, el) |
| 193 |
-- print("testing", key, value, el:get_element_name()) |
| 194 |
if key == "tag" then |
| 195 |
return el:get_element_name() == value |
| 196 |
elseif key == "id" then |
| 197 |
local id = el:get_attribute "id" |
| 198 |
return id and id == value |
| 199 |
elseif key == "class" then |
| 200 |
local class = el:get_attribute "class" |
| 201 |
if not class then return false end |
| 202 |
local c = {} |
| 203 |
for part in class:gmatch "([^%s]+)" do |
| 204 |
c[part] = true |
| 205 |
end |
| 206 |
return c[value] == true |
| 207 |
elseif key == "nth-child" then |
| 208 |
return test_nth_child(el, value) |
| 209 |
elseif key == "first-child" then |
| 210 |
return test_first_child(el, value) |
| 211 |
elseif key == "first-of-type" then |
| 212 |
return test_first_of_type(el, value) |
| 213 |
elseif key == "last-child" then |
| 214 |
return test_last_child(el, value) |
| 215 |
elseif key == "last-of-type" then |
| 216 |
return test_last_of_type(el, value) |
| 217 |
elseif key == "attr" then |
| 218 |
return test_attr(el, value) |
| 219 |
elseif key == "attr_value" then |
| 220 |
return test_attr_value(el, value) |
| 221 |
elseif key == "any" then |
| 222 |
return test_any(el, value) |
| 223 |
elseif key == "combinator" then |
| 224 |
-- ignore combinators in this function |
| 225 |
else |
| 226 |
if type(value) == "table" then value = table.concat(value, ":") end |
| 227 |
self:debug_print("unsupported feature", key, value) |
| 228 |
return false |
| 229 |
end |
| 230 |
-- TODO: Add more cases |
| 231 |
-- just return true for not supported selectors |
| 232 |
return true |
| 233 |
end |
| 234 |
|
| 235 |
local function test_object(query, el) |
| 236 |
-- test one object in CSS selector |
| 237 |
local matched = {} |
| 238 |
-- for key, value in pairs(query) do |
| 239 |
for _, part in ipairs(query) do |
| 240 |
local key, value = part.key, part.value |
| 241 |
local test = test_part(key, value, el) |
| 242 |
if test~= true then return false end |
| 243 |
matched[#matched+1] = test |
| 244 |
end |
| 245 |
if #matched == 0 then return false end |
| 246 |
for k, v in ipairs(matched) do |
| 247 |
if v ~= true then return false end |
| 248 |
end |
| 249 |
return true |
| 250 |
end |
| 251 |
|
| 252 |
|
| 253 |
-- get next CSS selector |
| 254 |
local function get_next_selector(query) |
| 255 |
local query = query or {} |
| 256 |
local selector = table.remove(query) |
| 257 |
return selector |
| 258 |
end |
| 259 |
|
| 260 |
local function get_next_combinator(query) |
| 261 |
local query = query or {} |
| 262 |
local combinator = " " -- default combinator |
| 263 |
local selector = query[#query] -- get the last item in selector query |
| 264 |
if not selector then return nil end |
| 265 |
-- detect if this selector is a combinator" |
| 266 |
-- combinator object must have only one part, so we can assume that it is in the first part |
| 267 |
if selector and selector[1].key == "combinator" then |
| 268 |
-- save the combinator and select next selector from the query |
| 269 |
combinator = selector[1].value |
| 270 |
table.remove(query) -- remove combinator from query |
| 271 |
end |
| 272 |
return combinator |
| 273 |
end |
| 274 |
|
| 275 |
local function get_previous_element(el) |
| 276 |
-- try to find a previous element |
| 277 |
local prev = el:get_prev_node() |
| 278 |
if not prev then return nil end |
| 279 |
if prev:is_element() then return prev end |
| 280 |
return get_previous_element(prev) |
| 281 |
end |
| 282 |
|
| 283 |
|
| 284 |
local function match_query(query, el) |
| 285 |
local function match_parent(query, el) |
| 286 |
-- loop over the whole element tree and try to mach the css selector |
| 287 |
if el and el:is_element() then |
| 288 |
local query = query or {} |
| 289 |
local object = query[#query] |
| 290 |
local status = test_object(object, el) |
| 291 |
return status or match_parent(query, el:get_parent()) |
| 292 |
else |
| 293 |
-- break processing if we reach top of the element tree |
| 294 |
return false |
| 295 |
end |
| 296 |
end |
| 297 |
local function match_sibling(query, el) |
| 298 |
-- match potentially more distant sibling |
| 299 |
if el and el:is_element() then |
| 300 |
return match_query(query, el) or match_query(query, get_previous_element(el)) |
| 301 |
else |
| 302 |
return false |
| 303 |
end |
| 304 |
end |
| 305 |
local object = get_next_selector(query) -- get current object from the query stack |
| 306 |
if not object then return true end -- if the query stack is empty, then we can be sure that it matched previous items |
| 307 |
if not el or not el:is_element() then return false end -- if there is object to test, but current node isn't element, test failed |
| 308 |
local result = test_object(object, el) |
| 309 |
if result then |
| 310 |
local combinator = get_next_combinator(query) |
| 311 |
if combinator == " " then |
| 312 |
-- we must traverse all parent elements to find if any matches |
| 313 |
return match_parent(query, el:get_parent()) |
| 314 |
elseif combinator == ">" then -- simplest case, we need to match just the direct parent |
| 315 |
return match_query(query, el:get_parent()) |
| 316 |
elseif combinator == "+" then -- match previous element |
| 317 |
return match_query(query, get_previous_element(el)) |
| 318 |
elseif combinator == "~" then -- match all previous elements |
| 319 |
return match_sibling(query, get_previous_element(el)) |
| 320 |
elseif combinator == nil then |
| 321 |
return result |
| 322 |
end |
| 323 |
end |
| 324 |
return false |
| 325 |
end |
| 326 |
|
| 327 |
for _,element in ipairs(querylist) do |
| 328 |
local query = {} |
| 329 |
for k,v in ipairs(element.query) do query[k] = v end |
| 330 |
if #query > 0 then -- don't try to match empty query |
| 331 |
local result = match_query(query, domobj) |
| 332 |
if result then matches[#matches+1] = element end |
| 333 |
end |
| 334 |
end |
| 335 |
return matches |
| 336 |
end |
| 337 |
|
| 338 |
--- Get elements that match the selector |
| 339 |
-- @return table with DOM_Object elements |
| 340 |
function CssQuery:get_selector_path( |
| 341 |
domobj, -- DOM_Object |
| 342 |
selectorlist -- querylist table created using CssQuery:prepare_selector |
| 343 |
) |
| 344 |
local nodelist = {} |
| 345 |
domobj:traverse_elements(function(el) |
| 346 |
local matches = self:match_querylist(el, selectorlist) |
| 347 |
self:debug_print("Matching " .. el:get_element_name() .." "..#matches) |
| 348 |
if #matches > 0 then nodelist[#nodelist+1] = el |
| 349 |
end |
| 350 |
end) |
| 351 |
return nodelist |
| 352 |
end |
| 353 |
|
| 354 |
--- Parse CSS selector to a query table. |
| 355 |
-- XML namespaces can be supported using |
| 356 |
-- namespace|element syntax |
| 357 |
-- @return table querylist |
| 358 |
function CssQuery:prepare_selector( |
| 359 |
selector -- string CSS selector query |
| 360 |
) |
| 361 |
local querylist = {} |
| 362 |
local function parse_selector(item) |
| 363 |
local query = {} |
| 364 |
-- for i = #item, 1, -1 do |
| 365 |
-- local part = item[i] |
| 366 |
for _, part in ipairs(item) do |
| 367 |
local t = {} |
| 368 |
for _, atom in ipairs(part) do |
| 369 |
|
| 370 |
local key = atom[1] |
| 371 |
local value |
| 372 |
if not atom[3] then |
| 373 |
value = atom[2] |
| 374 |
else |
| 375 |
-- save additional selector parts when available |
| 376 |
value = atom |
| 377 |
end |
| 378 |
-- support for XML namespaces in selectors |
| 379 |
-- the namespace should be added using "|" |
| 380 |
-- like namespace|element |
| 381 |
if key=="tag" then |
| 382 |
-- LuaXML doesn't support namespaces, so it is necessary |
| 383 |
-- to match namespace:element |
| 384 |
value=value:gsub("|", ":") |
| 385 |
end |
| 386 |
t[#t+1] = {key=key, value=value} |
| 387 |
end |
| 388 |
query[#query + 1] = t |
| 389 |
end |
| 390 |
return query |
| 391 |
end |
| 392 |
-- for item in selector:gmatch("([^%s]+)") do |
| 393 |
-- elements[#elements+1] = parse_selector(item) |
| 394 |
-- end |
| 395 |
local parts = parse_query.parse_query(selector) or {} |
| 396 |
-- several selectors may be separated using ",", we must process them separately |
| 397 |
local sources = selector:explode(",") |
| 398 |
for i, part in ipairs(parts) do |
| 399 |
querylist[#querylist+1] = {query = parse_selector(part), source = sources[i]} |
| 400 |
end |
| 401 |
return querylist |
| 402 |
end |
| 403 |
|
| 404 |
--- Add selector to CSS object list of selectors, |
| 405 |
-- func is called when the selector matches a DOM object |
| 406 |
-- params is table which will be passed to the func |
| 407 |
-- @return integer number of elements in the prepared selector |
| 408 |
function CssQuery:add_selector( |
| 409 |
selector, -- CSS selector string |
| 410 |
func, -- function which will be executed on matched elements |
| 411 |
params -- table with parameters for the function |
| 412 |
) |
| 413 |
local selector_list = self:prepare_selector(selector) |
| 414 |
for k, query in ipairs(selector_list) do |
| 415 |
query.specificity = self:calculate_specificity(query) |
| 416 |
query.func = func |
| 417 |
query.params = params |
| 418 |
table.insert(self.querylist, query) |
| 419 |
end |
| 420 |
self:sort_querylist() |
| 421 |
return #selector_list |
| 422 |
end |
| 423 |
|
| 424 |
--- Sort selectors according to their specificity |
| 425 |
-- It is called automatically when the selector is added |
| 426 |
-- @return querylist table |
| 427 |
function CssQuery:sort_querylist( |
| 428 |
querylist -- [optional] querylist table |
| 429 |
) |
| 430 |
local querylist = querylist or self.querylist |
| 431 |
table.sort(self.querylist, function(a,b) |
| 432 |
return a.specificity > b.specificity |
| 433 |
end) |
| 434 |
return querylist |
| 435 |
end |
| 436 |
|
| 437 |
--- Remove selector from the CSS list object. |
| 438 |
-- All actions that literally match the given selector will be removed. |
| 439 |
function CssQuery:remove_selector( |
| 440 |
selector -- CSS selector to be removed |
| 441 |
) |
| 442 |
for pos, obj in ipairs(self.querylist) do |
| 443 |
if obj.source == selector then |
| 444 |
table.remove(self.querylist, pos) |
| 445 |
end |
| 446 |
end |
| 447 |
end |
| 448 |
|
| 449 |
--- It tests list of queries against a DOM element and executes the |
| 450 |
--- corresponding function that is saved for the matched query. |
| 451 |
-- @return nothing |
| 452 |
function CssQuery:apply_querylist( |
| 453 |
domobj, -- DOM element |
| 454 |
querylist -- querylist table |
| 455 |
) |
| 456 |
for _, query in ipairs(querylist) do |
| 457 |
-- use default empty function which will pass to another match |
| 458 |
local func = query.func or function() return true end |
| 459 |
local params = query.params or {} |
| 460 |
local status = func(domobj, params) |
| 461 |
-- break the execution when the function return false |
| 462 |
if status == false then |
| 463 |
break |
| 464 |
end |
| 465 |
end |
| 466 |
end |
| 467 |
|
| 468 |
return setmetatable({}, CssQuery) |
| 469 |
end |
| 470 |
|
| 471 |
return cssquery |