From bb328792fb2dc1a49130a275fdff1ecd7c3c5131 Mon Sep 17 00:00:00 2001 From: Ted Johansson Date: Fri, 16 Dec 2022 14:48:39 +0800 Subject: [PATCH] FIX: Remove magic time ranges (#19477) In #15474 we introduced dedicated support for date ranges. As part of that change we added a fallback of "magic" date ranges, which treats dates in any paragraph with exactly two dates as a range. There were discussions about migrating all such paragraphs to use the new date range element, but it was ultimately decided against. This change removes the fallback and, as a bonus, adds support for multiple date ranges in the same paragraph. --- .../initializers/discourse-local-dates.js | 24 ++++--- .../spec/system/local_dates_spec.rb | 64 +++++++++++++++++++ spec/support/system_helpers.rb | 12 ++++ 3 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 plugins/discourse-local-dates/spec/system/local_dates_spec.rb diff --git a/plugins/discourse-local-dates/assets/javascripts/initializers/discourse-local-dates.js b/plugins/discourse-local-dates/assets/javascripts/initializers/discourse-local-dates.js index 84b6842257..344d796cdf 100644 --- a/plugins/discourse-local-dates/assets/javascripts/initializers/discourse-local-dates.js +++ b/plugins/discourse-local-dates/assets/javascripts/initializers/discourse-local-dates.js @@ -124,18 +124,24 @@ function _rangeElements(element) { return []; } - // TODO: element.parentElement.children.length !== 2 is a fallback to old solution for ranges - // Condition can be removed after migration to [date-range] - if ( - element.dataset.range !== "true" && - element.parentElement.children.length !== 2 - ) { - return [element]; + if (element.dataset.range) { + return _partitionedRanges(element).find((pair) => pair.includes(element)); } - return Array.from(element.parentElement.children).filter( - (span) => span.dataset.date + return [element]; +} + +function _partitionedRanges(element) { + const partitions = []; + const ranges = Array.from(element.parentElement.children).filter( + (span) => span.dataset.range ); + + while (ranges.length > 0) { + partitions.push(ranges.splice(0, 2)); + } + + return partitions; } function initializeDiscourseLocalDates(api) { diff --git a/plugins/discourse-local-dates/spec/system/local_dates_spec.rb b/plugins/discourse-local-dates/spec/system/local_dates_spec.rb new file mode 100644 index 0000000000..32962713e8 --- /dev/null +++ b/plugins/discourse-local-dates/spec/system/local_dates_spec.rb @@ -0,0 +1,64 @@ +# frozen_string_literal: true + +describe "Local dates", type: :system, js: true do + fab!(:topic) { Fabricate(:topic) } + fab!(:user) { Fabricate(:user) } + + before do + create_post( + user: user, + topic: topic, + title: "Date range test post", + raw: <<~RAW + First option: [date=2022-12-15 time=14:19:00 timezone="Asia/Singapore"] + Second option: [date=2022-12-15 time=01:20:00 timezone="Asia/Singapore"], or [date=2022-12-15 time=02:40:00 timezone="Asia/Singapore"] + Third option: [date-range from=2022-12-15T11:25:00 to=2022-12-16T00:26:00 timezone="Asia/Singapore"] or [date-range from=2022-12-22T11:57:00 to=2022-12-23T11:58:00 timezone="Asia/Singapore"] + RAW + ) + end + + let(:topic_page) { PageObjects::Pages::Topic.new } + + it "renders local dates and date ranges correctly" do + using_browser_timezone("Asia/Singapore") do + sign_in user + + topic_page.visit_topic(topic) + + expect(topic_page).to have_content(topic.title) + + post_dates = topic_page.find_all("span[data-date]") + + # Single date in a paragraph. + # + post_dates[0].click + tippy_date = topic_page.find(".tippy-content .current .date-time") + + expect(tippy_date).to have_text("Thursday, December 15, 2022\n2:19 PM", exact: true) + + # Two single dates in the same paragraph. + # + post_dates[1].click + tippy_date = topic_page.find(".tippy-content .current .date-time") + + expect(tippy_date).to have_text("Thursday, December 15, 2022\n1:20 AM", exact: true) + + post_dates[2].click + tippy_date = topic_page.find(".tippy-content .current .date-time") + + expect(tippy_date).to have_text("Thursday, December 15, 2022\n2:40 AM", exact: true) + + # Two date ranges in the same paragraph. + # + post_dates[3].click + tippy_date = topic_page.find(".tippy-content .current .date-time") + + expect(tippy_date).to have_text("Thursday, December 15, 2022\n11:25 AM → 12:26 AM", exact: true) + + post_dates[5].click + tippy_date = topic_page.find(".tippy-content .current .date-time") + + expect(tippy_date).to have_text("Thursday, December 22, 2022 11:57 AM → Friday, December 23, 2022 11:58 AM", exact: true) + end + end +end diff --git a/spec/support/system_helpers.rb b/spec/support/system_helpers.rb index ad3c9b6b04..c7b592fe77 100644 --- a/spec/support/system_helpers.rb +++ b/spec/support/system_helpers.rb @@ -40,4 +40,16 @@ module SystemHelpers ensure page.driver.browser.manage.window.resize_to(original_size.width, original_size.height) end + + def using_browser_timezone(timezone, &example) + previous_browser_timezone = ENV["TZ"] + + ENV["TZ"] = timezone + + Capybara.using_session(timezone) do + freeze_time(&example) + end + + ENV["TZ"] = previous_browser_timezone + end end