-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathnode-set.lua
More file actions
151 lines (131 loc) · 3.28 KB
/
node-set.lua
File metadata and controls
151 lines (131 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
local NodeSet = {}
local methods = {}
local metatable = {}
function metatable.__index(node_set, key)
return methods[key]
end
function metatable.__add(added_node_set, add_node_set)
return methods.merge(added_node_set, add_node_set)
end
local function map(values, func)
local converted_values = {}
for i, value in ipairs(values) do
local converted_value = func(value)
table.insert(converted_values, converted_value)
end
return converted_values
end
function methods:to_xml(options)
return table.concat(map(self,
function(node)
return node:to_xml(options)
end),
"")
end
function methods:to_html(options)
return table.concat(map(self,
function(node)
return node:to_html(options)
end),
"")
end
function methods:search(xpath)
local node_set = nil
for _, node in ipairs(self) do
if node_set == nil then
node_set = node:search(xpath)
else
node_set = node_set + node:search(xpath)
end
end
return node_set
end
function methods:css_select(css_selectors)
local node_set = nil
for _, node in ipairs(self) do
if node_set == nil then
node_set = node:css_select(css_selectors)
else
node_set = node_set + node:css_select(css_selectors)
end
end
return node_set
end
function methods:content()
return table.concat(map(self,
function(node)
return node:content() or ""
end),
"")
end
methods.text = methods.content
function methods:paths()
return map(self,
function(node)
return node:path()
end)
end
function methods:insert(node_or_position, node)
local inserted_node = nil
local position = nil
if node == nil then
inserted_node = node_or_position
else
position = node_or_position
inserted_node = node
end
for i, self_node in ipairs(self) do
if self_node.node == inserted_node.node then
return
end
end
if position == nil then
table.insert(self, inserted_node)
else
table.insert(self, position, inserted_node)
end
end
function methods:remove(node_or_position)
if type(node_or_position) == "number" then
local position = node_or_position
return table.remove(self, position)
else
local node = node_or_position
for position, self_node in ipairs(self) do
if self_node.node == node.node then
return table.remove(self, position)
end
end
return nil
end
end
function methods:unlink()
for _, node in ipairs(self) do
node:unlink()
end
end
local function is_included(node_set, search_node)
for _, node in ipairs(node_set) do
if node.node == search_node.node then
return true
end
end
return false
end
function methods:merge(node_set)
local raw_node_set = {}
for _, node in ipairs(self) do
table.insert(raw_node_set, node)
end
for _, node in ipairs(node_set) do
if not is_included(self, node) then
table.insert(raw_node_set, node)
end
end
return NodeSet.new(raw_node_set)
end
function NodeSet.new(nodes)
setmetatable(nodes, metatable)
return nodes
end
return NodeSet