mirror of
https://github.com/TangentFoxy/lua-htmlparser.git
synced 2025-07-28 02:52:19 +00:00
__call = select
This commit is contained in:
@@ -89,7 +89,7 @@ function ElementNode:close(closestart, closeend)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function ElementNode:select(s)
|
local function select(self, s)
|
||||||
if not s or type(s) ~= "string" then return {} end
|
if not s or type(s) ~= "string" then return {} end
|
||||||
local subjects = Set:new({self})
|
local subjects = Set:new({self})
|
||||||
local resultset
|
local resultset
|
||||||
@@ -116,4 +116,7 @@ function ElementNode:select(s)
|
|||||||
return resultset:tolist()
|
return resultset:tolist()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function ElementNode:select(s) return select(self, s) end
|
||||||
|
ElementNode.mt.__call = select
|
||||||
|
|
||||||
return ElementNode
|
return ElementNode
|
6
test.lua
6
test.lua
@@ -51,7 +51,7 @@ select("ul > *")
|
|||||||
select("body [class]")
|
select("body [class]")
|
||||||
select("body > [class]")
|
select("body > [class]")
|
||||||
|
|
||||||
local sel, chapters = root:select("ol.chapters > li"), {}
|
local sel, chapters = root("ol.chapters > li"), {}
|
||||||
for i,v in ipairs(sel) do
|
for i,v in ipairs(sel) do
|
||||||
table.insert(chapters, v:getcontent())
|
table.insert(chapters, v:getcontent())
|
||||||
end
|
end
|
||||||
@@ -60,10 +60,10 @@ for i,v in ipairs(chapters) do
|
|||||||
print(i, v)
|
print(i, v)
|
||||||
end
|
end
|
||||||
|
|
||||||
local sel, contacts = root:select("ul.contacts > li"), {}
|
local sel, contacts = root("ul.contacts > li"), {}
|
||||||
for i,v in ipairs(sel) do
|
for i,v in ipairs(sel) do
|
||||||
local c = {}
|
local c = {}
|
||||||
for fi,fv in ipairs(v:select("span[class]")) do
|
for fi,fv in ipairs(v("span[class]")) do
|
||||||
c[fv.classes[1]] = fv:getcontent()
|
c[fv.classes[1]] = fv:getcontent()
|
||||||
end
|
end
|
||||||
contacts[v.id] = c
|
contacts[v.id] = c
|
||||||
|
Reference in New Issue
Block a user