Make formspec_ast.flatten ignore scroll containers
This commit is contained in:
parent
b56162912d
commit
8b18059cdf
16
helpers.lua
16
helpers.lua
|
@ -42,9 +42,7 @@ function formspec_ast.interpret(spec, custom_handlers)
|
||||||
return formspec_ast.unparse(ast)
|
return formspec_ast.unparse(ast)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Returns an iterator over all nodes in a formspec AST, including ones in
|
local function walk_inner(tree, container_elems)
|
||||||
-- containers.
|
|
||||||
function formspec_ast.walk(tree)
|
|
||||||
local parents = {}
|
local parents = {}
|
||||||
local i = 1
|
local i = 1
|
||||||
return function()
|
return function()
|
||||||
|
@ -59,7 +57,7 @@ function formspec_ast.walk(tree)
|
||||||
end
|
end
|
||||||
i = i + 1
|
i = i + 1
|
||||||
|
|
||||||
if res.type == 'container' or res.type == 'scroll_container' then
|
if container_elems[res.type] then
|
||||||
table.insert(parents, {tree, i})
|
table.insert(parents, {tree, i})
|
||||||
tree = res
|
tree = res
|
||||||
i = 1
|
i = 1
|
||||||
|
@ -68,6 +66,13 @@ function formspec_ast.walk(tree)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Returns an iterator over all nodes in a formspec AST, including ones in
|
||||||
|
-- containers.
|
||||||
|
local container_elems = {container = true, scroll_container = true}
|
||||||
|
function formspec_ast.walk(tree)
|
||||||
|
return walk_inner(tree, container_elems)
|
||||||
|
end
|
||||||
|
|
||||||
-- Similar to formspec_ast.walk(), however only returns nodes which have a type
|
-- Similar to formspec_ast.walk(), however only returns nodes which have a type
|
||||||
-- of `node_type`.
|
-- of `node_type`.
|
||||||
function formspec_ast.find(tree, node_type)
|
function formspec_ast.find(tree, node_type)
|
||||||
|
@ -114,9 +119,10 @@ function formspec_ast.apply_offset(elems, x, y)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Removes container elements and fixes nodes inside containers.
|
-- Removes container elements and fixes nodes inside containers.
|
||||||
|
local flatten_containers = {container = true}
|
||||||
function formspec_ast.flatten(tree)
|
function formspec_ast.flatten(tree)
|
||||||
local res = {formspec_version=tree.formspec_version}
|
local res = {formspec_version=tree.formspec_version}
|
||||||
for elem in formspec_ast.walk(table.copy(tree)) do
|
for elem in walk_inner(table.copy(tree), flatten_containers) do
|
||||||
if elem.type == 'container' then
|
if elem.type == 'container' then
|
||||||
formspec_ast.apply_offset(elem, elem.x, elem.y)
|
formspec_ast.apply_offset(elem, elem.x, elem.y)
|
||||||
else
|
else
|
||||||
|
|
17
tests.lua
17
tests.lua
|
@ -345,4 +345,21 @@ assert_equal(
|
||||||
}))
|
}))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
-- Ensure flatten works correctly
|
||||||
|
assert_equal(
|
||||||
|
'label[0,0;abc]label[2,2;def]scroll_container[1,1;2,2;test;vertical]' ..
|
||||||
|
'image[1,1;1,1;def]scroll_container_end[]',
|
||||||
|
formspec_ast.unparse(formspec_ast.flatten(assert(formspec_ast.parse([[
|
||||||
|
label[0,0;abc]
|
||||||
|
container[3,2]
|
||||||
|
container[-1,0]
|
||||||
|
label[0,0;def]
|
||||||
|
container_end[]
|
||||||
|
container_end[]
|
||||||
|
scroll_container[1,1;2,2;test;vertical]
|
||||||
|
image[1,1;1,1;def]
|
||||||
|
scroll_container_end[]
|
||||||
|
]]))))
|
||||||
|
)
|
||||||
|
|
||||||
print('Tests pass')
|
print('Tests pass')
|
||||||
|
|
Loading…
Reference in New Issue