Skip to content

Commit

Permalink
feat(template-compiler): as-element (#146)
Browse files Browse the repository at this point in the history
* feat(template-compiler): as-element

* remove redundant test
  • Loading branch information
bigopon authored and EisenbergEffect committed Sep 9, 2018
1 parent eccc71f commit 4aa8538
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/jit/src/templating/template-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export class TemplateCompiler implements ITemplateCompiler {
// if there is a custom element or template controller, then the attribute instructions become children
// of the hydrate instruction, otherwise they are added directly to instructions as a single array
const attributeInstructions: TargetedInstruction[] = [];
const tagResourceKey = tagName.toLowerCase();
const tagResourceKey = (node.getAttribute('as-element') || tagName).toLowerCase();
const element = resources.find(CustomElementResource, tagResourceKey) || null;
const attributes = node.attributes;
for (let i = 0, ii = attributes.length; i < ii; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@ function stringify(o) {


describe('TemplateCompiler (integration)', () => {
let container: IContainer;
let au: Aurelia;
let host: HTMLElement;
let component: ReturnType<typeof createCustomElement>;
let cs: IChangeSet

beforeEach(() => {
const container = DI.createContainer();
container = DI.createContainer();
cs = container.get(IChangeSet);
container.register(TestConfiguration, BasicConfiguration)
host = document.createElement('app');
Expand Down Expand Up @@ -519,6 +520,26 @@ describe('TemplateCompiler (integration)', () => {
expect(host.textContent).to.equal('bigopon');
});

describe('[as-element]', () => {

it('works with custom element with [as-element]', () => {
component = createCustomElement(
`<template><div as-element="name-tag" name="bigopon"></div></template>`,
);
au.app({ host, component: component }).start();
cs.flushChanges();
expect(host.textContent).to.equal('bigopon');
});

it('ignores tag name', () => {
component = createCustomElement(
`<template><name-tag as-element="div" name="bigopon">Fred</name-tag></template>`,
);
au.app({ host, component: component }).start();
expect(host.textContent).to.equal('Fred');
});
});

it('<let/>', () => {
component = createCustomElement(
'<template><let full-name.bind="firstName + ` ` + lastName"></let><div>\${fullName}</div></template>',
Expand Down
17 changes: 17 additions & 0 deletions packages/jit/test/unit/templating/template-compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,23 @@ describe('TemplateCompiler', () => {
type: TargetedInstructionType.propertyBinding, dest: 'title', srcOrExpr: new AccessScope('title') },
]);
});

describe('[as-element]', () => {
it('understands [as-element]', () => {
@customElement('not-div')
class NotDiv {}
const { instructions } = compileWith('<template><div as-element="not-div"></div></template>', [NotDiv]);
verifyInstructions(instructions[0] as any, [
{ toVerify: ['type', 'res'],
type: TargetedInstructionType.hydrateElement, res: 'not-div' }
]);
});

it('does not throw when element is not found', () => {
const { instructions } = compileWith('<template><div as-element="not-div"></div></template>');
expect(instructions.length).to.equal(0);
});
});
});

describe('<let/> element', () => {
Expand Down

0 comments on commit 4aa8538

Please sign in to comment.