打开/关闭搜索
搜索
打开/关闭菜单
1
40
1
297
末世录
导航
首页
最近更改
随机页面
MediaWiki帮助
特殊页面
上传文件
末世录
角色
国家
教派
我想嗦牛子
嗦塞尔达牛子
嗦萨图恩牛子
嗦奥利奥头牛子
打开/关闭外观设置菜单
通知
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。
user-interface-preferences
个人工具
登录
查看“︁模块:HtmlBuilder”︁的源代码
来自末世录
查看
阅读
查看源代码
查看历史
associated-pages
模块
讨论
更多操作
←
模块:HtmlBuilder
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
-- Module:HtmlBuilder - 安全版本,修复 tagName 为表导致的错误 local HtmlBuilder = {} local metatable = {} -- 辅助函数:确保值为字符串,非字符串则尝试转换 local function ensureString(v) if v == nil then return nil end if type(v) == 'string' then return v end return tostring(v) end metatable.__index = function(t, key) local ret = rawget(t, key) if ret then return ret end ret = metatable[key] if type(ret) == 'function' then return function(...) return ret(t, ...) end else return ret end end metatable.__tostring = function(t) local ret = {} t._build(ret) return table.concat(ret) end metatable._build = function(t, ret) if t.tagName and type(t.tagName) == 'string' then table.insert(ret, '<' .. t.tagName) for i, attr in ipairs(t.attributes) do table.insert(ret, ' ' .. attr.name .. '="' .. attr.val .. '"') end if #t.styles > 0 then table.insert(ret, ' style="') for i, prop in ipairs(t.styles) do if type(prop) == 'string' then table.insert(ret, prop .. ';') else table.insert(ret, prop.name .. ':' .. prop.val .. ';') end end table.insert(ret, '"') end if t.selfClosing then table.insert(ret, ' /') end table.insert(ret, '>') end for i, node in ipairs(t.nodes) do if node then if type(node) == 'table' then node._build(ret) else table.insert(ret, tostring(node)) end end end if t.tagName and type(t.tagName) == 'string' and not t.unclosed and not t.selfClosing then table.insert(ret, '</' .. t.tagName .. '>') end end metatable.node = function(t, builder) if builder then table.insert(t.nodes, builder) end return t end metatable.wikitext = function(t, ...) local vals = {...} for i = 1, #vals do if vals[i] then table.insert(t.nodes, vals[i]) end end return t end metatable.newline = function(t) table.insert(t.nodes, '\n') return t end metatable.tag = function(t, tagName, args) args = args or {} args.parent = t local safeTagName = ensureString(tagName) if not safeTagName then error("HtmlBuilder.tag: tagName 必须是字符串,传入类型为 " .. type(tagName)) end local builder = HtmlBuilder.create(safeTagName, args) table.insert(t.nodes, builder) return builder end local function getAttr(t, name) for i, attr in ipairs(t.attributes) do if attr.name == name then return attr end end end metatable.attr = function(t, name, val) if val ~= nil then local nameStr = ensureString(name) local valStr = ensureString(val) if nameStr == 'style' then t.styles = {valStr} return t end local attr = getAttr(t, nameStr) if attr then attr.val = valStr else table.insert(t.attributes, {name = nameStr, val = valStr}) end end return t end metatable.addClass = function(t, class) if class then local classStr = ensureString(class) if classStr then local attr = getAttr(t, 'class') if attr then attr.val = attr.val .. ' ' .. classStr else t.attr('class', classStr) end end end return t end metatable.css = function(t, name, val) if name and val then local nameStr = ensureString(name) local valStr = ensureString(val) for i, prop in ipairs(t.styles) do if prop.name == nameStr then prop.val = valStr return t end end table.insert(t.styles, {name = nameStr, val = valStr}) end return t end metatable.cssText = function(t, css) if css then table.insert(t.styles, ensureString(css)) end return t end metatable.done = function(t) return t.parent or t end metatable.allDone = function(t) while t.parent do t = t.parent end return t end function HtmlBuilder.create(tagName, args) args = args or {} local safeTagName = ensureString(tagName) if not safeTagName then error("HtmlBuilder.create: tagName 必须是字符串,传入类型为 " .. type(tagName)) end local builder = {} setmetatable(builder, metatable) builder.nodes = {} builder.attributes = {} builder.styles = {} builder.tagName = safeTagName builder.parent = args.parent builder.unclosed = args.unclosed or false builder.selfClosing = args.selfClosing or false return builder end return HtmlBuilder
该页面嵌入的页面:
模块:HtmlBuilder/doc
(
查看源代码
)
返回
模块:HtmlBuilder
。
查看“︁模块:HtmlBuilder”︁的源代码
来自末世录