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 way to set documentURI on Document #111

Open
henningko opened this issue Jun 25, 2022 · 6 comments
Open

Add way to set documentURI on Document #111

henningko opened this issue Jun 25, 2022 · 6 comments

Comments

@henningko
Copy link

Referencing #68, there currently is no way to set the documentURI on Document (MDN Docs), which is required by modules such as @mozilla/readability.

JSDOM does this via an url parameter, passed into the constructor with the options:

const dom = new JSDOM(``, {
  url: "https://example.org/",
  referrer: "https://example.com/",
  contentType: "text/html",
  includeNodeLocations: true,
  storageQuota: 10000000
});

url sets the value returned by window.location, document.URL, and document.documentURI, and affects things like resolution of relative URLs within the document and the same-origin restrictions and referrer used while fetching subresources. It defaults to "about:blank".
see JSDOM

@sno2
Copy link
Contributor

sno2 commented Jun 25, 2022

Deno allows setting the window.location via a command-line argument. Would it be best to have these properties dynamically loaded if you access Node.baseURI, Document.documentURI, Document.baseURI, and Document.URL they would be dynamically computed off of window.location which would make Deno ask you to provide a --location parameter? Although, this wouldn't really allow for fine-grained control at runtime so it might still be necessary to have a runtime API for this for those few number of libraries and users that need it.

@b-fuze
Copy link
Owner

b-fuze commented Jun 26, 2022

I'm thinking of doing something like

import { denoDom, DOMParser } from "https://deno.land/x/deno_dom/deno-dom-wasm.ts";
const doc = new DOMParser().parseFromString("<h1>foo</h1>", "text/html");

denoDom.setDocumentMetadata(doc, {
  uri: "https://example.org/example.html",
  referrer: "https://example.com/",
});

console.log(doc.documentURI); // prints "https://example.org/example.html"
console.log(doc.referrer); // prints "https://example.com/"

I'd like to avoid extending the actual DOM interface if possible

@henningko
Copy link
Author

henningko commented Jun 27, 2022

Yes, that looks good. Wonder if you might want to include options parameter in parseFromString:

const doc = new DOMParser().parseFromString("<h1>foo</h1>", {
    encoding: "text/html",
    uri: "https://example.org/example.html",
    referrer: "https://example.com/",
});

Great work, appreciate your responsiveness :-)

@b-fuze
Copy link
Owner

b-fuze commented Jun 27, 2022

Yeah, I'd rather avoid extending the actual DOM API. What I think we can do to make it nicer is return the document from setDocumentMetadata. Something like:

import { denoDom, DOMParser } from "https://deno.land/x/deno_dom/deno-dom-wasm.ts";

const doc = denoDom.setDocumentMetadata(
  new DOMParser().parseFromString("<h1>foo</h1>", "text/html"),
  {
    uri: "https://example.org/example.html",
    referrer: "https://example.com/",
  },
);

console.log(doc.documentURI); // prints "https://example.org/example.html"
console.log(doc.referrer); // prints "https://example.com/"

@guy-borderless
Copy link

I want to take a shot at fixing this. Can anyone point me where to look?

@b-fuze
Copy link
Owner

b-fuze commented Jun 5, 2024

The document implementation is here and you can another file for that denoDom export probably... I'd like to make that a separate export though, so maybe something like

import { setDocumentMetadata } from "jsr:@b-fuze/deno-dom/util";

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

4 participants