Skip to content

Commit

Permalink
[Robustness] use safe-regex-test
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jan 9, 2024
1 parent 2d8d557 commit 4c7e781
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"minimatch": "^3.1.2",
"object.entries": "^1.1.7",
"object.fromentries": "^2.0.7",
"safe-regex-test": "^1.0.1",
"string.prototype.includes": "^2.0.0"
},
"peerDependencies": {
Expand Down
5 changes: 4 additions & 1 deletion src/rules/accessible-emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import emojiRegex from 'emoji-regex';
import { getProp, getLiteralPropValue } from 'jsx-ast-utils';
import safeRegexTest from 'safe-regex-test';
import { generateObjSchema } from '../util/schemas';
import getElementType from '../util/getElementType';
import isHiddenFromScreenReader from '../util/isHiddenFromScreenReader';
Expand All @@ -29,11 +30,13 @@ export default {

create: (context) => {
const elementType = getElementType(context);

const testEmoji = safeRegexTest(emojiRegex());
return {
JSXOpeningElement: (node) => {
const literalChildValue = node.parent.children.find((child) => child.type === 'Literal' || child.type === 'JSXText');

if (literalChildValue && emojiRegex().test(literalChildValue.value)) {
if (literalChildValue && testEmoji(literalChildValue.value)) {
const elementIsHidden = isHiddenFromScreenReader(elementType(node), node.attributes);
if (elementIsHidden) {
return; // emoji is decorative
Expand Down
5 changes: 4 additions & 1 deletion src/rules/anchor-is-valid.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import { getProp, getPropValue } from 'jsx-ast-utils';
import type { JSXOpeningElement } from 'ast-types-flow';
import safeRegexTest from 'safe-regex-test';
import type { ESLintConfig, ESLintContext, ESLintVisitorSelectorConfig } from '../../flow/eslint';
import { generateObjSchema, arraySchema, enumArraySchema } from '../util/schemas';
import getElementType from '../util/getElementType';
Expand Down Expand Up @@ -39,6 +40,8 @@ export default ({

create: (context: ESLintContext): ESLintVisitorSelectorConfig => {
const elementType = getElementType(context);
const testJShref = safeRegexTest(/^\W*?javascript:/);

return {
JSXOpeningElement: (node: JSXOpeningElement): void => {
const { attributes } = node;
Expand Down Expand Up @@ -98,7 +101,7 @@ export default ({
.filter((value) => (
value != null
&& (typeof value === 'string' && (
!value.length || value === '#' || /^\W*?javascript:/.test(value)
!value.length || value === '#' || testJShref(value)
))
));
if (invalidHrefValues.length !== 0) {
Expand Down
6 changes: 4 additions & 2 deletions src/rules/img-redundant-alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import { getProp, getLiteralPropValue } from 'jsx-ast-utils';
import includes from 'array-includes';
import stringIncludes from 'string.prototype.includes';
import safeRegexTest from 'safe-regex-test';
import { generateObjSchema, arraySchema } from '../util/schemas';
import getElementType from '../util/getElementType';
import isHiddenFromScreenReader from '../util/isHiddenFromScreenReader';
Expand All @@ -27,11 +28,12 @@ const schema = generateObjSchema({
words: arraySchema,
});

const isASCII = safeRegexTest(/[\x20-\x7F]+/);

function containsRedundantWord(value, redundantWords) {
const lowercaseRedundantWords = redundantWords.map((redundantWord) => redundantWord.toLowerCase());
const isASCII = /[\x20-\x7F]+/.test(value);

if (isASCII) {
if (isASCII(value)) {
return value.split(/\s+/).some((valueWord) => includes(lowercaseRedundantWords, valueWord.toLowerCase()));
}
return lowercaseRedundantWords.some((redundantWord) => stringIncludes(value.toLowerCase(), redundantWord));
Expand Down

0 comments on commit 4c7e781

Please sign in to comment.