Skip to content

Commit

Permalink
Move some frequently used things to constants (#4579)
Browse files Browse the repository at this point in the history
* Move some frequently used things to constants

* Try moving math to a constant despite single usage
  • Loading branch information
JoviDeCroock authored Nov 29, 2024
1 parent cfd2f41 commit 99e5e04
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export const MATCHED = 1 << 17;
/** Reset all mode flags */
export const RESET_MODE = ~(MODE_HYDRATE | MODE_SUSPENDED);

export const SVG_NAMESPACE = 'http://www.w3.org/2000/svg';
export const XHTML_NAMESPACE = 'http://www.w3.org/1999/xhtml';
export const MATH_NAMESPACE = 'http://www.w3.org/1998/Math/MathML';

export const UNDEFINED = undefined;
export const EMPTY_OBJ = /** @type {any} */ ({});
export const EMPTY_ARR = [];
Expand Down
16 changes: 8 additions & 8 deletions src/diff/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import {
EMPTY_OBJ,
MATH_NAMESPACE,
MODE_HYDRATE,
MODE_SUSPENDED,
RESET_MODE,
UNDEFINED
SVG_NAMESPACE,
UNDEFINED,
XHTML_NAMESPACE
} from '../constants';
import { BaseComponent, getDomSibling } from '../component';
import { Fragment } from '../create-element';
Expand Down Expand Up @@ -390,10 +393,9 @@ function diffElementNodes(
let checked;

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

if (excessDomChildren != null) {
for (i = 0; i < excessDomChildren.length; i++) {
Expand Down Expand Up @@ -516,9 +518,7 @@ function diffElementNodes(
newVNode,
oldVNode,
globalContext,
nodeType === 'foreignObject'
? 'http://www.w3.org/1999/xhtml'
: namespace,
nodeType === 'foreignObject' ? XHTML_NAMESPACE : namespace,
excessDomChildren,
commitQueue,
excessDomChildren
Expand Down
13 changes: 8 additions & 5 deletions src/diff/props.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IS_NON_DIMENSIONAL } from '../constants';
import { IS_NON_DIMENSIONAL, SVG_NAMESPACE } from '../constants';
import options from '../options';

function setStyle(style, key, value) {
Expand All @@ -13,6 +13,8 @@ function setStyle(style, key, value) {
}
}

const CAPTURE_REGEX = /(PointerCapture)$|Capture$/i;

// A logical clock to solve issues like https://github.com/preactjs/preact/issues/3927.
// When the DOM performs an event it leaves micro-ticks in between bubbling up which means that
// an event can trigger on a newly reated DOM-node while the event bubbles up.
Expand Down Expand Up @@ -64,8 +66,7 @@ export function setProperty(dom, name, value, oldValue, namespace) {
}
// Benchmark for comparison: https://esbench.com/bench/574c954bdb965b9a00965ac6
else if (name[0] === 'o' && name[1] === 'n') {
useCapture =
name !== (name = name.replace(/(PointerCapture)$|Capture$/i, '$1'));
useCapture = name !== (name = name.replace(CAPTURE_REGEX, '$1'));

// Infer correct casing for DOM built-in events:
if (
Expand Down Expand Up @@ -98,7 +99,7 @@ export function setProperty(dom, name, value, oldValue, namespace) {
);
}
} else {
if (namespace == 'http://www.w3.org/2000/svg') {
if (namespace == SVG_NAMESPACE) {
// Normalize incorrect prop usage for SVG:
// - xlink:href / xlinkHref --> href (xlink:href was removed from SVG and isn't needed)
// - className --> class
Expand Down Expand Up @@ -167,7 +168,9 @@ function createEventProxy(useCapture) {
return;
}
if (options.event) e = options.event(e);
return "handleEvent" in eventHandler ? eventHandler.handleEvent(e) : eventHandler(e);
return 'handleEvent' in eventHandler
? eventHandler.handleEvent(e)
: eventHandler(e);
}
};
}
Expand Down

0 comments on commit 99e5e04

Please sign in to comment.