Fix element blurring (removing focus) #116
6
index.js
6
index.js
@@ -6,6 +6,7 @@ var forEachEls = require("./lib/foreach-els.js")
|
|||||||
var newUid = require("./lib/uniqueid.js")
|
var newUid = require("./lib/uniqueid.js")
|
||||||
|
|
||||||
var noop = require("./lib/util/noop")
|
var noop = require("./lib/util/noop")
|
||||||
|
var contains = require("./lib/util/contains.js")
|
||||||
|
|
|||||||
|
|
||||||
var on = require("./lib/events/on.js")
|
var on = require("./lib/events/on.js")
|
||||||
// var off = 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.
|
// Clear out any focused controls before inserting new page contents.
|
||||||
// we clear focus on non form elements
|
// we clear focus on non form elements
|
||||||
if (document.activeElement && !document.activeElement.value) {
|
if (document.activeElement && contains(this.options.selectors, document.activeElement)) {
|
||||||
try {
|
try {
|
||||||
document.activeElement.blur()
|
document.activeElement.blur()
|
||||||
} catch (e) { }
|
} catch (e) { }
|
||||||
@@ -180,9 +181,6 @@ Pjax.prototype = {
|
|||||||
},
|
},
|
||||||
document.title, window.location)
|
document.title, window.location)
|
||||||
|
|
||||||
// Clear out any focused controls before inserting new page contents.
|
|
||||||
document.activeElement.blur()
|
|
||||||
|
|
||||||
var oldHref = href
|
var oldHref = href
|
||||||
if (request.responseURL) {
|
if (request.responseURL) {
|
||||||
if (href !== request.responseURL) {
|
if (href !== request.responseURL) {
|
||||||
|
|||||||
12
lib/util/contains.js
Normal file
12
lib/util/contains.js
Normal file
@@ -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
|
||||||
|
}
|
||||||
16
tests/lib/util/contains.js
Normal file
16
tests/lib/util/contains.js
Normal file
@@ -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 = "<div><p id='el' class='js-Pjax'></p></div><span></span>"
|
||||||
|
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()
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user
I think these vars were originally alphabetically ordered, but grouping modules by path is also something that makes sense -- another thing to standardise on for #97?
Good idea. I'll mention that there.