Skip to content

fix: improve invalid nested interactive element error #10199

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

Merged
merged 3 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/giant-moons-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: improve invalid nested interactive element error
9 changes: 8 additions & 1 deletion packages/svelte/src/compiler/phases/1-parse/utils/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,14 @@ function validate_code(code) {
// based on http://developers.whatwg.org/syntax.html#syntax-tag-omission

// while `input` is also an interactive element, it is never moved by the browser, so we don't need to check for it
const interactive_elements = new Set(['a', 'button', 'iframe', 'embed', 'select', 'textarea']);
export const interactive_elements = new Set([
'a',
'button',
'iframe',
'embed',
'select',
'textarea'
]);

/** @type {Record<string, Set<string>>} */
const disallowed_contents = {
Expand Down
14 changes: 14 additions & 0 deletions packages/svelte/src/compiler/phases/2-analyze/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '../../utils/ast.js';
import { warn } from '../../warnings.js';
import fuzzymatch from '../1-parse/utils/fuzzymatch.js';
import { interactive_elements } from '../1-parse/utils/html.js';
import { binding_properties } from '../bindings.js';
import { ContentEditableBindings, EventModifiers, SVGElements } from '../constants.js';
import { is_custom_element_node } from '../nodes.js';
Expand Down Expand Up @@ -530,6 +531,19 @@ export const validation = {
}
}

if (interactive_elements.has(node.name)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this use the more complete disallowed_contents from the util instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially tried that., but it gets complicated, as you can nest some contents there (if you nest a table inside a cell of another table, this is valid).

const path = context.path;
for (let parent of path) {
if (
parent.type === 'RegularElement' &&
parent.name === node.name &&
interactive_elements.has(parent.name)
) {
error(node, 'invalid-node-placement', `<${node.name}>`, parent.name);
}
}
}

context.next({
...context.state,
parent_element: node.name
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"code": "invalid-node-placement",
"message": "<a> is invalid inside <a>",
"start": {
"line": 4,
"column": 6
},
"end": {
"line": 4,
"column": 34
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div>
<a href="/foo">
<div>
<p><a href="/foo">{`hello`}</a></p>
</div>
</a>
</div>