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

[Bug] SelectAll changes array of elements passed to it by reference #847

Open
IAkumaI opened this issue Aug 10, 2022 · 2 comments
Open

[Bug] SelectAll changes array of elements passed to it by reference #847

IAkumaI opened this issue Aug 10, 2022 · 2 comments

Comments

@IAkumaI
Copy link

IAkumaI commented Aug 10, 2022

First of, select two elements. First - any. Second - child of the first element.
Then, just run SelectAll on this array.
Array (as any of js objects) passed by reference and css-select changes it somewhere.

Example:

const CSSselect = require('css-select');
const {parseDocument} = require('htmlparser2');

const dom = parseDocument('<div class="root"><div class="product"><h1></h1></div></div>');
const elements = CSSselect.selectAll('h1, .product', dom);
console.log(elements.length); // elements.length === 2
// elements contains both h1 AND .product (which are parent of h1)

CSSselect.selectAll('no matter', elements);
console.log(elements.length); // elements.length become 1
// elements contains only .product
@IAkumaI
Copy link
Author

IAkumaI commented Aug 10, 2022

As I found, this happens after removeSubset() call:

? adapter.removeSubsets(elems)
which refer to default adapter https://github.com/fb55/domutils/blob/d3e1af0f3b98c0e9f028443385760aaa068be624/src/helpers.ts#L11

But. Is there normal to change original array?

@fb55
Copy link
Owner

fb55 commented Sep 13, 2022

This works as expected, but should be documented. Thanks for raising the issue!

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