This repository has been archived by the owner on May 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from rpaschoal/development
Merge from Development (1.0.1 changes)
- Loading branch information
Showing
11 changed files
with
136 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
|
||
/* | ||
* Transforms text containing URLs or E-mails to valid links/mailtos | ||
*/ | ||
@Pipe({name: 'linkfy'}) | ||
export class LinkfyPipe implements PipeTransform { | ||
transform(message: string, pipeEnabled: boolean): string { | ||
if (pipeEnabled && message && message.length > 1) | ||
{ | ||
let replacedText; | ||
let replacePatternProtocol; | ||
let replacePatternWWW; | ||
let replacePatternMailTo; | ||
|
||
// URLs starting with http://, https://, or ftp:// | ||
replacePatternProtocol = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim; | ||
replacedText = message.replace(replacePatternProtocol, '<a href="$1" target="_blank">$1</a>'); | ||
|
||
// URLs starting with "www." (ignoring // before it). | ||
replacePatternWWW = /(^|[^\/])(www\.[\S]+(\b|$))/gim; | ||
replacedText = replacedText.replace(replacePatternWWW, '$1<a href="http://$2" target="_blank">$2</a>'); | ||
|
||
// Change email addresses to mailto:: links. | ||
replacePatternMailTo = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim; | ||
replacedText = replacedText.replace(replacePatternMailTo, '<a href="mailto:$1">$1</a>'); | ||
|
||
return replacedText; | ||
} | ||
else | ||
return message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { LinkfyPipe } from '../ng-chat/pipes/linkfy.pipe'; | ||
|
||
describe('LinkfyPipe', () => { | ||
beforeEach(() => { | ||
this.subject = new LinkfyPipe(); | ||
}); | ||
|
||
it('Must work on empty messages', () => { | ||
let result = this.subject.transform('', true); | ||
|
||
expect(result).toBe(''); | ||
}); | ||
|
||
it('Must not replace with link when piple is disabled', () => { | ||
let result = this.subject.transform('www.github.com/rpaschoal/ng-chat', false); | ||
|
||
expect(result).toBe('www.github.com/rpaschoal/ng-chat'); | ||
}); | ||
|
||
it('Must not replace with HTTP link when piple is disabled', () => { | ||
let result = this.subject.transform('http://github.com/rpaschoal/ng-chat', false); | ||
|
||
expect(result).toBe('http://github.com/rpaschoal/ng-chat'); | ||
}); | ||
|
||
it('Must not replace with HTTPs link when piple is disabled', () => { | ||
let result = this.subject.transform('https://github.com/rpaschoal/ng-chat', false); | ||
|
||
expect(result).toBe('https://github.com/rpaschoal/ng-chat'); | ||
}); | ||
|
||
it('Must not replace with FTP link when piple is disabled', () => { | ||
let result = this.subject.transform('ftp://127.0.0.1', false); | ||
|
||
expect(result).toBe('ftp://127.0.0.1'); | ||
}); | ||
|
||
it('Must not replace e-mail with mailto link when piple is disabled', () => { | ||
let result = this.subject.transform('[email protected]', false); | ||
|
||
expect(result).toBe('[email protected]'); | ||
}); | ||
|
||
it('Must replace www.{0} text with link', () => { | ||
let result = this.subject.transform('www.github.com/rpaschoal/ng-chat', true); | ||
|
||
expect(result).toBe('<a href="http://www.github.com/rpaschoal/ng-chat" target="_blank">www.github.com/rpaschoal/ng-chat</a>'); | ||
}); | ||
|
||
it('Must replace http://{0} text with link', () => { | ||
let result = this.subject.transform('http://github.com/rpaschoal/ng-chat', true); | ||
|
||
expect(result).toBe('<a href="http://github.com/rpaschoal/ng-chat" target="_blank">http://github.com/rpaschoal/ng-chat</a>'); | ||
}); | ||
|
||
it('Must replace https://{0} text with link', () => { | ||
let result = this.subject.transform('https://github.com/rpaschoal/ng-chat', true); | ||
|
||
expect(result).toBe('<a href="https://github.com/rpaschoal/ng-chat" target="_blank">https://github.com/rpaschoal/ng-chat</a>'); | ||
}); | ||
|
||
it('Must replace ftp://{0} text with link', () => { | ||
let result = this.subject.transform('ftp://127.0.0.1', true); | ||
|
||
expect(result).toBe('<a href="ftp://127.0.0.1" target="_blank">ftp://127.0.0.1</a>'); | ||
}); | ||
|
||
it('Must replace e-mail with mailto link', () => { | ||
let result = this.subject.transform('[email protected]', true); | ||
|
||
expect(result).toBe('<a href="mailto:[email protected]">[email protected]</a>'); | ||
}); | ||
}); |