Module:Optional CSS attribute

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules


Usage

[edit]
{{#invoke:Optional CSS attribute|optional_CSS_attribute}}
local make_attribute_string = require('Module:Optional CSS attribute').make_attribute_string


Code

--[=[
Simple module to construct a CSS attribute with an undefined number (including zero) of CSS properties
]=]
require('strict')

local p = {} --p stands for package
local getArgs = require('Module:Arguments').getArgs

--[=[
Construct the string from the given table of property:values
]=]
function p.make_attribute_string(attribute, properties)
	if not attribute or not properties or mw.text.trim(attribute) == '' or properties == {} then
		return ''
	end
	attribute = mw.text.trim(attribute)
	
	local out  = ''
	for p, v in pairs(properties) do
		local property = mw.text.trim(p)
		local value = mw.text.trim(v)
		if property ~= attribute and property ~= '' and value ~= '' then
			out = out .. property .. ':' .. value .. ';'
		end
	end
	if properties[attribute] then
		out = out .. mw.text.trim(properties[attribute])
	end
	
	if out == '' then
		return ''
	end
	return attribute .. '="' .. out .. '"'
end

--[=[
The main entry function from templates

Arguments are taken from both frame and parent argument lists
]=]
function p.optional_CSS_attribute(frame)
	local args = getArgs(frame)
	local attribute = args[1] or args.attribute
	args[1] = nil
	args.attribute = nil
	return p.make_attribute_string(attribute, args)
end

return p