Skip to content

Commit

Permalink
refactor: commons.color.getBackgroundColor method (#1451)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeeyyy authored Apr 25, 2019
1 parent a7842e5 commit d25cad8
Show file tree
Hide file tree
Showing 8 changed files with 570 additions and 299 deletions.
30 changes: 30 additions & 0 deletions lib/commons/color/center-point-of-rect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* global color */

/**
* Get coordinates for an element's client rects or bounding client rect
*
* @method centerPointOfRect
* @memberof axe.commons.color
* @param {DOMRect} rect
* @returns {Object | undefined}
*/
color.centerPointOfRect = function centerPointOfRect(rect) {
if (rect.left > window.innerWidth) {
return undefined;
}

if (rect.top > window.innerHeight) {
return undefined;
}

const x = Math.min(
Math.ceil(rect.left + rect.width / 2),
window.innerWidth - 1
);
const y = Math.min(
Math.ceil(rect.top + rect.height / 2),
window.innerHeight - 1
);

return { x, y };
};
36 changes: 36 additions & 0 deletions lib/commons/color/element-has-image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* global color */

/**
* Reports if an element has a background image or gradient
*
* @method elementHasImage
* @memberof axe.commons.color
* @private
* @param {Element} elm
* @param {Object|null} style
* @return {Boolean}
*/
color.elementHasImage = function elementHasImage(elm, style) {
const graphicNodes = ['IMG', 'CANVAS', 'OBJECT', 'IFRAME', 'VIDEO', 'SVG'];
const nodeName = elm.nodeName.toUpperCase();

if (graphicNodes.includes(nodeName)) {
axe.commons.color.incompleteData.set('bgColor', 'imgNode');
return true;
}

style = style || window.getComputedStyle(elm);

const bgImageStyle = style.getPropertyValue('background-image');
const hasBgImage = bgImageStyle !== 'none';

if (hasBgImage) {
const hasGradient = /gradient/.test(bgImageStyle);
axe.commons.color.incompleteData.set(
'bgColor',
hasGradient ? 'bgGradient' : 'bgImage'
);
}

return hasBgImage;
};
Loading

0 comments on commit d25cad8

Please sign in to comment.