Skip to content
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

Inconsistent selectors in Chrome and Firefox #79

Open
qmegas opened this issue Nov 10, 2022 · 1 comment
Open

Inconsistent selectors in Chrome and Firefox #79

qmegas opened this issue Nov 10, 2022 · 1 comment

Comments

@qmegas
Copy link

qmegas commented Nov 10, 2022

I know that library not maintained anymore, but hope that some day author will fix the issue

Accidently I have discovered that in some cases Chrome and Firefox generate different optimal selectors for same element. After debugging I found the problem in findAttributesPattern function. The problem in the way Array.sort function works in different browsers.

In my case in that line

const sortedKeys = Object.keys(attributes).sort((curr, next) => {
attributes variable is

0: class
1: data-css
class: class
data-css: data-css
length: 2
[[Prototype]]: NamedNodeMap

Object.keys(attributes) returns identical array ['0', '1'], but when sort callback function called in Chrome values are curr = '1', next = '0', but in Firefox curr = '0', next = '1'. So, at the end following logic:

if (nextPos === -1) {
  if (currPos === -1) {
    return 0;
  }
  return -1;
}
return currPos - nextPos;

works differently and sortedKeys in Chrome will be ['1', '0'], but in Firefox ['0', '1'].

Additional information:

  • select function called with default settings, priority variable is
0: "id"
1: "class"
2: "href"
3: "src"
length: 4
@xiemms
Copy link

xiemms commented Nov 10, 2022 via email

qmegas added a commit to qmegas/optimal-select that referenced this issue Jul 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants