Skip to content

Commit

Permalink
no Uint32Array
Browse files Browse the repository at this point in the history
  • Loading branch information
straker committed Oct 1, 2019
1 parent 149d008 commit 8c8fdee
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 23 deletions.
4 changes: 2 additions & 2 deletions lib/checks/label/label-content-name-mismatch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { text } = axe.commons;
const { pixelThreshold, occuranceThreshold } = options || {};
const { differenceThreshold, occuranceThreshold } = options || {};

const accText = text.accessibleText(node).toLowerCase();
if (text.isHumanInterpretable(accText) < 1) {
Expand All @@ -10,7 +10,7 @@ const textVNodes = text.visibleTextNodes(virtualNode);
const nonLigatureText = textVNodes
.filter(
textVNode =>
!text.isIconLigature(textVNode, pixelThreshold, occuranceThreshold)
!text.isIconLigature(textVNode, differenceThreshold, occuranceThreshold)
)
.map(textVNode => textVNode.actualNode.nodeValue)
.join('');
Expand Down
2 changes: 1 addition & 1 deletion lib/checks/label/label-content-name-mismatch.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "label-content-name-mismatch",
"evaluate": "label-content-name-mismatch.js",
"options": {
"pixelThreshold": 0.1,
"differenceThreshold": 0.15,
"occuranceThreshold": 3
},
"metadata": {
Expand Down
32 changes: 17 additions & 15 deletions lib/commons/text/is-icon-ligature.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,7 @@ text.isIconLigature = function(
context.textAlign = 'left';
context.textBaseline = 'top';
context.fillText(firstChar, 0, 0);
const compareData = new Uint32Array(
context.getImageData(0, 0, width, fontSize).data.buffer
);
const compareData = context.getImageData(0, 0, width, fontSize).data;

// if the font doesn't even have character data for a single char then
// it has to be an icon ligature (e.g. Material Icon)
Expand All @@ -172,21 +170,25 @@ text.isIconLigature = function(

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

// calculate the number of differences between the first letter and the
// entire string, ignoring color differences
const differences = compareData.reduce((diff, pixel, i) => {
if (pixel === 0 && compareWith[i] === 0) {
return diff;
// entire string
let differences = 0;
const size = compareWith.length / 4;
for (let i = 0; i < compareWith.length; i += 4) {
// ignore color differences, only deal with pixel placement differences
let compareDataHasPixel = false;
let compareWithHasPixel = false;
for (let j = 0; j < 4; j++) {
compareDataHasPixel = compareDataHasPixel || !!compareData[i + j];
compareWithHasPixel = compareWithHasPixel || !!compareWith[i + j];
}
if (pixel !== 0 && compareWith[i] !== 0) {
return diff;

if (compareDataHasPixel !== compareWithHasPixel) {
differences++;
}
return ++diff;
}, 0);
}

// calculate the difference between the width of each character and the
// combined with of all characters
Expand All @@ -195,7 +197,7 @@ text.isIconLigature = function(
}, 0);
const actualWidth = context.measureText(nodeValue).width;

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

if (
Expand Down
4 changes: 0 additions & 4 deletions lib/core/imports/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ if (!('Promise' in window)) {
require('es6-promise').polyfill();
}

if (!('Uint32Array' in window)) {
require('core-js/features/typed-array/uint32-array');
}

/**
* Polyfill `WeakMap`
* Reference: https://github.com/polygonplanet/weakmap-polyfill
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
"browserify": "^16.2.3",
"chai": "~4.2.0",
"clone": "~2.1.1",
"core-js": "^3.2.1",
"css-selector-parser": "^1.3.0",
"derequire": "^2.0.6",
"emoji-regex": "8.0.0",
Expand Down

0 comments on commit 8c8fdee

Please sign in to comment.