Skip to content

Commit 68fee96

Browse files
committed
avoid magic strings
1 parent e613454 commit 68fee96

File tree

9 files changed

+45
-20
lines changed

9 files changed

+45
-20
lines changed

.prettierignore

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ packages/svelte/tests/**/_actual*
1010
packages/svelte/tests/**/expected*
1111
packages/svelte/tests/**/_output
1212
packages/svelte/tests/**/shards/*.test.js
13-
packages/svelte/tests/hydration/samples/*/_before.html
14-
packages/svelte/tests/hydration/samples/*/_before_head.html
15-
packages/svelte/tests/hydration/samples/*/_after.html
16-
packages/svelte/tests/hydration/samples/*/_after_head.html
13+
packages/svelte/tests/hydration/samples/*/_expected.html
14+
packages/svelte/tests/hydration/samples/*/_override.html
1715
packages/svelte/types
1816
packages/svelte/compiler.cjs
1917
playgrounds/demo/src

packages/svelte/src/compiler/phases/3-transform/server/transform-server.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ import { binding_properties } from '../../bindings.js';
2626
import { regex_starts_with_newline, regex_whitespaces_strict } from '../../patterns.js';
2727
import { DOMBooleanAttributes } from '../../../../constants.js';
2828
import { sanitize_template_string } from '../../../utils/sanitize_template_string.js';
29+
import { HYDRATION_END, HYDRATION_START } from '../../../../internal/client/dom/hydration.js';
2930

30-
const block_open = t_string('<!--[-->');
31-
const block_close = t_string('<!--]-->');
31+
export const block_open = t_string(`<!--${HYDRATION_START}-->`);
32+
export const block_close = t_string(`<!--${HYDRATION_END}-->`);
3233

3334
/**
3435
* @param {string} value

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

packages/svelte/src/internal/server/index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import {
99
} from '../../constants.js';
1010
import { DEV } from 'esm-env';
1111
import { current_component, pop, push } from './context.js';
12+
import {
13+
block_close,
14+
block_open
15+
} from '../../compiler/phases/3-transform/server/transform-server.js';
1216

1317
/**
1418
* @typedef {{
@@ -161,11 +165,11 @@ export function element(payload, tag, attributes_fn, children_fn) {
161165

162166
if (!VoidElements.has(tag)) {
163167
if (tag !== 'textarea') {
164-
payload.out += '<!--[-->';
168+
payload.out += block_open.value;
165169
}
166170
children_fn();
167171
if (tag !== 'textarea') {
168-
payload.out += '<!--]-->';
172+
payload.out += block_close.value;
169173
}
170174
payload.out += `</${tag}>`;
171175
}
@@ -187,7 +191,7 @@ export function render(component, options) {
187191

188192
const prev_on_destroy = on_destroy;
189193
on_destroy = [];
190-
payload.out += '<!--[-->';
194+
payload.out += block_open.value;
191195

192196
if (options.context) {
193197
push();
@@ -200,14 +204,14 @@ export function render(component, options) {
200204
pop();
201205
}
202206

203-
payload.out += '<!--]-->';
207+
payload.out += block_close.value;
204208
for (const cleanup of on_destroy) cleanup();
205209
on_destroy = prev_on_destroy;
206210

207211
return {
208212
head:
209213
payload.head.out || payload.head.title
210-
? payload.head.title + '<!--[-->' + payload.head.out + '<!--]-->'
214+
? payload.head.title + block_open.value + payload.head.out + block_close.value
211215
: '',
212216
html: payload.out
213217
};

packages/svelte/tests/hydration/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const { test, run } = suite<HydrationTest>(async (config, cwd) => {
5252
});
5353

5454
fs.writeFileSync(`${cwd}/_output/body.html`, rendered.html + '\n');
55-
target.innerHTML = read(`${cwd}/override.html`) ?? rendered.html;
55+
target.innerHTML = read(`${cwd}/_override.html`) ?? rendered.html;
5656

5757
if (rendered.head) {
5858
fs.writeFileSync(`${cwd}/_output/head.html`, rendered.head + '\n');

0 commit comments

Comments
 (0)