模块:CSS:修订间差异
来自星砂岛百科
更多操作
无编辑摘要 |
性能测试 |
||
| 第12行: | 第12行: | ||
local function get_args(frame) | local function get_args(frame) | ||
if frame == mw.getCurrentFrame() then | if frame == mw.getCurrentFrame() and frame:getParent() then | ||
return require('Module:ProcessArgs').merge(true) | return require('Module:ProcessArgs').merge(true) | ||
end | end | ||
| 第46行: | 第46行: | ||
function p.call(name, ns_code) | function p.call(name, ns_code) | ||
local frame = mw.getCurrentFrame() | |||
local target = trim(name) | |||
if target == '' then | |||
target = get_fullpagename() | |||
end | |||
local ts_path = namespace_prefix(ns_code) .. target .. '/styles.css' | |||
local loaded_key = get_fullpagename() .. '|' .. ts_path | |||
if is_loaded(loaded_key) then | |||
return '' | |||
end | |||
mark_loaded(loaded_key) | |||
return frame:extensionTag('templatestyles', '', { src = ts_path }) | |||
end | |||
function p.quickCall(name, ns_code) | |||
local frame = mw.getCurrentFrame() | local frame = mw.getCurrentFrame() | ||
local target = trim(name) | local target = trim(name) | ||
2026年3月12日 (四) 15:09的版本
概述
CSS 用于按需注入 TemplateStyles,并确保同一页面上同一路径的样式只加载一次。
用法
{{#invoke:CSS|main|Item}}
local css = require('Module:CSS')
return css.quickCall('Item')
示例
{{#invoke:CSS|main|Item|10}}
函数
call:按名称、命名空间代码和文件名拼出样式页路径并注入。quickCall:call的简写封装。main:供#invoke直接调用的入口。
local p = {}
local static = require('Module:Static')
static.CSS = static.CSS or {}
local function trim(value)
if value == nil then
return ''
end
return mw.text.trim(tostring(value))
end
local function get_args(frame)
if frame == mw.getCurrentFrame() and frame:getParent() then
return require('Module:ProcessArgs').merge(true)
end
return frame
end
local function namespace_prefix(code)
local normalized = trim(code)
if normalized == '828' then
return 'Module:'
end
if normalized == '4' then
return 'Project:'
end
if normalized == '0' then
return ':'
end
return 'Template:'
end
local function get_fullpagename()
local title = mw.title.getCurrentTitle()
return title and title.prefixedText or ''
end
local function mark_loaded(key)
static.CSS[key] = true
end
local function is_loaded(key)
return static.CSS[key] == true
end
function p.call(name, ns_code)
local frame = mw.getCurrentFrame()
local target = trim(name)
if target == '' then
target = get_fullpagename()
end
local ts_path = namespace_prefix(ns_code) .. target .. '/styles.css'
local loaded_key = get_fullpagename() .. '|' .. ts_path
if is_loaded(loaded_key) then
return ''
end
mark_loaded(loaded_key)
return frame:extensionTag('templatestyles', '', { src = ts_path })
end
function p.quickCall(name, ns_code)
local frame = mw.getCurrentFrame()
local target = trim(name)
if target == '' then
target = get_fullpagename()
end
local ts_path = namespace_prefix(ns_code) .. target .. '/styles.css'
local loaded_key = get_fullpagename() .. '|' .. ts_path
if is_loaded(loaded_key) then
return ''
end
mark_loaded(loaded_key)
return frame:extensionTag('templatestyles', '', { src = ts_path })
end
function p.main(frame)
local args = get_args(frame)
return p.call(args[1], args[2])
end
return p