From 5bb5ba4c7620865c2b137e3bb95b532383670ea7 Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Sun, 15 Mar 2020 17:54:06 +0800 Subject: [PATCH] complain if named slots other than direct descendant of component (#4509) --- CHANGELOG.md | 1 + src/compiler/compile/nodes/Element.ts | 31 +++++++------------ .../Nested.svelte | 1 + .../component-slot-nested-error-2/_config.js | 3 ++ .../component-slot-nested-error-2/main.svelte | 11 +++++++ .../Nested.svelte | 1 + .../component-slot-nested-error-3/_config.js | 3 ++ .../component-slot-nested-error-3/main.svelte | 11 +++++++ .../component-slot-nested-error/Nested.svelte | 1 + .../component-slot-nested-error/_config.js | 3 ++ .../component-slot-nested-error/main.svelte | 9 ++++++ .../errors.json | 9 ++++++ .../input.svelte | 14 +++++++++ .../errors.json | 1 + .../input.svelte | 15 +++++++++ .../component-slotted-each-block/errors.json | 2 +- .../component-slotted-if-block/errors.json | 2 +- .../slot-attribute-invalid/errors.json | 2 +- 18 files changed, 98 insertions(+), 22 deletions(-) create mode 100644 test/runtime/samples/component-slot-nested-error-2/Nested.svelte create mode 100644 test/runtime/samples/component-slot-nested-error-2/_config.js create mode 100644 test/runtime/samples/component-slot-nested-error-2/main.svelte create mode 100644 test/runtime/samples/component-slot-nested-error-3/Nested.svelte create mode 100644 test/runtime/samples/component-slot-nested-error-3/_config.js create mode 100644 test/runtime/samples/component-slot-nested-error-3/main.svelte create mode 100644 test/runtime/samples/component-slot-nested-error/Nested.svelte create mode 100644 test/runtime/samples/component-slot-nested-error/_config.js create mode 100644 test/runtime/samples/component-slot-nested-error/main.svelte create mode 100644 test/validator/samples/component-slotted-custom-element-2/errors.json create mode 100644 test/validator/samples/component-slotted-custom-element-2/input.svelte create mode 100644 test/validator/samples/component-slotted-custom-element/errors.json create mode 100644 test/validator/samples/component-slotted-custom-element/input.svelte diff --git a/CHANGELOG.md b/CHANGELOG.md index 11648dbcd801..b85ec4ba16de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Allow `` to be used in a slot ([#2798](https://github.com/sveltejs/svelte/issues/2798)) * Expose object of unknown props in `$$restProps` ([#2930](https://github.com/sveltejs/svelte/issues/2930)) +* Prevent passing named slots other than from the top level within a component ([#3385](https://github.com/sveltejs/svelte/issues/3385)) * Allow transitions and animations to work within iframes ([#3624](https://github.com/sveltejs/svelte/issues/3624)) * Fix initialising slot fallbacks when unnecessary ([#3763](https://github.com/sveltejs/svelte/issues/3763)) * Disallow binding directly to `const` variables ([#4479](https://github.com/sveltejs/svelte/issues/4479)) diff --git a/src/compiler/compile/nodes/Element.ts b/src/compiler/compile/nodes/Element.ts index e8108858c53b..8c9c0bfe1841 100644 --- a/src/compiler/compile/nodes/Element.ts +++ b/src/compiler/compile/nodes/Element.ts @@ -278,7 +278,7 @@ export default class Element extends Node { } validate_attributes() { - const { component } = this; + const { component, parent } = this; const attribute_map = new Map(); @@ -395,26 +395,10 @@ export default class Element extends Node { component.slot_outlets.add(name); } - let ancestor = this.parent; - do { - if (ancestor.type === 'InlineComponent') break; - if (ancestor.type === 'Element' && /-/.test(ancestor.name)) break; - - if (ancestor.type === 'IfBlock' || ancestor.type === 'EachBlock') { - const type = ancestor.type === 'IfBlock' ? 'if' : 'each'; - const message = `Cannot place slotted elements inside an ${type}-block`; - - component.error(attribute, { - code: `invalid-slotted-content`, - message - }); - } - } while (ancestor = ancestor.parent); - - if (!ancestor) { + if (!(parent.type === 'InlineComponent' || within_custom_element(parent))) { component.error(attribute, { code: `invalid-slotted-content`, - message: `Element with a slot='...' attribute must be a descendant of a component or custom element` + message: `Element with a slot='...' attribute must be a child of a component or a descendant of a custom element`, }); } } @@ -776,3 +760,12 @@ function should_have_attribute( message: `A11y: <${name}> element should have ${article} ${sequence} attribute` }); } + +function within_custom_element(parent: INode) { + while (parent) { + if (parent.type === 'InlineComponent') return false; + if (parent.type === 'Element' && /-/.test(parent.name)) return true; + parent = parent.parent; + } + return false; +} \ No newline at end of file diff --git a/test/runtime/samples/component-slot-nested-error-2/Nested.svelte b/test/runtime/samples/component-slot-nested-error-2/Nested.svelte new file mode 100644 index 000000000000..c6f086d96c19 --- /dev/null +++ b/test/runtime/samples/component-slot-nested-error-2/Nested.svelte @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/runtime/samples/component-slot-nested-error-2/_config.js b/test/runtime/samples/component-slot-nested-error-2/_config.js new file mode 100644 index 000000000000..98a9f1cab00d --- /dev/null +++ b/test/runtime/samples/component-slot-nested-error-2/_config.js @@ -0,0 +1,3 @@ +export default { + error: [`Element with a slot='...' attribute must be a child of a component or a descendant of a custom element`] +}; diff --git a/test/runtime/samples/component-slot-nested-error-2/main.svelte b/test/runtime/samples/component-slot-nested-error-2/main.svelte new file mode 100644 index 000000000000..a7142d6dc13b --- /dev/null +++ b/test/runtime/samples/component-slot-nested-error-2/main.svelte @@ -0,0 +1,11 @@ + + + +
+
+
+
+
+ diff --git a/test/runtime/samples/component-slot-nested-error-3/Nested.svelte b/test/runtime/samples/component-slot-nested-error-3/Nested.svelte new file mode 100644 index 000000000000..c6f086d96c19 --- /dev/null +++ b/test/runtime/samples/component-slot-nested-error-3/Nested.svelte @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/runtime/samples/component-slot-nested-error-3/_config.js b/test/runtime/samples/component-slot-nested-error-3/_config.js new file mode 100644 index 000000000000..98a9f1cab00d --- /dev/null +++ b/test/runtime/samples/component-slot-nested-error-3/_config.js @@ -0,0 +1,3 @@ +export default { + error: [`Element with a slot='...' attribute must be a child of a component or a descendant of a custom element`] +}; diff --git a/test/runtime/samples/component-slot-nested-error-3/main.svelte b/test/runtime/samples/component-slot-nested-error-3/main.svelte new file mode 100644 index 000000000000..a63b1defdee6 --- /dev/null +++ b/test/runtime/samples/component-slot-nested-error-3/main.svelte @@ -0,0 +1,11 @@ + + + +
+
+
+
+
+ diff --git a/test/runtime/samples/component-slot-nested-error/Nested.svelte b/test/runtime/samples/component-slot-nested-error/Nested.svelte new file mode 100644 index 000000000000..c6f086d96c19 --- /dev/null +++ b/test/runtime/samples/component-slot-nested-error/Nested.svelte @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/runtime/samples/component-slot-nested-error/_config.js b/test/runtime/samples/component-slot-nested-error/_config.js new file mode 100644 index 000000000000..98a9f1cab00d --- /dev/null +++ b/test/runtime/samples/component-slot-nested-error/_config.js @@ -0,0 +1,3 @@ +export default { + error: [`Element with a slot='...' attribute must be a child of a component or a descendant of a custom element`] +}; diff --git a/test/runtime/samples/component-slot-nested-error/main.svelte b/test/runtime/samples/component-slot-nested-error/main.svelte new file mode 100644 index 000000000000..531c96f08c2d --- /dev/null +++ b/test/runtime/samples/component-slot-nested-error/main.svelte @@ -0,0 +1,9 @@ + + + +
+
+
+ diff --git a/test/validator/samples/component-slotted-custom-element-2/errors.json b/test/validator/samples/component-slotted-custom-element-2/errors.json new file mode 100644 index 000000000000..06be51d72d09 --- /dev/null +++ b/test/validator/samples/component-slotted-custom-element-2/errors.json @@ -0,0 +1,9 @@ +[ + { + "code": "invalid-slotted-content", + "message": "Element with a slot='...' attribute must be a child of a component or a descendant of a custom element", + "start": { "line": 10, "column": 9, "character": 138 }, + "end": { "line": 10, "column": 19, "character": 148 }, + "pos": 138 + } +] diff --git a/test/validator/samples/component-slotted-custom-element-2/input.svelte b/test/validator/samples/component-slotted-custom-element-2/input.svelte new file mode 100644 index 000000000000..221659ca4d94 --- /dev/null +++ b/test/validator/samples/component-slotted-custom-element-2/input.svelte @@ -0,0 +1,14 @@ + + + + + {#if thing} +
+
+
+ {/if} + + \ No newline at end of file diff --git a/test/validator/samples/component-slotted-custom-element/errors.json b/test/validator/samples/component-slotted-custom-element/errors.json new file mode 100644 index 000000000000..0637a088a01e --- /dev/null +++ b/test/validator/samples/component-slotted-custom-element/errors.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/test/validator/samples/component-slotted-custom-element/input.svelte b/test/validator/samples/component-slotted-custom-element/input.svelte new file mode 100644 index 000000000000..e0e27834c10e --- /dev/null +++ b/test/validator/samples/component-slotted-custom-element/input.svelte @@ -0,0 +1,15 @@ + + + + +
+
+
+ {#if thing} +
+ {/if} + + \ No newline at end of file diff --git a/test/validator/samples/component-slotted-each-block/errors.json b/test/validator/samples/component-slotted-each-block/errors.json index 89f394bca438..2944acae1717 100644 --- a/test/validator/samples/component-slotted-each-block/errors.json +++ b/test/validator/samples/component-slotted-each-block/errors.json @@ -1,6 +1,6 @@ [{ "code": "invalid-slotted-content", - "message": "Cannot place slotted elements inside an each-block", + "message": "Element with a slot='...' attribute must be a child of a component or a descendant of a custom element", "start": { "line": 7, "column": 7, diff --git a/test/validator/samples/component-slotted-if-block/errors.json b/test/validator/samples/component-slotted-if-block/errors.json index ab35a77fcebc..3ae07c1b3b90 100644 --- a/test/validator/samples/component-slotted-if-block/errors.json +++ b/test/validator/samples/component-slotted-if-block/errors.json @@ -1,6 +1,6 @@ [{ "code": "invalid-slotted-content", - "message": "Cannot place slotted elements inside an if-block", + "message": "Element with a slot='...' attribute must be a child of a component or a descendant of a custom element", "start": { "line": 7, "column": 7, diff --git a/test/validator/samples/slot-attribute-invalid/errors.json b/test/validator/samples/slot-attribute-invalid/errors.json index fc01fa979224..a75fdc065c26 100644 --- a/test/validator/samples/slot-attribute-invalid/errors.json +++ b/test/validator/samples/slot-attribute-invalid/errors.json @@ -1,6 +1,6 @@ [{ "code": "invalid-slotted-content", - "message": "Element with a slot='...' attribute must be a descendant of a component or custom element", + "message": "Element with a slot='...' attribute must be a child of a component or a descendant of a custom element", "start": { "line": 1, "column": 5,