diff --git a/src/linkify-element.js b/src/linkify-element.js index 456b5a18..a51d88dc 100644 --- a/src/linkify-element.js +++ b/src/linkify-element.js @@ -92,8 +92,10 @@ function linkifyElementHelper(element, opts, doc) { throw new Error(`Cannot linkify ${element} - Invalid DOM Node type`); } + let ignoreTags = opts.ignoreTags; + // Is this element already a link? - if (element.tagName === 'A' /*|| element.hasClass('linkified')*/) { + if (element.tagName === 'A' || ignoreTags.indexOf(element.tagName) >= 0) { // No need to linkify return element; } diff --git a/src/linkify-html.js b/src/linkify-html.js index 17dcac78..1c1e9d33 100644 --- a/src/linkify-html.js +++ b/src/linkify-html.js @@ -22,11 +22,16 @@ export default function linkifyHtml(str, opts={}) { for (i = 0; i < tokens.length; i++) { let token = tokens[i]; - if (token.type === StartTag && token.tagName.toUpperCase() === 'A') { - // Ignore all the contents of an anchor tag + if (token.type === StartTag) { linkifiedTokens.push(token); + + // Ignore all the contents of ignored tags + let tagName = token.tagName.toUpperCase(); + let isIgnored = tagName === 'A' || opts.ignoreTags.indexOf(tagName) >= 0; + if (!isIgnored) continue; + let preskipLen = linkifiedTokens.length; - skipTagTokens('A', tokens, ++i, linkifiedTokens); + skipTagTokens(tagName, tokens, ++i, linkifiedTokens); i += linkifiedTokens.length - preskipLen - 1; continue; diff --git a/src/linkify-jquery.js b/src/linkify-jquery.js index 090df225..1bee2434 100644 --- a/src/linkify-jquery.js +++ b/src/linkify-jquery.js @@ -60,6 +60,7 @@ function apply($, doc=null) { target: data.linkifyTarget, linkClass: data.linkifyLinkclass, validate: data.linkifyValidate, + ignoreTags: data.linkifyIgnoreTags }; let $target = target === 'this' ? $this : $this.find(target); $target.linkify(options); diff --git a/src/linkify/utils/options.js b/src/linkify/utils/options.js index 2740ab68..b999f189 100644 --- a/src/linkify/utils/options.js +++ b/src/linkify/utils/options.js @@ -13,6 +13,13 @@ function typeToTarget(href, type) { function normalize(opts) { opts = opts || {}; let newLine = opts.newLine || false; // deprecated + let ignoreTags = opts.ignoreTags || []; + + // Make all tags names upper case + for (var i = 0; i < ignoreTags.length; i++) { + ignoreTags[i] = ignoreTags[i].toUpperCase(); + } + return { attributes: opts.linkAttributes || null, defaultProtocol: opts.defaultProtocol || 'http', @@ -24,7 +31,8 @@ function normalize(opts) { nl2br: !!newLine || opts.nl2br || false, tagName: opts.tagName || 'a', target: opts.target || typeToTarget, - linkClass: opts.linkClass || 'linkified' + linkClass: opts.linkClass || 'linkified', + ignoreTags: ignoreTags }; } diff --git a/test/qunit/test/main.js b/test/qunit/test/main.js index 3bd0cab4..36172396 100644 --- a/test/qunit/test/main.js +++ b/test/qunit/test/main.js @@ -105,7 +105,8 @@ QUnit.test('returns in the hash of default options when given an empty object', nl2br: false, tagName: 'a', target: function () {}, - linkClass: 'linkified' + linkClass: 'linkified', + ignoreTags: [] }); assert.equal(typeof result.format, 'function'); assert.equal(typeof result.validate, 'function'); diff --git a/test/spec/html/linkified-alt.html b/test/spec/html/linkified-alt.html index a6b137f3..099b26dd 100644 --- a/test/spec/html/linkified-alt.html +++ b/test/spec/html/linkified-alt.html @@ -1,4 +1,4 @@ -Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right?

Here's a nested github.com/SoapBox/linkifyjs paragraph

and another link to www.google.com -Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right?

Here's a nested github.com/SoapBox/linkifyjs paragraph

and another link to www.google.com -Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right?

Here's a nested github.com/SoapBox/linkifyjs paragraph

and another link to www.google.com -Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right?

Here's a nested github.com/SoapBox/linkifyjs paragraph

and another link to www.google.com +Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right?

Here's a nested github.com/SoapBox/linkifyjs paragraph

and another link to www.google.com and a +Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right?

Here's a nested github.com/SoapBox/linkifyjs paragraph

and another link to www.google.com and a +Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right?

Here's a nested github.com/SoapBox/linkifyjs paragraph

and another link to www.google.com and a +Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right?

Here's a nested github.com/SoapBox/linkifyjs paragraph

and another link to www.google.com and a diff --git a/test/spec/html/linkified-validate.html b/test/spec/html/linkified-validate.html index f2ee5dbb..e70a1490 100644 --- a/test/spec/html/linkified-validate.html +++ b/test/spec/html/linkified-validate.html @@ -1,3 +1,3 @@ -Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right?

Here's a nested github.com/SoapBox/linkifyjs paragraph

and another link to www.google.com -Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right?

Here's a nested github.com/SoapBox/linkifyjs paragraph

and another link to www.google.com -Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right?

Here's a nested github.com/SoapBox/linkifyjs paragraph

and another link to www.google.com \ No newline at end of file +Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right?

Here's a nested github.com/SoapBox/linkifyjs paragraph

and another link to www.google.com and a +Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right?

Here's a nested github.com/SoapBox/linkifyjs paragraph

and another link to www.google.com and a +Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right?

Here's a nested github.com/SoapBox/linkifyjs paragraph

and another link to www.google.com and a diff --git a/test/spec/html/linkified.html b/test/spec/html/linkified.html index f9dc5477..85cc61aa 100644 --- a/test/spec/html/linkified.html +++ b/test/spec/html/linkified.html @@ -1,3 +1,3 @@ -Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right?

Here's a nested github.com/SoapBox/linkifyjs paragraph

and another link to www.google.com -Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right?

Here's a nested github.com/SoapBox/linkifyjs paragraph

and another link to www.google.com -Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right?

Here's a nested github.com/SoapBox/linkifyjs paragraph

and another link to www.google.com +Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right?

Here's a nested github.com/SoapBox/linkifyjs paragraph

and another link to www.google.com and a +Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right?

Here's a nested github.com/SoapBox/linkifyjs paragraph

and another link to www.google.com and a +Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right?

Here's a nested github.com/SoapBox/linkifyjs paragraph

and another link to www.google.com and a diff --git a/test/spec/html/options.js b/test/spec/html/options.js index 0badc50a..b87eeb12 100644 --- a/test/spec/html/options.js +++ b/test/spec/html/options.js @@ -29,7 +29,11 @@ module.exports = { mouseover: function () { throw 'Hovered!'; } - } + }, + ignoreTags: [ + 'script', + 'style' + ] }, validateOptions: { @@ -37,4 +41,5 @@ module.exports = { return type !== 'url' || /^(http|ftp)s?:\/\//.test(text) || text.slice(0,3) === 'www'; } } + }; diff --git a/test/spec/html/original.html b/test/spec/html/original.html index 0d8f7dac..0d73c0e8 100644 --- a/test/spec/html/original.html +++ b/test/spec/html/original.html @@ -1 +1 @@ -Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right?

Here's a nested github.com/SoapBox/linkifyjs paragraph

and another link to www.google.com +Hello here are some links to ftp://awesome.com/?where=this and localhost:8080, pretty neat right?

Here's a nested github.com/SoapBox/linkifyjs paragraph

and another link to www.google.com and a diff --git a/test/spec/linkify-html-test.js b/test/spec/linkify-html-test.js index 672edb4c..4c436b1d 100644 --- a/test/spec/linkify-html-test.js +++ b/test/spec/linkify-html-test.js @@ -21,7 +21,11 @@ describe('linkify-html', function () { href += '?subject=Hello%20from%20Linkify'; } return href; - } + }, + ignoreTags: [ + 'script', + 'style' + ] }, // For each element in this array @@ -49,6 +53,10 @@ describe('linkify-html', function () { 'Unterminated anchor tag This is a link google.com and this works!! https://reddit.com/r/photography/', 'Unterminated anchor tag This is a link google.com and this works!! https://reddit.com/r/photography/', 'Unterminated anchor tag This is a link google.com and this works!! https://reddit.com/r/photography/' + ], [ + 'Ignore tags like and ', + 'Ignore tags like and ', + 'Ignore tags like and ' ] ];