Skip to content

Commit

Permalink
Links with a download attribute must be ignored
Browse files Browse the repository at this point in the history
This PR also updated PhantomJS to a 2.1
  • Loading branch information
cibernox committed Jun 13, 2016
1 parent b8cf069 commit e9c104a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ before_install:
- export PATH=/usr/local/phantomjs-2.0.0/bin:$PATH
- "npm config set spin false"
- "npm install -g npm@^3"
- "npm install -g phantomjs-prebuilt@2"

install:
- npm install -g bower
Expand Down
15 changes: 11 additions & 4 deletions app/instance-initializers/browser/ember-href-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ function _lookupRouter(applicationInstance) {
return container.lookup('router:main');
}

export function canHandle(e) {
let $target = Em.$(e.currentTarget);
let handleClick = (e.which === 1 && !e.ctrlKey && !e.metaKey);
return handleClick &&
!$target.hasClass('ember-view') &&
Em.isNone($target.attr('data-ember-action')) &&
$target.attr('download') === undefined;
}

export default {
name: 'ember-href-to',
initialize: function(applicationInstance) {
Expand All @@ -22,10 +31,8 @@ export default {

$body.off('click.href-to', 'a');
$body.on('click.href-to', 'a', function(e) {
let $target = Em.$(e.currentTarget);
let handleClick = (e.which === 1 && !e.ctrlKey && !e.metaKey);

if(handleClick && !$target.hasClass('ember-view') && Em.isNone($target.attr('data-ember-action'))) {
if(canHandle(e)) {
let $target = Em.$(e.currentTarget);
let url = $target.attr('href');

if(url && url.indexOf(rootURL) === 0) {
Expand Down
1 change: 1 addition & 0 deletions tests/dummy/app/templates/application.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
[<a href="{{href-to 'pages.second'}}"><span id="inner-span">Second Page (with inner span)</span></a>]
[<a>An anchor with no href</a>]
[<a href="http://localhost:4200/about">An anchor with an absolute href</a>]
[<a href="/about" download>An anchor with a download attribute</a>]
</div>
<hr />

Expand Down
12 changes: 12 additions & 0 deletions tests/unit/instance-initializers/browser/ember-href-to-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { module, test } from 'qunit';
import { canHandle } from '../../../../instance-initializers/browser/ember-href-to';

module('Unit | Instance Initializer | browser/ember-href-to#canHandle');

test('returns false for events on links with a download attribute', function(assert) {
let anchor = document.createElement('a');
anchor.href = '/file.pdf';
anchor.download = '';
let event = { currentTarget: anchor, which: 1, ctrlKey: false, metaKey: false };
assert.equal(canHandle(event), false);
});

0 comments on commit e9c104a

Please sign in to comment.