Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions build/tasks/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ const categories = [
'sensory-and-visual-cues',
'structure',
'tables',
'labels',
'text-alternatives',
'time-and-media',
'images'
Expand Down
78 changes: 75 additions & 3 deletions lib/checks/label/label-content-name-mismatch-evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,42 @@ import {
sanitize,
removeUnicode
} from '../../commons/text';
import stem from 'wink-porter2-stemmer';

const threshold = 0.75;

function cleanText(str) {
return str
?.toLowerCase()
.normalize('NFKC')
.replace(/[\u200B-\u200D\u2060\uFEFF]/g, '')
.trim();
}

function replaceSynonyms(text) {
const synonymMap = {
'&': 'and'
};
return text
.split(/[^\p{L}\p{N}]+/u)
.map(word => synonymMap[word] || word)
.join(' ');
}

function stringStemmer(str) {
return replaceSynonyms(str)
.split(/[^\p{L}\p{N}]+/u)
.filter(Boolean)
.map(word => {
const w = cleanText(word).replace(/[^\p{L}\p{N}]/gu, '');
try {
return stem(w);
} catch (err) {
return w;
}
})
.join(' ');
}

/**
* Check if a given text exists in another
Expand All @@ -14,12 +50,45 @@ import {
* @returns {Boolean}
*/
function isStringContained(compare, compareWith) {
compare = stringStemmer(compare);
compareWith = stringStemmer(compareWith);

const curatedCompareWith = curateString(compareWith);
const curatedCompare = curateString(compare);
if (!curatedCompareWith || !curatedCompare) {
return false;
}
return curatedCompareWith.includes(curatedCompare);
const res = curatedCompareWith.includes(curatedCompare);
if (res) {
return res;
}

const tokensA = compare.split(/[^\p{L}\p{N}]+/u);
const tokensB = compareWith.split(/[^\p{L}\p{N}]+/u);
const freqA = {},
freqB = {};
tokensA.forEach(word => {
freqA[word] = (freqA[word] || 0) + 1;
});
tokensB.forEach(word => {
freqB[word] = (freqB[word] || 0) + 1;
});

let dot = 0,
magA = 0,
magB = 0;
const allTerms = new Set([...Object.keys(freqA), ...Object.keys(freqB)]);
allTerms.forEach(term => {
const a = freqA[term] || 0;
const b = freqB[term] || 0;
dot += a * b;
magA += a * a;
magB += b * b;
});

const similarity =
magA && magB ? dot / (Math.sqrt(magA) * Math.sqrt(magB)) : 0;
return similarity >= threshold; // comparision with threshold as 75%
}

/**
Expand All @@ -32,7 +101,8 @@ function curateString(str) {
const noUnicodeStr = removeUnicode(str, {
emoji: true,
nonBmp: true,
punctuations: true
punctuations: true,
whitespace: true
});
return sanitize(noUnicodeStr);
}
Expand All @@ -52,9 +122,11 @@ function labelContentNameMismatchEvaluate(node, options, virtualNode) {
subtreeDescendant: true,
ignoreIconLigature: true,
pixelThreshold,
occurrenceThreshold
occurrenceThreshold,
ignoreNativeTextAlternative: true // To Skip for nativeTextAlternative
})
).toLowerCase();

if (!visibleText) {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/commons/text/native-text-alternative.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import nativeTextMethods from './native-text-methods';
* @return {String} Accessible text
*/
export default function nativeTextAlternative(virtualNode, context = {}) {
if (context.ignoreNativeTextAlternative) {
return '';
}

const { actualNode } = virtualNode;
if (
virtualNode.props.nodeType !== 1 ||
Expand Down
5 changes: 4 additions & 1 deletion lib/commons/text/remove-unicode.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import emojiRegexText from 'emoji-regex';
* @returns {String}
*/
function removeUnicode(str, options) {
const { emoji, nonBmp, punctuations } = options;
const { emoji, nonBmp, punctuations, whitespace } = options;

if (emoji) {
str = str.replace(emojiRegexText(), '');
Expand All @@ -34,6 +34,9 @@ function removeUnicode(str, options) {
if (punctuations) {
str = str.replace(getPunctuationRegExp(), '');
}
if (whitespace) {
str = str.replace(/\s+/g, '');
}

return str;
}
Expand Down
12 changes: 12 additions & 0 deletions lib/commons/text/subtree-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,19 @@ function subtreeText(virtualNode, context = {}) {

const phrasingElements = getElementsByContentType('phrasing').concat(['#text']);

function skipByInlineOverflow(virtualNode) {
const computedStyleOverflow = virtualNode._cache.computedStyle_overflow;
if (computedStyleOverflow && computedStyleOverflow === 'hidden') {
return true;
}
return false;
}

function appendAccessibleText(contentText, virtualNode, context) {
if (skipByInlineOverflow(virtualNode)) {
return contentText;
}

const nodeName = virtualNode.props.nodeName;
let contentTextAdd = accessibleTextVirtual(virtualNode, context);
if (!contentTextAdd) {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/autocomplete-valid.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"actIds": ["73f2c2"],
"metadata": {
"description": "Ensure that the necessary form fields use the autocomplete attribute with a valid input.",
"description": "Ensures that the necessary form fields use the autocomplete attribute with a valid input.",
"help": "Autocomplete attribute must have a valid value"
},
"all": ["autocomplete-valid"],
Expand Down
19 changes: 19 additions & 0 deletions lib/rules/heading-order-bp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"id": "heading-order-bp",
"impact": "moderate",
"selector": "h1, h2, h3, h4, h5, h6, [role=heading]",
"matches": "heading-matches",
"tags": [
"cat.structure",
"best-practice",
"a11y-engine",
"a11y-engine-experimental"
],
"metadata": {
"description": "Ensures the order of headings is semantically correct",
"help": "Heading levels should only increase by one"
},
"all": [],
"any": ["heading-order"],
"none": []
}
15 changes: 14 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@
"typescript": "^5.2.2",
"uglify-js": "^3.17.4",
"wcag-act-rules": "github:w3c/wcag-act-rules#dc90495a5533d326b300ee5a9487afdfc6d493c0",
"weakmap-polyfill": "^2.0.4"
"weakmap-polyfill": "^2.0.4",
"wink-porter2-stemmer": "^2.0.1"
},
"lint-staged": {
"*.{md,json,ts,html}": [
Expand Down
Loading