aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s+removethis@s-ol.nu>2022-03-23 21:17:50 +0000
committers-ol <s+removethis@s-ol.nu>2022-03-23 21:17:50 +0000
commite677b8e2201a8232d6aab04db23d9c5e584338a3 (patch)
tree64f7782dc3305f3156aa8bd56b45a155a82b327e
parentlook for plugins in own dir (diff)
downloadmmm-e677b8e2201a8232d6aab04db23d9c5e584338a3.tar.gz
mmm-e677b8e2201a8232d6aab04db23d9c5e584338a3.zip
add _facet support to mmm.dom
-rw-r--r--mmm/dom/init.client.moon48
-rw-r--r--mmm/dom/init.server.moon12
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 ..= "</#{element}>"
- b
+ b .. "</#{element}>"
-setmetatable {}, __index: (name) =>
+setmetatable { _frag: element! }, __index: (name) =>
with val = element name
@[name] = val