mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
8e018fbbda
The code converting html to a vnode did not handle comment nodes as expected. closes #761
35 lines
921 B
TypeScript
35 lines
921 B
TypeScript
import { VNode, h, addNS } from "./vdom";
|
|
|
|
const parser = new DOMParser();
|
|
|
|
export function htmlToVDOM(html: string): VNode[] {
|
|
const doc = parser.parseFromString(html, "text/html");
|
|
const result: VNode[] = [];
|
|
for (let child of doc.body.childNodes) {
|
|
result.push(htmlToVNode(child));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
function htmlToVNode(node: ChildNode): VNode {
|
|
if (!(node instanceof Element)) {
|
|
if (node instanceof Comment) {
|
|
return h("!", node.textContent);
|
|
}
|
|
return { text: node.textContent! } as VNode;
|
|
}
|
|
const attrs = {};
|
|
for (let attr of node.attributes) {
|
|
attrs[attr.name] = attr.textContent;
|
|
}
|
|
const children: VNode[] = [];
|
|
for (let c of node.childNodes) {
|
|
children.push(htmlToVNode(c));
|
|
}
|
|
const vnode = h((node as Element).tagName, { attrs }, children);
|
|
if (vnode.sel === "svg") {
|
|
addNS(vnode.data, (vnode as any).children, vnode.sel);
|
|
}
|
|
return vnode;
|
|
}
|