From e78df3e0f89c561691e2aecb5b8bcd1798ba990a Mon Sep 17 00:00:00 2001 From: Nathan Fritzler Date: Thu, 27 Jul 2023 11:19:15 -0600 Subject: [PATCH] feat(walk): Allow overriding container elms This can, for example, allow for walking over flow formspecs. This would allow for mods to modify forms from other mods. --- README.md | 5 +++-- helpers.lua | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 988a537..8e18c41 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,10 @@ A Minetest mod library to make modifying formspecs easier. and returns a formspec string. - `formspec_ast.interpret(string_or_tree)`: Returns a formspec string after (optionally parsing) and unparsing the formspec provided. - - `formspec_ast.walk(tree)`: Returns an iterator (use this directly in a for + - `formspec_ast.walk(tree, optional_container_set)`: Returns an iterator (use this directly in a for loop) that will return all nodes in a tree, including ones inside - containers. + containers. The containers are recognised by `type`, and can be overriden + with a table of `name` to `true` relationships in `optional_container_set` - `formspec_ast.find(tree, node_type)`: Similar to `walk(tree)`, however only returns `node_type` nodes. - `formspec_ast.get_element_by_name(tree, name)`: Returns the first element in diff --git a/helpers.lua b/helpers.lua index df071fd..73aa8a1 100644 --- a/helpers.lua +++ b/helpers.lua @@ -71,9 +71,9 @@ 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) +local default_container_elems = {container = true, scroll_container = true} +function formspec_ast.walk(tree, provided_container_elms) + return walk_inner(tree, provided_container_elms or default_container_elems) end -- Similar to formspec_ast.walk(), however only returns nodes which have a type