Sha256: 58b9321f2c00910067cc92d13bf73b42d505da067593a37e526a20be4e04eee5

Contents?: true

Size: 1.71 KB

Versions: 104

Compression:

Stored size: 1.71 KB

Contents

/**
 * Return the scroll position of scrollable elements
 */
function getScroll(elm) {
	const style = window.getComputedStyle(elm);
	const visibleOverflowY = style.getPropertyValue('overflow-y') === 'visible';
	const visibleOverflowX = style.getPropertyValue('overflow-x') === 'visible';

	if (
		// See if the element hides overflowing content
		(!visibleOverflowY && elm.scrollHeight > elm.clientHeight) ||
		(!visibleOverflowX && elm.scrollWidth > elm.clientWidth)
	) {
		return { elm, top: elm.scrollTop, left: elm.scrollLeft };
	}
}

/**
 * set the scroll position of an element
 */
function setScroll(elm, top, left) {
	if (elm === window) {
		return elm.scroll(top, left);
	} else {
		elm.scrollTop = top;
		elm.scrollLeft = left;
	}
}

/**
 * Create an array scroll positions from descending elements
 */
function getElmScrollRecursive(root) {
	return Array.from(root.children).reduce((scrolls, elm) => {
		const scroll = getScroll(elm);
		if (scroll) {
			scrolls.push(scroll);
		}
		return scrolls.concat(getElmScrollRecursive(elm));
	}, []);
}

/**
 * Get the scroll position of all scrollable elements in a page
 */
axe.utils.getScrollState = function getScrollState(win = window) {
	const root = win.document.documentElement;
	const windowScroll = [
		win.pageXOffset !== undefined
			? {
					elm: win,
					top: win.pageYOffset,
					left: win.pageXOffset
			  }
			: {
					elm: root,
					top: root.scrollTop,
					left: root.scrollLeft
			  }
	];

	return windowScroll.concat(getElmScrollRecursive(document.body));
};

/**
 * set the scroll position of all items in the scrollState array
 */
axe.utils.setScrollState = function setScrollState(scrollState) {
	scrollState.forEach(({ elm, top, left }) => setScroll(elm, top, left));
};

Version data entries

104 entries across 104 versions & 1 rubygems

Version Path
govuk_publishing_components-21.16.3 node_modules/axe-core/lib/core/utils/scroll-state.js
govuk_publishing_components-21.16.2 node_modules/axe-core/lib/core/utils/scroll-state.js
govuk_publishing_components-21.16.1 node_modules/axe-core/lib/core/utils/scroll-state.js
govuk_publishing_components-21.16.0 node_modules/axe-core/lib/core/utils/scroll-state.js
govuk_publishing_components-21.15.2 node_modules/axe-core/lib/core/utils/scroll-state.js
govuk_publishing_components-21.15.1 node_modules/axe-core/lib/core/utils/scroll-state.js
govuk_publishing_components-21.15.0 node_modules/axe-core/lib/core/utils/scroll-state.js
govuk_publishing_components-21.14.0 node_modules/axe-core/lib/core/utils/scroll-state.js
govuk_publishing_components-21.13.5 node_modules/axe-core/lib/core/utils/scroll-state.js
govuk_publishing_components-21.13.4 node_modules/axe-core/lib/core/utils/scroll-state.js
govuk_publishing_components-21.13.3 node_modules/axe-core/lib/core/utils/scroll-state.js
govuk_publishing_components-21.13.2 node_modules/axe-core/lib/core/utils/scroll-state.js
govuk_publishing_components-21.13.1 node_modules/axe-core/lib/core/utils/scroll-state.js
govuk_publishing_components-21.13.0 node_modules/axe-core/lib/core/utils/scroll-state.js
govuk_publishing_components-21.12.0 node_modules/axe-core/lib/core/utils/scroll-state.js
govuk_publishing_components-21.11.0 node_modules/axe-core/lib/core/utils/scroll-state.js
govuk_publishing_components-21.10.0 node_modules/axe-core/lib/core/utils/scroll-state.js
govuk_publishing_components-21.9.0 node_modules/axe-core/lib/core/utils/scroll-state.js
govuk_publishing_components-21.8.1 node_modules/axe-core/lib/core/utils/scroll-state.js
govuk_publishing_components-21.8.0 node_modules/axe-core/lib/core/utils/scroll-state.js