Skip to content

Commit

Permalink
fix(lib): fix error patching children of an already replaced element
Browse files Browse the repository at this point in the history
  • Loading branch information
Masquerade-Circus committed Jul 29, 2021
1 parent 26e282f commit 3ce399d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 36 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
### [5.0.12](https://github.com/Masquerade-Circus/valyrian.js/compare/5.0.11...5.0.12) (2021-07-28)


### Code Refactoring

* Refactor 2021 06 14 ([#20](https://github.com/Masquerade-Circus/valyrian.js/issues/20)) ([ceda5ea](https://github.com/Masquerade-Circus/valyrian.js/commit/ceda5ea1f8561983dd3e680a4d9a00e56f3b5292))
* Refactor 2021 06 14 ([#20](https://github.com/Masquerade-Circus/valyrian.js/issues/20)) ([ceda5ea](https://github.com/Masquerade-Circus/valyrian.js/commit/ceda5ea1f8561983dd3e680a4d9a00e56f3b5292))

### [5.0.11](https://github.com/Masquerade-Circus/valyrian.js/compare/5.0.10...5.0.11) (2021-05-01)

Expand Down
4 changes: 1 addition & 3 deletions dist/@types/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ interface Valyrian {
}
declare let isNode: boolean;
declare function createElement(tagName: string, isSVG?: boolean): DomElement;
declare function domToVnode(dom: DomElement): Vnode & {
dom: DomElement;
};
declare function domToVnode(dom: DomElement): Vnode;
declare const trust: (htmlString: string) => Vnode[];
declare function valyrian(): Valyrian;
2 changes: 1 addition & 1 deletion dist/valyrian.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/valyrian.min.js.map

Large diffs are not rendered by default.

45 changes: 16 additions & 29 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,36 +124,23 @@ function createElement(tagName: string, isSVG: boolean = false): DomElement {
}

// Transforms a DOM node to a VNode
function domToVnode(dom: DomElement): Vnode & { dom: DomElement } {
const el = dom;
const nodeName = el.nodeName.toLowerCase();
const props: Props = {};
const children: VnodeOrUnknown[] = [];
let i: number;
let childNode;
let attr: Attr;

// attributes
for (i = 0; i < el.attributes.length; i++) {
attr = el.attributes[i];
props[attr.nodeName] = attr.nodeValue;
}

// children
for (i = 0; i < el.childNodes.length; i++) {
childNode = el.childNodes[i];
if (childNode.nodeType === 1) {
children.push(domToVnode(childNode as DomElement));
} else if (childNode.nodeType === 3) {
let textVnode = new TextVnode(childNode.nodeValue || "");
textVnode.dom = childNode as Text;
children.push(textVnode);
function domToVnode(dom: DomElement): Vnode {
let props: Props = {};
[].forEach.call(dom.attributes, (prop: Attr) => (props[prop.nodeName] = prop.nodeValue));

let vnode: Vnode = new Vnode(dom.nodeName.toLowerCase(), props, []);
vnode.dom = dom;

for (let i = 0, l = dom.childNodes.length; i < l; i++) {
if (dom.childNodes[i].nodeType === 1) {
vnode.children.push(domToVnode(dom.childNodes[i] as DomElement));
} else if (dom.childNodes[i].nodeType === 3) {
let textVnode = new TextVnode(dom.childNodes[i].nodeValue || "");
textVnode.dom = dom.childNodes[i] as unknown as Text;
vnode.children.push(textVnode);
}
}

let newNode = new Vnode(nodeName, props, children);
newNode.dom = dom;
return newNode as Vnode & { dom: DomElement };
return vnode;
}

const trust = (htmlString: string) => {
Expand Down Expand Up @@ -447,7 +434,7 @@ function valyrian(): Valyrian {
childVnode.props.oncreate && childVnode.props.oncreate(childVnode);
oldChildVnode instanceof Vnode && callRemove(oldChildVnode);
newParentVnode.dom.replaceChild(childVnode.dom, oldChildVnode.dom);
patch(childVnode as Vnode & { dom: DomElement }, oldChildVnode);
patch(childVnode as Vnode & { dom: DomElement });
}
} else {
if (oldChildVnode instanceof Vnode) {
Expand Down

0 comments on commit 3ce399d

Please sign in to comment.