focus-lock
Advanced tools
Comparing version 0.11.4 to 0.11.5
/** | ||
* @returns {Boolean} true, if the current focus is inside given node or nodes | ||
*/ | ||
export declare const focusInside: (topNode: HTMLElement | HTMLElement[]) => boolean; | ||
export declare const focusInside: (topNode: HTMLElement | HTMLElement[], activeElement?: HTMLElement | undefined) => boolean; |
import { contains } from './utils/DOMutils'; | ||
import { getAllAffectedNodes } from './utils/all-affected'; | ||
import { toArray } from './utils/array'; | ||
import { getFirst, toArray } from './utils/array'; | ||
import { getActiveElement } from './utils/getActiveElement'; | ||
var focusInFrame = function (frame) { return frame === document.activeElement; }; | ||
var focusInsideIframe = function (topNode) { | ||
return Boolean(toArray(topNode.querySelectorAll('iframe')).some(function (node) { return focusInFrame(node); })); | ||
var focusInFrame = function (frame, activeElement) { return frame === activeElement; }; | ||
var focusInsideIframe = function (topNode, activeElement) { | ||
return Boolean(toArray(topNode.querySelectorAll('iframe')).some(function (node) { return focusInFrame(node, activeElement); })); | ||
}; | ||
@@ -12,8 +12,11 @@ /** | ||
*/ | ||
export var focusInside = function (topNode) { | ||
var activeElement = document && getActiveElement(); | ||
export var focusInside = function (topNode, activeElement) { | ||
// const activeElement = document && getActiveElement(); | ||
if (activeElement === void 0) { activeElement = getActiveElement(getFirst(topNode).ownerDocument); } | ||
if (!activeElement || (activeElement.dataset && activeElement.dataset.focusGuard)) { | ||
return false; | ||
} | ||
return getAllAffectedNodes(topNode).some(function (node) { return contains(node, activeElement) || focusInsideIframe(node); }); | ||
return getAllAffectedNodes(topNode).some(function (node) { | ||
return contains(node, activeElement) || focusInsideIframe(node, activeElement); | ||
}); | ||
}; |
@@ -6,2 +6,2 @@ /** | ||
*/ | ||
export declare const focusIsHidden: () => boolean; | ||
export declare const focusIsHidden: (inDocument?: Document) => boolean; |
@@ -10,4 +10,5 @@ import { FOCUS_ALLOW } from './constants'; | ||
*/ | ||
export var focusIsHidden = function () { | ||
var activeElement = document && getActiveElement(); | ||
export var focusIsHidden = function (inDocument) { | ||
if (inDocument === void 0) { inDocument = document; } | ||
var activeElement = getActiveElement(inDocument); | ||
if (!activeElement) { | ||
@@ -17,3 +18,3 @@ return false; | ||
// this does not support setting FOCUS_ALLOW within shadow dom | ||
return toArray(document.querySelectorAll("[".concat(FOCUS_ALLOW, "]"))).some(function (node) { return contains(node, activeElement); }); | ||
return toArray(inDocument.querySelectorAll("[".concat(FOCUS_ALLOW, "]"))).some(function (node) { return contains(node, activeElement); }); | ||
}; |
import { NEW_FOCUS, newFocus } from './solver'; | ||
import { getAllTabbableNodes, getTabbableNodes } from './utils/DOMutils'; | ||
import { getAllAffectedNodes } from './utils/all-affected'; | ||
import { asArray, getFirst } from './utils/array'; | ||
import { pickAutofocus } from './utils/auto-focus'; | ||
@@ -21,3 +22,3 @@ import { getActiveElement } from './utils/getActiveElement'; | ||
export var getFocusMerge = function (topNode, lastNode) { | ||
var activeElement = document && getActiveElement(); | ||
var activeElement = getActiveElement(asArray(topNode).length > 0 ? document : getFirst(topNode).ownerDocument); | ||
var entries = getAllAffectedNodes(topNode).filter(isNotAGuard); | ||
@@ -24,0 +25,0 @@ var commonParent = getTopCommonParent(activeElement || topNode, topNode, entries); |
@@ -7,2 +7,3 @@ interface ListOf<TNode> { | ||
export declare const asArray: <T>(a: T | T[]) => T[]; | ||
export declare const getFirst: <T>(a: T | T[]) => T; | ||
export {}; |
@@ -12,1 +12,2 @@ /* | ||
export var asArray = function (a) { return (Array.isArray(a) ? a : [a]); }; | ||
export var getFirst = function (a) { return (Array.isArray(a) ? a[0] : a); }; |
@@ -53,4 +53,14 @@ import { toArray } from './array'; | ||
} | ||
return toArray(scope.children).some(function (child) { return contains(child, element); }); | ||
return toArray(scope.children).some(function (child) { | ||
var _a; | ||
if (child instanceof HTMLIFrameElement) { | ||
var iframeBody = (_a = child.contentDocument) === null || _a === void 0 ? void 0 : _a.body; | ||
if (iframeBody) { | ||
return contains(iframeBody, element); | ||
} | ||
return false; | ||
} | ||
return contains(child, element); | ||
}); | ||
} | ||
}; |
/** | ||
* returns active element from document or from nested shadowdoms | ||
*/ | ||
export declare const getActiveElement: () => HTMLElement | undefined; | ||
export declare const getActiveElement: (inDocument?: Document | ShadowRoot | undefined) => HTMLElement | undefined; |
@@ -1,17 +0,16 @@ | ||
var getNestedShadowActiveElement = function (shadowRoot) { | ||
return shadowRoot.activeElement | ||
? shadowRoot.activeElement.shadowRoot | ||
? getNestedShadowActiveElement(shadowRoot.activeElement.shadowRoot) | ||
: shadowRoot.activeElement | ||
: undefined; | ||
}; | ||
/** | ||
* returns active element from document or from nested shadowdoms | ||
*/ | ||
export var getActiveElement = function () { | ||
return (document.activeElement | ||
? document.activeElement.shadowRoot | ||
? getNestedShadowActiveElement(document.activeElement.shadowRoot) | ||
: document.activeElement | ||
: undefined); // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export var getActiveElement = function (inDocument) { | ||
var _a; | ||
if (inDocument === void 0) { inDocument = document; } | ||
if (!inDocument || !inDocument.activeElement) { | ||
return undefined; | ||
} | ||
var activeElement = inDocument.activeElement; | ||
return (activeElement.shadowRoot | ||
? getActiveElement(activeElement.shadowRoot) | ||
: activeElement instanceof HTMLIFrameElement && ((_a = activeElement.contentWindow) === null || _a === void 0 ? void 0 : _a.document) | ||
? getActiveElement(activeElement.contentWindow.document) | ||
: activeElement); | ||
}; |
@@ -12,7 +12,16 @@ import { FOCUS_AUTO } from '../constants'; | ||
}; | ||
var getFocusablesWithIFrame = function (parent, withGuards) { | ||
if (parent instanceof HTMLIFrameElement && parent.contentDocument) { | ||
return getFocusables([parent.contentDocument.body], withGuards); | ||
} | ||
return [parent]; | ||
}; | ||
export var getFocusables = function (parents, withGuards) { | ||
return parents.reduce(function (acc, parent) { | ||
var _a; | ||
var focusableWithShadowDom = getFocusablesWithShadowDom(parent, withGuards); | ||
var focusableWithIframes = (_a = []).concat.apply(_a, focusableWithShadowDom.map(function (node) { return getFocusablesWithIFrame(node, withGuards); })); | ||
return acc.concat( | ||
// add all tabbables inside and within shadow DOMs in DOM order | ||
getFocusablesWithShadowDom(parent, withGuards), | ||
focusableWithIframes, | ||
// add if node is tabbable itself | ||
@@ -19,0 +28,0 @@ parent.parentNode |
/** | ||
* @returns {Boolean} true, if the current focus is inside given node or nodes | ||
*/ | ||
export declare const focusInside: (topNode: HTMLElement | HTMLElement[]) => boolean; | ||
export declare const focusInside: (topNode: HTMLElement | HTMLElement[], activeElement?: HTMLElement | undefined) => boolean; |
import { contains } from './utils/DOMutils'; | ||
import { getAllAffectedNodes } from './utils/all-affected'; | ||
import { toArray } from './utils/array'; | ||
import { getFirst, toArray } from './utils/array'; | ||
import { getActiveElement } from './utils/getActiveElement'; | ||
const focusInFrame = (frame) => frame === document.activeElement; | ||
const focusInsideIframe = (topNode) => Boolean(toArray(topNode.querySelectorAll('iframe')).some((node) => focusInFrame(node))); | ||
const focusInFrame = (frame, activeElement) => frame === activeElement; | ||
const focusInsideIframe = (topNode, activeElement) => Boolean(toArray(topNode.querySelectorAll('iframe')).some((node) => focusInFrame(node, activeElement))); | ||
/** | ||
* @returns {Boolean} true, if the current focus is inside given node or nodes | ||
*/ | ||
export const focusInside = (topNode) => { | ||
const activeElement = document && getActiveElement(); | ||
export const focusInside = (topNode, activeElement = getActiveElement(getFirst(topNode).ownerDocument)) => { | ||
// const activeElement = document && getActiveElement(); | ||
if (!activeElement || (activeElement.dataset && activeElement.dataset.focusGuard)) { | ||
return false; | ||
} | ||
return getAllAffectedNodes(topNode).some((node) => contains(node, activeElement) || focusInsideIframe(node)); | ||
return getAllAffectedNodes(topNode).some((node) => { | ||
return contains(node, activeElement) || focusInsideIframe(node, activeElement); | ||
}); | ||
}; |
@@ -6,2 +6,2 @@ /** | ||
*/ | ||
export declare const focusIsHidden: () => boolean; | ||
export declare const focusIsHidden: (inDocument?: Document) => boolean; |
@@ -10,4 +10,4 @@ import { FOCUS_ALLOW } from './constants'; | ||
*/ | ||
export const focusIsHidden = () => { | ||
const activeElement = document && getActiveElement(); | ||
export const focusIsHidden = (inDocument = document) => { | ||
const activeElement = getActiveElement(inDocument); | ||
if (!activeElement) { | ||
@@ -17,3 +17,3 @@ return false; | ||
// this does not support setting FOCUS_ALLOW within shadow dom | ||
return toArray(document.querySelectorAll(`[${FOCUS_ALLOW}]`)).some((node) => contains(node, activeElement)); | ||
return toArray(inDocument.querySelectorAll(`[${FOCUS_ALLOW}]`)).some((node) => contains(node, activeElement)); | ||
}; |
import { NEW_FOCUS, newFocus } from './solver'; | ||
import { getAllTabbableNodes, getTabbableNodes } from './utils/DOMutils'; | ||
import { getAllAffectedNodes } from './utils/all-affected'; | ||
import { asArray, getFirst } from './utils/array'; | ||
import { pickAutofocus } from './utils/auto-focus'; | ||
@@ -21,3 +22,3 @@ import { getActiveElement } from './utils/getActiveElement'; | ||
export const getFocusMerge = (topNode, lastNode) => { | ||
const activeElement = document && getActiveElement(); | ||
const activeElement = getActiveElement(asArray(topNode).length > 0 ? document : getFirst(topNode).ownerDocument); | ||
const entries = getAllAffectedNodes(topNode).filter(isNotAGuard); | ||
@@ -24,0 +25,0 @@ const commonParent = getTopCommonParent(activeElement || topNode, topNode, entries); |
@@ -7,2 +7,3 @@ interface ListOf<TNode> { | ||
export declare const asArray: <T>(a: T | T[]) => T[]; | ||
export declare const getFirst: <T>(a: T | T[]) => T; | ||
export {}; |
@@ -12,1 +12,2 @@ /* | ||
export const asArray = (a) => (Array.isArray(a) ? a : [a]); | ||
export const getFirst = (a) => (Array.isArray(a) ? a[0] : a); |
@@ -42,4 +42,14 @@ import { toArray } from './array'; | ||
} | ||
return toArray(scope.children).some((child) => contains(child, element)); | ||
return toArray(scope.children).some((child) => { | ||
var _a; | ||
if (child instanceof HTMLIFrameElement) { | ||
const iframeBody = (_a = child.contentDocument) === null || _a === void 0 ? void 0 : _a.body; | ||
if (iframeBody) { | ||
return contains(iframeBody, element); | ||
} | ||
return false; | ||
} | ||
return contains(child, element); | ||
}); | ||
} | ||
}; |
/** | ||
* returns active element from document or from nested shadowdoms | ||
*/ | ||
export declare const getActiveElement: () => HTMLElement | undefined; | ||
export declare const getActiveElement: (inDocument?: Document | ShadowRoot | undefined) => HTMLElement | undefined; |
@@ -1,15 +0,15 @@ | ||
const getNestedShadowActiveElement = (shadowRoot) => shadowRoot.activeElement | ||
? shadowRoot.activeElement.shadowRoot | ||
? getNestedShadowActiveElement(shadowRoot.activeElement.shadowRoot) | ||
: shadowRoot.activeElement | ||
: undefined; | ||
/** | ||
* returns active element from document or from nested shadowdoms | ||
*/ | ||
export const getActiveElement = () => { | ||
return (document.activeElement | ||
? document.activeElement.shadowRoot | ||
? getNestedShadowActiveElement(document.activeElement.shadowRoot) | ||
: document.activeElement | ||
: undefined); // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export const getActiveElement = (inDocument = document) => { | ||
var _a; | ||
if (!inDocument || !inDocument.activeElement) { | ||
return undefined; | ||
} | ||
const { activeElement } = inDocument; | ||
return (activeElement.shadowRoot | ||
? getActiveElement(activeElement.shadowRoot) | ||
: activeElement instanceof HTMLIFrameElement && ((_a = activeElement.contentWindow) === null || _a === void 0 ? void 0 : _a.document) | ||
? getActiveElement(activeElement.contentWindow.document) | ||
: activeElement); | ||
}; |
@@ -10,9 +10,21 @@ import { FOCUS_AUTO } from '../constants'; | ||
}; | ||
export const getFocusables = (parents, withGuards) => parents.reduce((acc, parent) => acc.concat( | ||
// add all tabbables inside and within shadow DOMs in DOM order | ||
getFocusablesWithShadowDom(parent, withGuards), | ||
// add if node is tabbable itself | ||
parent.parentNode | ||
? toArray(parent.parentNode.querySelectorAll(queryTabbables)).filter((node) => node === parent) | ||
: []), []); | ||
const getFocusablesWithIFrame = (parent, withGuards) => { | ||
if (parent instanceof HTMLIFrameElement && parent.contentDocument) { | ||
return getFocusables([parent.contentDocument.body], withGuards); | ||
} | ||
return [parent]; | ||
}; | ||
export const getFocusables = (parents, withGuards) => { | ||
return parents.reduce((acc, parent) => { | ||
const focusableWithShadowDom = getFocusablesWithShadowDom(parent, withGuards); | ||
const focusableWithIframes = [].concat(...focusableWithShadowDom.map((node) => getFocusablesWithIFrame(node, withGuards))); | ||
return acc.concat( | ||
// add all tabbables inside and within shadow DOMs in DOM order | ||
focusableWithIframes, | ||
// add if node is tabbable itself | ||
parent.parentNode | ||
? toArray(parent.parentNode.querySelectorAll(queryTabbables)).filter((node) => node === parent) | ||
: []); | ||
}, []); | ||
}; | ||
/** | ||
@@ -19,0 +31,0 @@ * return a list of focusable nodes within an area marked as "auto-focusable" |
/** | ||
* @returns {Boolean} true, if the current focus is inside given node or nodes | ||
*/ | ||
export declare const focusInside: (topNode: HTMLElement | HTMLElement[]) => boolean; | ||
export declare const focusInside: (topNode: HTMLElement | HTMLElement[], activeElement?: HTMLElement | undefined) => boolean; |
@@ -8,5 +8,5 @@ "use strict"; | ||
var getActiveElement_1 = require("./utils/getActiveElement"); | ||
var focusInFrame = function (frame) { return frame === document.activeElement; }; | ||
var focusInsideIframe = function (topNode) { | ||
return Boolean((0, array_1.toArray)(topNode.querySelectorAll('iframe')).some(function (node) { return focusInFrame(node); })); | ||
var focusInFrame = function (frame, activeElement) { return frame === activeElement; }; | ||
var focusInsideIframe = function (topNode, activeElement) { | ||
return Boolean((0, array_1.toArray)(topNode.querySelectorAll('iframe')).some(function (node) { return focusInFrame(node, activeElement); })); | ||
}; | ||
@@ -16,9 +16,12 @@ /** | ||
*/ | ||
var focusInside = function (topNode) { | ||
var activeElement = document && (0, getActiveElement_1.getActiveElement)(); | ||
var focusInside = function (topNode, activeElement) { | ||
// const activeElement = document && getActiveElement(); | ||
if (activeElement === void 0) { activeElement = (0, getActiveElement_1.getActiveElement)((0, array_1.getFirst)(topNode).ownerDocument); } | ||
if (!activeElement || (activeElement.dataset && activeElement.dataset.focusGuard)) { | ||
return false; | ||
} | ||
return (0, all_affected_1.getAllAffectedNodes)(topNode).some(function (node) { return (0, DOMutils_1.contains)(node, activeElement) || focusInsideIframe(node); }); | ||
return (0, all_affected_1.getAllAffectedNodes)(topNode).some(function (node) { | ||
return (0, DOMutils_1.contains)(node, activeElement) || focusInsideIframe(node, activeElement); | ||
}); | ||
}; | ||
exports.focusInside = focusInside; |
@@ -6,2 +6,2 @@ /** | ||
*/ | ||
export declare const focusIsHidden: () => boolean; | ||
export declare const focusIsHidden: (inDocument?: Document) => boolean; |
@@ -13,4 +13,5 @@ "use strict"; | ||
*/ | ||
var focusIsHidden = function () { | ||
var activeElement = document && (0, getActiveElement_1.getActiveElement)(); | ||
var focusIsHidden = function (inDocument) { | ||
if (inDocument === void 0) { inDocument = document; } | ||
var activeElement = (0, getActiveElement_1.getActiveElement)(inDocument); | ||
if (!activeElement) { | ||
@@ -20,4 +21,4 @@ return false; | ||
// this does not support setting FOCUS_ALLOW within shadow dom | ||
return (0, array_1.toArray)(document.querySelectorAll("[".concat(constants_1.FOCUS_ALLOW, "]"))).some(function (node) { return (0, DOMutils_1.contains)(node, activeElement); }); | ||
return (0, array_1.toArray)(inDocument.querySelectorAll("[".concat(constants_1.FOCUS_ALLOW, "]"))).some(function (node) { return (0, DOMutils_1.contains)(node, activeElement); }); | ||
}; | ||
exports.focusIsHidden = focusIsHidden; |
@@ -7,2 +7,3 @@ "use strict"; | ||
var all_affected_1 = require("./utils/all-affected"); | ||
var array_1 = require("./utils/array"); | ||
var auto_focus_1 = require("./utils/auto-focus"); | ||
@@ -25,3 +26,3 @@ var getActiveElement_1 = require("./utils/getActiveElement"); | ||
var getFocusMerge = function (topNode, lastNode) { | ||
var activeElement = document && (0, getActiveElement_1.getActiveElement)(); | ||
var activeElement = (0, getActiveElement_1.getActiveElement)((0, array_1.asArray)(topNode).length > 0 ? document : (0, array_1.getFirst)(topNode).ownerDocument); | ||
var entries = (0, all_affected_1.getAllAffectedNodes)(topNode).filter(is_1.isNotAGuard); | ||
@@ -28,0 +29,0 @@ var commonParent = (0, parenting_1.getTopCommonParent)(activeElement || topNode, topNode, entries); |
@@ -7,2 +7,3 @@ interface ListOf<TNode> { | ||
export declare const asArray: <T>(a: T | T[]) => T[]; | ||
export declare const getFirst: <T>(a: T | T[]) => T; | ||
export {}; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.asArray = exports.toArray = void 0; | ||
exports.getFirst = exports.asArray = exports.toArray = void 0; | ||
var toArray = function (a) { | ||
@@ -18,1 +18,3 @@ var ret = Array(a.length); | ||
exports.asArray = asArray; | ||
var getFirst = function (a) { return (Array.isArray(a) ? a[0] : a); }; | ||
exports.getFirst = getFirst; |
@@ -61,5 +61,15 @@ "use strict"; | ||
} | ||
return (0, array_1.toArray)(scope.children).some(function (child) { return (0, exports.contains)(child, element); }); | ||
return (0, array_1.toArray)(scope.children).some(function (child) { | ||
var _a; | ||
if (child instanceof HTMLIFrameElement) { | ||
var iframeBody = (_a = child.contentDocument) === null || _a === void 0 ? void 0 : _a.body; | ||
if (iframeBody) { | ||
return (0, exports.contains)(iframeBody, element); | ||
} | ||
return false; | ||
} | ||
return (0, exports.contains)(child, element); | ||
}); | ||
} | ||
}; | ||
exports.contains = contains; |
/** | ||
* returns active element from document or from nested shadowdoms | ||
*/ | ||
export declare const getActiveElement: () => HTMLElement | undefined; | ||
export declare const getActiveElement: (inDocument?: Document | ShadowRoot | undefined) => HTMLElement | undefined; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getActiveElement = void 0; | ||
var getNestedShadowActiveElement = function (shadowRoot) { | ||
return shadowRoot.activeElement | ||
? shadowRoot.activeElement.shadowRoot | ||
? getNestedShadowActiveElement(shadowRoot.activeElement.shadowRoot) | ||
: shadowRoot.activeElement | ||
: undefined; | ||
}; | ||
/** | ||
* returns active element from document or from nested shadowdoms | ||
*/ | ||
var getActiveElement = function () { | ||
return (document.activeElement | ||
? document.activeElement.shadowRoot | ||
? getNestedShadowActiveElement(document.activeElement.shadowRoot) | ||
: document.activeElement | ||
: undefined); // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getActiveElement = void 0; | ||
var getActiveElement = function (inDocument) { | ||
var _a; | ||
if (inDocument === void 0) { inDocument = document; } | ||
if (!inDocument || !inDocument.activeElement) { | ||
return undefined; | ||
} | ||
var activeElement = inDocument.activeElement; | ||
return (activeElement.shadowRoot | ||
? (0, exports.getActiveElement)(activeElement.shadowRoot) | ||
: activeElement instanceof HTMLIFrameElement && ((_a = activeElement.contentWindow) === null || _a === void 0 ? void 0 : _a.document) | ||
? (0, exports.getActiveElement)(activeElement.contentWindow.document) | ||
: activeElement); | ||
}; | ||
exports.getActiveElement = getActiveElement; |
@@ -15,7 +15,16 @@ "use strict"; | ||
}; | ||
var getFocusablesWithIFrame = function (parent, withGuards) { | ||
if (parent instanceof HTMLIFrameElement && parent.contentDocument) { | ||
return (0, exports.getFocusables)([parent.contentDocument.body], withGuards); | ||
} | ||
return [parent]; | ||
}; | ||
var getFocusables = function (parents, withGuards) { | ||
return parents.reduce(function (acc, parent) { | ||
var _a; | ||
var focusableWithShadowDom = getFocusablesWithShadowDom(parent, withGuards); | ||
var focusableWithIframes = (_a = []).concat.apply(_a, focusableWithShadowDom.map(function (node) { return getFocusablesWithIFrame(node, withGuards); })); | ||
return acc.concat( | ||
// add all tabbables inside and within shadow DOMs in DOM order | ||
getFocusablesWithShadowDom(parent, withGuards), | ||
focusableWithIframes, | ||
// add if node is tabbable itself | ||
@@ -22,0 +31,0 @@ parent.parentNode |
{ | ||
"name": "focus-lock", | ||
"version": "0.11.4", | ||
"version": "0.11.5", | ||
"description": "DOM trap for a focus", | ||
@@ -5,0 +5,0 @@ "main": "dist/es5/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
127718
3047