WebReflection / heresy-ssr

🔥 heresy 🔥 Server Side Rendering
ISC License
88 stars 1 forks source link

Different ownerDocument #26

Closed CaptainJKoB closed 3 years ago

CaptainJKoB commented 3 years ago

Hello 👋

If you want to use multiple documents for rendering the ownerDocument property of the element rendered inside the document always points towards the document exported from heresy-ssr. Is there any way to point it towards the newly created document?

Thanks

Reproduction example:

const http = require('http');
const {Document, document, render, html, define} = require('heresy-ssr');

const doc = new Document;

define({
  name: 'TestElement',
  tagName: 'div',
  onconnected() {
    console.log('isNewDocument', this.ownerDocument === doc);
    console.log('isDefaultDocument', this.ownerDocument === document);
  },
  render() {
    this.html`Hello world!`;
  }
});

render(doc, html`<html><TestElement /></html>`);

console.log('newDocumentToString', doc.toString());
console.log('defaultDocumentToString', document.toString());

const server = http.createServer(function(req, res) {
    res.writeHead(200, {"content-type": "text/html;charset=utf-8"});
    res.end(doc.toString());
}).listen(8080, () => console.log("server running"))

Logging output:

isNewDocument false
isDefaultDocument true
newDocumentToString <!DOCTYPE html><html><div is="test-element">Hello world!</div></html>
defaultDocumentToString <!DOCTYPE html><html></html>
WebReflection commented 3 years ago

This is a very ugly limitation of basicHTML, which I am planning to deprecate soon and use linkedom instead, which never needs, nor use, global document or global window things (although here needed by heresy itself due lighterhtml needing it too).

A workaround is to set the element ownerDocument via the oninit() callback, but it's an ugly workaround indeed, although it should produce the expected result.

CaptainJKoB commented 3 years ago

Okey, thank you for the insight. I will try to workaroud it somehow.