-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
104 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
// Author: Anton Medvedev <[email protected]> | ||
// Source: https://github.com/antonmedv/finder | ||
export function finder(input, options) { | ||
const startTime = new Date(); | ||
if (input.nodeType !== Node.ELEMENT_NODE) { | ||
throw new Error(`Can't generate CSS selector for non-element node type.`); | ||
} | ||
|
@@ -15,15 +14,47 @@ export function finder(input, options) { | |
className: wordLike, | ||
tagName: (name) => true, | ||
attr: useAttr, | ||
timeoutMs: 1000, | ||
seedMinLength: 2, | ||
optimizedMinLength: 2, | ||
timeoutMs: 1000, | ||
maxNumberOfPathChecks: Infinity, | ||
}; | ||
const startTime = new Date(); | ||
const config = { ...defaults, ...options }; | ||
const rootDocument = findRootDocument(config.root, defaults); | ||
let foundPath; | ||
let count = 0; | ||
for (const candidate of search(input, config, rootDocument)) { | ||
const elapsedTimeMs = new Date().getTime() - startTime.getTime(); | ||
if (elapsedTimeMs > config.timeoutMs || | ||
count >= config.maxNumberOfPathChecks) { | ||
const fPath = fallback(input, rootDocument); | ||
if (!fPath) { | ||
throw new Error(`Timeout: Can't find a unique selector after ${config.timeoutMs}ms`); | ||
} | ||
return selector(fPath); | ||
} | ||
count++; | ||
if (unique(candidate, rootDocument)) { | ||
foundPath = candidate; | ||
break; | ||
} | ||
} | ||
if (!foundPath) { | ||
throw new Error(`Selector was not found.`); | ||
} | ||
const optimized = [ | ||
...optimize(foundPath, input, config, rootDocument, startTime), | ||
]; | ||
optimized.sort(byPenalty); | ||
if (optimized.length > 0) { | ||
return selector(optimized[0]); | ||
} | ||
return selector(foundPath); | ||
} | ||
function* search(input, config, rootDocument) { | ||
const stack = []; | ||
let paths = []; | ||
let foundPath; | ||
let current = input; | ||
let i = 0; | ||
while (current && current !== rootDocument) { | ||
|
@@ -36,27 +67,17 @@ export function finder(input, options) { | |
i++; | ||
paths.push(...combinations(stack)); | ||
if (i >= config.seedMinLength) { | ||
foundPath = search(paths, config, input, rootDocument, startTime); | ||
if (foundPath) { | ||
break; | ||
paths.sort(byPenalty); | ||
for (const candidate of paths) { | ||
yield candidate; | ||
} | ||
paths = []; | ||
} | ||
} | ||
if (paths.length > 0) { | ||
foundPath = search(paths, config, input, rootDocument, startTime); | ||
} | ||
if (!foundPath) { | ||
throw new Error(`Selector was not found.`); | ||
} | ||
const optimized = [ | ||
...optimize(foundPath, input, config, rootDocument, startTime), | ||
]; | ||
optimized.sort(byPenalty); | ||
if (optimized.length > 0) { | ||
return selector(optimized[0]); | ||
paths.sort(byPenalty); | ||
for (const candidate of paths) { | ||
yield candidate; | ||
} | ||
return selector(foundPath); | ||
} | ||
export function wordLike(name) { | ||
if (/^[a-z0-9\-]{3,}$/i.test(name)) { | ||
|
@@ -81,22 +102,6 @@ export function useAttr(name, value) { | |
valueIsOk ||= value.startsWith('#') && wordLike(value.slice(1)); | ||
return nameIsOk && valueIsOk; | ||
} | ||
function search(paths, config, input, rootDocument, startTime) { | ||
paths.sort(byPenalty); | ||
for (const candidate of paths) { | ||
const elapsedTimeMs = new Date().getTime() - startTime.getTime(); | ||
if (elapsedTimeMs > config.timeoutMs) { | ||
const path = fallback(input, rootDocument); | ||
if (path) { | ||
return path; | ||
} | ||
throw new Error(`Timeout: Can't find a unique selector after ${config.timeoutMs}ms`); | ||
} | ||
if (unique(candidate, rootDocument)) { | ||
return candidate; | ||
} | ||
} | ||
} | ||
function tie(element, config) { | ||
const level = []; | ||
const elementId = element.getAttribute('id'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters