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

Add a baseURI option to resolve to arbitrary absolute URLs #961

Open
robinmetral opened this issue Mar 7, 2025 · 2 comments
Open

Add a baseURI option to resolve to arbitrary absolute URLs #961

robinmetral opened this issue Mar 7, 2025 · 2 comments

Comments

@robinmetral
Copy link
Contributor

I'm running into something similar to #671: relative URLs don't resolve to the expected absolute URL.

I use Readability from a WebExtension background script. The script fetch()es HTML resources and parses them into DOMs using the DOMParser browser API.

The resulting absolute URLs look something like moz-extension://<extension-base-url>/<relative-link-here>, since Readability gets the base URI from document.baseURI (which is window.location by default):

var baseURI = this._doc.baseURI;

In #671, getting the expected absolute URLs was achieved by passing JSDOM a url option. This is now well-documented on the README:

Remember to pass the page's URI as the `url` option in the `JSDOM` constructor (as shown in the example above), so that Readability can convert relative URLs for images, hyperlinks, etc. to their absolute counterparts.

However, DOMParser doesn't have a url option.

A workaround I've found is to inject a <base> element into the document's <head> before handing it over to Readability. This works because document.baseURI resolves to whatever is in <base>, only falling back to window.location if no <base> is found.

This works but feels a little bit brittle (it could break if Readability changes how it deals with relative URLs internally). Have we considered adding a baseURI option that, if explicitly set, overrides document.baseURI?

@robinmetral robinmetral changed the title Add a baseURI option to resolve to the expected absolute URLs Add a baseURI option to resolve to arbitrary absolute URLs Mar 7, 2025
@gijsk
Copy link
Contributor

gijsk commented Mar 25, 2025

This works but feels a little bit brittle (it could break if Readability changes how it deals with relative URLs internally). Have we considered adding a baseURI option that, if explicitly set, overrides document.baseURI?

I've not come across this usecase but the DOMParser scenario makes sense - it's a bit unfortunate the consumer cannot set a base URI. I'd take a patch? :-)

@robinmetral
Copy link
Contributor Author

I'll look at opening a PR for this then 🙂 it might take a little while

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants