Editing Module:HtmlBuilder
Warning: You are not logged in.
Your IP address will be recorded in this page's edit history.The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
− | -- | + | -- Experimental module for building complex HTML (e.g. infoboxes, navboxes) using a fluent interface |
local HtmlBuilder = {} | local HtmlBuilder = {} | ||
Line 24: | Line 24: | ||
local ret = {} | local ret = {} | ||
t._build(ret) | t._build(ret) | ||
− | return table.concat(ret) | + | return table.concat(ret, '') |
end | end | ||
Line 43: | Line 43: | ||
end | end | ||
table.insert(ret, '"') | table.insert(ret, '"') | ||
− | |||
− | |||
− | |||
end | end | ||
table.insert(ret, '>') | table.insert(ret, '>') | ||
Line 58: | Line 55: | ||
end | end | ||
end | end | ||
− | if t.tagName and not t.unclosed | + | if t.tagName and not t.unclosed then |
table.insert(ret, '</' .. t.tagName .. '>') | table.insert(ret, '</' .. t.tagName .. '>') | ||
end | end | ||
end | end | ||
− | + | ||
metatable.node = function(t, builder) | metatable.node = function(t, builder) | ||
if builder then | if builder then | ||
Line 93: | Line 90: | ||
end | end | ||
− | + | function getAttr(t, name) | |
for i, attr in ipairs(t.attributes) do | for i, attr in ipairs(t.attributes) do | ||
if attr.name == name then | if attr.name == name then | ||
Line 102: | Line 99: | ||
metatable.attr = function(t, name, val) | metatable.attr = function(t, name, val) | ||
− | + | -- if caller sets the style attribute explicitly, then replace all styles previously added with css() and cssText() | |
− | + | if name == 'style' then | |
− | + | t.styles = {val} | |
− | + | return t | |
− | + | end | |
− | + | ||
− | + | local attr = getAttr(t, name) | |
− | + | if attr then | |
− | + | attr.val = val | |
− | + | else | |
− | + | table.insert(t.attributes, {name = name, val = val}) | |
− | + | ||
− | + | ||
end | end | ||
Line 129: | Line 124: | ||
end | end | ||
end | end | ||
− | + | ||
return t | return t | ||
end | end | ||
Line 176: | Line 171: | ||
builder.parent = args.parent | builder.parent = args.parent | ||
builder.unclosed = args.unclosed or false | builder.unclosed = args.unclosed or false | ||
− | |||
return builder | return builder | ||
end | end | ||
return HtmlBuilder | return HtmlBuilder |