Skip to content

Commit 277dbd3

Browse files
committed
fix: add compiler error for invalid <p> contents
1 parent b04c5bb commit 277dbd3

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-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 === node.name && 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

0 commit comments

Comments
 (0)