Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: commons.color.getBackgroundColor method #1451

Merged
merged 13 commits into from
Apr 25, 2019
39 changes: 39 additions & 0 deletions lib/commons/color/element-has-background-image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* global color */

/**
* Reports if an element has a background image or gradient
*
* @method elementHasBackgroundImage
* @memberof axe.commons.color
* @private
* @param {Element} elm
* @param {Object|null} style
* @return {Boolean}
*/
color.elementHasBackgroundImage = function elementHasBackgroundImage(
jeeyyy marked this conversation as resolved.
Show resolved Hide resolved
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