2015-09-14 18:59:09 -04:00
|
|
|
import {
|
|
|
|
AsyncTestCompleter,
|
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
el,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
inject,
|
|
|
|
it,
|
|
|
|
xit,
|
|
|
|
TestComponentBuilder,
|
|
|
|
beforeEachBindings
|
|
|
|
} from 'angular2/test_lib';
|
|
|
|
|
|
|
|
import {
|
2015-09-18 13:33:23 -04:00
|
|
|
CompileTypeMetadata,
|
|
|
|
CompileTemplateMetadata
|
2015-09-14 18:59:09 -04:00
|
|
|
} from 'angular2/src/compiler/directive_metadata';
|
|
|
|
import {ViewEncapsulation} from 'angular2/src/core/render/api';
|
|
|
|
|
|
|
|
import {TemplateNormalizer} from 'angular2/src/compiler/template_normalizer';
|
|
|
|
import {XHR} from 'angular2/src/core/render/xhr';
|
|
|
|
import {MockXHR} from 'angular2/src/core/render/xhr_mock';
|
|
|
|
import {TEST_BINDINGS} from './test_bindings';
|
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('TemplateNormalizer', () => {
|
2015-09-18 13:33:23 -04:00
|
|
|
var dirType: CompileTypeMetadata;
|
2015-09-14 18:59:09 -04:00
|
|
|
|
|
|
|
beforeEachBindings(() => TEST_BINDINGS);
|
|
|
|
|
2015-09-18 13:33:23 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
dirType = new CompileTypeMetadata({moduleId: 'some/module/id', name: 'SomeComp'});
|
|
|
|
});
|
2015-09-14 18:59:09 -04:00
|
|
|
|
|
|
|
describe('loadTemplate', () => {
|
|
|
|
describe('inline template', () => {
|
|
|
|
it('should store the template',
|
|
|
|
inject([AsyncTestCompleter, TemplateNormalizer],
|
|
|
|
(async, normalizer: TemplateNormalizer) => {
|
2015-09-18 13:33:23 -04:00
|
|
|
normalizer.normalizeTemplate(dirType, new CompileTemplateMetadata({
|
2015-09-14 18:59:09 -04:00
|
|
|
encapsulation: null,
|
|
|
|
template: 'a',
|
|
|
|
templateUrl: null,
|
|
|
|
styles: [],
|
|
|
|
styleUrls: ['test.css']
|
|
|
|
}))
|
2015-09-18 13:33:23 -04:00
|
|
|
.then((template: CompileTemplateMetadata) => {
|
2015-09-14 18:59:09 -04:00
|
|
|
expect(template.template).toEqual('a');
|
2015-09-18 13:33:23 -04:00
|
|
|
expect(template.templateUrl).toEqual('some/module/id');
|
2015-09-14 18:59:09 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should resolve styles on the annotation against the moduleId',
|
|
|
|
inject([AsyncTestCompleter, TemplateNormalizer],
|
|
|
|
(async, normalizer: TemplateNormalizer) => {
|
2015-09-18 13:33:23 -04:00
|
|
|
normalizer.normalizeTemplate(dirType, new CompileTemplateMetadata({
|
2015-09-14 18:59:09 -04:00
|
|
|
encapsulation: null,
|
|
|
|
template: '',
|
|
|
|
templateUrl: null,
|
|
|
|
styles: [],
|
|
|
|
styleUrls: ['test.css']
|
|
|
|
}))
|
2015-09-18 13:33:23 -04:00
|
|
|
.then((template: CompileTemplateMetadata) => {
|
|
|
|
expect(template.styleUrls).toEqual(['some/module/test.css']);
|
2015-09-14 18:59:09 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should resolve styles in the template against the moduleId',
|
|
|
|
inject([AsyncTestCompleter, TemplateNormalizer],
|
|
|
|
(async, normalizer: TemplateNormalizer) => {
|
2015-09-18 13:33:23 -04:00
|
|
|
normalizer.normalizeTemplate(dirType, new CompileTemplateMetadata({
|
2015-09-14 18:59:09 -04:00
|
|
|
encapsulation: null,
|
|
|
|
template: '<style>@import test.css</style>',
|
|
|
|
templateUrl: null,
|
|
|
|
styles: [],
|
|
|
|
styleUrls: []
|
|
|
|
}))
|
2015-09-18 13:33:23 -04:00
|
|
|
.then((template: CompileTemplateMetadata) => {
|
|
|
|
expect(template.styleUrls).toEqual(['some/module/test.css']);
|
2015-09-14 18:59:09 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('templateUrl', () => {
|
|
|
|
|
|
|
|
it('should load a template from a url that is resolved against moduleId',
|
|
|
|
inject([AsyncTestCompleter, TemplateNormalizer, XHR],
|
|
|
|
(async, normalizer: TemplateNormalizer, xhr: MockXHR) => {
|
|
|
|
xhr.expect('some/module/sometplurl', 'a');
|
2015-09-18 13:33:23 -04:00
|
|
|
normalizer.normalizeTemplate(dirType, new CompileTemplateMetadata({
|
2015-09-14 18:59:09 -04:00
|
|
|
encapsulation: null,
|
|
|
|
template: null,
|
|
|
|
templateUrl: 'sometplurl',
|
|
|
|
styles: [],
|
|
|
|
styleUrls: ['test.css']
|
|
|
|
}))
|
2015-09-18 13:33:23 -04:00
|
|
|
.then((template: CompileTemplateMetadata) => {
|
2015-09-14 18:59:09 -04:00
|
|
|
expect(template.template).toEqual('a');
|
2015-09-18 13:33:23 -04:00
|
|
|
expect(template.templateUrl).toEqual('some/module/sometplurl');
|
2015-09-14 18:59:09 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
xhr.flush();
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should resolve styles on the annotation against the moduleId',
|
|
|
|
inject([AsyncTestCompleter, TemplateNormalizer, XHR],
|
|
|
|
(async, normalizer: TemplateNormalizer, xhr: MockXHR) => {
|
|
|
|
xhr.expect('some/module/tpl/sometplurl', '');
|
2015-09-18 13:33:23 -04:00
|
|
|
normalizer.normalizeTemplate(dirType, new CompileTemplateMetadata({
|
2015-09-14 18:59:09 -04:00
|
|
|
encapsulation: null,
|
|
|
|
template: null,
|
|
|
|
templateUrl: 'tpl/sometplurl',
|
|
|
|
styles: [],
|
|
|
|
styleUrls: ['test.css']
|
|
|
|
}))
|
2015-09-18 13:33:23 -04:00
|
|
|
.then((template: CompileTemplateMetadata) => {
|
|
|
|
expect(template.styleUrls).toEqual(['some/module/test.css']);
|
2015-09-14 18:59:09 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
xhr.flush();
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should resolve styles in the template against the templateUrl',
|
|
|
|
inject([AsyncTestCompleter, TemplateNormalizer, XHR],
|
|
|
|
(async, normalizer: TemplateNormalizer, xhr: MockXHR) => {
|
|
|
|
xhr.expect('some/module/tpl/sometplurl', '<style>@import test.css</style>');
|
2015-09-18 13:33:23 -04:00
|
|
|
normalizer.normalizeTemplate(dirType, new CompileTemplateMetadata({
|
2015-09-14 18:59:09 -04:00
|
|
|
encapsulation: null,
|
|
|
|
template: null,
|
|
|
|
templateUrl: 'tpl/sometplurl',
|
|
|
|
styles: [],
|
|
|
|
styleUrls: []
|
|
|
|
}))
|
2015-09-18 13:33:23 -04:00
|
|
|
.then((template: CompileTemplateMetadata) => {
|
|
|
|
expect(template.styleUrls).toEqual(['some/module/tpl/test.css']);
|
2015-09-14 18:59:09 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
xhr.flush();
|
|
|
|
}));
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-09-18 13:33:23 -04:00
|
|
|
it('should throw if no template was specified',
|
|
|
|
inject([TemplateNormalizer], (normalizer: TemplateNormalizer) => {
|
|
|
|
expect(() => normalizer.normalizeTemplate(
|
|
|
|
dirType, new CompileTemplateMetadata(
|
|
|
|
{encapsulation: null, styles: [], styleUrls: []})))
|
|
|
|
.toThrowError('No template specified for component SomeComp');
|
|
|
|
}));
|
|
|
|
|
2015-09-14 18:59:09 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('normalizeLoadedTemplate', () => {
|
|
|
|
it('should store the viewEncapsulationin the result',
|
|
|
|
inject([TemplateNormalizer], (normalizer: TemplateNormalizer) => {
|
|
|
|
|
|
|
|
var viewEncapsulation = ViewEncapsulation.Native;
|
|
|
|
var template = normalizer.normalizeLoadedTemplate(
|
2015-09-18 13:33:23 -04:00
|
|
|
dirType, new CompileTemplateMetadata(
|
|
|
|
{encapsulation: viewEncapsulation, styles: [], styleUrls: []}),
|
2015-09-14 18:59:09 -04:00
|
|
|
'', 'some/module/');
|
|
|
|
expect(template.encapsulation).toBe(viewEncapsulation);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should keep the template as html',
|
|
|
|
inject([TemplateNormalizer], (normalizer: TemplateNormalizer) => {
|
|
|
|
var template = normalizer.normalizeLoadedTemplate(
|
2015-09-18 13:33:23 -04:00
|
|
|
dirType,
|
|
|
|
new CompileTemplateMetadata({encapsulation: null, styles: [], styleUrls: []}), 'a',
|
2015-09-14 18:59:09 -04:00
|
|
|
'some/module/');
|
|
|
|
expect(template.template).toEqual('a')
|
|
|
|
}));
|
|
|
|
|
2015-09-18 13:33:23 -04:00
|
|
|
it('should collect ngContent',
|
2015-09-14 18:59:09 -04:00
|
|
|
inject([TemplateNormalizer], (normalizer: TemplateNormalizer) => {
|
|
|
|
var template = normalizer.normalizeLoadedTemplate(
|
2015-09-18 13:33:23 -04:00
|
|
|
dirType,
|
|
|
|
new CompileTemplateMetadata({encapsulation: null, styles: [], styleUrls: []}),
|
2015-09-14 18:59:09 -04:00
|
|
|
'<ng-content select="a"></ng-content>', 'some/module/');
|
|
|
|
expect(template.ngContentSelectors).toEqual(['a']);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should normalize ngContent wildcard selector',
|
|
|
|
inject([TemplateNormalizer], (normalizer: TemplateNormalizer) => {
|
|
|
|
var template = normalizer.normalizeLoadedTemplate(
|
2015-09-18 13:33:23 -04:00
|
|
|
dirType,
|
|
|
|
new CompileTemplateMetadata({encapsulation: null, styles: [], styleUrls: []}),
|
2015-09-14 18:59:09 -04:00
|
|
|
'<ng-content></ng-content><ng-content select></ng-content><ng-content select="*"></ng-content>',
|
|
|
|
'some/module/');
|
|
|
|
expect(template.ngContentSelectors).toEqual(['*', '*', '*']);
|
|
|
|
}));
|
|
|
|
|
2015-09-18 13:33:23 -04:00
|
|
|
it('should collect top level styles in the template',
|
2015-09-14 18:59:09 -04:00
|
|
|
inject([TemplateNormalizer], (normalizer: TemplateNormalizer) => {
|
|
|
|
var template = normalizer.normalizeLoadedTemplate(
|
2015-09-18 13:33:23 -04:00
|
|
|
dirType,
|
|
|
|
new CompileTemplateMetadata({encapsulation: null, styles: [], styleUrls: []}),
|
2015-09-14 18:59:09 -04:00
|
|
|
'<style>a</style>', 'some/module/');
|
|
|
|
expect(template.styles).toEqual(['a']);
|
|
|
|
}));
|
|
|
|
|
2015-09-18 13:33:23 -04:00
|
|
|
it('should collect styles inside in elements',
|
2015-09-14 18:59:09 -04:00
|
|
|
inject([TemplateNormalizer], (normalizer: TemplateNormalizer) => {
|
|
|
|
var template = normalizer.normalizeLoadedTemplate(
|
2015-09-18 13:33:23 -04:00
|
|
|
dirType,
|
|
|
|
new CompileTemplateMetadata({encapsulation: null, styles: [], styleUrls: []}),
|
2015-09-14 18:59:09 -04:00
|
|
|
'<div><style>a</style></div>', 'some/module/');
|
|
|
|
expect(template.styles).toEqual(['a']);
|
|
|
|
}));
|
|
|
|
|
2015-09-18 13:33:23 -04:00
|
|
|
it('should collect styleUrls in the template',
|
2015-09-14 18:59:09 -04:00
|
|
|
inject([TemplateNormalizer], (normalizer: TemplateNormalizer) => {
|
|
|
|
var template = normalizer.normalizeLoadedTemplate(
|
2015-09-18 13:33:23 -04:00
|
|
|
dirType,
|
|
|
|
new CompileTemplateMetadata({encapsulation: null, styles: [], styleUrls: []}),
|
2015-09-14 18:59:09 -04:00
|
|
|
'<link rel="stylesheet" href="aUrl">', 'some/module/');
|
2015-09-18 13:33:23 -04:00
|
|
|
expect(template.styleUrls).toEqual(['some/module/aUrl']);
|
2015-09-14 18:59:09 -04:00
|
|
|
}));
|
|
|
|
|
2015-09-18 13:33:23 -04:00
|
|
|
it('should collect styleUrls in elements',
|
2015-09-14 18:59:09 -04:00
|
|
|
inject([TemplateNormalizer], (normalizer: TemplateNormalizer) => {
|
|
|
|
var template = normalizer.normalizeLoadedTemplate(
|
2015-09-18 13:33:23 -04:00
|
|
|
dirType,
|
|
|
|
new CompileTemplateMetadata({encapsulation: null, styles: [], styleUrls: []}),
|
2015-09-14 18:59:09 -04:00
|
|
|
'<div><link rel="stylesheet" href="aUrl"></div>', 'some/module/');
|
2015-09-18 13:33:23 -04:00
|
|
|
expect(template.styleUrls).toEqual(['some/module/aUrl']);
|
2015-09-14 18:59:09 -04:00
|
|
|
}));
|
|
|
|
|
2015-09-18 13:33:23 -04:00
|
|
|
it('should ignore link elements with non stylesheet rel attribute',
|
2015-09-14 18:59:09 -04:00
|
|
|
inject([TemplateNormalizer], (normalizer: TemplateNormalizer) => {
|
|
|
|
var template = normalizer.normalizeLoadedTemplate(
|
2015-09-18 13:33:23 -04:00
|
|
|
dirType,
|
|
|
|
new CompileTemplateMetadata({encapsulation: null, styles: [], styleUrls: []}),
|
2015-09-14 18:59:09 -04:00
|
|
|
'<link href="b" rel="a"></link>', 'some/module/');
|
2015-09-18 13:33:23 -04:00
|
|
|
expect(template.styleUrls).toEqual([]);
|
2015-09-14 18:59:09 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
it('should extract @import style urls into styleAbsUrl',
|
|
|
|
inject([TemplateNormalizer], (normalizer: TemplateNormalizer) => {
|
|
|
|
var template = normalizer.normalizeLoadedTemplate(
|
2015-09-18 13:33:23 -04:00
|
|
|
dirType, new CompileTemplateMetadata(
|
2015-09-14 18:59:09 -04:00
|
|
|
{encapsulation: null, styles: ['@import "test.css";'], styleUrls: []}),
|
|
|
|
'', 'some/module/id');
|
|
|
|
expect(template.styles).toEqual(['']);
|
2015-09-18 13:33:23 -04:00
|
|
|
expect(template.styleUrls).toEqual(['some/module/test.css']);
|
2015-09-14 18:59:09 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
it('should resolve relative urls in inline styles',
|
|
|
|
inject([TemplateNormalizer], (normalizer: TemplateNormalizer) => {
|
|
|
|
var template = normalizer.normalizeLoadedTemplate(
|
2015-09-18 13:33:23 -04:00
|
|
|
dirType, new CompileTemplateMetadata({
|
2015-09-14 18:59:09 -04:00
|
|
|
encapsulation: null,
|
|
|
|
styles: ['.foo{background-image: url(\'double.jpg\');'],
|
|
|
|
styleUrls: []
|
|
|
|
}),
|
|
|
|
'', 'some/module/id');
|
|
|
|
expect(template.styles)
|
|
|
|
.toEqual(['.foo{background-image: url(\'some/module/double.jpg\');']);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should resolve relative style urls in styleUrls',
|
|
|
|
inject([TemplateNormalizer], (normalizer: TemplateNormalizer) => {
|
|
|
|
var template = normalizer.normalizeLoadedTemplate(
|
2015-09-18 13:33:23 -04:00
|
|
|
dirType, new CompileTemplateMetadata(
|
|
|
|
{encapsulation: null, styles: [], styleUrls: ['test.css']}),
|
|
|
|
'', 'some/module/id');
|
2015-09-14 18:59:09 -04:00
|
|
|
expect(template.styles).toEqual([]);
|
2015-09-18 13:33:23 -04:00
|
|
|
expect(template.styleUrls).toEqual(['some/module/test.css']);
|
2015-09-14 18:59:09 -04:00
|
|
|
}));
|
|
|
|
|
2015-09-18 13:33:23 -04:00
|
|
|
it('should normalize ViewEncapsulation.Emulated to ViewEncapsulation.None if there are no stlyes nor stylesheets',
|
|
|
|
inject([TemplateNormalizer], (normalizer: TemplateNormalizer) => {
|
|
|
|
var template = normalizer.normalizeLoadedTemplate(
|
|
|
|
dirType, new CompileTemplateMetadata(
|
|
|
|
{encapsulation: ViewEncapsulation.Emulated, styles: [], styleUrls: []}),
|
|
|
|
'', 'some/module/id');
|
|
|
|
expect(template.encapsulation).toEqual(ViewEncapsulation.None);
|
|
|
|
}));
|
2015-09-14 18:59:09 -04:00
|
|
|
|
2015-09-18 13:33:23 -04:00
|
|
|
it('should ignore ng-content in elements with ng-non-bindable',
|
2015-09-18 13:33:23 -04:00
|
|
|
inject([TemplateNormalizer], (normalizer: TemplateNormalizer) => {
|
|
|
|
var template = normalizer.normalizeLoadedTemplate(
|
|
|
|
dirType,
|
|
|
|
new CompileTemplateMetadata({encapsulation: null, styles: [], styleUrls: []}),
|
2015-09-18 13:33:23 -04:00
|
|
|
'<div ng-non-bindable><ng-content select="a"></ng-content></div>', 'some/module/');
|
2015-09-18 13:33:23 -04:00
|
|
|
expect(template.ngContentSelectors).toEqual([]);
|
|
|
|
}));
|
2015-09-14 18:59:09 -04:00
|
|
|
|
2015-09-18 13:33:23 -04:00
|
|
|
it('should still collect <style> in elements with ng-non-bindable',
|
|
|
|
inject([TemplateNormalizer], (normalizer: TemplateNormalizer) => {
|
|
|
|
var template = normalizer.normalizeLoadedTemplate(
|
|
|
|
dirType,
|
|
|
|
new CompileTemplateMetadata({encapsulation: null, styles: [], styleUrls: []}),
|
|
|
|
'<div ng-non-bindable><style>div {color:red}</style></div>', 'some/module/');
|
|
|
|
expect(template.styles).toEqual(['div {color:red}']);
|
|
|
|
}));
|
2015-09-18 13:33:23 -04:00
|
|
|
});
|
2015-09-14 18:59:09 -04:00
|
|
|
});
|
|
|
|
}
|