diff options
Diffstat (limited to 'Master/texmf-dist/tex/luatex/luaxml/luaxml-cssquery.lua')
-rw-r--r-- | Master/texmf-dist/tex/luatex/luaxml/luaxml-cssquery.lua | 62 |
1 files changed, 57 insertions, 5 deletions
diff --git a/Master/texmf-dist/tex/luatex/luaxml/luaxml-cssquery.lua b/Master/texmf-dist/tex/luatex/luaxml/luaxml-cssquery.lua index 558f72089bc..d353a310c47 100644 --- a/Master/texmf-dist/tex/luatex/luaxml/luaxml-cssquery.lua +++ b/Master/texmf-dist/tex/luatex/luaxml/luaxml-cssquery.lua @@ -3,6 +3,18 @@ -- @author Michal Hoftich <michal.h21@gmail.com local parse_query = require("luaxml-parse-query") +-- the string.explode function is provided by LuaTeX +-- this is alternative for stock Lua +-- source: http://lua-users.org/wiki/SplitJoin +local function string_split(str, sep) + local sep, fields = sep or ":", {} + local pattern = string.format("([^%s]+)", sep) + str:gsub(pattern, function(c) fields[#fields+1] = c end) + return fields +end + +string.explode = string.explode or string_split + --- CssQuery constructor -- @function cssquery -- @return CssQuery object @@ -42,6 +54,42 @@ local function cssquery() return specificity end + -- save element position in the current siblings list + local function make_nth(curr_el) + local pos = 0 + local el_pos = 0 + -- get current node list + local siblings = curr_el:get_siblings() + if siblings then + for _, other_el in ipairs(siblings) do + -- number the elements + if other_el:is_element() then + pos = pos + 1 + other_el.nth = pos + -- save the current element position + if other_el == curr_el then + el_pos = pos + end + end + end + else + return false + end + return el_pos + end + + local function test_first_child(el, nth) + local el_pos = el.nth or make_nth(el) + return el_pos == 1 + end + + -- test element for nth-child selector + local function test_nth_child(el, nth) + local el_pos = el.nth or make_nth(el) + -- we support only the nth-child(number) form + return el_pos == tonumber(nth) + end + --- Test prepared querylist -- @param domobj DOM element to test -- @param querylist [optional] List of queries to test @@ -66,6 +114,10 @@ local function cssquery() c[part] = true end return c[value] == true + elseif key == "nth-child" then + return test_nth_child(el, value) + elseif key == "first-child" then + return test_first_child(el, value) end -- TODO: Add more cases -- just return true for not supported selectors @@ -84,7 +136,7 @@ local function cssquery() end return true end - + local function match_query(query, el) local query = query or {} local object = table.remove(query) -- get current object from the query stack @@ -129,12 +181,12 @@ local function cssquery() -- @return table querylist function CssQuery:prepare_selector( selector -- string CSS selector query - ) + ) local querylist = {} local function parse_selector(item) local query = {} -- for i = #item, 1, -1 do - -- local part = item[i] + -- local part = item[i] for _, part in ipairs(item) do local t = {} for _, atom in ipairs(part) do @@ -191,7 +243,7 @@ local function cssquery() -- @return querylist table function CssQuery:sort_querylist( querylist -- [optional] querylist table - ) + ) local querylist = querylist or self.querylist table.sort(self.querylist, function(a,b) return a.specificity > b.specificity @@ -217,7 +269,7 @@ local function cssquery() end end end - + return setmetatable({}, CssQuery) end |