From 97c8b2d749aa28229fc9ab1e2dd87eab43a88f45 Mon Sep 17 00:00:00 2001 From: darylteo Date: Mon, 4 Jan 2016 23:09:04 +1100 Subject: [PATCH 1/2] [NEW] #5 Ignore default prevented clicks --- lib/proto/attach-link.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/proto/attach-link.js b/lib/proto/attach-link.js index d53e351..647558d 100644 --- a/lib/proto/attach-link.js +++ b/lib/proto/attach-link.js @@ -53,14 +53,26 @@ var linkAction = function(el, event) { this.loadUrl(el.href, clone(this.options)) } +var isDefaultPrevented = function(event) { + return event.defaultPrevented || event.returnValue === false; +} + module.exports = function(el) { var that = this on(el, "click", function(event) { + if (isDefaultPrevented(event)) { + return + } + linkAction.call(that, el, event) }) on(el, "keyup", function(event) { + if (isDefaultPrevented(event)) { + return + } + // Don’t break browser special behavior on links (like page in new window) if (event.which > 1 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) { el.setAttribute(attrKey, "modifier") From 791400ed2080c1fb2c262249c9202eff1b3eb78e Mon Sep 17 00:00:00 2001 From: darylteo Date: Mon, 4 Jan 2016 23:45:25 +1100 Subject: [PATCH 2/2] Tests for #5 --- tests/lib/proto/attach-link.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/lib/proto/attach-link.js b/tests/lib/proto/attach-link.js index 29202c1..fef1bfc 100644 --- a/tests/lib/proto/attach-link.js +++ b/tests/lib/proto/attach-link.js @@ -56,3 +56,22 @@ tape("test attach link prototype method", function(t) { t.end() }) + +tape("test attach link preventDefaulted events", function(t) { + var callbacked = false + var a = document.createElement("a") + + attachLink.call({ + options: {}, + loadUrl: function() { + callbacked = true + } + }, a) + + a.href = "#" + on(a, "click", preventDefault) + trigger(a, "click") + t.equal(callbacked, false, "events that are preventDefaulted should not fire callback") + + t.end() +})