Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added linkify filter and linkify tests #62

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/_filter/string/linkify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @ngdoc filter
* @name linkify
* @kind function
*
* @description
* takes string/html and scans for any links and convert them to links.
* Supports converting to ftp, mails and normal url links.
*/

angular.module('a8m.linkify', [])

.filter('linkify',[ function () {
return function (input) {

var stringToBeReturn = '';

// replace for url links and ftp links
stringToBeReturn = input.replace(/((?:ht|f)tps?:\/?\/?([^\s|^\<]+))/g, "<a target='_blank' href='$1'>$2</a>");

// replace for email links
return stringToBeReturn.replace(/(?:([^\s|^\>]+\@[0-9a-zA-Z\_]+\.[^\s|^\<]+))/g, "<a href='mailto:$1'>$1</a>");
}
}]);
1 change: 1 addition & 0 deletions src/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ angular.module('angular.filter', [
'a8m.ltrim',
'a8m.rtrim',
'a8m.repeat',
'a8m.linkify',

'a8m.to-array',
'a8m.concat',
Expand Down
47 changes: 47 additions & 0 deletions test/spec/filter/string/linkify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

describe('linkifyFilter', function () {

var filter;

beforeEach(module('a8m.linkify'));

beforeEach(inject(function ($filter) {
filter = $filter('linkify');
}));

it('should return proper format for domain url either https or not, and either with tags or space before/after', function() {

expect(filter('http://domain.com')).toEqual("<a target='_blank' href='http://domain.com'>domain.com</a>");
expect(filter('http://www.domain.com')).toEqual("<a target='_blank' href='http://www.domain.com'>www.domain.com</a>");
expect(filter('https://www.domain.com')).toEqual("<a target='_blank' href='https://www.domain.com'>www.domain.com</a>");
expect(filter('<p>some tags before</p>http://www.domain.com some txt')).toEqual("<p>some tags before</p><a target='_blank' href='http://www.domain.com'>www.domain.com</a> some txt");
expect(filter('some text http://www.domain.com<p>some tags after</p>')).toEqual("some text <a target='_blank' href='http://www.domain.com'>www.domain.com</a><p>some tags after</p>");

});

it('should return proper format for ftp url either https or not, and either with tags or space before/after', function() {

expect(filter('ftp://domain.com')).toEqual("<a target='_blank' href='ftp://domain.com'>domain.com</a>");
expect(filter('ftp://www.domain.com')).toEqual("<a target='_blank' href='ftp://www.domain.com'>www.domain.com</a>");
expect(filter('ftps://www.domain.com')).toEqual("<a target='_blank' href='ftps://www.domain.com'>www.domain.com</a>");
expect(filter('<p>some tags before</p>ftps://www.domain.com some txt')).toEqual("<p>some tags before</p><a target='_blank' href='ftps://www.domain.com'>www.domain.com</a> some txt");
expect(filter('some text ftps://www.domain.com<p>some tags after</p>')).toEqual("some text <a target='_blank' href='ftps://www.domain.com'>www.domain.com</a><p>some tags after</p>");

});

it('should return proper format for mail url', function() {

expect(filter('[email protected]')).toEqual("<a href='mailto:[email protected]'>[email protected]</a>");

});

it('should return proper format for mail url even for space or with tags', function() {

var textToBeScan = "someText here [email protected]<p>someTag</p>",
textToBeReturned = "someText here <a href='mailto:[email protected]'>[email protected]</a><p>someTag</p>";

expect(filter(textToBeScan)).toEqual(textToBeReturned);

});
});