Skip to content

Commit

Permalink
[close #3] with parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
cmilfont committed May 22, 2012
1 parent 5799ef4 commit 5f8c5dc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
10 changes: 8 additions & 2 deletions lib/linkify.js
Expand Up @@ -3,11 +3,17 @@
var noProtocolUrl = /(^|["'(\s]|<)(www\..+?\..+?)((?:[:?]|\.+)?(?=(?:\s|$)|>|[)"',]))/g,
httpOrMailtoUrl = /(^|["'(\s]|<)((?:(?:https?|ftp):\/\/|mailto:).+?)((?:[:?]|\.+)?(?=(?:\s|$)|>|[)"',]))/g;

$.fn.linkify = function() {
$.fn.linkify = function(params) {
var statement = '$1<a href="$2">$2</a>$3';

if(typeof params !== 'undefined') {
statement = '$1<a href="$2" ' + params + '>$2</a>$3';
}

$(this).html(
$(this).html()
.replace( noProtocolUrl, '$1<a href="<``>://$2">$2</a>$3' )
.replace( httpOrMailtoUrl, '$1<a href="$2">$2</a>$3' )
.replace( httpOrMailtoUrl, statement )
.replace( /"<``>/g, '"http' )
);
return $(this);
Expand Down
2 changes: 1 addition & 1 deletion minified/linkify.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions specs/linkifyconsecutive.spec.js
@@ -1,16 +1,10 @@
xdescribe("When there are consecutive URLs", function() {
xit('should have 3 URLs');
});

var linkify = function(url) {
return jQuery("<div>").html(url).linkify();
},

urlsInSequence = "https://www.mifont.org www.grupofortes.com.br www.google.com",
alternateUrls = "https://www.mifont.org bla ble www.grupofortes.com.br oh my gosh www.google.com",

linkifiedInSequence = "<a href=\"https://www.mifont.org\">https://www.mifont.org</a> <a href=\"http://www.grupofortes.com.br\">www.grupofortes.com.br</a> <a href=\"http://www.google.com\">www.google.com</a>",

linkifiedAlternate = "<a href=\"https://www.mifont.org\">https://www.mifont.org</a> bla ble <a href=\"http://www.grupofortes.com.br\">www.grupofortes.com.br</a> oh my gosh <a href=\"http://www.google.com\">www.google.com</a>";

describe("Linkify consecutive URLs", function() {
Expand Down
16 changes: 16 additions & 0 deletions specs/passingTarget.spec.js
@@ -0,0 +1,16 @@
var linkify = function(url, target) {
return jQuery("<div>").html(url).linkify(target);
},
linkified = "<a href=\"http://www.mifont.org\" target=\"_blank\">http://www.mifont.org</a>";

describe("Linkify with Target", function() {

beforeEach( function () {
jQuery("body").html("");
});

it('should target in link tag', function() {
expect( linkified ).toEqual( linkify( "http://www.mifont.org", "target='_blank'" ).html() );
});

});

0 comments on commit 5f8c5dc

Please sign in to comment.