WebReflection / uhtml

A micro HTML/SVG render

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

V4 crashes on empty array

gbishop opened this issue · comments

My grid component can sometimes be empty. The simplified version of the template is

    const body = html`<div style=${styleString(style)}>${result}</div>`;

Where result is an array of Holes. If that array happens to be empty, I get a crash with the message

Error: Failed to execute 'setEndAfter' on 'Range': the given Node has no parent. rendering Page UI

I can easily work around by making sure it isn't empty. This template is used to render different grids on various tabs in the interface.

In your example nothing is an array so I don’t get it as arrays are working and code covered 100%

That case array empty then filled then empty again is also covered so please write a minimal example of your issue or I won’t know where to look at, thank you

I'll work on boiling it down to something you can debug on.

Here is a small example that illustrates the issue. Click on any of the buttons to see it fail. It dies on the second render.

import { html, render } from "uhtml";

function grid(n) {
  const cells = [];
  for (let i = 0; i < n; i++) {
    cells.push(cell(i));
  }
  const grid = html`<div class="grid">${cells}</div>`;
  return grid;
}

function cell(i) {
  const content = [html`<span>C</span>`, html`<span>${i}</span>`];
  return html`<button>${content}</button>`;
}

let n1 = 1;
let n2 = 0;

function page() {
  return html`<div
    class="page"
    onclick=${() => {
      n1 = 0;
      n2 = 1;
      draw();
    }}
  >
    ${grid(n1)} ${grid(n2)}
  </div> `;
}

function draw() {
  render(document.body, page());
}

draw();

this is odd ... I have tests around [] as new hole content but I'll surely have a look, thank you for the minimal example!

The key (I think) is that the buttons contain arrays. I couldn't get it to fail until I added that. So this is an array of holes that themselves contain arrays.

it's posible this comes from v3 too then, otherwise it's a regression.

I updated the example above replacing the large number of buttons with 1 in each case. That is sufficient to trigger the bug and likely easier to examine the internals.

Fixed ... I was too smart with that fast path ... of course fragments would break the world with previous logic ...

Thanks. Working now.