Skip to content

Add initial support for {{yield}} #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/generate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,10 @@ export default function generate ( parsed, source, options ) {
state = {};
};

this.appendChild = function appendChild ( child ) {
this.yield.appendChild(child);
}

${initStatements.join( '\n\n' )}
}
` );
Expand Down
21 changes: 16 additions & 5 deletions compiler/generate/visitors/MustacheTag.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@ import deindent from '../utils/deindent.js';
export default {
enter ( generator, node ) {
const name = generator.current.counter( 'text' );
const { snippet } = generator.contextualise( node.expression );

generator.addSourcemapLocations( node.expression );
const { snippet } = generator.contextualise( node.expression );

generator.addElement( name, `document.createTextNode( ${snippet} )`, true );
if (node.expression.name === 'yield') {
if (generator.hasYield === undefined) {
generator.hasYield = true;
generator.current.initStatements.push( deindent`
component.yield = ${generator.current.target};
` );
} else {
throw new Error( `Only one {{yield}} per component.` );
}
} else {
generator.addElement( name, `document.createTextNode( ${snippet} )`, true );

generator.current.updateStatements.push( deindent`
${name}.data = ${snippet};
` );
generator.current.updateStatements.push( deindent`
${name}.data = ${snippet};
` );
}
}
};
5 changes: 5 additions & 0 deletions test/compiler/component-yield-one/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
compileError: function ( assert, err ) {
assert.equal('Error: Only one {{yield}} per component.', err)
}
};
2 changes: 2 additions & 0 deletions test/compiler/component-yield-one/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<p>{{yield}}</p>
<p>{{yield}}</p>
1 change: 1 addition & 0 deletions test/compiler/component-yield/Widget.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="sidebar-widget">{{yield}}</div>
8 changes: 8 additions & 0 deletions test/compiler/component-yield/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
html: '<main><div class="sidebar-widget">Hello Hello</div></main>',
test ( assert, component, target ) {
component.set({ a: 'World' });
assert.equal( component.get( 'a' ), 'World' );
assert.equal( target.innerHTML, '<main><div class="sidebar-widget">Hello World</div></main>' );
}
};
16 changes: 16 additions & 0 deletions test/compiler/component-yield/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<main>
<Widget>Hello {{a}}</Widget>
</main>

<script>
import Widget from './Widget.html';

export default {
components: { Widget },
data(){
return {
a: "Hello"
}
}
};
</script>
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ describe( 'svelte', () => {
compiled = svelte.compile( source );
} catch ( err ) {
if ( config.compileError ) {
config.compileError( err );
config.compileError( assert, err );
return;
} else {
throw err;
Expand Down