Page MenuHomePhabricator

Migrate bare division math to parens-division in Less stylesheet files (in WMF-deployed extensions)
Open, Needs TriagePublic

Description

Change

In order to support native CSS features that utilize the slash operator, upstream Less.js has decided to deprecate use of slash in bare math mode. That is, math without parenthesis.

Demo: Try it yourself

input.less Example
.bare {
  @foo: 12px / 2;
  height: @foo;
  max-height: @foo * 2;
  width: @foo / 3;
}
.parens {
  @foo: (12px / 2);
  height: @foo;
  max-height: @foo * 2;
  width: (@foo / 3);
}
output.css Before Less.js 4.0 (and before Less.php 5.0 math: always)
.bare {
  height: 6px;
  max-height: 12px;
  width: 2px;
}
.parens {
  height: 6px;
  max-height: 12px;
  width: 2px;
}
output.css After Less.js 4.0 (and Less.php 5.0 math: parens-division)
.bare { /* Changed */
  height: 12px / 2;
  max-height: 12px / 2 * 2;
  width: 12px / 2 / 3;
}
.parens { /* Unchanged */
  height: 6px;
  max-height: 12px;
  width: 2px;
}

Background

These are examples of CSS browser features that (until now) required MediaWiki developers to use string evaluation workarounds, because Less.js and Less.php would try to compile them as math, even though they are not intended to be math. For example:

From T366445 task description:

Historically, upstream Less.js has had two math options:

  • "always" (default, aka "strictMath false"). This eagerly performs math almost everywhere.
  • "parens" (aka "strict" ot "strictMath true"). This restricts math to inside optional parenthesis only (i.e. parenthesis not relating to other CSS syntax).

As a compromise, to be the last disruptive to the ecosystem, Less.js 3.0 introduded a third mode: "parens-division". This continous to allow math without parenthesis, but in order to perform math with a slash for division, it needs to be wrapped in parenthesis. It also introduces ./ as a way to force math outside parenthesis. Documentation at: https://lesscss.org/usage/#less-options-math.

We have implemented thenew Less.js 3.0 behaviours and switched the default to "parens-division" as part of Less.php 5.0. Details at T366445: Add support for Maths(strictMath) option when it is set to parens-division and T288498: Update less.php port to support Less.js 3.13 behaviours.

Migration

Codesearch: Bare math slash in Less files.

There are approximately 50 files using the deprecated as of writing.

Before we can switch to math=parens-division we have to update these to use parenthesis.

  • Codex (unaffected, already prepared and Less.js 4-compatible)
  • MediaWiki core: HTMLForm
  • MediaWiki core: RCFilters, part of MediaWiki-Recent-changes
  • MediaWiki core: OOUI "mediawiki.widgets", part of MediaWiki-User-Interface
  • GrowthExperiments extension
  • MediaSearch extension
  • AdvancedSearch extension
  • CheckUser extension
  • ContentTranslation extension
  • DiscussionTools extension
  • Echo extension, Notifications
  • Flow extension, StructuredDiscussions
  • Kartographer extension
  • MobileFrontend extension
  • Popups extension, Page-Previews
  • TemplateData extension
  • TimedMediaHandler extension (unaffected)
  • UniversalLanguageSelector extension
  • UploadWizard extension
  • WikiLambda extension
  • WikibaseMediaInfo extension
  • services-mobileapps (unafffected, doesn't involve MediaWiki or Less.php)
  • Minerva skin
  • Vector skin

Event Timeline

Krinkle renamed this task from Migrate bare slash-division math to parens-division in Less styles (in WMF-deployed extensions) to Migrate bare division math to parens-division in Less stylesheet files (in WMF-deployed extensions) .Mon, Jul 1, 2:55 PM
Krinkle updated the task description. (Show Details)

Change #1051152 had a related patch set uploaded (by Krinkle; author: Krinkle):

[mediawiki/extensions/Flow@master] Prepare Less styles for math=parens-division

https://gerrit.wikimedia.org/r/1051152

Change #1051153 had a related patch set uploaded (by Krinkle; author: Krinkle):

[mediawiki/extensions/TemplateData@master] Prepare Less styles for math=parens-division

https://gerrit.wikimedia.org/r/1051153

Change #1051153 merged by jenkins-bot:

[mediawiki/extensions/TemplateData@master] Prepare Less styles for math=parens-division

https://gerrit.wikimedia.org/r/1051153

Can you write a stylelint-config-wikimedia rule to detect (and ideally auto-fix) this?

Change #1051426 had a related patch set uploaded (by Jforrester; author: Jforrester):

[mediawiki/extensions/WikiLambda@master] ext.wikilambda.edit.variables.less: Prepare Less styles for math=parens-division

https://gerrit.wikimedia.org/r/1051426

Change #1051426 merged by jenkins-bot:

[mediawiki/extensions/WikiLambda@master] ext.wikilambda.edit.variables.less: Prepare Less styles for math=parens-division

https://gerrit.wikimedia.org/r/1051426

Can you write a stylelint-config-wikimedia rule to detect (and ideally auto-fix) this?

I'm afraid not. Both variants are syntactically valid Less and CSS. You can find examples under "Background" in the task description.

In my analysis so far, unwrapped math division is fortunately quite rare in our code base. Likely because generally "out there" for many years Less.js has defaulted to not compiling division in such case. So good habits (or copy/pasta) produces Less code that is generally unambigous and correct by default.

The dozen or so cares I found so far, about 1/4th are meant to be preserved but broken (author intended for slashes to be perserved but due to lack of testing or happy accidents that cancel out, it actually compiles as math today), 1/4th are preserved and correctly so (e.g. existing exclusion in Less.php for "font" prevents unwanted math there, and a handful of other corner cases like it), and about 2/4th are the ones I listed in this task, where compiling is intended and parenthesis are needed to disambiguate for Less.js 4+ interop and Less.php 5.0 prep.

Change #1052194 had a related patch set uploaded (by Krinkle; author: Krinkle):

[mediawiki/core@master] HTMLForm: Prepare Less styles for math=parens-division

https://gerrit.wikimedia.org/r/1052194