Skip to content

Commit f4551bb

Browse files
committed
fix: Minimise scrolling in getBackgroundColor
1 parent eeb052f commit f4551bb

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/commons/color/get-background-color.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ color.getBackgroundStack = function(elm) {
211211

212212
color.getBackgroundColor = function(elm, bgElms = [], noScroll = false) {
213213
if(noScroll !== true) {
214-
elm.scrollIntoView();
214+
// Avoid scrolling overflow:hidden containers, by only aligning to top
215+
// when not doing so would move the center point above the viewport top.
216+
const alignToTop = elm.clientHeight - 2 >= window.innerHeight * 2;
217+
elm.scrollIntoView(alignToTop);
215218
}
216219
let bgColors = [];
217220
let elmStack = color.getBackgroundStack(elm);

test/commons/color/get-background-color.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,4 +498,31 @@ describe('color.getBackgroundColor', function () {
498498
assert.closeTo(actual.alpha, expected.alpha, 0.1);
499499

500500
});
501+
502+
it('avoids scrolling elements with overflow:hidden', function () {
503+
fixture.innerHTML =
504+
'<div style="position:relative; color: yellow">' +
505+
'<div style="overflow: hidden">' +
506+
'<div style="background: black; height: 40px; padding-top: 20px;">' +
507+
'<div id="tgt1">Some text here</div>' +
508+
'<div style="height: 100px;"></div>' +
509+
'</div>' +
510+
'</div>' +
511+
'<div style="position: absolute; margin-top: -20px;" id="tgt2">R_20</div>' +
512+
'</div>';
513+
514+
// This shouldn't cause a scroll
515+
var target1 = document.getElementById('tgt1');
516+
axe.commons.color.getBackgroundColor(target1, []);
517+
518+
// Otherwise this would not be on the black bg anymore:
519+
var target2 = document.getElementById('tgt2');
520+
var actual = axe.commons.color.getBackgroundColor(target2, []);
521+
522+
assert.closeTo(actual.red, 0, 0.5);
523+
assert.closeTo(actual.green, 0, 0.5);
524+
assert.closeTo(actual.blue, 0, 0.5);
525+
assert.closeTo(actual.alpha, 1, 0.1);
526+
});
527+
501528
});

0 commit comments

Comments
 (0)