Skip to content

Commit c451e64

Browse files
authored
Merge pull request #678 from GetStream/fix-url-parsing-errors
fix: url parsing errors #612
2 parents c92f999 + 2262a62 commit c451e64

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

projects/stream-chat-angular/src/lib/message/message.component.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,28 @@ describe('MessageComponent', () => {
930930
expect(component.messageTextParts![0].content).toContain(
931931
'<a href="https://getstream.io/" target="_blank" rel="nofollow">https://getstream.io/</a>'
932932
);
933+
934+
component.message.text = 'This is a message with a link google.com';
935+
component.ngOnChanges({ message: {} as SimpleChange });
936+
937+
expect(component.messageTextParts![0].content).toContain(
938+
'<a href="https://google.com" target="_blank" rel="nofollow">google.com</a>'
939+
);
940+
941+
component.message.text = 'This is a message with a link www.google.com';
942+
component.ngOnChanges({ message: {} as SimpleChange });
943+
944+
expect(component.messageTextParts![0].content).toContain(
945+
'<a href="https://www.google.com" target="_blank" rel="nofollow">www.google.com</a>'
946+
);
947+
948+
component.message.text =
949+
'This is a message with a link file:///C:/Users/YourName/Documents/example.txt';
950+
component.ngOnChanges({ message: {} as SimpleChange });
951+
952+
expect(component.messageTextParts![0].content).toContain(
953+
'<a href="file:///C:/Users/YourName/Documents/example.txt" target="_blank" rel="nofollow">file:///C:/Users/YourName/Documents/example.txt</a>'
954+
);
933955
});
934956

935957
it('should display reply count for parent messages', () => {

projects/stream-chat-angular/src/lib/message/message.component.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export class MessageComponent
115115
private isViewInited = false;
116116
private userId?: string;
117117
private readonly urlRegexp =
118-
/(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[A-Z0-9+&@#/%=~_|$])/gim;
118+
/(?:(?:https?|ftp|file):\/\/|www\.|ftp\.|(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,})(?![^\s]*@[^\s]*)(?:[^\s()<>]+|\([\w\d]+\))*(?<!@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/gim;
119119
private emojiRegexp = new RegExp(emojiRegex(), 'g');
120120
@ViewChild('messageMenuTrigger')
121121
messageMenuTrigger!: NgxFloatUiLooseDirective;
@@ -579,11 +579,21 @@ export class MessageComponent
579579
if (this.displayAs === 'html') {
580580
return content;
581581
}
582-
content = content.replace(this.urlRegexp, (match) =>
583-
this.messageService.customLinkRenderer
584-
? this.messageService.customLinkRenderer(match)
585-
: `<a href="${match}" target="_blank" rel="nofollow">${match}</a>`
586-
);
582+
content = content.replace(this.urlRegexp, (match) => {
583+
if (this.messageService.customLinkRenderer) {
584+
return this.messageService.customLinkRenderer(match);
585+
} else {
586+
let href = match;
587+
if (
588+
!href.startsWith('http') &&
589+
!href.startsWith('ftp') &&
590+
!href.startsWith('file')
591+
) {
592+
href = `https://${match}`;
593+
}
594+
return `<a href="${href}" target="_blank" rel="nofollow">${match}</a>`;
595+
}
596+
});
587597

588598
return content;
589599
}

0 commit comments

Comments
 (0)