Skip to content

feat: add <svelte:html> element #14397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
warn on duplicates
  • Loading branch information
dummdidumm committed Nov 27, 2024
commit a88e8148a703cee70b37945b5bb4c565f884de97
8 changes: 8 additions & 0 deletions documentation/docs/98-reference/.generated/shared-warnings.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ The following properties cannot be cloned with `$state.snapshot` — the return

%properties%
```

### svelte_html_duplicate_attribute

```
Duplicate attribute '%name%' across multiple `<svelte:html>` blocks, the latest value will be used.
```

This warning appears when you have multiple `<svelte:html>` blocks across several files, and they set the same attribute. In that case, the latest value wins. On the server and on the client for static attributes, that's the last occurence of the attribute. On the client for dynamic attributes that's the value which was updated last across all `<svelte:html>` blocks.
6 changes: 6 additions & 0 deletions packages/svelte/messages/shared-warnings/warnings.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@
> The following properties cannot be cloned with `$state.snapshot` — the return value contains the originals:
>
> %properties%

## svelte_html_duplicate_attribute

> Duplicate attribute '%name%' across multiple `<svelte:html>` blocks, the latest value will be used.

This warning appears when you have multiple `<svelte:html>` blocks across several files, and they set the same attribute. In that case, the latest value wins. On the server and on the client for static attributes, that's the last occurence of the attribute. On the client for dynamic attributes that's the value which was updated last across all `<svelte:html>` blocks.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ export function SvelteHTML(element, context) {
} else {
context.state.init.push(update);
}

if (context.state.options.dev) {
context.state.init.push(
b.stmt(b.call('$.validate_svelte_html_attribute', b.literal(name)))
);
}
}
}
}
Expand Down
27 changes: 26 additions & 1 deletion packages/svelte/src/internal/client/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { dev_current_component_function } from './runtime.js';
import { get_descriptor, is_array } from '../shared/utils.js';
import * as e from './errors.js';
import { FILENAME } from '../../constants.js';
import { render_effect } from './reactivity/effects.js';
import { render_effect, teardown } from './reactivity/effects.js';
import * as w from './warnings.js';
import { capture_store_binding } from './reactivity/store.js';
import { svelte_html_duplicate_attribute } from '../shared/warnings.js';

/**
* @param {() => any} collection
Expand Down Expand Up @@ -104,3 +105,27 @@ export function validate_binding(binding, get_object, get_property, line, column
}
});
}

let svelte_html_attributes = new Map();

/**
* @param {string} name
*/
export function validate_svelte_html_attribute(name) {
const count = svelte_html_attributes.get(name) || 0;

if (count > 0) {
svelte_html_duplicate_attribute(name);
}

svelte_html_attributes.set(name, count + 1);

teardown(() => {
const count = svelte_html_attributes.get(name) || 1;
if (count === 1) {
svelte_html_attributes.delete(name);
} else {
svelte_html_attributes.set(name, count - 1);
}
});
}
4 changes: 4 additions & 0 deletions packages/svelte/src/internal/server/blocks/svelte-html.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
/** @import { Payload } from '#server' */

import { escape_html } from '../../../escaping.js';
import { svelte_html_duplicate_attribute } from '../../shared/warnings.js';

/**
* @param {Payload} payload
* @param {Record<string, string>} attributes
*/
export function svelte_html(payload, attributes) {
for (const name in attributes) {
if (payload.htmlAttributes.has(name)) {
svelte_html_duplicate_attribute(name);
}
payload.htmlAttributes.set(name, escape_html(attributes[name], true));
}
}
13 changes: 13 additions & 0 deletions packages/svelte/src/internal/shared/warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,17 @@ ${properties}`
// TODO print a link to the documentation
console.warn("state_snapshot_uncloneable");
}
}

/**
* Duplicate attribute '%name%' across multiple `<svelte:html>` blocks, the latest value will be used.
* @param {string} name
*/
export function svelte_html_duplicate_attribute(name) {
if (DEV) {
console.warn(`%c[svelte] svelte_html_duplicate_attribute\n%cDuplicate attribute '${name}' across multiple \`<svelte:html>\` blocks, the latest value will be used.`, bold, normal);
} else {
// TODO print a link to the documentation
console.warn("svelte_html_duplicate_attribute");
}
}