Skip to content

Commit

Permalink
refactor: golf
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed May 1, 2024
1 parent 19f7d5b commit e985a80
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 35 deletions.
7 changes: 1 addition & 6 deletions src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,7 @@ function renderComponent(component) {
newVNode,
oldVNode,
component._globalContext,
component._parentDom.namespaceURI == 'http://www.w3.org/2000/svg'
? 2
: component._parentDom.namespaceURI ==
'http://www.w3.org/1998/Math/MathML'
? 3
: 1,
component._parentDom.namespaceURI,
oldVNode._flags & MODE_HYDRATE ? [oldDom] : null,
commitQueue,
oldDom == null ? getDomSibling(oldVNode) : oldDom,
Expand Down
2 changes: 1 addition & 1 deletion src/diff/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { getDomSibling } from '../component';
* diff'ed against newParentVNode
* @param {object} globalContext The current context object - modified by
* getChildContext
* @param {ElementNamespace} namespace Current namespace of the DOM node (HTML, SVG, or MathML)
* @param {string} namespace Current namespace of the DOM node (HTML, SVG, or MathML)
* @param {Array<PreactElement>} excessDomChildren
* @param {Array<Component>} commitQueue List of components which have callbacks
* to invoke in commitRoot
Expand Down
29 changes: 14 additions & 15 deletions src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import options from '../options';
* @param {VNode} oldVNode The old virtual node
* @param {object} globalContext The current context object. Modified by
* getChildContext
* @param {ElementNamespace} namespace Current namespace of the DOM node (HTML, SVG, or MathML)
* @param {string} namespace Current namespace of the DOM node (HTML, SVG, or MathML)
* @param {Array<PreactElement>} excessDomChildren
* @param {Array<Component>} commitQueue List of components which have callbacks
* to invoke in commitRoot
Expand Down Expand Up @@ -341,7 +341,7 @@ export function commitRoot(commitQueue, root, refQueue) {
* @param {VNode} newVNode The new virtual node
* @param {VNode} oldVNode The old virtual node
* @param {object} globalContext The current context object
* @param {ElementNamespace} namespace Current namespace of the DOM node (HTML, SVG, or MathML)
* @param {string} namespace Current namespace of the DOM node (HTML, SVG, or MathML)
* @param {Array<PreactElement>} excessDomChildren
* @param {Array<Component>} commitQueue List of components which have callbacks
* to invoke in commitRoot
Expand Down Expand Up @@ -376,8 +376,10 @@ function diffElementNodes(
let checked;

// Tracks entering and exiting namespaces when descending through the tree.
if (nodeType === 'svg') namespace = 2;
else if (nodeType === 'math') namespace = 3;
if (nodeType === 'svg') namespace = 'http://www.w3.org/2000/svg';
else if (nodeType === 'math')
namespace = 'http://www.w3.org/1998/Math/MathML';
else namespace = 'http://www.w3.org/1999/xhtml';

if (excessDomChildren != null) {
for (i = 0; i < excessDomChildren.length; i++) {
Expand All @@ -403,16 +405,11 @@ function diffElementNodes(
return document.createTextNode(newProps);
}

if (namespace == 1) {
dom = document.createElement(nodeType, newProps.is && newProps);
} else {
dom = document.createElementNS(
namespace == 2
? 'http://www.w3.org/2000/svg'
: 'http://www.w3.org/1998/Math/MathML',
nodeType
);
}
dom = document.createElementNS(
namespace,
nodeType,
newProps.is && newProps
);

// we created a new parent, so none of the previously attached children can be reused:
excessDomChildren = null;
Expand Down Expand Up @@ -502,7 +499,9 @@ function diffElementNodes(
newVNode,
oldVNode,
globalContext,
nodeType === 'foreignObject' ? 1 : namespace,
nodeType === 'foreignObject'
? 'http://www.w3.org/1999/xhtml'
: namespace,
excessDomChildren,
commitQueue,
excessDomChildren
Expand Down
4 changes: 2 additions & 2 deletions src/diff/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ let eventClock = 0;
* @param {string} name The name of the property to set
* @param {*} value The value to set the property to
* @param {*} oldValue The old value the property had
* @param {ElementNamespace} namespace Whether or not this DOM node is an SVG node or not
* @param {string} namespace Whether or not this DOM node is an SVG node or not
*/
export function setProperty(dom, name, value, oldValue, namespace) {
let useCapture;
Expand Down Expand Up @@ -98,7 +98,7 @@ export function setProperty(dom, name, value, oldValue, namespace) {
);
}
} else {
if (namespace == 2) {
if (namespace == 'http://www.w3.org/2000/svg') {
// Normalize incorrect prop usage for SVG:
// - xlink:href / xlinkHref --> href (xlink:href was removed from SVG and isn't needed)
// - className --> class
Expand Down
6 changes: 0 additions & 6 deletions src/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ declare global {
useDebugvalue = 11
}

export enum ElementNamespace {
html = 1,
svg = 2,
mathml = 3
}

export interface DevSource {
fileName: string;
lineNumber: number;
Expand Down
6 changes: 1 addition & 5 deletions src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ export function render(vnode, parentDom, replaceNode) {
vnode,
oldVNode || EMPTY_OBJ,
EMPTY_OBJ,
parentDom.namespaceURI == 'http://www.w3.org/2000/svg'
? 2
: parentDom.namespaceURI == 'http://www.w3.org/1998/Math/MathML'
? 3
: 1,
parentDom.namespaceURI,
!isHydrating && replaceNode
? [replaceNode]
: oldVNode
Expand Down

0 comments on commit e985a80

Please sign in to comment.