Skip to content

CSS variable stripped from calls to jQuery .css() #2411

Description

@Krinkle

I found an odd behavior in the jQuery Core .css() function when passing values that contain CSS variables, with a color value as fallback. This bug only happens when jQuery UI is also loaded on the page, and seems to be caused by the jQuery Color plugin (as vendored by jQuery UI) and its cssHooks for converting colors to rgb() notation.

Test case

$('<div>').css('color', 'var(--color-base, #202122)').css('color');

Expected
If you run this e.g. on https://jquery.com where jQuery UI is not loaded, it returns var(--color-base, #202122).

Actual
If you run it on https://jqueryui.com/resources/demos/draggable/default.html where jQuery UI is present, it returns rgb(32, 33, 34).

This is not limited to the return value, the style attribute itself is altered so this happens during the setting, not the getting.

The impact is that when you later attach the element to the document, where the CSS variable exists in a stylesheet, its value will not be used, and instead the fallback is always used.

This was found downstream at Wikipedia (https://phabricator.wikimedia.org/T391461), where we found theme variables (especially dark mode) causing some challenging color combinations. The issue only happened on the subset of forms where jQuery UI is loaded by something elsewhere on the page.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions