Module:Optional CSS attribute

From Ekatra Wiki
Revision as of 06:57, 27 February 2023 by enws>CalendulaAsteraceae
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Optional CSS attribute/doc

--[=[
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