Skip to content

Commit 401c8b2

Browse files
authored
chore: simplify internal DOM operations (#11741)
* chore: simplify internal DOM operations * chore: simplify internal DOM operations * chore: simplify internal DOM operations
1 parent 4411584 commit 401c8b2

File tree

4 files changed

+12
-91
lines changed

4 files changed

+12
-91
lines changed

packages/svelte/src/internal/client/dom/elements/class.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { hydrating } from '../hydration.js';
2-
import { set_class_name } from '../operations.js';
32

43
/**
54
* @param {SVGElement} dom
@@ -83,7 +82,7 @@ export function set_class(dom, value) {
8382
if (value == null) {
8483
dom.removeAttribute('class');
8584
} else {
86-
set_class_name(dom, next_class_name);
85+
dom.className = next_class_name;
8786
}
8887

8988
// @ts-expect-error need to add __className to patched prototype

packages/svelte/src/internal/client/dom/operations.js

Lines changed: 4 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { hydrate_anchor, hydrate_nodes, hydrating } from './hydration.js';
2-
import { get_descriptor } from '../utils.js';
32
import { DEV } from 'esm-env';
43
import { init_array_prototype_warnings } from '../dev/equality.js';
54

@@ -15,24 +14,6 @@ var element_prototype;
1514
/** @type {Text} */
1615
var text_prototype;
1716

18-
/** @type {typeof Node.prototype.appendChild} */
19-
var append_child_method;
20-
21-
/** @type {typeof Node.prototype.cloneNode} */
22-
var clone_node_method;
23-
24-
/** @type {(this: Node) => ChildNode | null} */
25-
var first_child_get;
26-
27-
/** @type {(this: Node) => ChildNode | null} */
28-
var next_sibling_get;
29-
30-
/** @type {(this: Node, text: string ) => void} */
31-
var text_content_set;
32-
33-
/** @type {(this: Element, class_name: string) => void} */
34-
var class_name_set;
35-
3617
// export these for reference in the compiled code, making global name deduplication unnecessary
3718
/**
3819
* @type {Window}
@@ -56,9 +37,6 @@ export function init_operations() {
5637
element_prototype = Element.prototype;
5738
text_prototype = Text.prototype;
5839

59-
append_child_method = node_prototype.appendChild;
60-
clone_node_method = node_prototype.cloneNode;
61-
6240
$window = window;
6341
$document = document;
6442

@@ -80,47 +58,6 @@ export function init_operations() {
8058

8159
init_array_prototype_warnings();
8260
}
83-
84-
first_child_get = /** @type {(this: Node) => ChildNode | null} */ (
85-
// @ts-ignore
86-
get_descriptor(node_prototype, 'firstChild').get
87-
);
88-
89-
next_sibling_get = /** @type {(this: Node) => ChildNode | null} */ (
90-
// @ts-ignore
91-
get_descriptor(node_prototype, 'nextSibling').get
92-
);
93-
94-
text_content_set = /** @type {(this: Node, text: string ) => void} */ (
95-
// @ts-ignore
96-
get_descriptor(node_prototype, 'textContent').set
97-
);
98-
99-
class_name_set = /** @type {(this: Element, class_name: string) => void} */ (
100-
// @ts-ignore
101-
get_descriptor(element_prototype, 'className').set
102-
);
103-
}
104-
105-
/**
106-
* @template {Element} E
107-
* @template {Node} T
108-
* @param {E} element
109-
* @param {T} child
110-
*/
111-
export function append_child(element, child) {
112-
append_child_method.call(element, child);
113-
}
114-
115-
/**
116-
* @template {Node} N
117-
* @param {N} node
118-
* @param {boolean} deep
119-
* @returns {N}
120-
*/
121-
/*#__NO_SIDE_EFFECTS__*/
122-
export function clone_node(node, deep) {
123-
return /** @type {N} */ (clone_node_method.call(node, deep));
12461
}
12562

12663
/** @returns {Text} */
@@ -135,7 +72,7 @@ export function empty() {
13572
*/
13673
/*#__NO_SIDE_EFFECTS__*/
13774
export function child(node) {
138-
const child = first_child_get.call(node);
75+
const child = node.firstChild;
13976
if (!hydrating) return child;
14077

14178
// Child can be null if we have an element with a single child, like `<p>{text}</p>`, where `text` is empty
@@ -155,7 +92,7 @@ export function child(node) {
15592
export function first_child(fragment, is_text) {
15693
if (!hydrating) {
15794
// when not hydrating, `fragment` is a `DocumentFragment` (the result of calling `open_frag`)
158-
return first_child_get.call(/** @type {DocumentFragment} */ (fragment));
95+
return /** @type {DocumentFragment} */ (fragment).firstChild;
15996
}
16097

16198
// when we _are_ hydrating, `fragment` is an array of nodes
@@ -181,7 +118,7 @@ export function first_child(fragment, is_text) {
181118
*/
182119
/*#__NO_SIDE_EFFECTS__*/
183120
export function sibling(node, is_text = false) {
184-
const next_sibling = next_sibling_get.call(node);
121+
const next_sibling = node.nextSibling;
185122

186123
if (!hydrating) {
187124
return next_sibling;
@@ -205,23 +142,13 @@ export function sibling(node, is_text = false) {
205142
return hydrate_anchor(/** @type {Node} */ (next_sibling));
206143
}
207144

208-
/**
209-
* @template {Element} N
210-
* @param {N} node
211-
* @param {string} class_name
212-
* @returns {void}
213-
*/
214-
export function set_class_name(node, class_name) {
215-
class_name_set.call(node, class_name);
216-
}
217-
218145
/**
219146
* @template {Node} N
220147
* @param {N} node
221148
* @returns {void}
222149
*/
223150
export function clear_text_content(node) {
224-
text_content_set.call(node, '');
151+
node.textContent = '';
225152
}
226153

227154
/** @param {string} name */

packages/svelte/src/internal/client/dom/template.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { hydrate_nodes, hydrating } from './hydration.js';
2-
import { clone_node, empty } from './operations.js';
2+
import { empty } from './operations.js';
33
import { create_fragment_from_html } from './reconciler.js';
44
import { current_effect } from '../runtime.js';
55
import { TEMPLATE_FRAGMENT, TEMPLATE_USE_IMPORT_NODE } from '../../../constants.js';
@@ -55,7 +55,7 @@ export function template(content, flags) {
5555
node = create_fragment_from_html(content);
5656
if (!is_fragment) node = /** @type {Node} */ (node.firstChild);
5757
}
58-
var clone = use_import_node ? document.importNode(node, true) : clone_node(node, true);
58+
var clone = use_import_node ? document.importNode(node, true) : node.cloneNode(true);
5959

6060
push_template_node(
6161
is_fragment
@@ -122,7 +122,7 @@ export function svg_template(content, flags) {
122122
}
123123
}
124124

125-
var clone = clone_node(node, true);
125+
var clone = node.cloneNode(true);
126126

127127
push_template_node(
128128
is_fragment
@@ -189,7 +189,7 @@ export function mathml_template(content, flags) {
189189
}
190190
}
191191

192-
var clone = clone_node(node, true);
192+
var clone = node.cloneNode(true);
193193

194194
push_template_node(
195195
is_fragment

packages/svelte/src/internal/client/render.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import { DEV } from 'esm-env';
2-
import {
3-
append_child,
4-
clear_text_content,
5-
create_element,
6-
empty,
7-
init_operations
8-
} from './dom/operations.js';
2+
import { clear_text_content, create_element, empty, init_operations } from './dom/operations.js';
93
import { HYDRATION_ERROR, HYDRATION_START, PassiveDelegatedEvents } from '../../constants.js';
104
import { flush_sync, push, pop, current_component_context } from './runtime.js';
115
import { effect_root, branch } from './reactivity/effects.js';
@@ -330,7 +324,8 @@ export async function append_styles(target, style_sheet_id, styles) {
330324
const style = create_element('style');
331325
style.id = style_sheet_id;
332326
style.textContent = styles;
333-
append_child(/** @type {Document} */ (append_styles_to).head || append_styles_to, style);
327+
const target = /** @type {Document} */ (append_styles_to).head || append_styles_to;
328+
target.appendChild(style);
334329
}
335330
}
336331

0 commit comments

Comments
 (0)