Skip to content

fix: add compiler error for invalid <p> contents #10201

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 2 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/empty-tools-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: add compiler error for invalid <p> contents
30 changes: 30 additions & 0 deletions packages/svelte/src/compiler/phases/1-parse/utils/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,36 @@ const disallowed_contents = {
th: new Set(['td', 'th', 'tr'])
};

export const disallowed_parapgraph_contents = [
'address',
'article',
'aside',
'blockquote',
'details',
'div',
'dl',
'fieldset',
'figcapture',
'figure',
'footer',
'form',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'header',
'hr',
'menu',
'nav',
'ol',
'pre',
'section',
'table',
'ul'
];

for (const interactive_element of interactive_elements) {
disallowed_contents[interactive_element] = interactive_elements;
}
Expand Down
11 changes: 10 additions & 1 deletion packages/svelte/src/compiler/phases/2-analyze/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +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 { disallowed_parapgraph_contents, 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 @@ -544,6 +544,15 @@ export const validation = {
}
}

if (disallowed_parapgraph_contents.includes(node.name)) {
const path = context.path;
for (let parent of path) {
if (parent.type === 'RegularElement' && parent.name === 'p') {
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": "<div> is invalid inside <p>",
"start": {
"line": 4,
"column": 3
},
"end": {
"line": 4,
"column": 17
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div>
<p>
<span>
<div>foo</div>
</span>
</p>
</div>