From ac33400cf4268f17e23b5ca737bdcb5c04eeabbe Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Wed, 9 Mar 2022 00:23:12 +0800 Subject: [PATCH] Avoid length-based filter in `inferSelectors` (#2905) --- src/contentScript/nativeEditor/infer.test.tsx | 17 ----------------- src/contentScript/nativeEditor/infer.ts | 12 ++---------- 2 files changed, 2 insertions(+), 27 deletions(-) diff --git a/src/contentScript/nativeEditor/infer.test.tsx b/src/contentScript/nativeEditor/infer.test.tsx index d54a45d1d3..a00b02ae2b 100644 --- a/src/contentScript/nativeEditor/infer.test.tsx +++ b/src/contentScript/nativeEditor/infer.test.tsx @@ -21,7 +21,6 @@ import { inferButtonHTML, inferPanelHTML, inferSelectors, - isSelectorPotentiallyUseful, safeCssSelector, } from "@/contentScript/nativeEditor/infer"; import { PIXIEBRIX_DATA_ATTR, EXTENSION_POINT_DATA_ATTR } from "@/common"; @@ -354,22 +353,6 @@ test("getSelectorPreference: matches expected sorting", () => { expect(getSelectorPreference(selector)).toBe(selector.length); }); -test("isSelectorPotentiallyUseful", () => { - const fn = isSelectorPotentiallyUseful; - expect(fn(".navItem")).toBeTruthy(); - expect(fn(".birdsArentReal")).toBeTruthy(); - expect(fn('[aria-label="Click elsewhere"]')).toBeTruthy(); - - // Always allow IDs - expect(fn("#yes")).toBeTruthy(); - - // Exclude utility classes - expect(fn(".p-1")).toBeFalsy(); - - // Exclude some short random classes - expect(fn("._d3f32f")).toBeFalsy(); -}); - describe("inferSelectors", () => { const expectSelectors = (selectors: string[], body: string) => { document.body.innerHTML = body; diff --git a/src/contentScript/nativeEditor/infer.ts b/src/contentScript/nativeEditor/infer.ts index 229c8ee478..c8393aa106 100644 --- a/src/contentScript/nativeEditor/infer.ts +++ b/src/contentScript/nativeEditor/infer.ts @@ -15,7 +15,7 @@ * along with this program. If not, see . */ -import { uniq, sortBy, unary, intersection } from "lodash"; +import { uniq, sortBy, unary, intersection, isEmpty } from "lodash"; import { getCssSelector } from "css-selector-generator"; import { isNullOrBlank, mostCommonElement, matchesAnyPattern } from "@/utils"; import { BusinessError } from "@/errors"; @@ -124,14 +124,6 @@ export function getSelectorPreference(selector: string): number { return selector.length; } -/** Excludes empty or short selectors (must have more than 3 letters, no numbers) */ -export function isSelectorPotentiallyUseful(selector: string): boolean { - // Remove the non-letter characters, and then compare the number of remaining letter characters - return ( - selector.startsWith("#") || selector.replace(/[^a-z]/gi, "").length > 3 - ); -} - function outerHTML(element: Element | string): string { if (typeof element === "string") { return element; @@ -603,7 +595,7 @@ export function inferSelectors( makeSelector(["id", "tag", "attribute", "nthchild"]), makeSelector(["id", "tag", "attribute"]), makeSelector(), - ]).filter((x) => isSelectorPotentiallyUseful(x)); + ]).filter((x) => !isEmpty(x)); return sortBy(generatedSelectors, getSelectorPreference); }