Skip to content

Commit 4ba3853

Browse files
committed
update names
1 parent dfde459 commit 4ba3853

File tree

11 files changed

+30
-22
lines changed

11 files changed

+30
-22
lines changed

.changeset/smart-boats-accept.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'svelte': minor
33
---
44

5-
feat: functional template generation
5+
feat: add `fragments: 'html' | 'tree'` option for wider CSP compliance

packages/svelte/src/compiler/phases/3-transform/client/transform-template/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function build_locations(nodes) {
3636
* @param {number} [flags]
3737
*/
3838
export function transform_template(state, namespace, flags = 0) {
39-
const tree = state.options.templatingMode === 'functional';
39+
const tree = state.options.fragments === 'tree';
4040

4141
const expression = tree ? state.template.as_tree() : state.template.as_html();
4242

packages/svelte/src/compiler/types/index.d.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,14 @@ export interface CompileOptions extends ModuleCompileOptions {
123123
*/
124124
preserveWhitespace?: boolean;
125125
/**
126-
* If `functional`, the template will get compiled to a series of `document.createElement` calls, if `string` it will render the template tp a string and use `template.innerHTML`.
126+
* Which strategy to use when cloning DOM fragments:
127127
*
128-
* @default 'string'
128+
* - `html` populates a `<template>` with `innerHTML` and clones it. This is faster, but cannot be used if your app's [Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CSP) includes [`require-trusted-types-for 'script'`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/require-trusted-types-for)
129+
* - `tree` creates the fragment one element at a time and _then_ clones it. This is slower, but works everywhere
130+
*
131+
* @default 'html'
129132
*/
130-
templatingMode?: 'string' | 'functional';
133+
fragments?: 'html' | 'tree';
131134
/**
132135
* Set to `true` to force the compiler into runes mode, even if there are no indications of runes usage.
133136
* Set to `false` to force the compiler into ignoring runes, even if there are indications of runes usage.

packages/svelte/src/compiler/validate-options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export const validate_component_options =
110110

111111
preserveComments: boolean(false),
112112

113-
templatingMode: list(['string', 'functional']),
113+
fragments: list(['html', 'tree']),
114114

115115
preserveWhitespace: boolean(false),
116116

packages/svelte/tests/helpers.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,5 +191,4 @@ if (typeof window !== 'undefined') {
191191
});
192192
}
193193

194-
export const templatingMode =
195-
/** @type {'string' | 'functional'} */ (process.env.TEMPLATING_MODE) ?? 'string';
194+
export const fragments = /** @type {'html' | 'tree'} */ (process.env.FRAGMENTS) ?? 'html';

packages/svelte/tests/hydration/test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as fs from 'node:fs';
44
import { assert } from 'vitest';
55
import { compile_directory } from '../helpers.js';
66
import { assert_html_equal } from '../html_equal.js';
7-
import { templatingMode } from '../helpers.js';
7+
import { fragments } from '../helpers.js';
88
import { assert_ok, suite, type BaseTest } from '../suite.js';
99
import { createClassComponent } from 'svelte/legacy';
1010
import { render } from 'svelte/server';
@@ -46,7 +46,7 @@ const { test, run } = suite<HydrationTest>(async (config, cwd) => {
4646
if (!config.load_compiled) {
4747
await compile_directory(cwd, 'client', {
4848
accessors: true,
49-
templatingMode,
49+
fragments,
5050
...config.compileOptions
5151
});
5252

packages/svelte/tests/runtime-browser/test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as path from 'node:path';
55
import { compile } from 'svelte/compiler';
66
import { afterAll, assert, beforeAll, describe } from 'vitest';
77
import { suite, suite_with_variants } from '../suite';
8-
import { write, templatingMode } from '../helpers';
8+
import { write, fragments } from '../helpers';
99
import type { Warning } from '#compiler';
1010

1111
const assert_file = path.resolve(__dirname, 'assert.js');
@@ -87,7 +87,7 @@ async function run_test(
8787
build.onLoad({ filter: /\.svelte$/ }, (args) => {
8888
const compiled = compile(fs.readFileSync(args.path, 'utf-8').replace(/\r/g, ''), {
8989
generate: 'client',
90-
templatingMode,
90+
fragments,
9191
...config.compileOptions,
9292
immutable: config.immutable,
9393
customElement: test_dir.includes('custom-elements-samples'),

packages/svelte/tests/runtime-legacy/shared.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { proxy } from 'svelte/internal/client';
66
import { flushSync, hydrate, mount, unmount } from 'svelte';
77
import { render } from 'svelte/server';
88
import { afterAll, assert, beforeAll } from 'vitest';
9-
import { compile_directory, templatingMode } from '../helpers.js';
9+
import { compile_directory, fragments } from '../helpers.js';
1010
import { setup_html_equal } from '../html_equal.js';
1111
import { raf } from '../animation-helpers.js';
1212
import type { CompileOptions } from '#compiler';
@@ -158,7 +158,7 @@ async function common_setup(cwd: string, runes: boolean | undefined, config: Run
158158
rootDir: cwd,
159159
dev: force_hmr ? true : undefined,
160160
hmr: force_hmr ? true : undefined,
161-
templatingMode,
161+
fragments,
162162
...config.compileOptions,
163163
immutable: config.immutable,
164164
accessors: 'accessors' in config ? config.accessors : true,

packages/svelte/tests/runtime-runes/samples/functional-templating/_config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { test } from '../../test';
22

33
export default test({
44
compileOptions: {
5-
templatingMode: 'functional'
5+
fragments: 'tree'
66
},
77

88
html: `<p>hello</p>`

packages/svelte/tests/snapshot/samples/functional-templating/_config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import { test } from '../../test';
22

33
export default test({
44
compileOptions: {
5-
templatingMode: 'functional'
5+
fragments: 'tree'
66
}
77
});

0 commit comments

Comments
 (0)