Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tolerate comma instead of dot typo in style value input #3926

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const parseIntermediateOrInvalidValue = (
property: StyleProperty,
styleValue: IntermediateStyleValue | InvalidValue
): StyleValue => {
const value = styleValue.value.trim();
let value = styleValue.value.trim();

if (property.startsWith("--")) {
return {
Expand All @@ -23,6 +23,12 @@ export const parseIntermediateOrInvalidValue = (
};
}

// If input contains a number, we assume its either a value or value + unit.
// Users often mistype comma instead of dot and we want to be tolerant to that.
if (Number.isNaN(Number.parseFloat(value)) === false) {
value = value.replace(",", ".");
}

const valueInfo = properties[property as keyof typeof properties];

// - When user enters a number, we don't know if its a valid unit value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,33 @@ describe("Parse intermediate or invalid value without math evaluation", () => {
value: "border-box",
});
});

test("tolerate comma instead of dot typo", () => {
const result = parseIntermediateOrInvalidValue("width", {
type: "intermediate",
value: "2,5",
unit: "rem",
});

expect(result).toEqual({
type: "unit",
value: 2.5,
unit: "rem",
});
});

test("tolerate comma instead of dot typo with unit input", () => {
const result = parseIntermediateOrInvalidValue("width", {
type: "intermediate",
value: "2,5rem",
});

expect(result).toEqual({
type: "unit",
value: 2.5,
unit: "rem",
});
});
});

describe("Parse intermediate or invalid value with math evaluation", () => {
Expand Down
Loading