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 #3700

Open
aralroca opened this issue Apr 2, 2024 · 0 comments
Open

doc.write is not working as browsers #3700

aralroca opened this issue Apr 2, 2024 · 0 comments

Comments

@aralroca
Copy link

aralroca commented Apr 2, 2024

Related with #3699

Basic info:

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

Minimal reproduction case

In Node:

  1. Install jsdom
  2. Run this code
const { JSDOM } = require("jsdom");

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

(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)
  }
})();

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?

To reproduce in browsers:

  1. Open devtools
  2. Paste 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 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>
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

1 participant