Skip to content

Commit

Permalink
fix(get-background-color): scroll element into view horizontally (#1845)
Browse files Browse the repository at this point in the history
* fix(get-background-color): scroll element into view horizontally

* prevent infinite loop

* comment
  • Loading branch information
straker authored Oct 14, 2019
1 parent 754d56b commit 50df70a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
45 changes: 45 additions & 0 deletions lib/commons/color/get-background-color.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ color.getBackgroundColor = function getBackgroundColor(
const clientHeight = elm.getBoundingClientRect().height;
const alignToTop = clientHeight - 2 >= window.innerHeight * 2;
elm.scrollIntoView(alignToTop);

// ensure element is scrolled into view horizontally
let center, scrollParent;
do {
const rect = elm.getBoundingClientRect();

// 'x' does not exist in IE11
const x = 'x' in rect ? rect.x : rect.left;
center = x + rect.width / 2;

if (center < 0) {
scrollParent = getScrollParent(elm);
scrollParent.scrollLeft = 0;
}
} while (center < 0 && scrollParent !== document.documentElement);
}

let bgColors = [];
Expand Down Expand Up @@ -378,6 +393,36 @@ function contentOverlapping(targetElement, bgNode) {
return false;
}

/**
* Return the scrolling parent element
* @see https://stackoverflow.com/questions/35939886/find-first-scrollable-parent#42543908
* @param {Element} element
* @param {Boolean} includeHidden
* @return {Element}
*/
function getScrollParent(element, includeHidden) {
var style = getComputedStyle(element);
var excludeStaticParent = style.position === 'absolute';
var overflowRegex = includeHidden ? /(auto|scroll|hidden)/ : /(auto|scroll)/;

if (style.position === 'fixed') {
return document.documentElement;
}
for (var parent = element; (parent = parent.parentElement); ) {
style = getComputedStyle(parent);
if (excludeStaticParent && style.position === 'static') {
continue;
}
if (
overflowRegex.test(style.overflow + style.overflowY + style.overflowX)
) {
return parent;
}
}

return document.documentElement;
}

/**
* Determines whether an element has a fully opaque background, whether solid color or an image
* @param {Element} node
Expand Down
13 changes: 13 additions & 0 deletions test/commons/color/get-background-color.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,19 @@ describe('color.getBackgroundColor', function() {
assert.notEqual(window.pageYOffset, 0);
});

it('scrolls horizontally into view when scroll is enabled', function() {
fixture.innerHTML =
'<div style="width: 1000px;"><span id="target">long text to test scrolling</span></div>';
var targetEl = fixture.querySelector('#target');
var bgNodes = [];
window.scroll(100, 0);

axe.testUtils.flatTreeSetup(fixture.firstChild);
axe.commons.color.getBackgroundColor(targetEl, bgNodes, false);

assert.equal(document.documentElement.scrollLeft, 0);
});

it('returns elements with negative z-index', function() {
fixture.innerHTML =
'<div id="sibling" ' +
Expand Down

0 comments on commit 50df70a

Please sign in to comment.