From e9c104abb5c18d0393f52c199235eeefda1cc1cb Mon Sep 17 00:00:00 2001 From: cibernox Date: Fri, 10 Jun 2016 21:43:19 +0100 Subject: [PATCH] Links with a download attribute must be ignored This PR also updated PhantomJS to a 2.1 --- .travis.yml | 1 + .../browser/ember-href-to.js | 15 +++++++++++---- tests/dummy/app/templates/application.hbs | 1 + .../browser/ember-href-to-test.js | 12 ++++++++++++ 4 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 tests/unit/instance-initializers/browser/ember-href-to-test.js diff --git a/.travis.yml b/.travis.yml index 8ed59cb..5cad5e4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/app/instance-initializers/browser/ember-href-to.js b/app/instance-initializers/browser/ember-href-to.js index c5425c0..bb53045 100644 --- a/app/instance-initializers/browser/ember-href-to.js +++ b/app/instance-initializers/browser/ember-href-to.js @@ -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) { @@ -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) { diff --git a/tests/dummy/app/templates/application.hbs b/tests/dummy/app/templates/application.hbs index ede4ae6..58d5dd3 100644 --- a/tests/dummy/app/templates/application.hbs +++ b/tests/dummy/app/templates/application.hbs @@ -19,6 +19,7 @@ [Second Page (with inner span)] [An anchor with no href] [An anchor with an absolute href] + [An anchor with a download attribute]
diff --git a/tests/unit/instance-initializers/browser/ember-href-to-test.js b/tests/unit/instance-initializers/browser/ember-href-to-test.js new file mode 100644 index 0000000..5f01303 --- /dev/null +++ b/tests/unit/instance-initializers/browser/ember-href-to-test.js @@ -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); +});