Skip to content

Commit

Permalink
fix: url parsing errors #612
Browse files Browse the repository at this point in the history
  • Loading branch information
szuperaz committed Jan 6, 2025
1 parent c92f999 commit 2262a62
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,28 @@ describe('MessageComponent', () => {
expect(component.messageTextParts![0].content).toContain(
'<a href="https://getstream.io/" target="_blank" rel="nofollow">https://getstream.io/</a>'
);

component.message.text = 'This is a message with a link google.com';
component.ngOnChanges({ message: {} as SimpleChange });

expect(component.messageTextParts![0].content).toContain(
'<a href="https://google.com" target="_blank" rel="nofollow">google.com</a>'
);

component.message.text = 'This is a message with a link www.google.com';
component.ngOnChanges({ message: {} as SimpleChange });

expect(component.messageTextParts![0].content).toContain(
'<a href="https://www.google.com" target="_blank" rel="nofollow">www.google.com</a>'
);

component.message.text =
'This is a message with a link file:///C:/Users/YourName/Documents/example.txt';
component.ngOnChanges({ message: {} as SimpleChange });

expect(component.messageTextParts![0].content).toContain(
'<a href="file:///C:/Users/YourName/Documents/example.txt" target="_blank" rel="nofollow">file:///C:/Users/YourName/Documents/example.txt</a>'
);
});

it('should display reply count for parent messages', () => {
Expand Down
22 changes: 16 additions & 6 deletions projects/stream-chat-angular/src/lib/message/message.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class MessageComponent
private isViewInited = false;
private userId?: string;
private readonly urlRegexp =
/(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[A-Z0-9+&@#/%=~_|$])/gim;
/(?:(?: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;
private emojiRegexp = new RegExp(emojiRegex(), 'g');
@ViewChild('messageMenuTrigger')
messageMenuTrigger!: NgxFloatUiLooseDirective;
Expand Down Expand Up @@ -579,11 +579,21 @@ export class MessageComponent
if (this.displayAs === 'html') {
return content;
}
content = content.replace(this.urlRegexp, (match) =>
this.messageService.customLinkRenderer
? this.messageService.customLinkRenderer(match)
: `<a href="${match}" target="_blank" rel="nofollow">${match}</a>`
);
content = content.replace(this.urlRegexp, (match) => {
if (this.messageService.customLinkRenderer) {
return this.messageService.customLinkRenderer(match);
} else {
let href = match;
if (
!href.startsWith('http') &&
!href.startsWith('ftp') &&
!href.startsWith('file')
) {
href = `https://${match}`;
}
return `<a href="${href}" target="_blank" rel="nofollow">${match}</a>`;
}
});

return content;
}
Expand Down

0 comments on commit 2262a62

Please sign in to comment.