Skip to content

Commit abff84e

Browse files
committed
avoid magic strings
1 parent 0f39f4b commit abff84e

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

packages/svelte/src/internal/client/dom/blocks/each.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ import {
66
EACH_ITEM_REACTIVE,
77
EACH_KEYED
88
} from '../../../../constants.js';
9-
import { hydrate_anchor, hydrate_nodes, hydrating, set_hydrating } from '../hydration.js';
9+
import {
10+
HYDRATION_START,
11+
hydrate_anchor,
12+
hydrate_nodes,
13+
hydrating,
14+
set_hydrating
15+
} from '../hydration.js';
1016
import { empty } from '../operations.js';
1117
import { remove } from '../reconciler.js';
1218
import { untrack } from '../../runtime.js';
@@ -117,7 +123,10 @@ function each(anchor, flags, get_collection, get_key, render_fn, fallback_fn, re
117123
var child_anchor = hydrate_nodes[0];
118124

119125
for (var i = 0; i < length; i++) {
120-
if (child_anchor.nodeType !== 8 || /** @type {Comment} */ (child_anchor).data !== '[') {
126+
if (
127+
child_anchor.nodeType !== 8 ||
128+
/** @type {Comment} */ (child_anchor).data !== HYDRATION_START
129+
) {
121130
// If `nodes` is null, then that means that the server rendered fewer items than what
122131
// expected, so break out and continue appending non-hydrated items
123132
mismatch = true;

packages/svelte/src/internal/client/dom/blocks/svelte-head.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { hydrate_anchor, hydrate_nodes, hydrating, set_hydrate_nodes } from '../hydration.js';
1+
import {
2+
HYDRATION_START,
3+
hydrate_anchor,
4+
hydrate_nodes,
5+
hydrating,
6+
set_hydrate_nodes
7+
} from '../hydration.js';
28
import { empty } from '../operations.js';
39
import { block } from '../../reactivity/effects.js';
410

@@ -19,7 +25,7 @@ export function head(render_fn) {
1925
previous_hydrate_nodes = hydrate_nodes;
2026

2127
let anchor = /** @type {import('#client').TemplateNode} */ (document.head.firstChild);
22-
while (anchor.nodeType !== 8 || /** @type {Comment} */ (anchor).data !== '[') {
28+
while (anchor.nodeType !== 8 || /** @type {Comment} */ (anchor).data !== HYDRATION_START) {
2329
anchor = /** @type {import('#client').TemplateNode} */ (anchor.nextSibling);
2430
}
2531

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
export const HYDRATION_START = '[';
2+
export const HYDRATION_END = ']';
3+
14
/**
25
* Use this variable to guard everything related to hydration code so it can be treeshaken out
36
* if the user doesn't use the `hydrate` method and these code paths are therefore not needed.
@@ -37,7 +40,7 @@ export function hydrate_anchor(node) {
3740
var current = /** @type {Node | null} */ (node);
3841

3942
// TODO this could have false positives, if a user comment consisted of `[`. need to tighten that up
40-
if (/** @type {Comment} */ (current)?.data !== '[') {
43+
if (/** @type {Comment} */ (current)?.data !== HYDRATION_START) {
4144
return node;
4245
}
4346

@@ -49,9 +52,9 @@ export function hydrate_anchor(node) {
4952
if (current.nodeType === 8) {
5053
var data = /** @type {Comment} */ (current).data;
5154

52-
if (data === '[') {
55+
if (data === HYDRATION_START) {
5356
depth += 1;
54-
} else if (data === ']') {
57+
} else if (data === HYDRATION_END) {
5558
if (depth === 0) {
5659
hydrate_nodes = /** @type {import('#client').TemplateNode[]} */ (nodes);
5760
return current;

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { PassiveDelegatedEvents } from '../../constants.js';
1010
import { flush_sync, push, pop, current_component_context, untrack } from './runtime.js';
1111
import { effect_root, branch } from './reactivity/effects.js';
1212
import {
13+
HYDRATION_START,
1314
hydrate_anchor,
1415
hydrate_nodes,
1516
hydrating,
@@ -132,7 +133,10 @@ export function hydrate(component, options) {
132133
set_hydrating(true);
133134

134135
var node = target.firstChild;
135-
while (node && (node.nodeType !== 8 || /** @type {Comment} */ (node).data !== '[')) {
136+
while (
137+
node &&
138+
(node.nodeType !== 8 || /** @type {Comment} */ (node).data !== HYDRATION_START)
139+
) {
136140
node = node.nextSibling;
137141
}
138142

0 commit comments

Comments
 (0)