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

[WIP] Issue 17 - Punctuation delimited url list #28

Open
wants to merge 3 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
7 changes: 4 additions & 3 deletions addon/helpers/linkify.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import Ember from 'ember';
import { urlRegex , shortenUrl } from 'ember-linkify/utils/url-regex';
import { urlRegex , shortenUrl, delimiterBuilder } from 'ember-linkify/utils/url-regex';

const ALLOWED_ATTRIBUTE_NAMES = [ 'rel', 'class' ];

export function linkify( params, options ) {
export function linkify( params, options={} ) {
let textToLinkify = Ember.Handlebars.Utils.escapeExpression(params[0]);
const windowTarget = params[1] || "_self";
const sharedAttributes = opts2attrs( options );
const urlParser = options.delimiter ? delimiterBuilder(options.delimiter.trim()) : urlRegex();

textToLinkify = textToLinkify.replace(urlRegex(), function (s) {
textToLinkify = textToLinkify.replace(urlParser, function (s) {
let url;
let displayText = s.trim();

Expand Down
13 changes: 12 additions & 1 deletion addon/utils/url-regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,15 @@ function shortenUrl ( url , length ) {
return url;
}

export { urlRegex, shortenUrl };
// Builds a regex with the escapes for a given delimiter
function delimiterBuilder (delimiter) {

let urlHeadPattern = /(["'])?(?:(?:(?:(?:https?|ftp|\w):)?\/\/)|(?:www.))(?:\S+(?::\S*)?@)?(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:1\d\d|2[0-4]\d|25[0-4]|[1-9]\d?))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?/ig,
urlTailPattern = /[a-z0-9\-._~:/?#%[\]@!$&'()*+;=`,"]/;

urlTailPattern = new RegExp(urlTailPattern.source.replace(delimiter,""),"ig");

return new RegExp(`${urlHeadPattern.source}(?:\/${urlTailPattern.source}*)?\\1`, "ig");
}

export { urlRegex, shortenUrl, delimiterBuilder };
6 changes: 4 additions & 2 deletions tests/dummy/app/templates/application.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<h1>Linkify</h1>

<style>a { border: 1px solid red; }</style>
{{textarea value=text placeholder='Enter some text with a url'}} <br>
{{linkify text }}
Period delimited: {{linkify text delimiter="."}} <br>
Comma delimited: {{linkify text delimiter=","}} <br>
Default delimiters: {{linkify text}} <br>

{{outlet}}
49 changes: 48 additions & 1 deletion tests/unit/helpers/linkify-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ test('it should turn a ip address into a link', function(assert) {
assert.equal(result, 'My link: <a href="https://62.123.123.123/test" target="_self">https://62.123.123.123/test</a> and some more text');
});

test('it should turn a url with multiple slashes into a link', function(assert) {
var result = linkify(['My link: http://google.com/my/name//is///slimshady and some more text']).toString().trim();
assert.equal(result, 'My link: <a href="http://google.com/my/name//is///slimshady" target="_self">http://google.com/my/name//is///slimshady</a> and some more text');
});

test('it should turn a url with encoded characters into a link', function(assert) {
var result = linkify(['My link: https://www.google.ca/maps/search/oops%2Bi%2Bdid%2Bit%2Bagain/ and some more text']).toString().trim();
assert.equal(result, 'My link: <a href="https://www.google.ca/maps/search/oops%2Bi%2Bdid%2Bit%2Bagain/" target="_self">https://www.google.ca/maps/search/oops%2Bi%2Bdid%2Bit%2Bagain/</a> and some more text');
});

test('it should turn a url with www. into a link', function(assert) {
var result = linkify(['www.johnotander.com']).toString().trim();
assert.equal(result, '<a href="//www.johnotander.com" target="_self">www.johnotander.com</a>');
Expand Down Expand Up @@ -103,4 +113,41 @@ test('it should turn a url into a link with a class of "amilkey" and a rel of "n
test('it should turn a space delimited list of urls into seperate links', function(assert) {
var result = linkify(['My link: http://google.com http://bing.com www.altavista.com']).toString().trim();
assert.equal(result, 'My link: <a href="http://google.com" target="_self">http://google.com</a> <a href="http://bing.com" target="_self">http://bing.com</a> <a href="//www.altavista.com" target="_self">www.altavista.com</a>');
});
});

test('it should turn a comma delimited list of urls without slashes into seperate links by default', function(assert) {
var result = linkify(['My link: http://google.com, http://bing.com,www.altavista.com']).toString().trim();
assert.equal(result, 'My link: <a href="http://google.com" target="_self">http://google.com</a>, <a href="http://bing.com" target="_self">http://bing.com</a>,<a href="//www.altavista.com" target="_self">www.altavista.com</a>');
});

test('it should turn a comma delimited list of urls with slashes into seperate links', function(assert) {
var options = {
delimiter : ","
};
var result = linkify(["http://emberjs.com/,http://emberjs.com/really", "_blank"] , options ).toString().trim();
assert.equal(result , '<a href="http://emberjs.com/" target="_blank">http://emberjs.com/</a>,<a href="http://emberjs.com/really" target="_blank">http://emberjs.com/really</a>' );
});

test('it should turn a period delimited list of urls with slashes into seperate links', function(assert) {
var options = {
delimiter : "."
};
var result = linkify(["http://emberjs.com/.http://emberjs.com/really.www.yes.really/", "_blank"] , options ).toString().trim();
assert.equal(result , '<a href="http://emberjs.com/" target="_blank">http://emberjs.com/</a>.<a href="http://emberjs.com/really" target="_blank">http://emberjs.com/really</a>.<a href="//www.yes.really/" target="_blank">www.yes.really/</a>' );
});

test('it should turn a space delimited list of urls with slashes into seperate links even with delimiter . passed', function(assert) {
var options = {
delimiter : "."
};
var result = linkify(["http://emberjs.com/ http://emberjs.com/really www.yes.really/. www.yes.seriously", "_blank"] , options ).toString().trim();
assert.equal(result , '<a href="http://emberjs.com/" target="_blank">http://emberjs.com/</a> <a href="http://emberjs.com/really" target="_blank">http://emberjs.com/really</a> <a href="//www.yes.really/" target="_blank">www.yes.really/</a>. <a href="//www.yes.seriously" target="_blank">www.yes.seriously</a>' );
});

test('it should turn a space delimited list of urls with slashes into seperate links even with delimiter , passed', function(assert) {
var options = {
delimiter : ","
};
var result = linkify(["http://emberjs.com/ http://emberjs.com/really www.yes.really/, www.yes.seriously", "_blank"] , options ).toString().trim();
assert.equal(result , '<a href="http://emberjs.com/" target="_blank">http://emberjs.com/</a> <a href="http://emberjs.com/really" target="_blank">http://emberjs.com/really</a> <a href="//www.yes.really/" target="_blank">www.yes.really/</a>, <a href="//www.yes.seriously" target="_blank">www.yes.seriously</a>' );
});