Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
markdown: Render invalid timestamps as plain text.
Aadyot146d5f103424f
diff --git a/api_docs/changelog.md b/api_docs/changelog.mdindex 604429937f8..220c5ee9dc9 100644--- a/api_docs/changelog.md+++ b/api_docs/changelog.md@@ -20,6 +20,12 @@ format used by the Zulip server that they are interacting with.## Changes in Zulip 12.0+**Feature level 451**++* [Message formatting](/api/message-formatting): Changed the+rendering of invalid timestamps from a `<span class="timestamp-error">`+element to plain escaped text.+**Feature level 450*** [`GET /events`](/api/get-events): The `push_device` events nowdiff --git a/api_docs/message-formatting.md b/api_docs/message-formatting.mdindex 82be7e138d2..2f47bbf3aff 100644--- a/api_docs/message-formatting.md+++ b/api_docs/message-formatting.md@@ -19,6 +19,19 @@ for syntax highlighting. This field is used in the## Global times+**Changes**: In Zulip 12.0 (feature level 451), invalid timestamp formats+are now rendered as escaped literal text instead of a `<span>` element with+`timestamp-error` class and an error message.++Previously, an invalid timestamp string would be rendered as:+``` html+<span class="timestamp-error">Invalid time format: invalid</span>+```+Now, it is rendered as:+``` html+<time:invalid>+```+**Changes**: In Zulip 3.0 (feature level 8), added [global timementions][help-global-time] to supported Markdown message formattingfeatures.diff --git a/version.py b/version.pyindex 000df52d1b4..88f8617b816 100644--- a/version.py+++ b/version.py@@ -35,7 +35,7 @@ DESKTOP_WARNING_VERSION = "5.9.3"# new level means in api_docs/changelog.md, as well as "**Changes**"# entries in the endpoint's documentation in `zulip.yaml`.-API_FEATURE_LEVEL = 450+API_FEATURE_LEVEL = 451# Bump the minor PROVISION_VERSION to indicate that folks should provision# only when going from an old version of the code to a newer version. Bumpdiff --git a/web/src/rendered_markdown.ts b/web/src/rendered_markdown.tsindex 0932b0ce349..12243ef39f7 100644--- a/web/src/rendered_markdown.ts+++ b/web/src/rendered_markdown.ts@@ -289,7 +289,8 @@ export const update_elements = ($content: JQuery): void => {blueslip.error("Could not parse datetime supplied by backend", {time_str});}});-+ // Maintains backward compatibility for legacy 'timestamp-error' spans.+ // The server no longer generates this markup, but older records may still contain it.$content.find("span.timestamp-error").each(function (): void {const match_array = /^Invalid time format: (.*)$/.exec($(this).text());assert(match_array !== null);diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.pyindex afd0c9f89a3..e030c0b2f2b 100644--- a/zerver/lib/markdown/__init__.py+++ b/zerver/lib/markdown/__init__.py@@ -1226,7 +1226,7 @@ class CompiledInlineProcessor(markdown.inlinepatterns.InlineProcessor):class Timestamp(markdown.inlinepatterns.Pattern):@override- def handleMatch(self, match: Match[str]) -> Element | None:+ def handleMatch(self, match: Match[str]) -> Element | str:time_input_string = match.group("time")try:timestamp = dateutil.parser.parse(time_input_string, tzinfos=common_timezones)@@ -1237,12 +1237,7 @@ class Timestamp(markdown.inlinepatterns.Pattern):timestamp = Noneif not timestamp:- error_element = Element("span")- error_element.set("class", "timestamp-error")- error_element.text = markdown.util.AtomicString(- f"Invalid time format: {time_input_string}"- )- return error_element+ return f"<time:{time_input_string}>"# Use HTML5 <time> element for valid timestamps.time_element = Element("time")@@ -1250,12 +1245,7 @@ class Timestamp(markdown.inlinepatterns.Pattern):try:timestamp = timestamp.astimezone(timezone.utc)except (ValueError, OverflowError):- error_element = Element("span")- error_element.set("class", "timestamp-error")- error_element.text = markdown.util.AtomicString(- f"Invalid time format: {time_input_string}"- )- return error_element+ return f"<time:{time_input_string}>"else:timestamp = timestamp.replace(tzinfo=timezone.utc)time_element.set("datetime", timestamp.isoformat().replace("+00:00", "Z"))diff --git a/zerver/tests/fixtures/markdown_test_cases.json b/zerver/tests/fixtures/markdown_test_cases.jsonindex c2b9af2793d..4108da9c672 100644--- a/zerver/tests/fixtures/markdown_test_cases.json+++ b/zerver/tests/fixtures/markdown_test_cases.json@@ -826,9 +826,9 @@{"name": "timestamp_invalid_input","input": "<time:<alert(1)>>",- "expected_output": "<p><span class=\"timestamp-error\">Invalid time format: <alert(1)</span>></p>",+ "expected_output": "<p><time:<alert(1)>></p>","marked_expected_output": "<p><span><alert(1)</span>></p>",- "text_content": "Invalid time format: <alert(1)>"+ "text_content": "<time:<alert(1)>>"},{"name": "timestamp_timezone",@@ -840,23 +840,23 @@{"name": "timestamp_incorrect","input": "<time:**hello world**>",- "expected_output": "<p><span class=\"timestamp-error\">Invalid time format: **hello world**</span></p>",+ "expected_output": "<p><time:**hello world**></p>","marked_expected_output": "<p><span>**hello world**</span></p>",- "text_content": "Invalid time format: **hello world**"+ "text_content": "<time:**hello world**>"},{"name": "timestamp_invalid_timezone","input": "<time:1969-12-31T00:00:00+32:00>",- "expected_output": "<p><span class=\"timestamp-error\">Invalid time format: 1969-12-31T00:00:00+32:00</span></p>",+ "expected_output": "<p><time:1969-12-31T00:00:00+32:00></p>","marked_expected_output": "<p><span>1969-12-31T00:00:00+32:00</span></p>",- "text_content": "Invalid time format: 1969-12-31T00:00:00+32:00"+ "text_content": "<time:1969-12-31T00:00:00+32:00>"},{"name": "timestamp_overflow","input": "<time:0001-01-01T01:00:00+02:00>",- "expected_output": "<p><span class=\"timestamp-error\">Invalid time format: 0001-01-01T01:00:00+02:00</span></p>",+ "expected_output": "<p><time:0001-01-01T01:00:00+02:00></p>","marked_expected_output": "<p><span>0001-01-01T01:00:00+02:00</span></p>",- "text_content": "Invalid time format: 0001-01-01T01:00:00+02:00"+ "text_content": "<time:0001-01-01T01:00:00+02:00>"},{"name": "timestamp_unix",@@ -867,9 +867,9 @@{"name": "timestamp_unix_overflow","input": "Let's meet at <time:1234567890123>.",- "expected_output": "<p>Let's meet at <span class=\"timestamp-error\">Invalid time format: 1234567890123</span>.</p>",+ "expected_output": "<p>Let's meet at <time:1234567890123>.</p>","marked_expected_output": "<p>Let's meet at <time datetime=\"+041091-11-25T05:02:03Z\">1234567890123</time>.</p>",- "text_content": "Let's meet at Invalid time format: 1234567890123."+ "text_content": "Let's meet at <time:1234567890123>."},{"name": "tex_inline",