Module:Sprite: Difference between revisions
Ignore empty links |
Fix mw.html performance issue (basically doubles performance). Fix the css parameter not actually working. |
||
| Line 35: | Line 35: | ||
local sprite = mw.html.create( 'span' ):addClass( 'sprite' ) | local sprite = mw.html.create( 'span' ):addClass( 'sprite' ) | ||
sprite:tag( 'br' ) | sprite:tag( 'br' ) | ||
-- mw.html's css method performs very slow escaping, which doubles the time it takes | |||
-- to run, so we'll construct the styles manually, and put them in the cssText | |||
-- method, which only does html escaping (which isn't slow) | |||
local styles = {} | |||
if setting( 'stylesheet' ) then | if setting( 'stylesheet' ) then | ||
| Line 42: | Line 47: | ||
) | ) | ||
else | else | ||
table.insert( styles, 'background-image:{{FileUrl|' .. ( | |||
setting( 'image' ) or setting( 'name' ) .. 'Sprite.png' | |||
) .. '}}' ) | |||
end | end | ||
local class = setting( 'class' ) | |||
sprite:addClass( | if class then | ||
sprite:addClass( class ) | |||
end | end | ||
local size = setting( 'size' ) | local size = setting( 'size' ) | ||
local pos = math.abs( setting( 'pos' ) ) - 1 | local pos = math.abs( setting( 'pos' ) ) - 1 | ||
local | local sheetWidth = setting( 'sheetsize' ) | ||
local tiles = sheetWidth / size | |||
local left = pos % tiles * size | local left = pos % tiles * size | ||
local top = math.floor( pos / tiles ) * size | local top = math.floor( pos / tiles ) * size | ||
local scale = setting( 'scale' ) | local scale = setting( 'scale' ) | ||
local autoScale = setting( 'autoscale' ) | |||
if left > 0 or top > 0 then | if left > 0 or top > 0 then | ||
table.insert( styles, 'background-position:-' .. left * scale .. 'px -' .. top * scale .. 'px' ) | |||
end | end | ||
if | if not autoScale and scale ~= defaultStyle.scale then | ||
table.insert( styles, 'background-size:' .. sheetWidth * scale .. 'px auto' ) | |||
end | end | ||
if | if size ~= defaultStyle.size or ( not autoScale and scale ~= defaultStyle.scale ) then | ||
table.insert( styles, 'height:' .. size * scale .. 'px' ) | |||
table.insert( styles, 'width:' .. size * scale .. 'px' ) | |||
end | end | ||
if | local align = setting( 'align' ) | ||
if align ~= defaultStyle.align then | |||
table.insert( styles, 'vertical-align:' .. align ) | |||
end | end | ||
table.insert( styles, setting( 'css' ) ) | |||
sprite:cssText( table.concat( styles, ';' ) ) | |||
local text = setting( 'text' ) | |||
local root | local root | ||
local spriteText | local spriteText | ||
if | if text then | ||
root = mw.html.create( 'span' ):addClass( 'nowrap' ) | root = mw.html.create( 'span' ):addClass( 'nowrap' ) | ||
spriteText = mw.html.create( 'span' ):addClass( 'sprite-text' ):wikitext( | spriteText = mw.html.create( 'span' ):addClass( 'sprite-text' ):wikitext( text ) | ||
end | end | ||
local title = setting( 'title' ) | |||
( root or sprite ):attr( 'title', | if title then | ||
( root or sprite ):attr( 'title', title ) | |||
end | end | ||
| Line 236: | Line 247: | ||
if args.refresh then | if args.refresh then | ||
return tostring( body ) | return '', tostring( body ) | ||
end | end | ||
return f:callParserFunction( '#widget:Stylesheet', { page = 'SpriteDoc' } ), tostring( body ) | return f:callParserFunction( '#widget:Stylesheet', { page = 'SpriteDoc' } ), tostring( body ) | ||
end | end | ||
return p | return p | ||