Skip to content

Commit e7ac584

Browse files
author
Nick Frasser
committed
Merge pull request #106 from fcarreiro/master
Added validate option (function) to be able to selectively filter tokens
2 parents 173e1b8 + f186859 commit e7ac584

15 files changed

+130
-19
lines changed

src/linkify-element.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ function tokensToNodes(tokens, opts, doc) {
3232

3333
for (let i = 0; i < tokens.length; i++) {
3434
let token = tokens[i];
35+
let validated = token.isLink && options.resolve(opts.validate, token.toString(), token.type);
3536

36-
if (token.isLink) {
37+
if (token.isLink && validated) {
3738

3839
let
3940
href = token.toHref(opts.defaultProtocol),

src/linkify-html.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ function linkifyChars(str, opts) {
7777

7878
for (var i = 0; i < tokens.length; i++) {
7979
let token = tokens[i];
80+
let validated = token.isLink && linkify.options.resolve(opts.validate, token.toString(), token.type);
81+
8082
if (token.type === 'nl' && opts.nl2br) {
8183
result.push({
8284
type: StartTag,
@@ -85,7 +87,7 @@ function linkifyChars(str, opts) {
8587
selfClosing: true
8688
});
8789
continue;
88-
} else if (!token.isLink) {
90+
} else if (!token.isLink || !validated) {
8991
result.push({type: Chars, chars: token.toString()});
9092
continue;
9193
}

src/linkify-jquery.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ function apply($, doc=null) {
5959
tagName: data.linkifyTagname,
6060
target: data.linkifyTarget,
6161
linkClass: data.linkifyLinkclass,
62+
validate: data.linkifyValidate,
6263
};
6364
let $target = target === 'this' ? $this : $this.find(target);
6465
$target.linkify(options);

src/linkify-string.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ function linkifyStr(str, opts={}) {
3737

3838
for (let i = 0; i < tokens.length; i++) {
3939
let token = tokens[i];
40-
if (token.isLink) {
40+
let validated = token.isLink && options.resolve(opts.validate, token.toString(), token.type);
4141

42+
if (token.isLink && validated) {
43+
4244
let
4345
href = token.toHref(opts.defaultProtocol),
4446
formatted = options.resolve(opts.format, token.toString(), token.type),
47+
4548
formattedHref = options.resolve(opts.formatHref, href, token.type),
4649
attributesHash = options.resolve(opts.attributes, href, token.type),
4750
tagName = options.resolve(opts.tagName, href, token.type),
@@ -59,7 +62,6 @@ function linkifyStr(str, opts={}) {
5962

6063
link += `>${escapeText(formatted)}</${tagName}>`;
6164
result.push(link);
62-
6365
} else if (token.type === 'nl' && opts.nl2br) {
6466
if (opts.newLine) {
6567
result.push(opts.newLine);

src/linkify/utils/options.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ function noop(val) {
22
return val;
33
}
44

5+
function yes(val) {
6+
return true;
7+
}
8+
59
function typeToTarget(href, type) {
610
return type === 'url' ? '_blank' : null;
711
}
@@ -14,6 +18,7 @@ function normalize(opts) {
1418
defaultProtocol: opts.defaultProtocol || 'http',
1519
events: opts.events || null,
1620
format: opts.format || noop,
21+
validate: opts.validate || yes,
1722
formatHref: opts.formatHref || noop,
1823
newLine: opts.newLine || false, // deprecated
1924
nl2br: !!newLine || opts.nl2br || false,

test/qunit/test/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ QUnit.test('returns in the hash of default options when given an empty object',
9999
defaultProtocol: 'http',
100100
events: null,
101101
format: function () {},
102+
validate: function () {},
102103
formatHref: function () {},
103104
newLine: false, // deprecated
104105
nl2br: false,
@@ -107,6 +108,7 @@ QUnit.test('returns in the hash of default options when given an empty object',
107108
linkClass: 'linkified'
108109
});
109110
assert.equal(typeof result.format, 'function');
111+
assert.equal(typeof result.validate, 'function');
110112
assert.equal(result.format('test'), 'test');
111113
assert.equal(typeof result.formatHref, 'function');
112114
assert.equal(result.formatHref('test'), 'test');

test/spec/html/linkified-alt.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Hello here are some links to <a href="ftp://awesome.com/?where=this" class="linkified" target="_blank" rel="nofollow">ftp://awesome.com/?where=this</a> and <a href="http://localhost:8080" class="linkified" target="_blank" rel="nofollow">localhost:8080</a>, pretty neat right? <p>Here's a nested <a href="http://github.com/SoapBox/linkifyjs" class="linkified" target="_blank" rel="nofollow">github.com/SoapBox/linkifyjs</a> paragraph</p>
2-
Hello here are some links to <a rel="nofollow" target="_blank" class="linkified" href="ftp://awesome.com/?where=this">ftp://awesome.com/?where=this</a> and <a rel="nofollow" target="_blank" class="linkified" href="http://localhost:8080">localhost:8080</a>, pretty neat right? <p>Here's a nested <a rel="nofollow" target="_blank" class="linkified" href="http://github.com/SoapBox/linkifyjs">github.com/SoapBox/linkifyjs</a> paragraph</p>
3-
Hello here are some links to <a class="linkified" href="ftp://awesome.com/?where=this" target="_blank" rel="nofollow">ftp://awesome.com/?where=this</a> and <a class="linkified" href="http://localhost:8080" target="_blank" rel="nofollow">localhost:8080</a>, pretty neat right? <p>Here's a nested <a class="linkified" href="http://github.com/SoapBox/linkifyjs" target="_blank" rel="nofollow">github.com/SoapBox/linkifyjs</a> paragraph</p>
4-
Hello here are some links to <a class="linkified" href="ftp://awesome.com/?where=this" rel="nofollow" target="_blank">ftp://awesome.com/?where=this</a> and <a class="linkified" href="http://localhost:8080" rel="nofollow" target="_blank">localhost:8080</a>, pretty neat right? <p>Here's a nested <a class="linkified" href="http://github.com/SoapBox/linkifyjs" rel="nofollow" target="_blank">github.com/SoapBox/linkifyjs</a> paragraph</p>
1+
Hello here are some links to <a href="ftp://awesome.com/?where=this" class="linkified" target="_blank" rel="nofollow">ftp://awesome.com/?where=this</a> and <a href="http://localhost:8080" class="linkified" target="_blank" rel="nofollow">localhost:8080</a>, pretty neat right? <p>Here's a nested <a href="http://github.com/SoapBox/linkifyjs" class="linkified" target="_blank" rel="nofollow">github.com/SoapBox/linkifyjs</a> paragraph</p> and another link to <a href="http://www.google.com" class="linkified" target="_blank" rel="nofollow">www.google.com</a>
2+
Hello here are some links to <a rel="nofollow" target="_blank" class="linkified" href="ftp://awesome.com/?where=this">ftp://awesome.com/?where=this</a> and <a rel="nofollow" target="_blank" class="linkified" href="http://localhost:8080">localhost:8080</a>, pretty neat right? <p>Here's a nested <a rel="nofollow" target="_blank" class="linkified" href="http://github.com/SoapBox/linkifyjs">github.com/SoapBox/linkifyjs</a> paragraph</p> and another link to <a href="http://www.google.com" class="linkified" target="_blank" rel="nofollow">www.google.com</a>
3+
Hello here are some links to <a class="linkified" href="ftp://awesome.com/?where=this" target="_blank" rel="nofollow">ftp://awesome.com/?where=this</a> and <a class="linkified" href="http://localhost:8080" target="_blank" rel="nofollow">localhost:8080</a>, pretty neat right? <p>Here's a nested <a class="linkified" href="http://github.com/SoapBox/linkifyjs" target="_blank" rel="nofollow">github.com/SoapBox/linkifyjs</a> paragraph</p> and another link to <a href="http://www.google.com" class="linkified" target="_blank" rel="nofollow">www.google.com</a>
4+
Hello here are some links to <a class="linkified" href="ftp://awesome.com/?where=this" rel="nofollow" target="_blank">ftp://awesome.com/?where=this</a> and <a class="linkified" href="http://localhost:8080" rel="nofollow" target="_blank">localhost:8080</a>, pretty neat right? <p>Here's a nested <a class="linkified" href="http://github.com/SoapBox/linkifyjs" rel="nofollow" target="_blank">github.com/SoapBox/linkifyjs</a> paragraph</p> and another link to <a href="http://www.google.com" class="linkified" target="_blank" rel="nofollow">www.google.com</a>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Hello here are some links to <a href="ftp://awesome.com/?where=this" class="linkified" target="_blank">ftp://awesome.com/?where=this</a> and localhost:8080, pretty neat right? <p>Here's a nested github.com/SoapBox/linkifyjs paragraph</p> and another link to <a href="http://www.google.com" class="linkified" target="_blank">www.google.com</a>
2+
Hello here are some links to <a href="ftp://awesome.com/?where=this" class="linkified" target="_blank">ftp://awesome.com/?where=this</a> and localhost:8080, pretty neat right? <p>Here's a nested github.com/SoapBox/linkifyjs paragraph</p> and another link to <a href="http://www.google.com" class="linkified" target="_blank">www.google.com</a>
3+
Hello here are some links to <a href="ftp://awesome.com/?where=this" class="linkified" target="_blank">ftp://awesome.com/?where=this</a> and localhost:8080, pretty neat right? <p>Here's a nested github.com/SoapBox/linkifyjs paragraph</p> and another link to <a href="http://www.google.com" class="linkified" target="_blank">www.google.com</a>

test/spec/html/linkified.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Hello here are some links to <a href="ftp://awesome.com/?where=this" class="linkified" target="_blank">ftp://awesome.com/?where=this</a> and <a href="http://localhost:8080" class="linkified" target="_blank">localhost:8080</a>, pretty neat right? <p>Here's a nested <a href="http://github.com/SoapBox/linkifyjs" class="linkified" target="_blank">github.com/SoapBox/linkifyjs</a> paragraph</p>
2-
Hello here are some links to <a target="_blank" class="linkified" href="ftp://awesome.com/?where=this">ftp://awesome.com/?where=this</a> and <a target="_blank" class="linkified" href="http://localhost:8080">localhost:8080</a>, pretty neat right? <p>Here's a nested <a target="_blank" class="linkified" href="http://github.com/SoapBox/linkifyjs">github.com/SoapBox/linkifyjs</a> paragraph</p>
3-
Hello here are some links to <a class="linkified" href="ftp://awesome.com/?where=this" target="_blank">ftp://awesome.com/?where=this</a> and <a class="linkified" href="http://localhost:8080" target="_blank">localhost:8080</a>, pretty neat right? <p>Here's a nested <a class="linkified" href="http://github.com/SoapBox/linkifyjs" target="_blank">github.com/SoapBox/linkifyjs</a> paragraph</p>
1+
Hello here are some links to <a href="ftp://awesome.com/?where=this" class="linkified" target="_blank">ftp://awesome.com/?where=this</a> and <a href="http://localhost:8080" class="linkified" target="_blank">localhost:8080</a>, pretty neat right? <p>Here's a nested <a href="http://github.com/SoapBox/linkifyjs" class="linkified" target="_blank">github.com/SoapBox/linkifyjs</a> paragraph</p> and another link to <a href="http://www.google.com" class="linkified" target="_blank">www.google.com</a>
2+
Hello here are some links to <a target="_blank" class="linkified" href="ftp://awesome.com/?where=this">ftp://awesome.com/?where=this</a> and <a target="_blank" class="linkified" href="http://localhost:8080">localhost:8080</a>, pretty neat right? <p>Here's a nested <a target="_blank" class="linkified" href="http://github.com/SoapBox/linkifyjs">github.com/SoapBox/linkifyjs</a> paragraph</p> and another link to <a href="http://www.google.com" class="linkified" target="_blank">www.google.com</a>
3+
Hello here are some links to <a class="linkified" href="ftp://awesome.com/?where=this" target="_blank">ftp://awesome.com/?where=this</a> and <a class="linkified" href="http://localhost:8080" target="_blank">localhost:8080</a>, pretty neat right? <p>Here's a nested <a class="linkified" href="http://github.com/SoapBox/linkifyjs" target="_blank">github.com/SoapBox/linkifyjs</a> paragraph</p> and another link to <a href="http://www.google.com" class="linkified" target="_blank">www.google.com</a>

test/spec/html/options.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ module.exports = {
1313
linkifiedAlt: fs.readFileSync(__dirname + '/linkified-alt.html', 'utf8')
1414
.trim()
1515
.split('\n'),
16+
linkifiedValidate: fs.readFileSync(__dirname + '/linkified-validate.html', 'utf8')
17+
.trim()
18+
.split('\n'),
1619

1720
extra: fs.readFileSync(__dirname + '/extra.html', 'utf8').trim(), // for jQuery plugin tests
1821
altOptions: {
@@ -27,5 +30,11 @@ module.exports = {
2730
throw 'Hovered!';
2831
}
2932
}
33+
},
34+
35+
validateOptions: {
36+
validate: function (text, type) {
37+
return type !== 'url' || /^(http|ftp)s?:\/\//.test(text) || text.slice(0,3) === 'www';
38+
}
3039
}
3140
};

0 commit comments

Comments
 (0)