Skip to content

Commit f186859

Browse files
author
fcmod
committed
[validate] Remove hasProtocol parameter, add short-circuit checks
1 parent 19868a9 commit f186859

File tree

6 files changed

+17
-23
lines changed

6 files changed

+17
-23
lines changed

src/linkify-element.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ function tokensToNodes(tokens, opts, doc) {
3232

3333
for (let i = 0; i < tokens.length; i++) {
3434
let token = tokens[i];
35-
let validated = options.resolve(opts.validate,
36-
token.hasProtocol ? token.hasProtocol() : false,
37-
token.toString(), token.type);
35+
let validated = token.isLink && options.resolve(opts.validate, token.toString(), token.type);
3836

3937
if (token.isLink && validated) {
4038

src/linkify-html.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ function linkifyChars(str, opts) {
7777

7878
for (var i = 0; i < tokens.length; i++) {
7979
let token = tokens[i];
80-
let validated = linkify.options.resolve(opts.validate,
81-
token.hasProtocol ? token.hasProtocol() : false,
82-
token.toString(), token.type);
80+
let validated = token.isLink && linkify.options.resolve(opts.validate, token.toString(), token.type);
8381

8482
if (token.type === 'nl' && opts.nl2br) {
8583
result.push({

src/linkify-string.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ function linkifyStr(str, opts={}) {
3737

3838
for (let i = 0; i < tokens.length; i++) {
3939
let token = tokens[i];
40-
let validated = options.resolve(opts.validate,
41-
token.hasProtocol ? token.hasProtocol() : false,
42-
token.toString(), token.type);
40+
let validated = token.isLink && options.resolve(opts.validate, token.toString(), token.type);
4341

4442
if (token.isLink && validated) {
4543

test/spec/html/options.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ module.exports = {
3333
},
3434

3535
validateOptions: {
36-
validate: function (hasProtocol, text, type) {
37-
return type === 'email' || (hasProtocol || text.slice(0,3) === 'www');
36+
validate: function (text, type) {
37+
return type !== 'url' || /^(http|ftp)s?:\/\//.test(text) || text.slice(0,3) === 'www';
3838
}
3939
}
4040
};

test/spec/linkify-html-test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ describe('linkify-html', function () {
6565
});
6666

6767
it('Works with overriden options (validate)', function () {
68-
var options_validate = {
69-
validate: function (hasProtocol, text, type) {
70-
return type === 'email' || (hasProtocol || text.slice(0,3) === 'www');
68+
var optionsValidate = {
69+
validate: function (text, type) {
70+
return type !== 'url' || /^(http|ftp)s?:\/\//.test(text) || text.slice(0,3) === 'www';
7171
}
7272
},
7373

74-
tests_validate = [
74+
testsValidate = [
7575
[
7676
'1.Test with no links',
7777
'1.Test with no links'
@@ -93,8 +93,8 @@ describe('linkify-html', function () {
9393
]
9494
];
9595

96-
tests_validate.map(function (test) {
97-
expect(linkifyHtml(test[0], options_validate)).to.be.eql(test[1]);
96+
testsValidate.map(function (test) {
97+
expect(linkifyHtml(test[0], optionsValidate)).to.be.eql(test[1]);
9898
});
9999
});
100100

test/spec/linkify-string-test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ describe('linkify-string', function () {
8080

8181
// Test specific options
8282
it('Works with overriden options (validate)', function () {
83-
var options_validate = {
84-
validate: function (hasProtocol, text, type) {
85-
return type === 'email' || (hasProtocol || text.slice(0,3) === 'www');
83+
var optionsValidate = {
84+
validate: function (text, type) {
85+
return type !== 'url' || /^(http|ftp)s?:\/\//.test(text) || text.slice(0,3) === 'www';
8686
}
8787
},
8888

89-
tests_validate = [
89+
testsValidate = [
9090
[
9191
'1.Test with no links',
9292
'1.Test with no links'
@@ -108,8 +108,8 @@ describe('linkify-string', function () {
108108
]
109109
];
110110

111-
tests_validate.map(function (test) {
112-
expect(linkifyStr(test[0], options_validate)).to.be.eql(test[1]);
111+
testsValidate.map(function (test) {
112+
expect(linkifyStr(test[0], optionsValidate)).to.be.eql(test[1]);
113113
});
114114
});
115115
});

0 commit comments

Comments
 (0)