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

chore(is-ligature-icon): rename canvas to canvasContext #1880

Merged
merged 1 commit into from
Nov 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions lib/commons/text/is-icon-ligature.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ text.isIconLigature = function(
return false;
}

if (!axe._cache.get('context')) {
if (!axe._cache.get('canvasContext')) {
axe._cache.set(
'context',
'canvasContext',
document.createElement('canvas').getContext('2d')
);
}
const context = axe._cache.get('context');
const canvas = context.canvas;
const canvasContext = axe._cache.get('canvasContext');
const canvas = canvasContext.canvas;

// keep track of each font encountered and the number of times it shows up
// as a ligature.
Expand Down Expand Up @@ -138,9 +138,9 @@ text.isIconLigature = function(
let fontStyle = `${fontSize}px ${fontFamily}`;

// set the size of the canvas to the width of the first letter
context.font = fontStyle;
canvasContext.font = fontStyle;
const firstChar = nodeValue.charAt(0);
let width = context.measureText(firstChar).width;
let width = canvasContext.measureText(firstChar).width;

// ensure font meets the 30px width requirement (30px font-size doesn't
// necessarily mean its 30px wide when drawn)
Expand All @@ -155,12 +155,12 @@ text.isIconLigature = function(

// changing the dimensions of a canvas resets all properties (include font)
// and clears it
context.font = fontStyle;
context.textAlign = 'left';
context.textBaseline = 'top';
context.fillText(firstChar, 0, 0);
canvasContext.font = fontStyle;
canvasContext.textAlign = 'left';
canvasContext.textBaseline = 'top';
canvasContext.fillText(firstChar, 0, 0);
const compareData = new Uint32Array(
context.getImageData(0, 0, width, fontSize).data.buffer
canvasContext.getImageData(0, 0, width, fontSize).data.buffer
);

// if the font doesn't even have character data for a single char then
Expand All @@ -170,10 +170,10 @@ text.isIconLigature = function(
return true;
}

context.clearRect(0, 0, width, fontSize);
context.fillText(nodeValue, 0, 0);
canvasContext.clearRect(0, 0, width, fontSize);
canvasContext.fillText(nodeValue, 0, 0);
const compareWith = new Uint32Array(
context.getImageData(0, 0, width, fontSize).data.buffer
canvasContext.getImageData(0, 0, width, fontSize).data.buffer
);

// calculate the number of differences between the first letter and the
Expand All @@ -191,9 +191,9 @@ text.isIconLigature = function(
// calculate the difference between the width of each character and the
// combined with of all characters
const expectedWidth = nodeValue.split('').reduce((width, char) => {
return width + context.measureText(char).width;
return width + canvasContext.measureText(char).width;
}, 0);
const actualWidth = context.measureText(nodeValue).width;
const actualWidth = canvasContext.measureText(nodeValue).width;

const pixelDifference = differences / compareData.length;
const sizeDifference = 1 - actualWidth / expectedWidth;
Expand Down