Skip to content

Commit

Permalink
changed handling of target so that any value other than _self will ma…
Browse files Browse the repository at this point in the history
…ke it external
  • Loading branch information
foiseworth committed Aug 21, 2015
1 parent 6d69fbb commit 0f51e87
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -5,4 +5,3 @@ node_js:
- "0.12"
- "0.11"
- "0.10"
- "iojs"
5 changes: 4 additions & 1 deletion README.md
Expand Up @@ -9,7 +9,7 @@ A simple function that tells you whether a link DOM node is 'external'.

A link is external if it:
* has the attribute rel="external"
* has the attribute target="\_blank"
* has the attribute target and the value is not \_self
* is an absolute link
* is a telephone link
* is a mailto link
Expand All @@ -22,3 +22,6 @@ var link = document.querySelector('a');

external(link);
```

## Versions
2.0.0 - changed handling of target so that any value other than \_self will make it external
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -3,7 +3,7 @@
function external(node) {
if (!node || !node.getAttribute) return false;

if (node.getAttribute('target') === '_blank') return true;
if (node.getAttribute('target') && node.getAttribute('target') !== '_self') return true;

if (node.getAttribute('rel') === 'external') return true;

Expand Down
29 changes: 23 additions & 6 deletions test.js
Expand Up @@ -9,7 +9,7 @@ var assert = require('assert');

function makeLinkNode(options, cb) {
options = extend(false, {
url: 'http://aboutandrew.co.uk',
url: '/',
pageUrl: 'http://github.com',
rel: '',
target: ''
Expand Down Expand Up @@ -147,15 +147,32 @@ describe('External link', function() {
});
});

it('should know a link with target="_blank" is external', function(done) {
makeLinkNode({url: 'http://github.com', target: '_blank'}, function(err, link) {
assert(external(link));
done();
context('when a link has a target attribute', function() {
it('should know it is external', function(done) {
makeLinkNode({target: 'new'}, function(err, link) {
assert(external(link));
done();
});
});

it('should know it is not external if it equals _blank', function(done) {
makeLinkNode({target: '_blank'}, function(err, link) {
assert(external(link));
done();
});
});

it('should know it is not external if it equals _self', function(done) {
makeLinkNode({target: '_self'}, function(err, link) {
assert.equal(external(link), false);
done();
});
});
});


it('should know a link with rel="external" is external', function(done) {
makeLinkNode({url: 'http://github.com', rel: 'external'}, function(err, link) {
makeLinkNode({rel: 'external'}, function(err, link) {
assert(external(link));
done();
});
Expand Down

0 comments on commit 0f51e87

Please sign in to comment.