You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
2.1 KiB
60 lines
2.1 KiB
import { addNS } from "./h";
|
|
import { vnode } from "./vnode";
|
|
import { htmlDomApi } from "./htmldomapi";
|
|
export function toVNode(node, domApi) {
|
|
const api = domApi !== undefined ? domApi : htmlDomApi;
|
|
let text;
|
|
if (api.isElement(node)) {
|
|
const id = node.id ? "#" + node.id : "";
|
|
const cn = node.getAttribute("class");
|
|
const c = cn ? "." + cn.split(" ").join(".") : "";
|
|
const sel = api.tagName(node).toLowerCase() + id + c;
|
|
const attrs = {};
|
|
const dataset = {};
|
|
const data = {};
|
|
const children = [];
|
|
let name;
|
|
let i, n;
|
|
const elmAttrs = node.attributes;
|
|
const elmChildren = node.childNodes;
|
|
for (i = 0, n = elmAttrs.length; i < n; i++) {
|
|
name = elmAttrs[i].nodeName;
|
|
if (name[0] === "d" &&
|
|
name[1] === "a" &&
|
|
name[2] === "t" &&
|
|
name[3] === "a" &&
|
|
name[4] === "-") {
|
|
dataset[name.slice(5)] = elmAttrs[i].nodeValue || "";
|
|
}
|
|
else if (name !== "id" && name !== "class") {
|
|
attrs[name] = elmAttrs[i].nodeValue;
|
|
}
|
|
}
|
|
for (i = 0, n = elmChildren.length; i < n; i++) {
|
|
children.push(toVNode(elmChildren[i], domApi));
|
|
}
|
|
if (Object.keys(attrs).length > 0)
|
|
data.attrs = attrs;
|
|
if (Object.keys(dataset).length > 0)
|
|
data.dataset = dataset;
|
|
if (sel[0] === "s" &&
|
|
sel[1] === "v" &&
|
|
sel[2] === "g" &&
|
|
(sel.length === 3 || sel[3] === "." || sel[3] === "#")) {
|
|
addNS(data, children, sel);
|
|
}
|
|
return vnode(sel, data, children, undefined, node);
|
|
}
|
|
else if (api.isText(node)) {
|
|
text = api.getTextContent(node);
|
|
return vnode(undefined, undefined, undefined, text, node);
|
|
}
|
|
else if (api.isComment(node)) {
|
|
text = api.getTextContent(node);
|
|
return vnode("!", {}, [], text, node);
|
|
}
|
|
else {
|
|
return vnode("", {}, [], undefined, node);
|
|
}
|
|
}
|
|
//# sourceMappingURL=tovnode.js.map
|