Skip to content

Commit

Permalink
refactor(exports): dry
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenybai committed May 20, 2022
1 parent ee28b23 commit debb89e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/jsx-runtime/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from '../million/vnode';
export { h } from '../million/vnode';
export * from './jsx';
export * from './types';
2 changes: 1 addition & 1 deletion src/million/drivers/useNode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fromDomNodeToVNode } from '../../utils/convert';
import { fromDomNodeToVNode } from '../../utils';
import { createElement } from '../createElement';
import { createEffectQueuer } from '../effect';
import {
Expand Down
1 change: 0 additions & 1 deletion src/ssr/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from '../utils';
export * from './refresh';
export * from './renderToString';
29 changes: 0 additions & 29 deletions src/utils/convert.ts

This file was deleted.

31 changes: 29 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
export * from './convert';
export * from '../million/vnode';
import { DOMNode, OLD_VNODE_FIELD, VNode, VProps, Flags } from '../million/types';
import { h } from '../million/vnode';

export const fromDomNodeToVNode = (el: DOMNode): VNode | undefined => {
if (el[OLD_VNODE_FIELD]) return el[OLD_VNODE_FIELD];
if (el instanceof Text) return String(el.nodeValue);
if (el instanceof Comment) return undefined;

const props: VProps = {};
// We know children length, so we created a fixed array
const children = new Array(el.children.length).fill(0);
for (let i = 0; i < el.attributes.length; i++) {
const { nodeName, nodeValue } = el.attributes[i];
props[nodeName] = nodeName === 'million-flag' ? Flags[nodeName] : nodeValue;
}
for (let i = 0; i < el.childNodes.length; i++) {
children[i] = fromDomNodeToVNode(el.childNodes.item(i) as DOMNode);
}

const vnode = h(el.tagName.toLowerCase(), props, ...children);
el[OLD_VNODE_FIELD] = vnode;
return vnode;
};

export const fromStringToDomNode = (html: string): DOMNode => {
const doc = new DOMParser().parseFromString(`<t>${html.trim()}</t>`, 'text/xml');
const el = doc.firstChild!.firstChild! as DOMNode;
return el;
};

0 comments on commit debb89e

Please sign in to comment.