Skip to content

Commit c7a7725

Browse files
authored
fix: add compiler error for invalid <p> contents (#10201)
* fix: add compiler error for invalid <p> contents * add test
1 parent 73c5983 commit c7a7725

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

.changeset/empty-tools-share.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: add compiler error for invalid <p> contents

packages/svelte/src/compiler/phases/1-parse/utils/html.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,36 @@ const disallowed_contents = {
153153
th: new Set(['td', 'th', 'tr'])
154154
};
155155

156+
export const disallowed_parapgraph_contents = [
157+
'address',
158+
'article',
159+
'aside',
160+
'blockquote',
161+
'details',
162+
'div',
163+
'dl',
164+
'fieldset',
165+
'figcapture',
166+
'figure',
167+
'footer',
168+
'form',
169+
'h1',
170+
'h2',
171+
'h3',
172+
'h4',
173+
'h5',
174+
'h6',
175+
'header',
176+
'hr',
177+
'menu',
178+
'nav',
179+
'ol',
180+
'pre',
181+
'section',
182+
'table',
183+
'ul'
184+
];
185+
156186
for (const interactive_element of interactive_elements) {
157187
disallowed_contents[interactive_element] = interactive_elements;
158188
}

packages/svelte/src/compiler/phases/2-analyze/validation.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from '../../utils/ast.js';
88
import { warn } from '../../warnings.js';
99
import fuzzymatch from '../1-parse/utils/fuzzymatch.js';
10-
import { interactive_elements } from '../1-parse/utils/html.js';
10+
import { disallowed_parapgraph_contents, interactive_elements } from '../1-parse/utils/html.js';
1111
import { binding_properties } from '../bindings.js';
1212
import { ContentEditableBindings, EventModifiers, SVGElements } from '../constants.js';
1313
import { is_custom_element_node } from '../nodes.js';
@@ -544,6 +544,15 @@ export const validation = {
544544
}
545545
}
546546

547+
if (disallowed_parapgraph_contents.includes(node.name)) {
548+
const path = context.path;
549+
for (let parent of path) {
550+
if (parent.type === 'RegularElement' && parent.name === 'p') {
551+
error(node, 'invalid-node-placement', `<${node.name}>`, parent.name);
552+
}
553+
}
554+
}
555+
547556
context.next({
548557
...context.state,
549558
parent_element: node.name
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"code": "invalid-node-placement",
4+
"message": "<div> is invalid inside <p>",
5+
"start": {
6+
"line": 4,
7+
"column": 3
8+
},
9+
"end": {
10+
"line": 4,
11+
"column": 17
12+
}
13+
}
14+
]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div>
2+
<p>
3+
<span>
4+
<div>foo</div>
5+
</span>
6+
</p>
7+
</div>

0 commit comments

Comments
 (0)