Truly independent web browser
LibJS: Ignore a third number in a date string that names its month
Andreas Kling2e3f07d132cf
diff --git a/Libraries/LibJS/Rust/src/runtime/date_parser.rs b/Libraries/LibJS/Rust/src/runtime/date_parser.rsindex 58b3d58e842..165819e0a19 100644--- a/Libraries/LibJS/Rust/src/runtime/date_parser.rs+++ b/Libraries/LibJS/Rust/src/runtime/date_parser.rs@@ -228,6 +228,14 @@ impl DateParser {let number1 = self.numbers[1];let number2 = self.numbers[2];+ if self.year.is_none() && self.month.is_some() {+ // INTEROP: With a named month, the first two numbers are the day and the year, and a third number is+ // ignored. This makes "November 16-22, 2026" the 16th of November 2022, which is what websites+ // that format such a date range get in the most widely used engine.+ self.numbers.truncate(2);+ return self.guess_date_from_2_numbers();+ }+if self.year.is_some() || self.month.is_some() {return false; // Too many numbers}diff --git a/Tests/LibJS/Runtime/builtins/Date/Date.parse.nonStandard.js b/Tests/LibJS/Runtime/builtins/Date/Date.parse.nonStandard.jsindex b29eae7a36c..e3a85fb6922 100644--- a/Tests/LibJS/Runtime/builtins/Date/Date.parse.nonStandard.js+++ b/Tests/LibJS/Runtime/builtins/Date/Date.parse.nonStandard.js@@ -328,10 +328,11 @@ test("ambiguous date: 3 numbers", () => {expect(Date.parse("40 18 10")).toBeNaN(); // YDM fails- expect(Date.parse("7 8 9 Feb")).toBeNaN(); // Introducing an explicit month fails (too many numbers).- expect(Date.parse("7 8 Feb 9")).toBeNaN();- expect(Date.parse("7 Feb 8 9")).toBeNaN();- expect(Date.parse("Feb 7 8 9")).toBeNaN();+ // With an explicit month, the first two numbers are the day and the year, and the third is ignored.+ expect("2008-02-07T08:00:00.000Z").toBe(new Date("7 8 9 Feb").toISOString());+ expect("2008-02-07T08:00:00.000Z").toBe(new Date("7 8 Feb 9").toISOString());+ expect("2008-02-07T08:00:00.000Z").toBe(new Date("7 Feb 8 9").toISOString());+ expect("2008-02-07T08:00:00.000Z").toBe(new Date("Feb 7 8 9").toISOString());expect(Date.parse("0 0")).toBeNaN(); // YM with M=0expect(Date.parse("2000 0")).toBeNaN();@@ -712,3 +713,18 @@ test("non-ASCII input", () => {expect(Date.parse("Jan 01 2024 GMT\u00a0")).toBeNaN();expect(Date.parse("Jan 01 2024 (UTC)\u00a0")).toBeNaN();});++test("named month followed by three numbers", () => {+ const originalTimeZone = setTimeZone("UTC");++ // The first two numbers are the day and the year, and the third is ignored.+ expect("2022-11-16T00:00:00.000Z").toBe(new Date("November 16-22, 2026").toISOString());+ expect("2022-11-16T00:00:00.000Z").toBe(new Date("Nov 16 22 2026").toISOString());+ expect("2022-11-16T00:00:00.000Z").toBe(new Date("16 November 22 2026").toISOString());+ expect("2022-11-16T10:00:00.000Z").toBe(new Date("Nov 16 22 2026 10:00").toISOString());+ expect("2040-11-22T00:00:00.000Z").toBe(new Date("Nov 40 22 2026").toISOString());++ expect(Date.parse("Nov 1 2 3 4")).toBeNaN();++ setTimeZone(originalTimeZone);+});