From 754688cf489b64c9c70bb3ac4a3ec45bde1dc2eb Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Tue, 25 Apr 2017 16:30:19 -0400 Subject: [PATCH 1/3] include destroy as valid built-in method for event handlers (fixes #523) --- src/validate/html/validateElement.js | 17 +++++++++++++--- .../samples/event-handler-destroy/_config.js | 20 +++++++++++++++++++ .../samples/event-handler-destroy/main.html | 1 + .../method-nonexistent-helper/errors.json | 2 +- .../samples/method-nonexistent/errors.json | 2 +- 5 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 test/runtime/samples/event-handler-destroy/_config.js create mode 100644 test/runtime/samples/event-handler-destroy/main.html diff --git a/src/validate/html/validateElement.js b/src/validate/html/validateElement.js index c3405c75a2..beca2086ec 100644 --- a/src/validate/html/validateElement.js +++ b/src/validate/html/validateElement.js @@ -1,5 +1,11 @@ import flattenReference from '../../utils/flattenReference.js'; +const validBuiltins = new Set([ + 'set', + 'fire', + 'destroy' +]); + export default function validateElement ( validator, node ) { const isComponent = node.name === ':Self' || validator.components.has( node.name ); @@ -56,10 +62,15 @@ export default function validateElement ( validator, node ) { const { name } = flattenReference( callee ); if ( name === 'this' || name === 'event' ) return; - if ( callee.type === 'Identifier' && callee.name === 'set' || callee.name === 'fire' || validator.methods.has( callee.name ) ) return; + if ( callee.type === 'Identifier' && validBuiltins.has( callee.name ) || validator.methods.has( callee.name ) ) return; + + const validCallees = [ 'this.*', 'event.*' ] + .concat( + Array.from( validBuiltins ), + Array.from( validator.methods.keys() ) + ); - const validCallees = list( [ 'this.*', 'event.*', 'set', 'fire' ].concat( Array.from( validator.methods.keys() ) ) ); - let message = `'${validator.source.slice( callee.start, callee.end )}' is an invalid callee (should be one of ${validCallees})`; + let message = `'${validator.source.slice( callee.start, callee.end )}' is an invalid callee (should be one of ${list( validCallees )})`; if ( callee.type === 'Identifier' && validator.helpers.has( callee.name ) ) { message += `. '${callee.name}' exists on 'helpers', did you put it in the wrong place?`; diff --git a/test/runtime/samples/event-handler-destroy/_config.js b/test/runtime/samples/event-handler-destroy/_config.js new file mode 100644 index 0000000000..417f0da041 --- /dev/null +++ b/test/runtime/samples/event-handler-destroy/_config.js @@ -0,0 +1,20 @@ +export default { + html: ` + + `, + + test ( assert, component, target, window ) { + const button = target.querySelector( 'button' ); + const event = new window.MouseEvent( 'click' ); + + let destroyed = false; + component.on( 'destroy', () => { + destroyed = true; + }); + + button.dispatchEvent( event ); + assert.htmlEqual( target.innerHTML, `` ); + + assert.ok( destroyed ); + } +}; diff --git a/test/runtime/samples/event-handler-destroy/main.html b/test/runtime/samples/event-handler-destroy/main.html new file mode 100644 index 0000000000..01b2bb8221 --- /dev/null +++ b/test/runtime/samples/event-handler-destroy/main.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/validator/samples/method-nonexistent-helper/errors.json b/test/validator/samples/method-nonexistent-helper/errors.json index 80183498ca..02512426a1 100644 --- a/test/validator/samples/method-nonexistent-helper/errors.json +++ b/test/validator/samples/method-nonexistent-helper/errors.json @@ -1,5 +1,5 @@ [{ - "message": "'foo' is an invalid callee (should be one of this.*, event.*, set, fire or bar). 'foo' exists on 'helpers', did you put it in the wrong place?", + "message": "'foo' is an invalid callee (should be one of this.*, event.*, set, fire, destroy or bar). 'foo' exists on 'helpers', did you put it in the wrong place?", "pos": 18, "loc": { "line": 1, diff --git a/test/validator/samples/method-nonexistent/errors.json b/test/validator/samples/method-nonexistent/errors.json index 2eaf6c215d..8debbf8132 100644 --- a/test/validator/samples/method-nonexistent/errors.json +++ b/test/validator/samples/method-nonexistent/errors.json @@ -1,5 +1,5 @@ [{ - "message": "'foo' is an invalid callee (should be one of this.*, event.*, set, fire or bar)", + "message": "'foo' is an invalid callee (should be one of this.*, event.*, set, fire, destroy or bar)", "pos": 18, "loc": { "line": 1, From 11d86981843d3275293ff093fb36fde20fb486d4 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Tue, 25 Apr 2017 16:57:59 -0400 Subject: [PATCH 2/3] fix for #524 --- src/generators/dom/preprocess.js | 2 +- .../samples/component-yield-follows-element/Foo.html | 2 ++ .../component-yield-follows-element/_config.js | 6 ++++++ .../samples/component-yield-follows-element/main.html | 11 +++++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 test/runtime/samples/component-yield-follows-element/Foo.html create mode 100644 test/runtime/samples/component-yield-follows-element/_config.js create mode 100644 test/runtime/samples/component-yield-follows-element/main.html diff --git a/src/generators/dom/preprocess.js b/src/generators/dom/preprocess.js index 8661dbb0fb..6e6c33eaff 100644 --- a/src/generators/dom/preprocess.js +++ b/src/generators/dom/preprocess.js @@ -264,7 +264,7 @@ function preprocessChildren ( generator, block, state, node, isTopLevel ) { if ( lastChild ) { lastChild.next = child; - lastChild.needsAnchor = !child._state.name; + lastChild.needsAnchor = !child._state || !child._state.name; } lastChild = child; diff --git a/test/runtime/samples/component-yield-follows-element/Foo.html b/test/runtime/samples/component-yield-follows-element/Foo.html new file mode 100644 index 0000000000..1e619f2ecd --- /dev/null +++ b/test/runtime/samples/component-yield-follows-element/Foo.html @@ -0,0 +1,2 @@ +
before
+{{yield}} \ No newline at end of file diff --git a/test/runtime/samples/component-yield-follows-element/_config.js b/test/runtime/samples/component-yield-follows-element/_config.js new file mode 100644 index 0000000000..93703c7dab --- /dev/null +++ b/test/runtime/samples/component-yield-follows-element/_config.js @@ -0,0 +1,6 @@ +export default { + html: ` +
before
+ test + ` +}; diff --git a/test/runtime/samples/component-yield-follows-element/main.html b/test/runtime/samples/component-yield-follows-element/main.html new file mode 100644 index 0000000000..15747d9d53 --- /dev/null +++ b/test/runtime/samples/component-yield-follows-element/main.html @@ -0,0 +1,11 @@ +test + + From 8e87c688aee1d5f6a3b0121854dd12d43722d295 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Tue, 25 Apr 2017 17:23:42 -0400 Subject: [PATCH 3/3] -> v1.18.1 --- CHANGELOG.md | 5 +++++ package.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66f18d2056..13228e0d42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Svelte changelog +## 1.18.1 + +* Allow `destroy()` in event handlers ([#523](https://github.com/sveltejs/svelte/issues/523)) +* Fix bug with `{{yield}}` blocks following elements ([#524](https://github.com/sveltejs/svelte/issues/524)) + ## 1.18.0 * Visit `