Skip to content

Commit

Permalink
content: Handle legacy website previews
Browse files Browse the repository at this point in the history
In legacy website preview messages, the image URL in
`message_embed_image` element's `style` is formatted as:

  background-image: url(https://example.com/image.png)

In the latest server revision, it is formatted as:

  background-image: url("https://example.com/image.png")

So, fix the regexp to match the URL whether or not it's
enclosed in quotes.
  • Loading branch information
rajveermalviya authored and gnprice committed Feb 26, 2025
1 parent 08dc485 commit e0df0ed
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/model/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1391,7 +1391,7 @@ class _ZulipContentParser {
return EmbedVideoNode(hrefUrl: href, previewImageSrcUrl: imgSrc, debugHtmlNode: debugHtmlNode);
}

static final _websitePreviewImageSrcRegexp = RegExp(r'background-image: url\("(.+)"\)');
static final _websitePreviewImageSrcRegexp = RegExp(r'background-image: url\(("?)(.+?)\1\)');

BlockContentNode parseWebsitePreviewNode(dom.Element divElement) {
assert(divElement.localName == 'div'
Expand All @@ -1415,7 +1415,7 @@ class _ZulipContentParser {
]) {
final match = _websitePreviewImageSrcRegexp.firstMatch(imageStyleAttr);
if (match == null) return null;
final imageSrcUrl = match.group(1);
final imageSrcUrl = match.group(2);
if (imageSrcUrl == null) return null;

String? parseTitle(dom.Element element) {
Expand Down
25 changes: 25 additions & 0 deletions test/model/content_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,30 @@ class ContentExample {
description: null),
]);

static const legacyWebsitePreviewSmoke = ContentExample(
'legacy website preview smoke',
// https://chat.zulip.org/#narrow/channel/7-test-here/topic/URL.20previews/near/192777
'www.youtube.com',
'<p><a href="http://www.youtube.com" target="_blank" title="http://www.youtube.com">www.youtube.com</a></p>\n'
'<div class="message_embed">'
'<a class="message_embed_image" href="http://www.youtube.com" style="background-image: url(https://youtube.com/yts/img/yt_1200-vfl4C3T0K.png)" target="_blank"></a>'
'<div class="data-container">'
'<div class="message_embed_title"><a href="http://www.youtube.com" target="_blank" title="YouTube">YouTube</a></div>'
'<div class="message_embed_description">Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.</div></div></div>', [
ParagraphNode(links: [], nodes: [
LinkNode(
nodes: [TextNode('www.youtube.com')],
url: 'http://www.youtube.com'),
]),
WebsitePreviewNode(
hrefUrl: 'http://www.youtube.com',
imageSrcUrl: 'https://youtube.com/yts/img/yt_1200-vfl4C3T0K.png',
title: 'YouTube',
description: 'Enjoy the videos and music you love, upload '
'original content, and share it all with friends, family, and '
'the world on YouTube.'),
]);

static const tableWithSingleRow = ContentExample(
'table with single row',
// https://chat.zulip.org/#narrow/channel/7-test-here/topic/Rajesh/near/1971202
Expand Down Expand Up @@ -1654,6 +1678,7 @@ void main() {
testParseExample(ContentExample.websitePreviewWithoutTitle);
testParseExample(ContentExample.websitePreviewWithoutDescription);
testParseExample(ContentExample.websitePreviewWithoutTitleOrDescription);
testParseExample(ContentExample.legacyWebsitePreviewSmoke);

testParseExample(ContentExample.tableWithSingleRow);
testParseExample(ContentExample.tableWithMultipleRows);
Expand Down

0 comments on commit e0df0ed

Please sign in to comment.