From e677b8e2201a8232d6aab04db23d9c5e584338a3 Mon Sep 17 00:00:00 2001 From: s-ol Date: Wed, 23 Mar 2022 22:17:50 +0100 Subject: add _facet support to mmm.dom --- mmm/dom/init.client.moon | 48 +++++++++++++++++++++++++++--------------------- mmm/dom/init.server.moon | 12 +++++++----- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/mmm/dom/init.client.moon b/mmm/dom/init.client.moon index acdbfa4..25fe7fc 100644 --- a/mmm/dom/init.client.moon +++ b/mmm/dom/init.client.moon @@ -8,26 +8,32 @@ element = (element) -> (...) -> else attributes = {} - with e = document\createElement element - for k,v in pairs attributes - k = 'className' if k == 'class' - if k == 'style' and 'table' == type v - for kk,vv in pairs v - e.style[kk] = vv - elseif 'string' == type k - e[k] = v - - -- if there is only one argument, - -- children can be in attributes table too - if #children == 0 - children = attributes - - for child in *children - if 'string' == type child - child = document\createTextNode child - - e\appendChild child - -setmetatable {}, __index: (name) => + e = if element then document\createElement element else document\createDocumentFragment! + + for k,v in pairs attributes + k = 'className' if k == 'class' + if k == 'style' and 'table' == type v + for kk,vv in pairs v + e.style[kk] = vv + elseif 'string' == type k + e[k] = v + + -- if there is only one argument, + -- children can be in attributes table too + if #children == 0 + children, attributes = attributes, {} + + if not element + assert not (next attributes), "_frag cannot take attributes" + + for child in *children + if 'string' == type child + child = document\createTextNode child + + e\appendChild child + + e + +setmetatable { _frag: element! }, __index: (name) => with val = element name @[name] = val diff --git a/mmm/dom/init.server.moon b/mmm/dom/init.server.moon index 2ddce33..5e7b865 100644 --- a/mmm/dom/init.server.moon +++ b/mmm/dom/init.server.moon @@ -25,7 +25,7 @@ element = (element) -> (...) -> -- if there is only one argument, -- children can be in attributes table too if #children == 0 - children = attributes + children, attributes = attributes, {} for i,v in ipairs children if 'string' != type v @@ -33,14 +33,16 @@ element = (element) -> (...) -> error "wrong type: #{type v}" children[i] = '' unless v - if void_tags[element] + if not element + assert not (next attributes), "_frag cannot take attributes" + table.concat children + else if void_tags[element] assert #children == 0, "void tag #{element} cannot have children!" b .. ">" else b ..= ">" .. table.concat children, '' - b ..= "" - b + b .. "" -setmetatable {}, __index: (name) => +setmetatable { _frag: element! }, __index: (name) => with val = element name @[name] = val -- cgit v1.2.3