-
Notifications
You must be signed in to change notification settings - Fork 182
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 validate option (function) to be able to selectively filter tokens #106
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,8 +32,11 @@ function tokensToNodes(tokens, opts, doc) { | |
|
||
for (let i = 0; i < tokens.length; i++) { | ||
let token = tokens[i]; | ||
let validated = options.resolve(opts.validate, | ||
token.hasProtocol ? token.hasProtocol() : false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would take out the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see what you mean. I just find it a pity that information obtained by the tokenizer (e.g. if there is a protocol -and which one-, the domain, the "path") is lost and unaccessible by the functions. It would be nice, in the future, to be able to access some (selected?) information about the token. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For example, if the tokenizer already knows how to handle protocols, it is redundant to have to rehandle them with a regex (or something like that). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In that sense, I wouldn't be opposed to passing the whole token as the final argument! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know how much of the token interface is already exposed to the library user; assuming that "none" or "not much", then I'd rather leave it like it is in the last commit (just removing |
||
token.toString(), token.type); | ||
|
||
if (token.isLink) { | ||
if (token.isLink && validated) { | ||
|
||
let | ||
href = token.toHref(opts.defaultProtocol), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,10 @@ function linkifyChars(str, opts) { | |
|
||
for (var i = 0; i < tokens.length; i++) { | ||
let token = tokens[i]; | ||
let validated = linkify.options.resolve(opts.validate, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment here about the short-circuit guard |
||
token.hasProtocol ? token.hasProtocol() : false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, take out |
||
token.toString(), token.type); | ||
|
||
if (token.type === 'nl' && opts.nl2br) { | ||
result.push({ | ||
type: StartTag, | ||
|
@@ -85,7 +89,7 @@ function linkifyChars(str, opts) { | |
selfClosing: true | ||
}); | ||
continue; | ||
} else if (!token.isLink) { | ||
} else if (!token.isLink || !validated) { | ||
result.push({type: Chars, chars: token.toString()}); | ||
continue; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,11 +37,16 @@ function linkifyStr(str, opts={}) { | |
|
||
for (let i = 0; i < tokens.length; i++) { | ||
let token = tokens[i]; | ||
if (token.isLink) { | ||
let validated = options.resolve(opts.validate, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 short circuit guard |
||
token.hasProtocol ? token.hasProtocol() : false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 remove protocol argument |
||
token.toString(), token.type); | ||
|
||
if (token.isLink && validated) { | ||
|
||
let | ||
href = token.toHref(opts.defaultProtocol), | ||
formatted = options.resolve(opts.format, token.toString(), token.type), | ||
|
||
formattedHref = options.resolve(opts.formatHref, href, token.type), | ||
attributesHash = options.resolve(opts.attributes, href, token.type), | ||
tagName = options.resolve(opts.tagName, href, token.type), | ||
|
@@ -59,7 +64,6 @@ function linkifyStr(str, opts={}) { | |
|
||
link += `>${escapeText(formatted)}</${tagName}>`; | ||
result.push(link); | ||
|
||
} else if (token.type === 'nl' && opts.nl2br) { | ||
if (opts.newLine) { | ||
result.push(opts.newLine); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
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> | ||
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> | ||
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> | ||
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> | ||
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> | ||
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> | ||
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> | ||
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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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> | ||
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> | ||
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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,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 <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> | ||
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> | ||
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> | ||
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> | ||
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> | ||
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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right? <p>Here's a nested github.com/SoapBox/linkifyjs paragraph</p> | ||
Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right? <p>Here's a nested github.com/SoapBox/linkifyjs paragraph</p> and another link to www.google.com |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,11 +53,12 @@ describe('linkify-string', function () { | |
[ | ||
'Test with no links', | ||
'Test with no links', | ||
'Test with no links' | ||
'Test with no links', | ||
], [ | ||
'The URL is google.com and the email is test@example.com', | ||
'The URL is <a href="http://google.com" class="linkified" target="_blank">google.com</a> and the email is <a href="mailto:test@example.com" class="linkified">test@example.com</a>', | ||
'The URL is <span href="https://google.com" class="my-linkify-class" target="_parent" rel="nofollow" onclick="javascript:;">google.com</span> and the email is <span href="mailto:test@example.com?subject=Hello%20from%20Linkify" class="my-linkify-class" target="_parent" rel="nofollow" onclick="javascript:;">test@example.com</span>' | ||
'The URL is <span href="https://google.com" class="my-linkify-class" target="_parent" rel="nofollow" onclick="javascript:;">google.com</span> and the email is <span href="mailto:test@example.com?subject=Hello%20from%20Linkify" class="my-linkify-class" target="_parent" rel="nofollow" onclick="javascript:;">test@example.com</span>', | ||
'The URL is google.com and the email is test@example.com', | ||
], [ | ||
'Super long maps URL https://www.google.ca/maps/@43.472082,-80.5426668,18z?hl=en, a #hash-tag, and an email: test."wut".yo@gmail.co.uk!\n', | ||
'Super long maps URL <a href="https://www.google.ca/maps/@43.472082,-80.5426668,18z?hl=en" class="linkified" target="_blank">https://www.google.ca/maps/@43.472082,-80.5426668,18z?hl=en</a>, a #hash-tag, and an email: <a href="mailto:test."wut".yo@gmail.co.uk" class="linkified">test."wut".yo@gmail.co.uk</a>!\n', | ||
|
@@ -71,9 +72,44 @@ describe('linkify-string', function () { | |
}); | ||
}); | ||
|
||
it('Works with overriden options', function () { | ||
it('Works with overriden options (general)', function () { | ||
tests.map(function (test) { | ||
expect(linkifyStr(test[0], options)).to.be.eql(test[2]); | ||
}); | ||
}); | ||
|
||
// Test specific options | ||
it('Works with overriden options (validate)', function () { | ||
var options_validate = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you change these variables to camelCase? |
||
validate: function (hasProtocol, text, type) { | ||
return type === 'email' || (hasProtocol || text.slice(0,3) === 'www'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would change the first check here to |
||
} | ||
}, | ||
|
||
tests_validate = [ | ||
[ | ||
'1.Test with no links', | ||
'1.Test with no links' | ||
], [ | ||
'2.The URL is google.com and the email is test@example.com', | ||
'2.The URL is google.com and the email is <a href="mailto:test@example.com" class="linkified">test@example.com</a>' | ||
], [ | ||
'3.The URL is www.google.com', | ||
'3.The URL is <a href="http://www.google.com" class="linkified" target="_blank">www.google.com</a>' | ||
], [ | ||
'4.The URL is http://google.com', | ||
'4.The URL is <a href="http://google.com" class="linkified" target="_blank">http://google.com</a>' | ||
], [ | ||
'5.The URL is ftp://google.com', | ||
'5.The URL is <a href="ftp://google.com" class="linkified" target="_blank">ftp://google.com</a>' | ||
], [ | ||
'6.Test with no links.It is sloppy avoiding spaces after the dot', | ||
'6.Test with no links.It is sloppy avoiding spaces after the dot' | ||
] | ||
]; | ||
|
||
tests_validate.map(function (test) { | ||
expect(linkifyStr(test[0], options_validate)).to.be.eql(test[1]); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add a short circuit guard here, so that we don't check on every token (i.e., generic text tokens)