2013-12-16 15:00:37 +00:00
|
|
|
--[[
|
|
|
|
A module for building complex HTML from Lua using a
|
|
|
|
fluent interface.
|
|
|
|
|
|
|
|
Originally written on the English Wikipedia by
|
|
|
|
Toohool and Mr. Stradivarius.
|
|
|
|
|
|
|
|
Code released under the GPL v2+ as per:
|
|
|
|
https://en.wikipedia.org/w/index.php?diff=next&oldid=581399786
|
|
|
|
https://en.wikipedia.org/w/index.php?diff=next&oldid=581403025
|
|
|
|
|
|
|
|
@license GNU GPL v2+
|
|
|
|
@author Marius Hoch < hoo@online.de >
|
|
|
|
]]
|
|
|
|
|
|
|
|
local HtmlBuilder = {}
|
|
|
|
|
2014-07-10 11:02:39 +00:00
|
|
|
local util = require 'libraryUtil'
|
|
|
|
local checkType = util.checkType
|
|
|
|
local checkTypeMulti = util.checkTypeMulti
|
|
|
|
|
2013-12-16 15:00:37 +00:00
|
|
|
local metatable = {}
|
|
|
|
local methodtable = {}
|
|
|
|
|
|
|
|
local selfClosingTags = {
|
|
|
|
area = true,
|
|
|
|
base = true,
|
|
|
|
br = true,
|
|
|
|
col = true,
|
|
|
|
command = true,
|
|
|
|
embed = true,
|
|
|
|
hr = true,
|
|
|
|
img = true,
|
|
|
|
input = true,
|
|
|
|
keygen = true,
|
|
|
|
link = true,
|
|
|
|
meta = true,
|
|
|
|
param = true,
|
|
|
|
source = true,
|
|
|
|
track = true,
|
|
|
|
wbr = true,
|
|
|
|
}
|
|
|
|
|
|
|
|
local htmlencodeMap = {
|
|
|
|
['>'] = '>',
|
|
|
|
['<'] = '<',
|
|
|
|
['&'] = '&',
|
|
|
|
['"'] = '"',
|
|
|
|
}
|
|
|
|
|
|
|
|
metatable.__index = methodtable
|
|
|
|
|
|
|
|
metatable.__tostring = function( t )
|
|
|
|
local ret = {}
|
|
|
|
t:_build( ret )
|
|
|
|
return table.concat( ret )
|
|
|
|
end
|
|
|
|
|
2014-06-05 14:57:42 +00:00
|
|
|
-- Get an attribute table (name, value) and its index
|
2013-12-16 15:00:37 +00:00
|
|
|
--
|
|
|
|
-- @param name
|
|
|
|
local function getAttr( t, name )
|
|
|
|
for i, attr in ipairs( t.attributes ) do
|
|
|
|
if attr.name == name then
|
2014-06-05 14:57:42 +00:00
|
|
|
return attr, i
|
2013-12-16 15:00:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Is this a valid attribute name?
|
|
|
|
--
|
|
|
|
-- @param s
|
|
|
|
local function isValidAttributeName( s )
|
|
|
|
-- Good estimate: http://www.w3.org/TR/2000/REC-xml-20001006#NT-Name
|
|
|
|
return s:match( '^[a-zA-Z_:][a-zA-Z0-9_.:-]*$' )
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Is this a valid tag name?
|
|
|
|
--
|
|
|
|
-- @param s
|
|
|
|
local function isValidTag( s )
|
2014-10-03 11:52:17 +00:00
|
|
|
return s:match( '^[a-zA-Z0-9]+$' )
|
2013-12-16 15:00:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Escape a value, for use in HTML
|
|
|
|
--
|
|
|
|
-- @param s
|
|
|
|
local function htmlEncode( s )
|
2014-09-10 23:08:34 +00:00
|
|
|
-- The parentheses ensure that there is only one return value
|
|
|
|
return ( string.gsub( s, '[<>&"]', htmlencodeMap ) )
|
2013-12-16 15:00:37 +00:00
|
|
|
end
|
|
|
|
|
2014-07-10 11:02:39 +00:00
|
|
|
local function cssEncode( s )
|
2013-12-16 15:00:37 +00:00
|
|
|
-- XXX: I'm not sure this character set is complete.
|
2014-09-22 02:59:52 +00:00
|
|
|
-- bug #68011: allow delete character (\127)
|
|
|
|
return mw.ustring.gsub( s, '[^\32-\57\60-\127]', function ( m )
|
2013-12-16 15:00:37 +00:00
|
|
|
return string.format( '\\%X ', mw.ustring.codepoint( m ) )
|
|
|
|
end )
|
|
|
|
end
|
|
|
|
|
2014-07-10 11:02:39 +00:00
|
|
|
-- Create a builder object. This is a separate function so that we can show the
|
|
|
|
-- correct error levels in both HtmlBuilder.create and metatable.tag.
|
|
|
|
--
|
|
|
|
-- @param tagName
|
|
|
|
-- @param args
|
|
|
|
local function createBuilder( tagName, args )
|
|
|
|
if tagName ~= nil and tagName ~= '' and not isValidTag( tagName ) then
|
|
|
|
error( string.format( "invalid tag name '%s'", tagName ), 3 )
|
|
|
|
end
|
|
|
|
|
|
|
|
args = args or {}
|
|
|
|
local builder = {}
|
|
|
|
setmetatable( builder, metatable )
|
|
|
|
builder.nodes = {}
|
|
|
|
builder.attributes = {}
|
|
|
|
builder.styles = {}
|
|
|
|
|
|
|
|
if tagName ~= '' then
|
|
|
|
builder.tagName = tagName
|
|
|
|
end
|
|
|
|
|
|
|
|
builder.parent = args.parent
|
|
|
|
builder.selfClosing = selfClosingTags[tagName] or args.selfClosing or false
|
|
|
|
return builder
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Append a builder to the current node. This is separate from methodtable.node
|
|
|
|
-- so that we can show the correct error level in both methodtable.node and
|
|
|
|
-- methodtable.wikitext.
|
|
|
|
--
|
|
|
|
-- @param builder
|
|
|
|
local function appendBuilder( t, builder )
|
|
|
|
if t.selfClosing then
|
|
|
|
error( "self-closing tags can't have child nodes", 3 )
|
|
|
|
end
|
|
|
|
|
|
|
|
if builder then
|
|
|
|
table.insert( t.nodes, builder )
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
2013-12-16 15:00:37 +00:00
|
|
|
methodtable._build = function( t, ret )
|
|
|
|
if t.tagName then
|
|
|
|
table.insert( ret, '<' .. t.tagName )
|
|
|
|
for i, attr in ipairs( t.attributes ) do
|
|
|
|
table.insert(
|
|
|
|
ret,
|
|
|
|
-- Note: Attribute names have already been validated
|
|
|
|
' ' .. attr.name .. '="' .. htmlEncode( attr.val ) .. '"'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
if #t.styles > 0 then
|
|
|
|
table.insert( ret, ' style="' )
|
2014-09-10 23:08:34 +00:00
|
|
|
local css = {}
|
2013-12-16 15:00:37 +00:00
|
|
|
for i, prop in ipairs( t.styles ) do
|
2014-06-27 16:13:10 +00:00
|
|
|
if type( prop ) ~= 'table' then -- added with cssText()
|
2014-09-10 23:08:34 +00:00
|
|
|
table.insert( css, htmlEncode( prop ) )
|
2013-12-16 15:00:37 +00:00
|
|
|
else -- added with css()
|
|
|
|
table.insert(
|
2014-09-10 23:08:34 +00:00
|
|
|
css,
|
|
|
|
htmlEncode( cssEncode( prop.name ) .. ':' .. cssEncode( prop.val ) )
|
2013-12-16 15:00:37 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2014-09-10 23:08:34 +00:00
|
|
|
table.insert( ret, table.concat( css, ';' ) )
|
2013-12-16 15:00:37 +00:00
|
|
|
table.insert( ret, '"' )
|
|
|
|
end
|
|
|
|
if t.selfClosing then
|
|
|
|
table.insert( ret, ' />' )
|
|
|
|
return
|
|
|
|
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 then
|
|
|
|
table.insert( ret, '</' .. t.tagName .. '>' )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Append a builder to the current node
|
|
|
|
--
|
|
|
|
-- @param builder
|
|
|
|
methodtable.node = function( t, builder )
|
2014-07-10 11:02:39 +00:00
|
|
|
return appendBuilder( t, builder )
|
2013-12-16 15:00:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Appends some markup to the node. This will be treated as wikitext.
|
|
|
|
methodtable.wikitext = function( t, ... )
|
2014-12-08 19:07:25 +00:00
|
|
|
for k,v in ipairs{...} do
|
|
|
|
checkTypeMulti( 'wikitext', k, v, { 'string', 'number' } )
|
|
|
|
appendBuilder( t, v )
|
2013-12-16 15:00:37 +00:00
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Appends a newline character to the node.
|
|
|
|
methodtable.newline = function( t )
|
2014-12-08 19:07:25 +00:00
|
|
|
return t:wikitext( '\n' )
|
2013-12-16 15:00:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Appends a new child node to the builder, and returns an HtmlBuilder instance
|
|
|
|
-- representing that new node.
|
|
|
|
--
|
|
|
|
-- @param tagName
|
|
|
|
-- @param args
|
|
|
|
methodtable.tag = function( t, tagName, args )
|
2014-07-10 11:02:39 +00:00
|
|
|
checkType( 'tag', 1, tagName, 'string' )
|
|
|
|
checkType( 'tag', 2, args, 'table', true )
|
2013-12-16 15:00:37 +00:00
|
|
|
args = args or {}
|
2014-07-10 11:02:39 +00:00
|
|
|
|
2013-12-16 15:00:37 +00:00
|
|
|
args.parent = t
|
2014-07-10 11:02:39 +00:00
|
|
|
local builder = createBuilder( tagName, args )
|
2013-12-16 15:00:37 +00:00
|
|
|
t:node( builder )
|
|
|
|
return builder
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Get the value of an html attribute
|
|
|
|
--
|
|
|
|
-- @param name
|
|
|
|
methodtable.getAttr = function( t, name )
|
2014-07-10 11:02:39 +00:00
|
|
|
checkType( 'getAttr', 1, name, 'string' )
|
|
|
|
|
2013-12-16 15:00:37 +00:00
|
|
|
local attr = getAttr( t, name )
|
2014-12-08 19:07:25 +00:00
|
|
|
return attr and attr.val
|
2013-12-16 15:00:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Set an HTML attribute on the node.
|
|
|
|
--
|
|
|
|
-- @param name Attribute to set, alternative table of name-value pairs
|
2014-06-05 14:57:42 +00:00
|
|
|
-- @param val Value of the attribute. Nil causes the attribute to be unset
|
2013-12-16 15:00:37 +00:00
|
|
|
methodtable.attr = function( t, name, val )
|
|
|
|
if type( name ) == 'table' then
|
|
|
|
if val ~= nil then
|
2014-07-10 11:02:39 +00:00
|
|
|
error(
|
|
|
|
"bad argument #2 to 'attr' " ..
|
|
|
|
'(if argument #1 is a table, argument #2 must be left empty)',
|
|
|
|
2
|
|
|
|
)
|
2013-12-16 15:00:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local callForTable = function()
|
|
|
|
for attrName, attrValue in pairs( name ) do
|
|
|
|
t:attr( attrName, attrValue )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if not pcall( callForTable ) then
|
2014-07-10 11:02:39 +00:00
|
|
|
error(
|
|
|
|
"bad argument #1 to 'attr' " ..
|
|
|
|
'(table keys must be strings, and values must be strings or numbers)',
|
|
|
|
2
|
|
|
|
)
|
2013-12-16 15:00:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
2014-07-10 11:02:39 +00:00
|
|
|
checkType( 'attr', 1, name, 'string' )
|
|
|
|
checkTypeMulti( 'attr', 2, val, { 'string', 'number', 'nil' } )
|
2013-12-16 15:00:37 +00:00
|
|
|
|
|
|
|
-- 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
|
|
|
|
|
|
|
|
if not isValidAttributeName( name ) then
|
2014-07-10 11:02:39 +00:00
|
|
|
error( string.format(
|
|
|
|
"bad argument #1 to 'attr' (invalid attribute name '%s')",
|
|
|
|
name
|
|
|
|
), 2 )
|
2013-12-16 15:00:37 +00:00
|
|
|
end
|
|
|
|
|
2014-06-05 14:57:42 +00:00
|
|
|
local attr, i = getAttr( t, name )
|
2013-12-16 15:00:37 +00:00
|
|
|
if attr then
|
2014-06-05 14:57:42 +00:00
|
|
|
if val ~= nil then
|
|
|
|
attr.val = val
|
|
|
|
else
|
|
|
|
table.remove( t.attributes, i )
|
|
|
|
end
|
|
|
|
elseif val ~= nil then
|
2013-12-16 15:00:37 +00:00
|
|
|
table.insert( t.attributes, { name = name, val = val } )
|
|
|
|
end
|
|
|
|
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Adds a class name to the node's class attribute. Spaces will be
|
|
|
|
-- automatically added to delimit each added class name.
|
|
|
|
--
|
|
|
|
-- @param class
|
|
|
|
methodtable.addClass = function( t, class )
|
2014-07-10 11:02:39 +00:00
|
|
|
checkTypeMulti( 'addClass', 1, class, { 'string', 'number', 'nil' } )
|
|
|
|
|
2014-12-08 19:07:25 +00:00
|
|
|
if class ~= nil then
|
|
|
|
local attr = getAttr( t, 'class' )
|
|
|
|
if attr then
|
|
|
|
attr.val = attr.val .. ' ' .. class
|
|
|
|
else
|
|
|
|
t:attr( 'class', class )
|
|
|
|
end
|
2013-12-16 15:00:37 +00:00
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Set a CSS property to be added to the node's style attribute.
|
|
|
|
--
|
|
|
|
-- @param name CSS attribute to set, alternative table of name-value pairs
|
2014-06-05 14:57:42 +00:00
|
|
|
-- @param val The value to set. Nil causes it to be unset
|
2013-12-16 15:00:37 +00:00
|
|
|
methodtable.css = function( t, name, val )
|
|
|
|
if type( name ) == 'table' then
|
|
|
|
if val ~= nil then
|
2014-07-10 11:02:39 +00:00
|
|
|
error(
|
|
|
|
"bad argument #2 to 'css' " ..
|
|
|
|
'(if argument #1 is a table, argument #2 must be left empty)',
|
|
|
|
2
|
|
|
|
)
|
2013-12-16 15:00:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local callForTable = function()
|
|
|
|
for attrName, attrValue in pairs( name ) do
|
|
|
|
t:css( attrName, attrValue )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if not pcall( callForTable ) then
|
2014-07-10 11:02:39 +00:00
|
|
|
error(
|
|
|
|
"bad argument #1 to 'css' " ..
|
|
|
|
'(table keys and values must be strings or numbers)',
|
|
|
|
2
|
|
|
|
)
|
2013-12-16 15:00:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
2014-07-10 11:02:39 +00:00
|
|
|
checkTypeMulti( 'css', 1, name, { 'string', 'number' } )
|
|
|
|
checkTypeMulti( 'css', 2, val, { 'string', 'number', 'nil' } )
|
2013-12-16 15:00:37 +00:00
|
|
|
|
|
|
|
for i, prop in ipairs( t.styles ) do
|
|
|
|
if prop.name == name then
|
2014-06-05 14:57:42 +00:00
|
|
|
if val ~= nil then
|
|
|
|
prop.val = val
|
|
|
|
else
|
|
|
|
table.remove( t.styles, i )
|
|
|
|
end
|
2013-12-16 15:00:37 +00:00
|
|
|
return t
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-05 14:57:42 +00:00
|
|
|
if val ~= nil then
|
|
|
|
table.insert( t.styles, { name = name, val = val } )
|
|
|
|
end
|
2013-12-16 15:00:37 +00:00
|
|
|
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Add some raw CSS to the node's style attribute. This is typically used
|
|
|
|
-- when a template allows some CSS to be passed in as a parameter
|
|
|
|
--
|
|
|
|
-- @param css
|
|
|
|
methodtable.cssText = function( t, css )
|
2014-07-10 11:02:39 +00:00
|
|
|
checkTypeMulti( 'cssText', 1, css, { 'string', 'number', 'nil' } )
|
2014-12-08 19:07:25 +00:00
|
|
|
table.insert( t.styles, css )
|
2013-12-16 15:00:37 +00:00
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Returns the parent node under which the current node was created. Like
|
|
|
|
-- jQuery.end, this is a convenience function to allow the construction of
|
|
|
|
-- several child nodes to be chained together into a single statement.
|
|
|
|
methodtable.done = function( t )
|
|
|
|
return t.parent or t
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Like .done(), but traverses all the way to the root node of the tree and
|
|
|
|
-- returns it.
|
|
|
|
methodtable.allDone = function( t )
|
|
|
|
while t.parent do
|
|
|
|
t = t.parent
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Create a new instance
|
|
|
|
--
|
|
|
|
-- @param tagName
|
|
|
|
-- @param args
|
|
|
|
function HtmlBuilder.create( tagName, args )
|
2014-07-10 11:02:39 +00:00
|
|
|
checkType( 'mw.html.create', 1, tagName, 'string', true )
|
|
|
|
checkType( 'mw.html.create', 2, args, 'table', true )
|
|
|
|
return createBuilder( tagName, args )
|
2013-12-16 15:00:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
mw_interface = nil
|
|
|
|
|
|
|
|
-- Register this library in the "mw" global
|
|
|
|
mw = mw or {}
|
|
|
|
mw.html = HtmlBuilder
|
|
|
|
|
|
|
|
package.loaded['mw.html'] = HtmlBuilder
|
|
|
|
|
|
|
|
return HtmlBuilder
|