Skip to content

Commit 1dc7621

Browse files
committed
Fix #106 - uhtml/node features parity with the rest
1 parent 4272c7c commit 1dc7621

4 files changed

Lines changed: 44 additions & 23 deletions

File tree

esm/node.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
11
/*! (c) Andrea Giammarchi - MIT */
2+
import { Hole, unroll } from './rabbit.js';
23
import { attr } from './handler.js';
3-
4-
import create from './creator.js';
5-
import parser from './parser.js';
6-
import render from './render/node.js';
4+
import { cache } from './literals.js';
5+
import { empty } from './utils.js';
76

87
/** @typedef {import("./literals.js").DOMValue} DOMValue */
98
/** @typedef {import("./literals.js").Target} Target */
109

11-
const tag = svg => {
12-
const parse = create(parser(svg));
13-
return (template, ...values) => parse(template, values).n;
14-
};
10+
const tag = svg => (template, ...values) => unroll(
11+
cache(empty),
12+
new Hole(svg, template, values)
13+
);
1514

1615
/** @type {(template: TemplateStringsArray, ...values:DOMValue[]) => Target} A tag to render HTML content. */
1716
const html = tag(false);
1817

1918
/** @type {(template: TemplateStringsArray, ...values:DOMValue[]) => Target} A tag to render SVG content. */
2019
const svg = tag(true);
2120

21+
/**
22+
* Render directly within a generic container.
23+
* @template T
24+
* @param {T} where the DOM node where to render content
25+
* @param {(() => Target) | Target} what the node to render
26+
* @returns
27+
*/
28+
const render = (where, what) => {
29+
where.replaceChildren(
30+
(typeof what === 'function' ? what() : what).valueOf()
31+
);
32+
return where;
33+
};
34+
2235
export { render, html, svg, attr };

esm/render/node.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

test/modern.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const assert = (real, expected, message = `expected ${expected} got ${real}`) =>
99
if (typeof document === 'undefined') {
1010
globalThis.document = (new DOMParser).parseFromString('...', 'text/html');
1111
globalThis.HTMLElement = document.createElement('e').constructor;
12+
globalThis.DocumentFragment = document.createDocumentFragment().constructor;
1213
}
1314

1415
Object.defineProperty(
@@ -203,3 +204,11 @@ assert(
203204
document.body.outerHTML,
204205
'<body><!--<>-->ab<!--[2]--><!--</>--></body>'
205206
);
207+
208+
import('../node.js').then(({ render, html }) => {
209+
render(document.head, html`<style>${'body{font-family:sans-serif;}'}</style>`);
210+
assert(
211+
document.head.outerHTML,
212+
'<head><style>body{font-family:sans-serif;}</style></head>'
213+
);
214+
});

test/node.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width,initial-scale=1.0">
6+
<title>uhtml</title>
7+
<script type="module">
8+
import { html } from '../node.js';
9+
var cssText = "p { font-weight: bold; }";
10+
var result = html`<style>${cssText}</style>`;
11+
console.log(result);
12+
</script>
13+
</head>
14+
</html>

0 commit comments

Comments
 (0)