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

doc.write is not working as browsers #3699

Closed
aralroca opened this issue Mar 31, 2024 · 7 comments
Closed

doc.write is not working as browsers #3699

aralroca opened this issue Mar 31, 2024 · 7 comments

Comments

@aralroca
Copy link

aralroca commented Mar 31, 2024

Basic info:

  • Node.js version: v21.6.2
  • jsdom version: 24.0.0

Minimal reproduction case

Run this code:

const doc = document.implementation.createHTMLDocument();
const encoder = new TextEncoder();
const decoder = new TextDecoder();
const readable = new ReadableStream({ start: (controller) => {
  controller.enqueue(encoder.encode('<div>'));
  controller.enqueue(encoder.encode('text a'));
  controller.enqueue(encoder.encode("text c"));
  controller.enqueue(encoder.encode("</div>"));
  controller.close();
}});
const reader = readable.getReader();

while(true) {
    const { done, value } = await reader.read();
    if(done) break
    doc.write(decoder.decode(value));
    console.log(doc.documentElement.outerHTML)
}

In JSDOM:

<html><head></head><body><div></div></body></html>
<html><head></head><body><div>text a</div></body></html>
<html><head></head><body><div>text c</div></body></html>

How does similar code behave in browsers?

In Chrome:

<html><head></head><body><div></div></body></html>
<html><head></head><body><div>text a</div></body></html>
<html><head></head><body><div>text atext c</div></body></html>

In Safari:

<html><head></head><body><div></div></body></html>
<html><head></head><body><div>text a</div></body></html>
<html><head></head><body><div>text atext c</div></body></html>

In Firefox:

<html><head></head><body><div></div></body></html>
<html><head></head><body><div>text a</div></body></html>
<html><head></head><body><div>text atext c</div></body></html>
@domenic
Copy link
Member

domenic commented Apr 1, 2024

This issue does not follow the issue template, and as such is not something we can help with.

If you edit it to do so, and leave a comment here, we can consider reopening.

@domenic domenic closed this as completed Apr 1, 2024
@aralroca
Copy link
Author

aralroca commented Apr 1, 2024

@domenic done. Please re-open the issue

@domenic
Copy link
Member

domenic commented Apr 2, 2024

This still does not follow the issue template, in particular this part:

<!--
Please create a minimal repro. Any reports involving third party libraries
will be closed, as we cannot debug third-party library interactions for you.

Please do not use syntax that is not supported in Node.js, such as JSX. If
we cannot run the code in Node.js, we will close the issue, as we cannot
debug whatever toolchain you are using.

TO BE CLEAR: your example needs to be self-contained enough that we can
copy-paste it into a file named `test.js`, and then run it using
`node test.js`. *No* Jest or similar.

IF YOU DO NOT FOLLOW THESE INSTRUCTIONS WE WILL CLOSE THE ISSUE.
-->

```js
const { JSDOM } = require("jsdom");

const options = {
  ... your options here ...
};
const dom = new JSDOM(`
  ... your HTML here ...
`, options);

... your code that reproduces the problem here, probably using dom.window ...
```

@aralroca
Copy link
Author

aralroca commented Apr 2, 2024

I already shared the code, you can copy&paste to devtools to see the browser behavior or inside a jsdom test. What do you want more?

@domenic
Copy link
Member

domenic commented Apr 2, 2024

I'll stop engaging after this response because this is exceeding the amount of time I've allocated to providing free support for people who aren't able to follow instructions.

When I paste the code you supplied into a file named test.js, and run it with node test.js, like the issue template requires, I get this:

$ node test.js
/home/domenic/test.js:14
    const { done, value } = await reader.read();
                            ^^^^^

SyntaxError: await is only valid in async functions and the top level bodies of modules
    at internalCompileFunction (node:internal/vm:77:18)
    at wrapSafe (node:internal/modules/cjs/loader:1290:20)
    at Module._compile (node:internal/modules/cjs/loader:1342:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1437:10)
    at Module.load (node:internal/modules/cjs/loader:1212:32)
    at Module._load (node:internal/modules/cjs/loader:1028:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12)
    at node:internal/main/run_main_module:28:49

Node.js v21.6.1

@aralroca
Copy link
Author

aralroca commented Apr 2, 2024

Let's see, I have put the version to work in the browsers as well so that it is easy to check it.

In jsom add it inside a test async.

+test('', async () => {
const doc = document.implementation.createHTMLDocument();
const encoder = new TextEncoder();
const decoder = new TextDecoder();
const readable = new ReadableStream({ start: (controller) => {
  controller.enqueue(encoder.encode('<div>'));
  controller.enqueue(encoder.encode('text a'));
  controller.enqueue(encoder.encode("text c"));
  controller.enqueue(encoder.encode("</div>"));
  controller.close();
}});
const reader = readable.getReader();

while(true) {
    const { done, value } = await reader.read();
    if(done) break
    doc.write(decoder.decode(value));
    console.log(doc.documentElement.outerHTML)
}
+})

However if you put it in devtools to see how it works in the browsers it will not work because they do not support "test" in the browsers. That's why I've put the code to work everywhere. Also to work with document you need to add:

const { JSDOM } = require("jsdom");

const dom = new JSDOM("<!DOCTYPE html><html><body></body></html>");
global.document = dom.window.document;
global.window = dom.window;

on top. You also need a package.json with jsdom version 24.0.0 as dependencies and is required to npm install before node test.js.

Do whatever you want, I have reported it to help the jsom library, I don't understand why I have been received so negatively.

@aralroca
Copy link
Author

aralroca commented Apr 2, 2024

I opened a new one with more details #3700 I hope this work

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