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

Can xmldom support querySelectorAll? #92

Closed
masx200 opened this issue Jul 16, 2020 · 15 comments
Closed

Can xmldom support querySelectorAll? #92

masx200 opened this issue Jul 16, 2020 · 15 comments
Labels
documentation Improvements or additions to documentation spec:DOM Living Standard https://dom.spec.whatwg.org/ wontfix This will not be worked on

Comments

@masx200
Copy link

masx200 commented Jul 16, 2020

support querySelectorAll

TypeError: document.querySelectorAll is not a function

@masx200
Copy link
Author

masx200 commented Jul 20, 2020

https://www.npmjs.com/package/jsdom

Use jsdom to solve the problem

@karfau
Copy link
Member

karfau commented Jul 25, 2020

In the current state of this library, I would consider it out of reach, potentially even for v1.0 since

Maybe it can be achieved by adding some dependency, but this also comes with an additional maintenance cost, since it currently doesn't have production dependencies.

So I think going for something like jsdom for that is a valid approach.

This is of course just my personal opinion.

@silverwind
Copy link

I'd say the project should stay zero-dependencies. I'd be happy if if there were a getElementsByClassName which should be sufficient for many use cases of querySelectorAll.

@morrisallison
Copy link

For those who can't use JSDOM, I was able to get my use case working using the query-selector package. Your mileage may vary.

import querySelector from "query-selector";
import { DOMParser } from "xmldom";

const doc = new DOMParser().parseFromString("<foo />", "text/xml");
const documentPrototype = Object.getPrototypeOf(doc);

documentPrototype.querySelectorAll = function querySelectorAll(selector) {
  return querySelector(selector, this);
};

@karfau
Copy link
Member

karfau commented Oct 31, 2020

I think this issue could already be closed.
We could of course add some lines in the docs (or in some example section?) to make it easier to find it, but the conclusion is that xmldom will not implement querySelectorAll itself any time soon.

What do you think @brodybits ?

@karfau karfau linked a pull request Jan 21, 2021 that will close this issue
@karfau karfau added the spec:DOM Living Standard https://dom.spec.whatwg.org/ label Jan 21, 2021
@karfau karfau closed this as completed Jan 21, 2021
@brodybits
Copy link
Member

It would be awesome if we could get this documented.

@karfau I may need a few days to get to the other recent threads on xmldom. Please remind me in case I forget.

@karfau karfau changed the title support querySelectorAll Can xmldom support querySelectorAll? Jan 21, 2021
@karfau karfau added the documentation Improvements or additions to documentation label Jan 21, 2021
@karfau
Copy link
Member

karfau commented Jan 21, 2021

I think it's a good idea to document this decision. It's also related to #176

@karfau karfau reopened this Jan 21, 2021
@karfau karfau added the wontfix This will not be worked on label Aug 23, 2021
@karfau
Copy link
Member

karfau commented Aug 23, 2021

Another round of trying to argue for closing this issue:

  1. It's currently not clear where to document this. (The readme should imo contain less documentation)
  2. The suggested workaround using query-selector is most likely not the only option. There has not been any update to that library in 2 years, which could mean it just works and is stable or it means it became inactive. Do we want to invest time to figure out the details and maintaining a recommendation?
  3. When people search this repo for querySelectorAll they will find this issue (even if it has been closed) including the work around(s) and be able to judge themselves.

Feel free to reopen, but please provide some arguments/proposals of how to solve the above points.

@karfau karfau closed this as completed Aug 23, 2021
@atava
Copy link

atava commented Nov 27, 2021

I feel like posting my use case.

I am using the cross-fetch library to implement isomorphic fetch in my app (i.e. relying on the browser fetch when running in the browser, using other code if running on node), and would have liked to proceed in the same way with regard to DOMParser. This library seemed to provide that up to the point of the querySelector methods, which are absent.

My app should be able to run the same code both in the browser and outside of it, hence my requirements.

Thanks anyway for clarifying this and for this library.

@karfau
Copy link
Member

karfau commented Nov 28, 2021

@atava thx for adding your use case.

There is thing I want to point out/clarify:
The code you write, that uses fetch is the same for node and browser, but using cross-fetch means that the implementation of fetch is not the same depending on the runtime (even though the code for the node version might be shipped when bundling).

I think it should be technically possible to create a cross-dom wrapper that would use xmldom and some other library for adding querySelector/querySelectorAll methods to the prototype and just rely on the native browser implementation when it is available.

But this is not something that the xmldom maintainers will do, since right now xmldom has quite some differences in behavior compared to the implementation of browsers, and with the current amount of resources it will take quite a while to align it, so it's most likely not a good idea.

So using the jsdom library to polyfill the native API when running your code with node, is the better option currently.

@atava
Copy link

atava commented Dec 3, 2021

@karfau Thanks, and sorry for replying so late.

To further elaborate on this, I would need an isomorphic fetch as well as isomorphic querySelector and querySelectorAll methods for my app as it does some Web site scraping and ideally it would need:

  • browser implementations of the methods for when it's run as a Web app (built with such frameworks as Svelte);
  • Node or "server" implementations of them for when it's run as a desktop app and also for testing.

Thanks for clarifying that this library could not and would not add a 1:1 implementation of querySelector & co.

@karfau
Copy link
Member

karfau commented Dec 23, 2021

Just found out about https://github.com/fb55/css-what which might be helpful if somebody wants to implement a node visitor based on it (outside of this repository).

@AlttiRi
Copy link

AlttiRi commented May 5, 2022

It would be great if DOMParser work same way as in modern browsers.

If parseFromString would return Document with body, head with textContent, innerHTML props querySelector, querySelectorAll methods.

In order to have the same code for Node.js and browsers.


Some examples:

let html = `
<style>a {color: red};</style>
<div style="display: none">qwe</div>
<div class="foo">asd <a href="#">[link]</a></div>
</div>`;
let doc = new DOMParser().parseFromString(html, "text/html");

doc.body.textContent:

"qwe
asd [link]
"

doc.body.innerHTML:

"<div style="display: none">qwe</div>
<div class="foo">asd <a href="#">[link]</a></div>
"

doc.querySelector("a").textContent:

"[link]"

@AlttiRi
Copy link

AlttiRi commented May 5, 2022

jsdom' DOMParser works fully as the browser's DOMParser.
But using jsdom just only for DOMParser is overkill, it's 10 MB of code, memory and computation wasting for unused functional.

Also they are not interested in this issue.
jsdom/jsdom#3364

@karfau
Copy link
Member

karfau commented May 6, 2022

There will not be any support for querySelector or querySelectorAll in this package without a major increase in maintainer capacity.
Which is the same reason why we can not possible have a goal to be feature complete with modern browsers.

And people already suggested above how this feature can be "added" on the fly using an external dependency.
As long as this package doesn't have any external dependencies, we will not add one just to support those functions.

But for adding body and/or some of it's properties, feel free to create a PR or create a new issue to discuss the details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation spec:DOM Living Standard https://dom.spec.whatwg.org/ wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

7 participants