diff options
| author | frosty <gabriel@bwaaa.monster> | 2026-09-19 22:24:20 -0400 |
|---|---|---|
| committer | frosty <gabriel@bwaaa.monster> | 2026-09-19 22:24:20 -0400 |
| commit | 3bff1316d1fc22f81b542d614c4bc90953bfa322 (patch) | |
| tree | a3b19fdabf3f913edd8ecea55f6566ea1045c69b /src/Dom/Dom.c | |
| parent | 4c8d764d171ec77ff626cec273c44ec7e7a995fc (diff) | |
feat: add html.element lua helper
Diffstat (limited to 'src/Dom/Dom.c')
| -rw-r--r-- | src/Dom/Dom.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/Dom/Dom.c b/src/Dom/Dom.c index ede7ddd..6dc6760 100644 --- a/src/Dom/Dom.c +++ b/src/Dom/Dom.c @@ -6,6 +6,50 @@ #include <string.h> #include <strings.h> +static int html_element(lua_State *lua) { + const char *tag; + + luaL_checktype(lua, 1, LUA_TSTRING); + tag = lua_tostring(lua, 1); + if (tag[0] == '\0') { + return luaL_argerror(lua, 1, "tag cannot be empty"); + } + if (!lua_isnoneornil(lua, 2)) { + luaL_checktype(lua, 2, LUA_TTABLE); + } + if (!lua_isnoneornil(lua, 3)) { + luaL_checktype(lua, 3, LUA_TTABLE); + } + + lua_newtable(lua); + + lua_pushvalue(lua, 1); + lua_setfield(lua, -2, "tag"); + + if (lua_istable(lua, 2)) { + lua_pushvalue(lua, 2); + } else { + lua_newtable(lua); + } + lua_setfield(lua, -2, "attributes"); + + if (lua_istable(lua, 3)) { + lua_pushvalue(lua, 3); + } else { + lua_newtable(lua); + } + lua_setfield(lua, -2, "children"); + + return 1; +} + +void dom_register_html(lua_State *lua) { + lua_newtable(lua); + lua_pushcfunction(lua, html_element); + lua_setfield(lua, -2, "element"); + lua_setglobal(lua, "html"); +} + static int absolute_index(lua_State *lua, int index) { return index < 0 ? lua_gettop(lua) + index + 1 : index; } |
