-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: better selection #396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -263,7 +263,12 @@ export const BrowserWindow = () => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (getList === true && !listSelector) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setListSelector(highlighterData.selector); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let cleanedSelector = highlighterData.selector; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (cleanedSelector.includes('nth-child')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cleanedSelector = cleanedSelector.replace(/:nth-child\(\d+\)/g, ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+266
to
+270
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix unused cleaned selector. The cleaned selector is computed but never used. The original selector with nth-child is still being set as the listSelector, which defeats the purpose of cleaning the selector. Apply this diff to fix the issue: let cleanedSelector = highlighterData.selector;
if (cleanedSelector.includes('nth-child')) {
cleanedSelector = cleanedSelector.replace(/:nth-child\(\d+\)/g, '');
}
-
- setListSelector(highlighterData.selector);
+ setListSelector(cleanedSelector);
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setListSelector(cleanedSelector); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| notify(`info`, t('browser_window.attribute_modal.notifications.list_select_success')); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setCurrentListId(Date.now()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setFields({}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -275,13 +280,25 @@ export const BrowserWindow = () => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Add fields to the list | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (options.length === 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const attribute = options[0].value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let currentSelector = highlighterData.selector; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (currentSelector.includes('>')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const [firstPart, ...restParts] = currentSelector.split('>').map(p => p.trim()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const listSelectorRightPart = listSelector.split('>').pop()?.trim().replace(/:nth-child\(\d+\)/g, ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (firstPart.includes('nth-child') && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| firstPart.replace(/:nth-child\(\d+\)/g, '') === listSelectorRightPart) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| currentSelector = `${firstPart.replace(/:nth-child\(\d+\)/g, '')} > ${restParts.join(' > ')}`; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+283
to
+294
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve robustness and readability of child selector handling. The current implementation has potential issues:
Apply this diff to improve the code: let currentSelector = highlighterData.selector;
if (currentSelector.includes('>')) {
const [firstPart, ...restParts] = currentSelector.split('>').map(p => p.trim());
- const listSelectorRightPart = listSelector.split('>').pop()?.trim().replace(/:nth-child\(\d+\)/g, '');
+ // Extract the rightmost part of the list selector (if it contains '>')
+ const listSelectorParts = listSelector.split('>');
+ const listSelectorRightPart = listSelectorParts.length > 1
+ ? listSelectorParts.pop()?.trim().replace(/:nth-child\(\d+\)/g, '')
+ : listSelector.replace(/:nth-child\(\d+\)/g, '');
+ // Only clean nth-child if the base selectors match
if (firstPart.includes('nth-child') &&
- firstPart.replace(/:nth-child\(\d+\)/g, '') === listSelectorRightPart) {
+ listSelectorRightPart &&
+ firstPart.replace(/:nth-child\(\d+\)/g, '') === listSelectorRightPart) {
currentSelector = `${firstPart.replace(/:nth-child\(\d+\)/g, '')} > ${restParts.join(' > ')}`;
}
}📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const newField: TextStep = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| id: Date.now(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: 'text', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| label: `Label ${Object.keys(fields).length + 1}`, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| data: data, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| selectorObj: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| selector: highlighterData.selector, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| selector: currentSelector, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tag: highlighterData.elementInfo?.tagName, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| shadow: highlighterData.elementInfo?.isShadowRoot, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| attribute | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Refactor duplicated selector generation logic into a shared function.
The identical sibling handling logic is duplicated in three locations. This violates the DRY principle and makes maintenance more difficult.
Consider extracting the common logic into a shared utility function:
Also applies to: 1922-1946, 2078-2102