-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
dummdidumm
wants to merge
22
commits into
main
Choose a base branch
from
svelte-html
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
81bd131
feat: add `<svelte:html>` element
dummdidumm 5ba013e
Apply suggestions from code review
dummdidumm d61291e
Merge branch 'main' into svelte-html
dummdidumm a88e814
warn on duplicates
dummdidumm 0c1c8b9
Merge branch 'main' into svelte-html
dummdidumm 8634231
robustify for multiple blocks:
dummdidumm 0a95949
fix test setup
dummdidumm 8f8a5b1
tweak implementation
dummdidumm 6bf6c74
test
dummdidumm dd748e2
Merge branch 'main' into svelte-html
dummdidumm 8df29aa
Merge branch 'main' into svelte-html
dummdidumm 4cf2077
Merge branch 'main' into svelte-html
dummdidumm e33e2cb
regenerate
dummdidumm 7c629e8
Update packages/svelte/src/internal/client/dom/blocks/svelte-html.js
Rich-Harris eb7a516
better server attribute stringification
dummdidumm a0b2c01
Merge branch 'main' into svelte-html
dummdidumm cea622a
better docs
dummdidumm edf31f1
naming things
dummdidumm c2f97c7
repair hydration mismatches
dummdidumm c30cf71
fix event handling
dummdidumm c723222
fix
dummdidumm 75e1dea
add to legacy wrapper
dummdidumm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': minor | ||
--- | ||
|
||
feat: add `<svelte:html>` element |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
title: <svelte:html> | ||
--- | ||
|
||
```svelte | ||
<svelte:html attribute={value} onevent={handler} /> | ||
``` | ||
|
||
Similarly to `<svelte:body>`, this element allows you to add properties and listeners to events on `document.documentElement`. This is useful for attributes such as `lang` which influence how the browser interprets the content. | ||
|
||
As with `<svelte:window>`, `<svelte:document>` and `<svelte:body>`, this element may only appear the top level of your component and must never be inside a block or element. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
packages/svelte/src/compiler/phases/2-analyze/visitors/SvelteHTML.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** @import { AST } from '#compiler' */ | ||
/** @import { Context } from '../types' */ | ||
import * as e from '../../../errors.js'; | ||
|
||
/** | ||
* @param {AST.SvelteHTML} node | ||
* @param {Context} context | ||
*/ | ||
export function SvelteHTML(node, context) { | ||
for (const attribute of node.attributes) { | ||
if (attribute.type !== 'Attribute') { | ||
e.svelte_html_illegal_attribute(attribute); | ||
} | ||
} | ||
|
||
if (node.fragment.nodes.length > 0) { | ||
e.svelte_meta_invalid_content(node, node.name); | ||
} | ||
|
||
context.next(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/svelte/src/compiler/phases/3-transform/client/visitors/SvelteHTML.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** @import { ExpressionStatement, Property } from 'estree' */ | ||
/** @import { AST } from '#compiler' */ | ||
/** @import { ComponentContext } from '../types' */ | ||
import { normalize_attribute } from '../../../../../utils.js'; | ||
import { is_event_attribute } from '../../../../utils/ast.js'; | ||
import * as b from '../../../../utils/builders.js'; | ||
import { build_attribute_value } from './shared/element.js'; | ||
import { visit_event_attribute } from './shared/events.js'; | ||
|
||
/** | ||
* @param {AST.SvelteHTML} element | ||
* @param {ComponentContext} context | ||
*/ | ||
export function SvelteHTML(element, context) { | ||
/** @type {Property[]} */ | ||
const attributes = []; | ||
|
||
for (const attribute of element.attributes) { | ||
if (attribute.type === 'Attribute') { | ||
if (is_event_attribute(attribute)) { | ||
visit_event_attribute(attribute, context); | ||
} else { | ||
const name = normalize_attribute(attribute.name); | ||
const { value } = build_attribute_value(attribute.value, context); | ||
|
||
attributes.push(b.init(name, value)); | ||
|
||
if (context.state.options.dev) { | ||
context.state.init.push( | ||
b.stmt(b.call('$.validate_svelte_html_attribute', b.literal(name))) | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (attributes.length > 0) { | ||
context.state.init.push(b.stmt(b.call('$.svelte_html', b.arrow([], b.object(attributes))))); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/svelte/src/compiler/phases/3-transform/server/visitors/SvelteHTML.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** @import { Property } from 'estree' */ | ||
/** @import { AST } from '#compiler' */ | ||
/** @import { ComponentContext } from '../types.js' */ | ||
import { normalize_attribute } from '../../../../../utils.js'; | ||
import { is_event_attribute } from '../../../../utils/ast.js'; | ||
import * as b from '../../../../utils/builders.js'; | ||
import { build_attribute_value } from './shared/utils.js'; | ||
|
||
/** | ||
* @param {AST.SvelteHTML} element | ||
* @param {ComponentContext} context | ||
*/ | ||
export function SvelteHTML(element, context) { | ||
/** @type {Property[]} */ | ||
const attributes = []; | ||
|
||
for (const attribute of element.attributes) { | ||
if (attribute.type === 'Attribute' && !is_event_attribute(attribute)) { | ||
const name = normalize_attribute(attribute.name); | ||
const value = build_attribute_value(attribute.value, context); | ||
attributes.push(b.init(name, value)); | ||
} | ||
} | ||
|
||
context.state.template.push( | ||
b.stmt(b.call('$.svelte_html', b.id('$$payload'), b.object(attributes))) | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
packages/svelte/src/internal/client/dom/blocks/svelte-html.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { render_effect, teardown } from '../../reactivity/effects.js'; | ||
import { set_attribute } from '../elements/attributes.js'; | ||
import { set_class } from '../elements/class.js'; | ||
import { hydrating } from '../hydration.js'; | ||
|
||
/** | ||
* @param {() => Record<string, any>} get_attributes | ||
* @returns {void} | ||
*/ | ||
export function svelte_html(get_attributes) { | ||
const node = document.documentElement; | ||
const own = {}; | ||
|
||
/** @type {Record<string, Array<[any, any]>>} to check who set the last value of each attribute */ | ||
dummdidumm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// @ts-expect-error | ||
const current_setters = (node.__attributes_setters ??= {}); | ||
|
||
/** @type {Record<string, any>} */ | ||
let attributes; | ||
|
||
render_effect(() => { | ||
attributes = get_attributes(); | ||
|
||
for (const name in attributes) { | ||
const current = (current_setters[name] ??= []); | ||
const idx = current.findIndex(([owner]) => owner === own); | ||
const old = idx === -1 ? null : current.splice(idx, 1)[0][1]; | ||
Rich-Harris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let value = attributes[name]; | ||
current.push([own, value]); | ||
dummdidumm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Do nothing on initial render during hydration: If there are attribute duplicates, the last value | ||
// wins, which could result in needless hydration repairs from earlier values. | ||
if (hydrating) continue; | ||
dummdidumm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (name === 'class') { | ||
// Avoid unrelated attribute changes from triggering class changes | ||
if (old !== value) { | ||
set_class(node, current_setters[name].map(([_, text]) => text).join(' ')); | ||
dummdidumm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} else { | ||
set_attribute(node, name, value); | ||
} | ||
} | ||
}); | ||
|
||
teardown(() => { | ||
for (const name in attributes) { | ||
const old = current_setters[name]; | ||
current_setters[name] = old.filter(([owner]) => owner !== own); | ||
dummdidumm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const current = current_setters[name]; | ||
|
||
if (name === 'class') { | ||
set_class(node, current.map(([_, text]) => text).join(' ')); | ||
dummdidumm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// If this was the last one setting this attribute, revert to the previous value | ||
} else if (old[old.length - 1][0] === own) { | ||
set_attribute(node, name, current[current.length - 1]?.[1]); | ||
dummdidumm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.