From fa05e94f07dc2f65e6d5015e6a81a8b692218cc2 Mon Sep 17 00:00:00 2001 From: Behind The Math Date: Wed, 24 Jan 2018 20:18:41 -0500 Subject: [PATCH 1/3] Only blur element if it's contained by one of the selectors Previously, Pjax would blur (remove focus) from the active element regardless of where it was on the page. This restricts that to happen only if the active element is contained by one of the elements represented by options.selectors, because only those are affected by Pjax. Fixes #4 --- index.js | 3 ++- lib/util/contains.js | 12 ++++++++++++ tests/lib/util/contains.js | 16 ++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 lib/util/contains.js create mode 100644 tests/lib/util/contains.js diff --git a/index.js b/index.js index 6db68c1..37e1187 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ var forEachEls = require("./lib/foreach-els.js") var newUid = require("./lib/uniqueid.js") var noop = require("./lib/util/noop") +var contains = require("./lib/util/contains.js") var on = require("./lib/events/on.js") // var off = require("./lib/events/on.js") @@ -129,7 +130,7 @@ Pjax.prototype = { // Clear out any focused controls before inserting new page contents. // we clear focus on non form elements - if (document.activeElement && !document.activeElement.value) { + if (document.activeElement && !document.activeElement.value && contains(this.options.selectors, document.activeElement)) { try { document.activeElement.blur() } catch (e) { } diff --git a/lib/util/contains.js b/lib/util/contains.js new file mode 100644 index 0000000..9f169a7 --- /dev/null +++ b/lib/util/contains.js @@ -0,0 +1,12 @@ +module.exports = function contains(doc, selectors, el) { + for (var i = 0; i < selectors.length; i++) { + var selectedEls = doc.querySelectorAll(selectors[i]) + for (var j = 0; j < selectedEls.length; j++) { + if (selectedEls[j].contains(el)) { + return true + } + } + } + + return false +} diff --git a/tests/lib/util/contains.js b/tests/lib/util/contains.js new file mode 100644 index 0000000..533bbe2 --- /dev/null +++ b/tests/lib/util/contains.js @@ -0,0 +1,16 @@ +var tape = require("tape") + +var contains = require("../../../lib/util/contains.js") + +tape("test contains function", function(t) { + var tempDoc = document.implementation.createHTMLDocument() + tempDoc.body.innerHTML = "

" + var selectors = ["div"] + var el = tempDoc.body.querySelector("#el") + t.equal(contains(tempDoc, selectors, el), true, "contains() returns true when a selector contains the element") + + selectors = ["span"] + t.equal(contains(tempDoc, selectors, el), false, "contains() returns false when the selectors do not contain the element") + + t.end() +}) From 2d4df39f727af249d8a9023b7328502756304fbc Mon Sep 17 00:00:00 2001 From: Behind The Math Date: Wed, 24 Jan 2018 20:19:02 -0500 Subject: [PATCH 2/3] Remove a redundant call to .blur() --- index.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/index.js b/index.js index 37e1187..25cb76e 100644 --- a/index.js +++ b/index.js @@ -181,9 +181,6 @@ Pjax.prototype = { }, document.title, window.location) - // Clear out any focused controls before inserting new page contents. - document.activeElement.blur() - var oldHref = href if (request.responseURL) { if (href !== request.responseURL) { From 90d26d641cbc76bd120646fa9aff3d16262a9973 Mon Sep 17 00:00:00 2001 From: Behind The Math Date: Mon, 29 Jan 2018 13:51:00 -0500 Subject: [PATCH 3/3] Remove focus from form elements as well. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 25cb76e..21e6d6b 100644 --- a/index.js +++ b/index.js @@ -130,7 +130,7 @@ Pjax.prototype = { // Clear out any focused controls before inserting new page contents. // we clear focus on non form elements - if (document.activeElement && !document.activeElement.value && contains(this.options.selectors, document.activeElement)) { + if (document.activeElement && contains(this.options.selectors, document.activeElement)) { try { document.activeElement.blur() } catch (e) { }