Skip to content

Commit

Permalink
Using manual version of closest() to mitigate browser support
Browse files Browse the repository at this point in the history
  • Loading branch information
ten1seven committed Mar 27, 2019
1 parent 8c6a5e2 commit 1229446
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dist/maps/what-input.min.js.map

Large diffs are not rendered by default.

15 changes: 14 additions & 1 deletion dist/what-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ return /******/ (function(modules) { // webpackBootstrap
if (shouldUpdate && currentIntent !== value) {
// preserve intent for keyboard interaction with form fields
var activeElem = document.activeElement;
var notFormInput = activeElem && activeElem.nodeName && formInputs.indexOf(activeElem.nodeName.toLowerCase()) === -1 || activeElem.nodeName.toLowerCase() === 'button' && !activeElem.closest('form');
var notFormInput = activeElem && activeElem.nodeName && formInputs.indexOf(activeElem.nodeName.toLowerCase()) === -1 || activeElem.nodeName.toLowerCase() === 'button' && !checkClosest(activeElem, 'form');

if (notFormInput) {
currentIntent = value;
Expand Down Expand Up @@ -407,6 +407,19 @@ return /******/ (function(modules) { // webpackBootstrap
}
};

// manual version of `closest()`
var checkClosest = function checkClosest(elem, tag) {
do {
if (elem.matches(tag)) {
return elem;
}

elem = elem.parentElement || elem.parentNode;
} while (elem !== null && elem.nodeType === 1);

return null;
};

/*
* init
*/
Expand Down
2 changes: 1 addition & 1 deletion dist/what-input.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion src/scripts/what-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ module.exports = (() => {
activeElem.nodeName &&
formInputs.indexOf(activeElem.nodeName.toLowerCase()) === -1) ||
(activeElem.nodeName.toLowerCase() === 'button' &&
!activeElem.closest('form'))
!checkClosest(activeElem, 'form'))

if (notFormInput) {
currentIntent = value
Expand Down Expand Up @@ -366,6 +366,19 @@ module.exports = (() => {
}
}

// manual version of `closest()`
const checkClosest = (elem, tag) => {
do {
if (elem.matches(tag)) {
return elem
}

elem = elem.parentElement || elem.parentNode
} while (elem !== null && elem.nodeType === 1)

return null;
}

/*
* init
*/
Expand Down

0 comments on commit 1229446

Please sign in to comment.