Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Dec 12, 2024
1 parent 6d99cca commit 5006933
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
27 changes: 19 additions & 8 deletions finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export function finder(input, options) {
}
const defaults = {
root: document.body,
idName: (name) => false,
attr: (name, value) => false,
className: (name) => wordLike(name),
idName: wordLike,
className: wordLike,
tagName: (name) => true,
attr: (name, value) => false,
timeoutMs: undefined,
};
const config = { ...defaults, ...options };
Expand All @@ -32,14 +32,28 @@ export function finder(input, options) {
i++;
const paths = sort(combinations(stack));
for (const candidate of paths) {
console.log(selector(candidate));
if (unique(candidate, rootDocument)) {
return selector(candidate);
}
}
}
throw new Error(`Selector was not found.`);
}
function wordLike(name) {
if (/^[a-z0-9\-]{3,}$/i.test(name)) {
const words = name.split(/-|[A-Z]/);
for (const word of words) {
if (word.length <= 2) { // No short words.
return false;
}
if (/[^aeiou]{3,}/i.test(word)) { // 3 or more consonants in a row.
return false;
}
}
return true;
}
return false;
}
function tie(element, config) {
const level = [];
const elementId = element.getAttribute('id');
Expand Down Expand Up @@ -85,14 +99,11 @@ function tie(element, config) {
if (nth !== undefined) {
level.push({
name: `*:nth-child(${nth})`,
penalty: 5,
penalty: 9,
});
}
return level;
}
function wordLike(name) {
return /^[a-zA-Z][a-z0-9]*(?:-[a-z0-9]+)*$/.test(name);
}
function selector(path) {
let node = path[0];
let query = node.name;
Expand Down
16 changes: 14 additions & 2 deletions finder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,19 @@ export function finder(input: Element, options?: Partial<Options>): string {
}

function wordLike(name: string) {
return /^[a-z0-9\-]{3,}$/i.test(name)
if (/^[a-z0-9\-]{3,}$/i.test(name)) {
const words = name.split(/-|[A-Z]/)
for (const word of words) {
if (word.length <= 2) { // No short words.
return false
}
if (/[^aeiou]{3,}/i.test(word)) { // 3 or more consonants in a row.
return false
}
}
return true
}
return false
}

function tie(element: Element, config: Options): Knot[] {
Expand Down Expand Up @@ -117,7 +129,7 @@ function tie(element: Element, config: Options): Knot[] {
if (nth !== undefined) {
level.push({
name: `*:nth-child(${nth})`,
penalty: 5,
penalty: 9,
})
}

Expand Down

0 comments on commit 5006933

Please sign in to comment.