summaryrefslogtreecommitdiff
path: root/macros/luatex/generic/luaxml/luaxml-domobject.lua
diff options
context:
space:
mode:
Diffstat (limited to 'macros/luatex/generic/luaxml/luaxml-domobject.lua')
-rw-r--r--macros/luatex/generic/luaxml/luaxml-domobject.lua87
1 files changed, 77 insertions, 10 deletions
diff --git a/macros/luatex/generic/luaxml/luaxml-domobject.lua b/macros/luatex/generic/luaxml/luaxml-domobject.lua
index a5c0d29fee..73689add5a 100644
--- a/macros/luatex/generic/luaxml/luaxml-domobject.lua
+++ b/macros/luatex/generic/luaxml/luaxml-domobject.lua
@@ -2,13 +2,26 @@
-- @module luaxml-domobject
-- @author Michal Hoftich <michal.h21@gmail.com
local dom = {}
-local xml = require("luaxml-mod-xml")
-local handler = require("luaxml-mod-handler")
-local css_query = require("luaxml-cssquery")
+
+local xml
+local handler
+local css_query
+if kpse then
+ xml = require("luaxml-mod-xml")
+ handler = require("luaxml-mod-handler")
+ css_query = require("luaxml-cssquery")
+else
+ xml = require("luaxml.mod-xml")
+ handler = require("luaxml.mod-handler")
+ css_query = require("luaxml.cssquery")
+end
local void = {area = true, base = true, br = true, col = true, hr = true, img = true, input = true, link = true, meta = true, param = true}
+-- support also upper case names
+for k,v in pairs(void) do void[string.upper(k)] = true end
+
local escapes = {
[">"] = "&gt;",
["<"] = "&lt;",
@@ -266,6 +279,8 @@ local parse = function(
for _, el in ipairs(current:get_children()) do
if el:is_text() then
text[#text+1] = el._text or ""
+ elseif el._type == "CDATA" then
+ text[#text+1] = el._text or ""
elseif el:is_element() then
text[#text+1] = el:get_text()
end
@@ -339,11 +354,12 @@ local parse = function(
return el._parent
end
- --- Execute function on the current element and all it's children elements.
+ --- Execute function on the current element and all it's children nodes.
+ -- The differenct to DOM_Object:traverse_elements() is that it executes the function
+ -- also on text nodes and all other kinds of XML nodes.
-- The traversing of child elements of a given node can be disabled when the executed
-- function returns false.
- -- @return nothing
- function DOM_Object:traverse_elements(
+ function DOM_Object:traverse(
fn, --- function which will be executed on the current element and all it's children
current --- [optional] element to be selected
)
@@ -353,17 +369,68 @@ local parse = function(
current = self:root_node()
end
local status = true
- if self:is_element(current) or self:get_node_type(current) == "ROOT"then
- local status = fn(current)
+ local status = fn(current)
+ if current:is_element() or current:get_node_type() == "ROOT" then
-- don't traverse child nodes when the user function return false
if status ~= false then
- for _, child in ipairs(self:get_children(current)) do
- self:traverse_elements(fn, child)
+ for _, child in ipairs(current:get_children()) do
+ self:traverse(fn, child)
end
end
end
end
+ --- Execute function on the current element and all it's children elements.
+ --- The traversing of child elements of a given node can be disabled when the executed
+ -- function returns false.
+ -- @return nothing
+ function DOM_Object:traverse_elements(
+ fn, --- function which will be executed on the current element and all it's children
+ current --- [optional] element to be selected
+ )
+ local current = current or self --
+ current:traverse(function(node)
+ if node:is_element() or node:get_node_type() == "ROOT" then
+ fn(node)
+ end
+ end)
+ end
+
+ --- Get table with the inner text of an element, every text node is a separate table item.
+ --- @return table
+ function DOM_Object:strings(
+ current --- [optional] element to be selected
+ )
+ local strings = {}
+ local current = current or self
+ current:traverse(function(node)
+ if node:get_node_type() == "TEXT" then
+ table.insert(strings, node._text or "")
+ end
+ end)
+ return strings
+ end
+
+ --- Get table with the inner text of an element -- leading and trailing spaces are removed and elements that contain only white space are ignored.
+ --- @return table
+ function DOM_Object:stripped_strings(
+ current --- [optional] element to be selected
+ )
+ local current = current or self
+ local strings = current:strings()
+ local cleaned = {}
+ for k,v in ipairs(strings) do
+ v = v:gsub("^%s*", ""):gsub("%s*$", "")
+ if v ~= "" then
+ table.insert(cleaned, v)
+ end
+ end
+ return cleaned
+ end
+
+
+
+
--- Execute function on list of elements returned by DOM_Object:get_path()
function DOM_Object:traverse_node_list(
nodelist --- table with nodes selected by DOM_Object:get_path()