Skip to content

Incorrect type-checking for CSS length units #3023

@rjgotten

Description

@rjgotten

For reference, the current contents of the Unit.prototype.isLength method:

Unit.prototype.isLength = function () {
    return Boolean(this.toCSS().match(/px|em|%|in|cm|mm|pc|pt|ex/));
};

If we look at the section of the CSS Values and Units Module Level 3 specification that treats <length> units, we find that the current isLength implementation is incorrect and incomplete.

The function fails to recognize the viewport-relative units (vw, vh, vmin, vmax); the quarter millimeter (Q) absolute unit; and the zero-advance (ch) font-relative units as length units.

Through a fluke, it does recognize the root em (rem) font-relative unit, because the used regex has no start and end anchors in place and it matches "em". However, this works both ways: the function currently also incorrectly recognizes the dots-per-pixel (dppx) resolution unit as a length unit, because it matches "px" in the regex.

The function also should not be recognizing percentages as a length unit. While many operations and properties that accept length units also accept percentages, percentages themselves are not length units according to the specification.

(Also; the implementation should be using the RegExp.prototype.test method which returns an actual boolean, instead of using the String.prototype.match method and casting the result.)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions