function domToJSON(node) {
let obj = Object.create(null);
if (node.children.length === 0 && node.innerHTML) {
obj.content = node.innerHTML;
}
const counts = childCounts(node);
for (const child of node.children) {
const { localName } = child;
if (counts[localName] > 1) {
(obj[localName] = obj[localName] || []).push(domToJSON(child));
} else {
obj[localName] = domToJSON(child);
}
}
let attrs = node.attributes;
if (attrs) {
for (let i = attrs.length - 1; i >= 0; i--) {
obj[attrs[i].name] = attrs[i].value;
}
}
return obj;
}