Skip to content

Commit e0df0ed

Browse files
rajveermalviyagnprice
authored andcommitted
content: Handle legacy website previews
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.
1 parent 08dc485 commit e0df0ed

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/model/content.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,7 @@ class _ZulipContentParser {
13911391
return EmbedVideoNode(hrefUrl: href, previewImageSrcUrl: imgSrc, debugHtmlNode: debugHtmlNode);
13921392
}
13931393

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

13961396
BlockContentNode parseWebsitePreviewNode(dom.Element divElement) {
13971397
assert(divElement.localName == 'div'
@@ -1415,7 +1415,7 @@ class _ZulipContentParser {
14151415
]) {
14161416
final match = _websitePreviewImageSrcRegexp.firstMatch(imageStyleAttr);
14171417
if (match == null) return null;
1418-
final imageSrcUrl = match.group(1);
1418+
final imageSrcUrl = match.group(2);
14191419
if (imageSrcUrl == null) return null;
14201420

14211421
String? parseTitle(dom.Element element) {

test/model/content_test.dart

+25
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,30 @@ class ContentExample {
11091109
description: null),
11101110
]);
11111111

1112+
static const legacyWebsitePreviewSmoke = ContentExample(
1113+
'legacy website preview smoke',
1114+
// https://chat.zulip.org/#narrow/channel/7-test-here/topic/URL.20previews/near/192777
1115+
'www.youtube.com',
1116+
'<p><a href="http://www.youtube.com" target="_blank" title="http://www.youtube.com">www.youtube.com</a></p>\n'
1117+
'<div class="message_embed">'
1118+
'<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>'
1119+
'<div class="data-container">'
1120+
'<div class="message_embed_title"><a href="http://www.youtube.com" target="_blank" title="YouTube">YouTube</a></div>'
1121+
'<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>', [
1122+
ParagraphNode(links: [], nodes: [
1123+
LinkNode(
1124+
nodes: [TextNode('www.youtube.com')],
1125+
url: 'http://www.youtube.com'),
1126+
]),
1127+
WebsitePreviewNode(
1128+
hrefUrl: 'http://www.youtube.com',
1129+
imageSrcUrl: 'https://youtube.com/yts/img/yt_1200-vfl4C3T0K.png',
1130+
title: 'YouTube',
1131+
description: 'Enjoy the videos and music you love, upload '
1132+
'original content, and share it all with friends, family, and '
1133+
'the world on YouTube.'),
1134+
]);
1135+
11121136
static const tableWithSingleRow = ContentExample(
11131137
'table with single row',
11141138
// https://chat.zulip.org/#narrow/channel/7-test-here/topic/Rajesh/near/1971202
@@ -1654,6 +1678,7 @@ void main() {
16541678
testParseExample(ContentExample.websitePreviewWithoutTitle);
16551679
testParseExample(ContentExample.websitePreviewWithoutDescription);
16561680
testParseExample(ContentExample.websitePreviewWithoutTitleOrDescription);
1681+
testParseExample(ContentExample.legacyWebsitePreviewSmoke);
16571682

16581683
testParseExample(ContentExample.tableWithSingleRow);
16591684
testParseExample(ContentExample.tableWithMultipleRows);

0 commit comments

Comments
 (0)