Skip to content

Commit e114bfd

Browse files
authored
fix(get-background-color): process tbody, thead, and tfoot when getting background color (#1653)
* feat(get-background-color): process tbody, thead, and tfoot when getting background color * uppercase nodenme * fix other nodeName
1 parent 7ea074d commit e114bfd

File tree

2 files changed

+96
-27
lines changed

2 files changed

+96
-27
lines changed

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

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -260,45 +260,48 @@ function sortPageBackground(elmStack) {
260260
*/
261261
function includeMissingElements(elmStack, elm) {
262262
/*eslint max-depth:["error",7]*/
263+
const nodeName = elm.nodeName.toUpperCase();
263264
const elementMap = {
264-
TD: ['TR', 'TBODY'],
265-
TH: ['TR', 'THEAD'],
265+
TD: ['TR', 'THEAD', 'TBODY', 'TFOOT'],
266+
TH: ['TR', 'THEAD', 'TBODY', 'TFOOT'],
266267
INPUT: ['LABEL']
267268
};
268269
const tagArray = elmStack.map(elm => {
269-
return elm.nodeName;
270+
return elm.nodeName.toUpperCase();
270271
});
271272
let bgNodes = elmStack;
272273
for (let candidate in elementMap) {
273274
// check that TR or LABEL has paired nodeName from elementMap, but don't expect elm to be that candidate
274275
if (tagArray.includes(candidate)) {
275-
for (let candidateIndex in elementMap[candidate]) {
276-
if (candidate.hasOwnProperty(candidateIndex)) {
277-
// look up the tree for a matching candidate
278-
let ancestorMatch = axe.commons.dom.findUp(
279-
elm,
280-
elementMap[candidate][candidateIndex]
276+
for (
277+
let candidateIndex = 0;
278+
candidateIndex < elementMap[candidate].length;
279+
candidateIndex++
280+
) {
281+
// look up the tree for a matching candidate
282+
let ancestorMatch = axe.commons.dom.findUp(
283+
elm,
284+
elementMap[candidate][candidateIndex]
285+
);
286+
if (ancestorMatch && elmStack.indexOf(ancestorMatch) === -1) {
287+
// found an ancestor not in elmStack, and it overlaps
288+
let overlaps = axe.commons.dom.visuallyOverlaps(
289+
elm.getBoundingClientRect(),
290+
ancestorMatch
281291
);
282-
if (ancestorMatch && elmStack.indexOf(ancestorMatch) === -1) {
283-
// found an ancestor not in elmStack, and it overlaps
284-
let overlaps = axe.commons.dom.visuallyOverlaps(
285-
elm.getBoundingClientRect(),
286-
ancestorMatch
287-
);
288-
if (overlaps) {
289-
// if target is in the elementMap, use its position.
290-
bgNodes.splice(tagArray.indexOf(candidate) + 1, 0, ancestorMatch);
291-
}
292-
}
293-
// nodeName matches value
294-
// (such as LABEL, when matching itself. It should be in the list, but Phantom skips it)
295-
if (
296-
elm.nodeName === elementMap[candidate][candidateIndex] &&
297-
tagArray.indexOf(elm.nodeName) === -1
298-
) {
299-
bgNodes.splice(tagArray.indexOf(candidate) + 1, 0, elm);
292+
if (overlaps) {
293+
// if target is in the elementMap, use its position.
294+
bgNodes.splice(tagArray.indexOf(candidate) + 1, 0, ancestorMatch);
300295
}
301296
}
297+
// nodeName matches value
298+
// (such as LABEL, when matching itself. It should be in the list, but Phantom skips it)
299+
if (
300+
nodeName === elementMap[candidate][candidateIndex] &&
301+
tagArray.indexOf(nodeName) === -1
302+
) {
303+
bgNodes.splice(tagArray.indexOf(candidate) + 1, 0, elm);
304+
}
302305
}
303306
}
304307
}

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,72 @@ describe('color.getBackgroundColor', function() {
358358
assert.deepEqual(bgNodes, [parent]);
359359
});
360360

361+
it('should count a THEAD as a background element for a child element', function() {
362+
fixture.innerHTML =
363+
'<div style="background-color:#007acc;">' +
364+
'<table style="width:100%">' +
365+
'<thead style="background-color:#f3f3f3; height:40px;" id="parent">' +
366+
'<td>' +
367+
'<span style="color:#007acc" id="target">Cell content</span>' +
368+
'</td></thead>' +
369+
'</table></div>';
370+
var target = fixture.querySelector('#target'),
371+
parent = fixture.querySelector('#parent');
372+
var bgNodes = [];
373+
axe.testUtils.flatTreeSetup(fixture.firstChild);
374+
var actual = axe.commons.color.getBackgroundColor(target, bgNodes);
375+
var expected = new axe.commons.color.Color(243, 243, 243, 1);
376+
assert.equal(actual.red, expected.red);
377+
assert.equal(actual.green, expected.green);
378+
assert.equal(actual.blue, expected.blue);
379+
assert.equal(actual.alpha, expected.alpha);
380+
assert.deepEqual(bgNodes, [parent]);
381+
});
382+
383+
it('should count a TBODY as a background element for a child element', function() {
384+
fixture.innerHTML =
385+
'<div style="background-color:#007acc;">' +
386+
'<table style="width:100%">' +
387+
'<tbody style="background-color:#f3f3f3; height:40px;" id="parent">' +
388+
'<td>' +
389+
'<span style="color:#007acc" id="target">Cell content</span>' +
390+
'</td></tbody>' +
391+
'</table></div>';
392+
var target = fixture.querySelector('#target'),
393+
parent = fixture.querySelector('#parent');
394+
var bgNodes = [];
395+
axe.testUtils.flatTreeSetup(fixture.firstChild);
396+
var actual = axe.commons.color.getBackgroundColor(target, bgNodes);
397+
var expected = new axe.commons.color.Color(243, 243, 243, 1);
398+
assert.equal(actual.red, expected.red);
399+
assert.equal(actual.green, expected.green);
400+
assert.equal(actual.blue, expected.blue);
401+
assert.equal(actual.alpha, expected.alpha);
402+
assert.deepEqual(bgNodes, [parent]);
403+
});
404+
405+
it('should count a TFOOT as a background element for a child element', function() {
406+
fixture.innerHTML =
407+
'<div style="background-color:#007acc;">' +
408+
'<table style="width:100%">' +
409+
'<tfoot style="background-color:#f3f3f3; height:40px;" id="parent">' +
410+
'<td>' +
411+
'<span style="color:#007acc" id="target">Cell content</span>' +
412+
'</td></tfoot>' +
413+
'</table></div>';
414+
var target = fixture.querySelector('#target'),
415+
parent = fixture.querySelector('#parent');
416+
var bgNodes = [];
417+
axe.testUtils.flatTreeSetup(fixture.firstChild);
418+
var actual = axe.commons.color.getBackgroundColor(target, bgNodes);
419+
var expected = new axe.commons.color.Color(243, 243, 243, 1);
420+
assert.equal(actual.red, expected.red);
421+
assert.equal(actual.green, expected.green);
422+
assert.equal(actual.blue, expected.blue);
423+
assert.equal(actual.alpha, expected.alpha);
424+
assert.deepEqual(bgNodes, [parent]);
425+
});
426+
361427
it("should ignore TR elements that don't overlap", function() {
362428
fixture.innerHTML =
363429
'<table style="position:relative; width:100%;">' +

0 commit comments

Comments
 (0)