WebReflection / uhtml

A micro HTML/SVG render

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

node is not instanceof HTMLElement when imported from uhtml v3

dbauszus-glx opened this issue · comments

I upgraded uhtml to v3 in my package.json and then build the library using esbuild.

I import uhtml like so but nodes from tagged html.node are not instanceof HTMLElement. This printed true when I install uhtml@2.8

import {render, html, svg} from 'uhtml'

let node = html.node`<div>Foo`

console.log(node instanceof HTMLElement) //false

Oddly though I tried to test this in a codepen and everything worked as expected.

uhtm@2.8 as well as 3.0 both return the node as instanceof HTMLElement.

// import { html } from "https://unpkg.com/uhtml@2.8.0/esm/index.js?module";

import { html } from "https://unpkg.com/uhtml@3.0.0/esm/index.js?module";

let node = html.node`<div>Foo`

console.log(node instanceof HTMLElement) //true

Not really sure what I am doing wrong or how to better decribe the issue.

Anyways. Not a biggie since we are super happy with uhtml@2.8 but maybe something we can look into at our side.

So … the bug is about code that works as expected? I’ve no idea how to help here.

The parser is different and it might be the culprit … try older uparser with uhtml 3 or just stick with uhtml 2 until you understand what’s failing and how I can reproduce it?

We ran into the same issue. The html.node function does not return an HTMLElement instance in our codebase when we import it from NPM and bundle it with webpack. However, if we load the package from a CDN, it does. The version is 3.0.0 in both cases.

We did a little more deep-dive:
It seems like, that if the template for the html.node function starts with an empty line it will return a #document-fragment instead of an HTMLElement.

Please check out this code to reproduce:

import { html } from "https://cdn.jsdelivr.net/npm/uhtml@3.0.0/esm.js";

const elementA = html.node`<div>foo</div>`;
const elementB = html.node`
  <div>bar</div>
`;

console.log(elementA instanceof HTMLElement);
console.log(elementB instanceof HTMLElement);

It has nothing to do with importing from NPM or a CDN, it's just that we had a different template in our production code.

I can confirm this. I just double checked my production code.

Unfortunately we use the empty line a lot at the beginning of tagged templates. Following syntax is quite common in our project:

      mapp.utils.render(mapview.attribution.target,
        mapp.utils.html`${Object.entries(o)
          .map(entry => mapp.utils.html`
              <a class="background-fade" target="_blank" href="${entry[1]}">${entry[0]}`)}`)

It was a uparser issue which has now been fixed (and covered).

Apologies for the inconvenient.

Thanks for the quick fix, @WebReflection <3