Page MenuHomePhabricator

Scribunto mw.html:css does not take order in account
Open, Needs TriagePublicBUG REPORT

Description

Consider the following Lua code:

local styles = { 'border-left-color', 'purple' }

local box = mw.html.create('div')
    :css(styles)
    :css('border', '1px solid white')
    :css('border-left-color', 'brown')

The expected behavior is that the color of the element's left border will be brown, however it will be white. The new value 'brown' of the 'border-left-color' property will replace the old one 'purple', but will remain "above" the declaration of the shorthand 'border'. When the object will be converted into a string, the output would be

<div style="border-left-color:brown; border:1px solid white;"></div>

The issue comes from lines 358-373 in mw.html.lua, which look to find whether that property is already used and if so - replace the value in it's current position. The code should probably be changed to:

	for i, prop in ipairs( t.styles ) do
		if prop.name == name then
			table.remove( t.styles, i )
			break
		end
	end

	if val ~= nil then
		table.insert( t.styles, { name = name, val = val } )
	end

	return t

It should be also noted that when storing shorthand and longhand contradicting values in one table and passing it to mw.html:css, for example

local styles = {
	['border'] = '1px solid white',
	['border-left-color'] = 'brown'
}

local box = mw.html.create('div'):css(styles)

the order of the properties in the results is undefined, since named fields in a Lua table have no defined order. It may in practice match the order of the code in most cases, but can break randomly. This limitation, however, seems inherent and the only way around it seems to be not passing a single table with contradicting shorthand and longhand CSS properties.

Event Timeline

You should ideally be authoring with TemplateStyles these days, which will better guarantee your order.

You should ideally be authoring with TemplateStyles these days, which will better guarantee your order.

Unfortunately, not all wikis have that installed (pre‑1.44) or enabled (1.44 or later).

You should ideally be authoring with TemplateStyles these days, which will better guarantee your order.

Unfortunately, not all wikis have that installed (pre‑1.44) or enabled (1.44 or later).

TemplateStyles has been available since 1.30 which was released in 2017, nearly a decade ago. Every supported version of MediaWiki can run TemplateStyles. The set of hosts documented at mwwiki:Extension:TemplateStyles is most of the large ones. So I'm not sure what this comment is actually about.

You should ideally be authoring with TemplateStyles these days, which will better guarantee your order.

Unfortunately, not all wikis have that installed (pre‑1.44) or enabled (1.44 or later).

TemplateStyles has been available since 1.30 which was released in 2017, nearly a decade ago. Every supported version of MediaWiki can run TemplateStyles. The set of hosts documented at mwwiki:Extension:TemplateStyles is most of the large ones. So I'm not sure what this comment is actually about.

There are production wikis in existence, some of which I contribute to, which unfortunately don’t have TemplateStyles, for example: The Perfect Tower II Wiki (not installed, pre‑1.44) or the Dolphin Emulator Wiki (not enabled, 1.45).