Skip to content

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

@robinmetral

Description

@robinmetral

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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions