Editing Module:Navbox

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
require('strict')
local p = {}
local p = {}
local navbar = require('Module:Navbar')._navbar
local navbar = require('Module:Navbar')._navbar
Line 71: Line 70:
end
end


-- we will want this later when we want to add tstyles for hlist/plainlist
local function has_navbar()
local function has_navbar()
return args[cfg.arg.navbar] ~= cfg.keyword.navbar_off
return args[cfg.arg.navbar] ~= cfg.keyword.navbar_off
Line 142: Line 142:
:tag('div')
:tag('div')
-- id for aria-labelledby attribute, if no title
-- id for aria-labelledby attribute, if no title
:attr('id', (not args[cfg.arg.title]) and mw.uri.anchorEncode(args[cfg.arg.above]) or nil)
:attr('id', args[cfg.arg.title] and nil or mw.uri.anchorEncode(args[cfg.arg.above]))
:wikitext(processItem(args[cfg.arg.above], args[cfg.arg.nowrapitems]))
:wikitext(processItem(args[cfg.arg.above], args[cfg.arg.nowrapitems]))
end
end
Line 262: Line 262:
end
end


-- uses this now to make the needHlistCategory correct
-- to use later for when we add list styles via navbox
local function has_list_class(htmlclass)
local function has_list_class(htmlclass)
local class_args = { -- rough order of probability of use
cfg.arg.bodyclass, cfg.arg.listclass, cfg.arg.aboveclass,
cfg.arg.belowclass, cfg.arg.titleclass, cfg.arg.navboxclass,
cfg.arg.groupclass, cfg.arg.imageclass
}
local patterns = {
local patterns = {
'^' .. htmlclass .. '$',
'^' .. htmlclass .. '$',
Line 270: Line 277:
}
}
for arg, _ in pairs(args) do
for _, arg in ipairs(class_args) do
if type(arg) == 'string' and mw.ustring.find(arg, cfg.pattern.class) then
for _, pattern in ipairs(patterns) do
for _, pattern in ipairs(patterns) do
if mw.ustring.find(args[arg] or '', pattern) then
if mw.ustring.find(args[arg] or '', pattern) then
return true
return true
end
end
end
end
end
end
end
return false
return false
end
-- there are a lot of list classes in the wild, so we add their TemplateStyles
local function add_list_styles()
local frame = mw.getCurrentFrame()
local function add_list_templatestyles(htmlclass, templatestyles)
if has_list_class(htmlclass) then
return frame:extensionTag{
name = 'templatestyles', args = { src = templatestyles }
}
else
return ''
end
end
local hlist_styles = add_list_templatestyles('hlist', cfg.hlist_templatestyles)
local plainlist_styles = add_list_templatestyles('plainlist', cfg.plainlist_templatestyles)
-- a second workaround for [[phab:T303378]]
-- when that issue is fixed, we can actually use has_navbar not to emit the
-- tag here if we want
if has_navbar() and hlist_styles == '' then
hlist_styles = frame:extensionTag{
name = 'templatestyles', args = { src = cfg.hlist_templatestyles }
}
end
-- hlist -> plainlist is best-effort to preserve old Common.css ordering.
-- this ordering is not a guarantee because most navboxes will emit only
-- one of these classes [hlist_note]
return hlist_styles .. plainlist_styles
end
end


Line 413: Line 387:
end
end


local function add_navbox_styles(hiding_templatestyles)
local function add_navbox_styles()
local frame = mw.getCurrentFrame()
local frame = mw.getCurrentFrame()
-- This is a lambda so that it doesn't need the frame as a parameter
-- This is a lambda so that it doesn't need the frame as a parameter
Line 431: Line 405:
local child_templatestyles = add_user_styles(args[cfg.arg.child_templatestyles])
local child_templatestyles = add_user_styles(args[cfg.arg.child_templatestyles])


-- The 'navbox-styles' div exists to wrap the styles to work around T200206
-- The 'navbox-styles' div exists for two reasons:
-- more elegantly. Instead of combinatorial rules, this ends up being linear
--  1. To wrap the styles to work around T200206 more elegantly. Instead
-- number of CSS rules.
--     of combinatorial rules, this ends up being linear number of CSS rules.
--  2. To allow MobileFrontend to rip the styles out with 'nomobile' such that
--     they are not dumped into the mobile view.
return mw.html.create('div')
return mw.html.create('div')
:addClass(cfg.class.navbox_styles)
:addClass(cfg.class.navbox_styles)
:wikitext(
:addClass(cfg.class.nomobile)
add_list_styles() .. -- see [hlist_note] applied to 'before base_templatestyles'
:wikitext(base_templatestyles .. templatestyles .. child_templatestyles)
base_templatestyles ..
templatestyles ..
child_templatestyles ..
table.concat(hiding_templatestyles)
)
:done()
:done()
end
-- work around [[phab:T303378]]
-- for each arg: find all the templatestyles strip markers, insert them into a
-- table. then remove all templatestyles markers from the arg
local function move_hiding_templatestyles(args)
local gfind = string.gfind
local gsub = string.gsub
local templatestyles_markers = {}
local strip_marker_pattern = '(\127[^\127]*UNIQ%-%-templatestyles%-%x+%-QINU[^\127]*\127)'
for k, arg in pairs(args) do
for marker in gfind(arg, strip_marker_pattern) do
table.insert(templatestyles_markers, marker)
end
args[k] = gsub(arg, strip_marker_pattern, '')
end
return templatestyles_markers
end
end


function p._navbox(navboxArgs)
function p._navbox(navboxArgs)
args = navboxArgs
args = navboxArgs
local hiding_templatestyles = move_hiding_templatestyles(args)
local listnums = {}
local listnums = {}


Line 488: Line 441:


if border == cfg.keyword.border_none then
if border == cfg.keyword.border_none then
res:node(add_navbox_styles(hiding_templatestyles))
res:node(add_navbox_styles())
local nav = res:tag('div')
local nav = res:tag('div')
:attr('role', 'navigation')
:attr('role', 'navigation')
Line 514: Line 467:
:wikitext('<div>')
:wikitext('<div>')
else
else
res:node(add_navbox_styles(hiding_templatestyles))
res:node(add_navbox_styles())
local nav = res:tag('div')
local nav = res:tag('div')
:attr('role', 'navigation')
:attr('role', 'navigation')

Please note that all contributions to Timeline of History may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, 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 (see Timeline of History:Copyrights for details). Do not submit copyrighted work without permission!

To edit this page, please answer the question that appears below (more info):

Cancel Editing help (opens in new window)

Template used on this page: