Skip to content

Commit

Permalink
fix(TemplateCompiler): properly startup compilation when passed a tem…
Browse files Browse the repository at this point in the history
…plate (#214)
  • Loading branch information
bigopon authored and EisenbergEffect committed Oct 9, 2018
1 parent 8ae6776 commit c74a44c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
7 changes: 4 additions & 3 deletions packages/jit/src/template-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ export class TemplateCompiler implements ITemplateCompiler {

public compile(definition: ITemplateSource, resources: IResourceDescriptions, flags?: ViewCompileFlags): TemplateDefinition {
const model = SemanticModel.create(definition, resources, this.attrParser, this.elParser, this.exprParser);
let $el = model.root;
const root = model.root;
let $el = root.isTemplate ? root.$content : root;
while ($el = this.compileNode($el));

// the flag should be passed correctly from rendering engine
if (model.root.isTemplate && (flags & ViewCompileFlags.surrogate)) {
this.compileSurrogate(model.root);
if (root.isTemplate && (flags & ViewCompileFlags.surrogate)) {
this.compileSurrogate(root);
}

return <TemplateDefinition>definition;
Expand Down
32 changes: 28 additions & 4 deletions packages/jit/test/unit/template-compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,30 @@ describe('TemplateCompiler', () => {
]);
});

it('compiles surrogate with binding expression', () => {
const { instructions, surrogates } = compileWith(
`<template class.bind="base"></template>`,
[],
ViewCompileFlags.surrogate
);
verifyInstructions(instructions as any, [], 'normal');
verifyInstructions(surrogates as any, [
{ toVerify: ['type', 'dest'], type: TT.propertyBinding, dest: 'class' }
], 'surrogate');
});

it('compiles surrogate with interpolation expression', () => {
const { instructions, surrogates } = compileWith(
`<template class="h-100 \${base}"></template>`,
[],
ViewCompileFlags.surrogate
);
verifyInstructions(instructions as any, [], 'normal');
verifyInstructions(surrogates as any, [
{ toVerify: ['type', 'dest'], type: TT.interpolation, dest: 'class' }
], 'surrogate');
});

it('throws on attributes that require to be unique', () => {
const attrs = ['id', 'part', 'replace-part'];
attrs.forEach(attr => {
Expand Down Expand Up @@ -366,21 +390,21 @@ describe('TemplateCompiler', () => {
return sut.compile(<any>{ templateOrNode: markup, instructions: [], surrogates: [] }, resources, viewCompileFlags);
}

function verifyInstructions(actual: any[], expectation: IExpectedInstruction[]) {
expect(actual.length).to.equal(expectation.length, `Expected to have ${expectation.length} instructions. Received: ${actual.length}`);
function verifyInstructions(actual: any[], expectation: IExpectedInstruction[], type?: string) {
expect(actual.length).to.equal(expectation.length, `Expected to have ${expectation.length} ${type ? `${type} ` : ''} instructions. Received: ${actual.length}`);
for (let i = 0, ii = actual.length; i < ii; ++i) {
const actualInst = actual[i];
const expectedInst = expectation[i];
for (const prop of expectedInst.toVerify) {
if (expectedInst[prop] instanceof Object) {
expect(
actualInst[prop]).to.deep.equal(expectedInst[prop],
`Expected actual instruction to have "${prop}": ${expectedInst[prop]}. Received: ${actualInst[prop]} (on index: ${i})`
`Expected actual instruction ${type ? `of ${type}` : ''} to have "${prop}": ${expectedInst[prop]}. Received: ${actualInst[prop]} (on index: ${i})`
);
} else {
expect(
actualInst[prop]).to.equal(expectedInst[prop],
`Expected actual instruction to have "${prop}": ${expectedInst[prop]}. Received: ${actualInst[prop]} (on index: ${i})`
`Expected actual instruction ${type ? `of ${type}` : ''} to have "${prop}": ${expectedInst[prop]}. Received: ${actualInst[prop]} (on index: ${i})`
);
}
}
Expand Down

0 comments on commit c74a44c

Please sign in to comment.