Skip to content

Commit

Permalink
assign custom methods to custom element prototype - fixes #1369
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Apr 29, 2018
1 parent 890da3b commit ef39f00
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 12 deletions.
13 changes: 7 additions & 6 deletions src/compile/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ export default function dom(
? 'svelte/shared.js'
: options.shared || '';

let prototypeBase = `${name}.prototype`;

const proto = sharedPath
? `@proto`
: deindent`
Expand Down Expand Up @@ -294,8 +292,9 @@ export default function dom(
`}
}
customElements.define("${compiler.tag}", ${name});
@assign(@assign(${prototypeBase}, ${proto}), {
@assign(${name}.prototype, ${proto});
${templateProperties.methods && `@assign(${name}.prototype, %methods);`}
@assign(${name}.prototype, {
_mount(target, anchor) {
target.insertBefore(this, anchor);
},
Expand All @@ -304,15 +303,17 @@ export default function dom(
this.parentNode.removeChild(this);
}
});
customElements.define("${compiler.tag}", ${name});
`);
} else {
builder.addBlock(deindent`
function ${name}(options) {
${constructorBody}
}
@assign(${prototypeBase}, ${proto});
${templateProperties.methods && `@assign(${prototypeBase}, %methods);`}
@assign(${name}.prototype, ${proto});
${templateProperties.methods && `@assign(${name}.prototype, %methods);`}
`);
}

Expand Down
13 changes: 13 additions & 0 deletions test/custom-elements/samples/custom-method/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<p>{foo}</p>

<script>
export default {
tag: 'custom-element',

methods: {
updateFoo(value) {
this.foo = value;
}
}
};
</script>
12 changes: 12 additions & 0 deletions test/custom-elements/samples/custom-method/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as assert from 'assert';
import './main.html';

export default function (target) {
target.innerHTML = '<custom-element name="world"></custom-element>';
const el = target.querySelector('custom-element');

el.updateFoo(42);

const p = el.shadowRoot.querySelector('p');
assert.equal(p.textContent, '42');
}
2 changes: 1 addition & 1 deletion test/js/samples/action/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ var proto = {
/* generated by Svelte vX.Y.Z */

function link(node) {

function onClick(event) {
event.preventDefault();
history.pushState(null, null, event.target.href);
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/action/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { assign, createElement, detachNode, init, insertNode, noop, proto } from "svelte/shared.js";

function link(node) {

function onClick(event) {
event.preventDefault();
history.pushState(null, null, event.target.href);
Expand Down
6 changes: 4 additions & 2 deletions test/js/samples/css-shadow-dom-keyframes/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ class SvelteComponent extends HTMLElement {
}
}

customElements.define("custom-element", SvelteComponent);
assign(assign(SvelteComponent.prototype, proto), {
assign(SvelteComponent.prototype, proto);
assign(SvelteComponent.prototype, {
_mount(target, anchor) {
target.insertBefore(this, anchor);
},
Expand All @@ -196,4 +196,6 @@ assign(assign(SvelteComponent.prototype, proto), {
}
});

customElements.define("custom-element", SvelteComponent);

export default SvelteComponent;
6 changes: 4 additions & 2 deletions test/js/samples/css-shadow-dom-keyframes/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class SvelteComponent extends HTMLElement {
}
}

customElements.define("custom-element", SvelteComponent);
assign(assign(SvelteComponent.prototype, proto), {
assign(SvelteComponent.prototype, proto);
assign(SvelteComponent.prototype, {
_mount(target, anchor) {
target.insertBefore(this, anchor);
},
Expand All @@ -61,4 +61,6 @@ assign(assign(SvelteComponent.prototype, proto), {
this.parentNode.removeChild(this);
}
});

customElements.define("custom-element", SvelteComponent);
export default SvelteComponent;

0 comments on commit ef39f00

Please sign in to comment.