Skip to content

Commit

Permalink
fix(is-ligature-icon): rename canvas to canvasContext (#1880)
Browse files Browse the repository at this point in the history
  • Loading branch information
straker committed Nov 5, 2019
1 parent 96ab457 commit de9885d
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions lib/commons/text/is-icon-ligature.js
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

0 comments on commit de9885d

Please sign in to comment.