Skip to content

Add YieldTag to parser and generator #112

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

Merged
merged 6 commits into from
Dec 5, 2016
Merged
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
1 change: 1 addition & 0 deletions compiler/generate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ export default function generate ( parsed, source, options ) {
};

this.root = options.root;
this.yield = options.yield;

${initStatements.join( '\n\n' )}
}
Expand Down
67 changes: 49 additions & 18 deletions compiler/generate/visitors/Element.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import deindent from '../utils/deindent.js';
import addComponentAttributes from './attributes/addComponentAttributes.js';
import addElementAttributes from './attributes/addElementAttributes.js';
import counter from '../utils/counter.js';

export default {
enter ( generator, node ) {
const hasChildren = node.children.length > 0;
const isComponent = node.name in generator.components;
const name = generator.current.counter( isComponent ? `${node.name[0].toLowerCase()}${node.name.slice( 1 )}` : node.name );

Expand All @@ -27,13 +29,49 @@ export default {

addComponentAttributes( generator, node, local );

const componentInitProperties = [
`target: ${!isToplevel ? generator.current.target: 'null'}`,
'root: component.root || component'
];
// Component has children
if ( hasChildren ) {
const yieldName = `render${name}YieldFragment`;

// {{YIELD STUFF}}
generator.push({
useAnchor: true,
name: generator.current.counter(yieldName),
target: 'target',
localElementDepth: 0,

initStatements: [],
mountStatements: [],
updateStatements: [],
teardownStatements: [],

counter: counter()
});

node.children.forEach( generator.visit );
generator.addRenderer( generator.current );
generator.pop();

// Don't render children twice
node.children = [];

generator.current.initStatements.push(`var ${name}_yieldFragment = ${yieldName}( root, component );`);
generator.current.updateStatements.push(`${name}_yieldFragment.update ( changed, root );`);

componentInitProperties.push(`yield: ${name}_yieldFragment`);
}

const statements = [];

if ( local.staticAttributes.length || local.dynamicAttributes.length || local.bindings.length ) {
const initialProps = local.staticAttributes
.concat( local.dynamicAttributes )
.map( attribute => `${attribute.name}: ${attribute.value}` );

const statements = [];

if ( initialProps.length ) {
statements.push( deindent`
var ${name}_initialData = {
Expand All @@ -53,24 +91,17 @@ export default {

statements.push( bindings.join( '\n' ) );
}
componentInitProperties.push(`data: ${name}_initialData`);
}

local.init.unshift( deindent`
${statements.join( '\n\n' )}
local.init.unshift( deindent`
${statements.join( '\n\n' )}

var ${name} = new template.components.${node.name}({
${componentInitProperties.join(',\n')}
});
` );

var ${name} = new template.components.${node.name}({
target: ${!isToplevel ? generator.current.target: 'null'},
root: component.root || component,
data: ${name}_initialData
});
` );
} else {
local.init.unshift( deindent`
var ${name} = new template.components.${node.name}({
target: ${!isToplevel ? generator.current.target: 'null'},
root: component.root || component
});
` );
}

if ( isToplevel ) {
local.mount.unshift( `${name}.mount( target, anchor );` );
Expand Down
7 changes: 7 additions & 0 deletions compiler/generate/visitors/YieldTag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
enter ( generator ) {
const anchor = generator.createAnchor( 'yield', 'yield' );
generator.current.mountStatements.push(`component.yield && component.yield.mount( ${generator.current.target}, ${anchor} );`);
generator.current.teardownStatements.push(`component.yield && component.yield.teardown( detach );`);
}
};
4 changes: 3 additions & 1 deletion compiler/generate/visitors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import Element from './Element.js';
import IfBlock from './IfBlock.js';
import MustacheTag from './MustacheTag.js';
import Text from './Text.js';
import YieldTag from './YieldTag.js';

export default {
Comment,
EachBlock,
Element,
IfBlock,
MustacheTag,
Text
Text,
YieldTag
};
13 changes: 13 additions & 0 deletions compiler/parse/state/mustache.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,19 @@ export default function mustache ( parser ) {
parser.stack.push( block );
}

// {{yield}}
else if ( parser.eat( 'yield' ) ) {

parser.allowWhitespace();
parser.eat( '}}', true );

parser.current().children.push({
start,
end: parser.index,
type: 'YieldTag'
});
}

else {
const expression = readExpression( parser );

Expand Down
15 changes: 15 additions & 0 deletions test/compiler/component-yield-if/Widget.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<p>
{{#if show}}
{{yield}}
{{/if}}
</p>
<script>
export default {
data(){
return {
show: false
}
}
}

</script>
13 changes: 13 additions & 0 deletions test/compiler/component-yield-if/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default {
html: '<div><p></p></div>',
test ( assert, component, target ) {
const widget = component.refs.widget;
assert.equal( widget.get( 'show' ), false );
widget.set({show: true});
assert.equal( target.innerHTML, '<div><p>Hello<!--yield--><!--#if show--></p></div>' );
component.set({data: 'World'});
assert.equal( target.innerHTML, '<div><p>World<!--yield--><!--#if show--></p></div>' );
widget.set({show: false});
assert.equal( target.innerHTML, '<div><p><!--#if show--></p></div>' );
}
}
14 changes: 14 additions & 0 deletions test/compiler/component-yield-if/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div>
<Widget ref:widget>{{data}}</Widget>
</div>
<script>
import Widget from './Widget.html'
export default {
components: { Widget },
data(){
return {
data: "Hello"
}
}
}
</script>
1 change: 1 addition & 0 deletions test/compiler/component-yield-parent/Widget.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>{{yield}}</p>
9 changes: 9 additions & 0 deletions test/compiler/component-yield-parent/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
html: '<div><p>Hello</p></div>',
test ( assert, component, target ) {
assert.equal( component.get( 'data' ), 'Hello' );
component.set({data: 'World'})
assert.equal( component.get( 'data' ), 'World' );
assert.equal( target.innerHTML, '<div><p>World<!--yield--></p></div>' );
}
}
14 changes: 14 additions & 0 deletions test/compiler/component-yield-parent/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div>
<Widget>{{data}}</Widget>
</div>
<script>
import Widget from './Widget.html'
export default {
components: { Widget },
data(){
return {
data: "Hello"
}
}
}
</script>
3 changes: 3 additions & 0 deletions test/compiler/component-yield/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
html: '<p>Hello</p>'
}
15 changes: 15 additions & 0 deletions test/compiler/component-yield/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<p>
Hello
{{#if test}}
{{yield}}
{{/if}}
</p>
<script>
export default {
data(){
return {
test: true
}
}
}
</script>
1 change: 1 addition & 0 deletions test/parser/yield/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{yield}}
14 changes: 14 additions & 0 deletions test/parser/yield/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"html": {
"start": 0,
"end": 9,
"type": "Fragment",
"children": [
{
"start": 0,
"end": 9,
"type": "YieldTag"
}
]
}
}