Skip to content

Commit

Permalink
Merge pull request #2942 from preactjs/golf-dom-diffing
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister authored Jan 20, 2021
2 parents 7dcc323 + 322d471 commit 8d3bbec
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,10 @@ function diffElementNodes(
let i;
let oldProps = oldVNode.props;
let newProps = newVNode.props;
let nodeType = newVNode.type;

// Tracks entering and exiting SVG namespace when descending through the tree.
isSvg = newVNode.type === 'svg' || isSvg;
if (nodeType === 'svg') isSvg = true;

if (excessDomChildren != null) {
for (i = 0; i < excessDomChildren.length; i++) {
Expand All @@ -314,13 +315,10 @@ function diffElementNodes(
// if newVNode matches an element in excessDomChildren or the `dom`
// argument matches an element in excessDomChildren, remove it from
// excessDomChildren so it isn't later removed in diffChildren
if (
child != null &&
((newVNode.type === null
? child.nodeType === 3
: child.localName === newVNode.type) ||
dom == child)
) {
//
// Note: This takes advantage of Text nodes having `.localName=undefined`,
// which is loosely equal to Text VNodes' `.type=null`. Elements use string equality.
if (child != null && (dom == child || child.localName == nodeType)) {
dom = child;
excessDomChildren[i] = null;
break;
Expand All @@ -329,7 +327,7 @@ function diffElementNodes(
}

if (dom == null) {
if (newVNode.type === null) {
if (nodeType === null) {
// @ts-ignore createTextNode returns Text, we expect PreactElement
return document.createTextNode(newProps);
}
Expand All @@ -338,12 +336,12 @@ function diffElementNodes(
dom = document.createElementNS(
'http://www.w3.org/2000/svg',
// @ts-ignore We know `newVNode.type` is a string
newVNode.type
nodeType
);
} else {
dom = document.createElement(
// @ts-ignore We know `newVNode.type` is a string
newVNode.type,
nodeType,
newProps.is && newProps
);
}
Expand All @@ -354,7 +352,7 @@ function diffElementNodes(
isHydrating = false;
}

if (newVNode.type === null) {
if (nodeType === null) {
// During hydration, we still have to split merged text from SSR'd HTML.
if (oldProps !== newProps && (!isHydrating || dom.data !== newProps)) {
dom.data = newProps;
Expand Down Expand Up @@ -406,7 +404,7 @@ function diffElementNodes(
newVNode,
oldVNode,
globalContext,
newVNode.type === 'foreignObject' ? false : isSvg,
isSvg && nodeType !== 'foreignObject',
excessDomChildren,
commitQueue,
dom.firstChild,
Expand All @@ -430,7 +428,7 @@ function diffElementNodes(
// despite the attribute not being present. When the attribute
// is missing the progress bar is treated as indeterminate.
// To fix that we'll always update it when it is 0 for progress elements
(i !== dom.value || (newVNode.type === 'progress' && !i))
(i !== dom.value || (nodeType === 'progress' && !i))
) {
setProperty(dom, 'value', i, oldProps.value, false);
}
Expand Down

0 comments on commit 8d3bbec

Please sign in to comment.