Editing Module:HtmlBuilder

Jump to: navigation, search

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:
-- Module for building complex HTML (e.g. infoboxes, navboxes) using a fluent interface.
+
-- 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
 
        if t.selfClosing then
 
            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 and not t.selfClosing then
+
     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
  
local function getAttr(t, name)
+
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 type(val) == 'string' or type(val) == 'number' then
+
     -- if caller sets the style attribute explicitly, then replace all styles previously added with css() and cssText()
        -- if caller sets the style attribute explicitly, then replace all styles previously added with css() and cssText()
+
    if name == 'style' then
        if name == 'style' then
+
        t.styles = {val}
            t.styles = {val}
+
        return t
            return t
+
    end
        end
+
   
       
+
    local attr = getAttr(t, name)
        local attr = getAttr(t, name)
+
    if attr then
        if attr then
+
        attr.val = val
            attr.val = val
+
    else
        else
+
        table.insert(t.attributes, {name = name, val = val})
            table.insert(t.attributes, {name = name, val = val})
+
        end
+
 
     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
    builder.selfClosing = args.selfClosing or false
 
 
     return builder
 
     return builder
 
end
 
end
  
 
return HtmlBuilder
 
return HtmlBuilder
 

Please note that all contributions to The Crowdsourced Resource-Based Economy Knowledgebase are considered to be released under the Creative Commons Zero (Public Domain) (see RBEMWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox
Share