From 7188614f0e2103f2cfc87dd0cb268f82fafd8c22 Mon Sep 17 00:00:00 2001 From: sanine Date: Sun, 22 May 2022 12:57:34 -0500 Subject: allow setting attributes --- marigold.lua | 8 ++++++++ test.lua | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/marigold.lua b/marigold.lua index 16aa054..4f04052 100644 --- a/marigold.lua +++ b/marigold.lua @@ -68,7 +68,15 @@ marigold.h = function(tag_type, content, tbl) tag.content = content tag.attributes = {} tag.children = {} + if tbl then + -- add attributes + for k, v in pairs(tbl) do + if type(k) == 'string' then + tag.attributes[k] = v + end + end + -- add children for _, child in ipairs(tbl) do table.insert(tag.children, child) end diff --git a/test.lua b/test.lua index 17aa46d..cd999b2 100755 --- a/test.lua +++ b/test.lua @@ -105,5 +105,27 @@ test("marigold.h correctly ignores missing content", function() assert(div.children[1].content == 'the first paragraph') assert(div.children[2].tag == 'p') assert(div.children[2].content == 'the second paragraph') +end) + + +test("marigold.h correctly sets attributes and children", function() + local h = marigold.h + local div = h('div', { + id="root", class="blinking bold", + h('p', "the first paragraph"), + h('p', "the second paragraph"), + }) + + assert(div.tag == 'div') + assert(div.content == "") + + assert(div.attributes.id == 'root') + assert(div.attributes.class == 'blinking bold') + + assert(#div.children == 2) + assert(div.children[1].tag == 'p') + assert(div.children[1].content == 'the first paragraph') + assert(div.children[2].tag == 'p') + assert(div.children[2].content == 'the second paragraph') end) -- cgit v1.2.1