-
Notifications
You must be signed in to change notification settings - Fork 0
/
lQuery.lua
462 lines (437 loc) · 11.1 KB
/
lQuery.lua
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
local lQuery = {}
_G.lQuery = lQuery
_G.l = _G.lQuery
function lQuery.toArray(tab)
local arr = {}
for n,v in pairs(tab) do
table.insert(arr, v)
end
return arr
end
function lQuery.mergeTable(nt, ...)
for i,v in ipairs({...}) do
for i2,v2 in ipairs(v) do
table.insert(nt, v2)
end
end
return nt
end
--Recursively gets children along a tree
function lQuery.recurChilds(...)
local nloop = {...}
local found = {}
while #nloop>0 do
table.insert(found, nloop[1])
for i,v in ipairs(nloop[1]:GetChildren()) do
table.insert(nloop, v)
end
table.remove(nloop,1)
end
return found
end
--Recursively gets parents along a tree
function lQuery.recurParents(...)
local nloop = {...}
local found = {}
while #nloop>0 do
table.insert(found, nloop[1])
table.insert(nloop, nloop[1].Parent)
table.remove(nloop,1)
end
return found
end
local QuerySet = {}
lQuery.fn = QuerySet
function QuerySet:set(p, v)
for x,i in ipairs(self._items) do
if type(p)=="table" then
for p2,v2 in pairs(p) do
self:set(p2,v2)
end
else
pcall(function() i[p]=v end)
end
end
return self
end;
function QuerySet:get(p)
for x,i in ipairs(self._items) do
local w,v = pcall(function() return i[p] end)
if w then return v end
end
return nil
end;
function QuerySet:each(f)
for x,i in ipairs(self._items) do
f(i, x)
end
return self
end;
function QuerySet:select()
game.Selection:Set(self._items)
return self
end;
function QuerySet:remove(sel)
for n,v in pairs(lQuery.selectorMatchList(sel or "*", self._items)) do
v:Remove()
end
return self
end;
function QuerySet:add(el)
return self + el
end;
function QuerySet:count()
return #self._items
end;
function QuerySet:insert(typ, attrs)
local ni = type(typ)=="string" and Instance.new(typ) or typ:Clone()
lQuery(ni):set(attrs)
for x,i in ipairs(self._items) do
ni:Clone().Parent = i
end
return self
end;
function QuerySet:__add(oth)
if type(oth)=="table" then
if getmetatable(oth)==QuerySet then
oth = oth._items
end
return lQuery(lQuery.mergeTable({}, self._items, oth))
elseif type(oth)=="userdata" then
return lQuery(lQuery.mergeTable({}, self._items, {oth}))
end
end;
function QuerySet:__call() --Shortcut for :select()
self:select()
end;
--Printing
function QuerySet:pcount()
print(self:count())
end;
--Traversing
function QuerySet:children(sel) --Get the children of each element in the set of matched elements, optionally filtered by a selector.
local nq = {}
self:each(function(inst)
lQuery.mergeTable(nq, inst:GetChildren())
end)
if sel ~= nil then
nq = lQuery.selectorMatchList(sel, nq)
end
return lQuery(nq)
end;
function QuerySet:find(sel) --Get the descendants of each element in the current set of matched elements, filtered by a selector.
local nq = {}
self:each(function(inst)
lQuery.mergeTable(nq, lQuery.execSelector(sel, inst))
end)
return lQuery(nq)
end;
function QuerySet:filter(sel) --Reduce the set of matched elements to those that match the selector or pass the function’s test.
local nq={}
if type(sel)=="function" then
self:each(function(inst)
if sel(inst) then
table.insert(nq, inst)
end
end)
else
nq = lQuery.selectorMatchList(sel, self._items)
end
return lQuery(nq)
end;
function QuerySet:parent(sel) --Get the parent of each element in the current set of matched elements, optionally filtered by a selector.
local nq = {}
self:each(function(inst)
table.insert(nq, inst.Parent)
end)
if sel ~= nil then
nq = lQuery.selectorMatchList(sel, nq)
end
return lQuery(nq)
end
function QuerySet:siblings(sel) --Get the siblings of each element in the set of matched elements, optionally filtered by a selector. (Does not include the original child)
local nq = {}
self:each(function(inst)
local c = inst.Parent
if c~=nil then
for i,v in ipairs(c:GetChildren()) do
if v~=inst then
table.insert(nq, v)
end
end
end
end)
if sel ~= nil then
nq = lQuery.selectorMatchList(sel, nq)
end
return lQuery(nq)
end;
QuerySet.__index = QuerySet;
local ws = "%s" --Whitespace
local nf = "[%w_]" --Name Fields
local excl = ">"
local selectors = {
{ --Property/value
pat = "%["..ws.."*("..nf.."+)"..ws.."*([|%*~!%^%$]?=)"..ws.."*['\"](.*)['\"]"..ws.."*%]";
f = function(obj, fld, opp, val)
local suc, fstr = pcall(function() return tostring(obj[fld]) end)
if suc then
if opp=="|=" then
return fstr:sub(1, val:len()+1)==val.."-"
elseif opp=="*=" then
return fstr:match(val)~=nil
elseif opp=="~=" then
return string.match(" "..fstr.." ", "%s"..val.."%s")~=nil
elseif opp=="^=" then
return fstr:match("^"..val)~=nil
elseif opp=="$=" then
return fstr:match(val.."$")~=nil
elseif opp=="!=" then
return fstr~=val
elseif opp=="=" then
return fstr == val;
end
end
end;
};
{ --Has/doesn't have property
pat = "%["..ws.."*("..nf..")"..ws.."*(!?)"..ws.."*%]";
f = function(obj, fld, opp)
local has, ret = pcall(function() return obj[fld] end)
return (opp=="!")==(not has)
end;
};
{ --Has 1+ children matching a selector
pat = ":has(%b())";
f = function(obj, subsel)
local nsel = subsel:sub(2,-2)
return #lQuery.toArray(lQuery.selectorMatchList(nsel, lQuery.recurChilds(unpack(obj:GetChildren())))) > 0
end;
};
{ --Selects all elements that do NOT match the given selector
pat = ":not(%b())";
multi = true;
f = function(objs, umobjs, subsel)
local nsel = subsel:sub(2,-2)
for n,v in pairs(lQuery.selectorMatchList(nsel, objs)) do
objs[n] = nil
umobjs[n] = v
end
return objs
end;
};
{ --
pat = ":first%-child";
f = function(obj)
return obj == obj.Parent:GetChildren()[1]
end;
};
{ --
pat = ":last%-child";
f = function(obj)
local chldrn = obj.Parent:GetChildren()
return obj == chldrn[#chldrn]
end;
};
{ --
pat = ":only%-child";
f = function(obj) return #obj.Parent:GetChildren()==1 end;
};
{ --No Children
pat = ":empty";
f = function(obj) return #obj:GetChildren()==0 end;
};
{ --Has children
pat = ":parent";
f = function(obj) return #obj:GetChildren()>0 end;
};
{ --Name
pat = "#([%$%^]?)("..nf.."+)";
f = function(obj, opp, nm)
if opp=="$" then
return obj.Name:lower()==nm:lower()
elseif opp=="^" then
return obj.Name:upper()==nm:upper()
end
return obj.Name==nm
end;
};
{ --className/type
pat = "%.(%^?)([%w_]+)";
f = function(obj, sub, cnm)
if sub=="^" then return obj:IsA(cnm) else
return obj.className==cnm end
end;
};
{ --Everything
pat = "%*";
f = function() return true end;
};
-- { --Direct Child
-- pat = "(.*)>"; -- Unfortunately the (.*) in the front causes confusion when used in quotes or ()
-- terminate = true;
-- multi = true;
-- consume = true;
-- f = function(objs, umobjs, subsel)
-- return lQuery.selectorMatchList(subsel, objs)
-- end
-- };
}
local function selBlockMatchMulti(section, left_objs)
local mobjs = left_objs
local umobjs = {} --Just used for taking the parents and trying again with them
for i,v in ipairs(section) do
if v.sel.multi then
mobjs = v.sel.f(mobjs, umobjs, unpack(v.args))
else
local objs = mobjs
mobjs = {}
for initial, obj in pairs(objs) do
if v.sel.f(obj, unpack(v.args)) then
mobjs[initial] = obj
elseif not v.sel.terminate then
umobjs[initial] = obj
end
end
end
end
return mobjs, umobjs
end
local function extractSelectors(sel)
local path = {{}}
local paths = {path}
local tsel = sel
local going = true
while going do
going = false
if (tsel:sub(1,1)==" ") then
going = true
if #(path[#path])>0 then table.insert(path, {}) end
tsel = tsel:sub(2)
elseif (tsel:sub(1,1)==",") then
going = true
table.insert(paths, {{}})
path = paths[#paths]
tsel = tsel:sub(2)
elseif (tsel:sub(1,1)==">") then
going = true
table.insert(path, ">")
tsel = tsel:sub(2)
else
for sp,sd in ipairs(selectors) do
while true do
local a,b = string.find(tsel, '^'..sd.pat) --(sd.match and sd:match(tsel)) or
if a ~= nil then
local part, rpart = string.sub(tsel, a,b), string.sub(tsel, b+1);
going = true
table.insert(path[#path], {
sel = sd;
args = {part:match(sd.pat)}; --(sd.extract and sd:extract(part)) or
comsumes = consumeds;
})
tsel = string.sub(tsel, 0,a-1)..string.sub(tsel, b+1)
else break
end
end
end
end
end
assert(string.len(tsel)==0, "Malformed Selector! Remnant: "..tsel) --Raise an error if there is a part that was not matched to anything
local cpaths = {}
for n, pth in ipairs(paths) do
local tc = {}
for i,v in ipairs(pth) do
if type(v)~="table" or #v>0 then
table.insert(tc, v)
end
end
if #tc > 0 then
table.insert(cpaths, tc)
end
end
return cpaths
end
local function testSelectorsMulti(paths, objs)
local fmatches = {}
for _, path in ipairs(paths) do
local pthEl = #path
local next_iter = {}
for i,v in pairs(objs) do
if type(i) == "number" then i = v end
next_iter[i] = v
end
while true do
local lsel = path[pthEl]
local c_iter = next_iter
next_iter = {}
local nmc = 0
if lsel==">" then
local nst = {}
for i,v in ipairs(path) do
if i<pthEl then
table.insert(nst, v)
else break end
end
next_iter = testSelectorsMulti({nst}, c_iter)
pthEl = 1
else
local matches, non_matches = {}, {}
repeat --Eliminate everything we can
matches, non_matches = selBlockMatchMulti(lsel, c_iter)
for initial, cobj in pairs(matches) do --Capture items that were successfully matched and queue them for the next selector
if not (cobj==game or cobj.Parent==nil) then --We've reached the top of the tree and can't go further
next_iter[initial] = cobj.Parent
end
end
c_iter = {}
nmc = 0
for initial, cobj in pairs(non_matches) do
if (not (cobj==game or cobj.Parent==nil)) and pthEl ~= #path then --We've reached the top of the tree and can't go further --Last selector must match the input obj
c_iter[initial] = cobj.Parent
nmc = nmc+1
end
end
until nmc == 0
end
pthEl = pthEl-1
if pthEl == 0 then --We're done here.
for initial, cobj in pairs(next_iter) do
fmatches[initial] = initial
end
break
end
end
end
return fmatches
end
lQuery.selectorMatch = function (sel, obj)
return #lQuery.selectorMatchList(paths, {obj})>0
end
lQuery.selectorMatchList = function (sel, objs)
local ft = {}
local paths = extractSelectors(sel)
return testSelectorsMulti(paths, objs)
end
lQuery.execSelector = function (sel, par)
local tree = lQuery.recurChilds(par)
local tbl = lQuery.selectorMatchList(sel, tree)
return lQuery.toArray(tbl)
end
local mt = {__call=function(self, inp, par)
local qo = {}
if par == nil then par = Workspace end
if inp==nil then
qo._items = game.Selection:Get()
elseif type(inp) == "string" then
qo._items = lQuery.execSelector(inp, par)
elseif type(inp) == "userdata" then
qo._items = {inp}
elseif type(inp) == "table" then
qo._items = {}
for i,v in pairs(inp) do table.insert(qo._items, v) end
end
setmetatable(qo, QuerySet)
return qo
end}
setmetatable(lQuery, mt);
return lQuery