Skip to content

Commit

Permalink
fix: don't mutate elmStack
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcy Sutton authored and marcysutton committed Jun 14, 2017
1 parent f849945 commit ad0794c
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions lib/commons/color/get-background-color.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ function includeMissingElements(elmStack, elm) {
const tagArray = elmStack.map((elm) => {
return elm.tagName;
});
let bgNodes = elmStack;
for (let candidate in elementMap) {
if (elementMap.hasOwnProperty(candidate)) {
// tagName matches key
Expand All @@ -134,39 +135,43 @@ function includeMissingElements(elmStack, elm) {
// found an ancestor not in elmStack, and it overlaps
let overlaps = axe.commons.dom.visuallyOverlaps(elm.getBoundingClientRect(), ancestorMatch);
if (overlaps) {
elmStack.splice(elmStack.indexOf(elm) + 1, 0, ancestorMatch);
bgNodes.splice(elmStack.indexOf(elm) + 1, 0, ancestorMatch);
}
}
}
// tagName matches value
// (such as LABEL, when matching itself. It should be in the list, but Phantom skips it)
if (elm.tagName === elementMap[candidate] && tagArray.indexOf(elm.tagName) === -1) {
elmStack.splice(tagArray.indexOf(candidate) + 1, 0, elm);
bgNodes.splice(tagArray.indexOf(candidate) + 1, 0, elm);
}
}
}
return bgNodes;
}

/**
* Look at document and body elements for relevant background information
* @private
* @param {Array} elmStack
*/
function consultDocumentBody(elmStack) {
function sortPageBackground(elmStack) {
let bodyIndex = elmStack.indexOf(document.body);

let bgNodes = elmStack;

if (// Check that the body background is the page's background
bodyIndex > 1 && // only if there are negative z-index elements
!elmHasImage(document.documentElement) &&
getBgColor(document.documentElement).alpha === 0
) {
// Remove body and html from it's current place
elmStack.splice(bodyIndex, 1);
elmStack.splice( elmStack.indexOf(document.documentElement), 1);
bgNodes.splice(bodyIndex, 1);
bgNodes.splice( elmStack.indexOf(document.documentElement), 1);

// Put the body background as the lowest element
elmStack.push(document.body);
bgNodes.push(document.body);
}
return bgNodes;
}

/**
Expand All @@ -190,10 +195,9 @@ color.getBackgroundStack = function(elm) {
window.innerHeight - 1);

let elmStack = document.elementsFromPoint(x, y);
includeMissingElements(elmStack, elm);
elmStack = includeMissingElements(elmStack, elm);
elmStack = dom.reduceToElementsBelowFloating(elmStack, elm);

consultDocumentBody(elmStack);
elmStack = sortPageBackground(elmStack);

// Return all elements BELOW the current element, null if the element is undefined
let elmIndex = elmStack.indexOf(elm);
Expand Down

0 comments on commit ad0794c

Please sign in to comment.