Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 92ed26f

Browse files
vicbmhevery
authored andcommitted
fix(NgComponent): Drop cssUrls, leaving cssUrl only
cssUrl can accept either a String or a List<String> It also fixes a bug in the template cache generator where cssUrls was not taken into account previously. BC Break: cssUrls has been dropped
1 parent 136ed23 commit 92ed26f

12 files changed

+118
-55
lines changed

lib/core/directive.dart

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -183,23 +183,10 @@ class NgComponent extends NgAnnotation {
183183
*/
184184
final String templateUrl;
185185

186-
/**
187-
* A CSS URL to load into the shadow DOM.
188-
*/
189-
final String cssUrl;
190-
191186
/**
192187
* A list of CSS URLs to load into the shadow DOM.
193188
*/
194-
final List<String> cssUrls;
195-
196-
List<String> get allCssUrls {
197-
if (cssUrls == null && cssUrl == null) return null;
198-
if (cssUrls == null && cssUrl != null) return [cssUrl];
199-
if (cssUrls != null && cssUrl == null) return cssUrls;
200-
assert(cssUrls != null && cssUrl != null);
201-
return [cssUrl]..addAll(cssUrls);
202-
}
189+
final _cssUrls;
203190

204191
/**
205192
* Set the shadow root applyAuthorStyles property. See shadow-DOM
@@ -223,8 +210,7 @@ class NgComponent extends NgAnnotation {
223210
const NgComponent({
224211
this.template,
225212
this.templateUrl,
226-
this.cssUrl,
227-
this.cssUrls,
213+
cssUrl,
228214
this.applyAuthorStyles,
229215
this.resetStyleInheritance,
230216
this.publishAs,
@@ -233,21 +219,25 @@ class NgComponent extends NgAnnotation {
233219
visibility,
234220
publishTypes : const <Type>[],
235221
exportExpressions,
236-
exportExpressionAttrs
237-
}) : super(selector: selector,
222+
exportExpressionAttrs})
223+
: _cssUrls = cssUrl,
224+
super(selector: selector,
238225
children: NgAnnotation.COMPILE_CHILDREN,
239226
visibility: visibility,
240227
publishTypes: publishTypes,
241228
map: map,
242229
exportExpressions: exportExpressions,
243230
exportExpressionAttrs: exportExpressionAttrs);
244231

232+
List<String> get cssUrls => _cssUrls == null ?
233+
const [] :
234+
_cssUrls is List ? _cssUrls : [_cssUrls];
235+
245236
NgAnnotation cloneWithNewMap(newMap) =>
246237
new NgComponent(
247238
template: template,
248239
templateUrl: templateUrl,
249-
cssUrl: cssUrl,
250-
cssUrls: cssUrls,
240+
cssUrl: cssUrls,
251241
applyAuthorStyles: applyAuthorStyles,
252242
resetStyleInheritance: resetStyleInheritance,
253243
publishAs: publishAs,
@@ -279,15 +269,13 @@ class NgDirective extends NgAnnotation {
279269
static const String CHILDREN_VISIBILITY = 'children';
280270
static const String DIRECT_CHILDREN_VISIBILITY = 'direct_children';
281271

282-
const NgDirective({
283-
children: NgAnnotation.COMPILE_CHILDREN,
272+
const NgDirective({children: NgAnnotation.COMPILE_CHILDREN,
284273
map,
285274
selector,
286275
visibility,
287276
publishTypes : const <Type>[],
288277
exportExpressions,
289-
exportExpressionAttrs
290-
}) : super(selector: selector, children: children, visibility: visibility,
278+
exportExpressionAttrs}) : super(selector: selector, children: children, visibility: visibility,
291279
publishTypes: publishTypes, map: map,
292280
exportExpressions: exportExpressions,
293281
exportExpressionAttrs: exportExpressionAttrs);

lib/core_dom/block_factory.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ class _ComponentFactory {
309309
// so change back to using @import once Chrome bug is fixed or a
310310
// better work around is found.
311311
List<async.Future<String>> cssFutures = new List();
312-
var cssUrls = component.allCssUrls;
313-
if (cssUrls != null) {
312+
var cssUrls = component.cssUrls;
313+
if (cssUrls.isNotEmpty) {
314314
cssUrls.forEach((css) => cssFutures.add( $http.getString(css, cache: $templateCache) ) );
315315
} else {
316316
cssFutures.add( new async.Future.value(null) );

lib/tools/template_cache_generator.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,15 @@ class TemplateCollectingVisitor {
149149
if (arg is NamedExpression) {
150150
NamedExpression namedArg = arg;
151151
var paramName = namedArg.name.label.name;
152-
if (paramName == 'templateUrl' || paramName == 'cssUrl') {
152+
if (paramName == 'templateUrl') {
153153
cacheUris.add(assertString(namedArg.expression).stringValue);
154+
} else if (paramName == 'cssUrl') {
155+
if (namedArg.expression is StringLiteral) {
156+
cacheUris.add(assertString(namedArg.expression).stringValue);
157+
} else {
158+
cacheUris.addAll(assertList(namedArg.expression).elements.map((e) =>
159+
assertString(e).stringValue));
160+
}
154161
}
155162
}
156163
});

test/core/core_directive_spec.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ main() => describe('DirectiveMap', () {
2121
expect(annotation.publishTypes).toEqual([String]);
2222
expect(annotation.template).toEqual('template');
2323
expect(annotation.templateUrl).toEqual('templateUrl');
24-
expect(annotation.cssUrl).toEqual('cssUrl');
2524
expect(annotation.cssUrls).toEqual(['cssUrls']);
2625
expect(annotation.applyAuthorStyles).toEqual(true);
2726
expect(annotation.resetStyleInheritance).toEqual(true);
@@ -73,8 +72,7 @@ main() => describe('DirectiveMap', () {
7372
selector: 'annotated-io',
7473
template: 'template',
7574
templateUrl: 'templateUrl',
76-
cssUrl: 'cssUrl',
77-
cssUrls: const ['cssUrls'],
75+
cssUrl: const ['cssUrls'],
7876
applyAuthorStyles: true,
7977
resetStyleInheritance: true,
8078
publishAs: 'ctrl',

test/core/templateurl_spec.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class HtmlAndCssComponent {
1818
@NgComponent(
1919
selector: 'html-and-css',
2020
templateUrl: 'simple.html',
21-
cssUrls: const ['simple.css', 'another.css'])
21+
cssUrl: const ['simple.css', 'another.css'])
2222
class HtmlAndMultipleCssComponent {
2323
}
2424

test/io/template_cache_generator_spec.dart

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,64 @@ import '../jasmine_syntax.dart';
88
import 'package:unittest/unittest.dart';
99

1010
main() => describe('template_cache_generator', () {
11-
it('should correctly generate the templates cache file', () {
11+
12+
it('should correctly generate the templates cache file (template)', () {
1213
var tmpDir = Directory.systemTemp.createTempSync();
1314
Future flush;
1415
try {
1516
flush = generator.main(['test/io/test_files/templates/main.dart',
16-
Platform.environment['DART_SDK'],
17-
'${tmpDir.path}/generated.dart', 'generated',
18-
'%SYSTEM_PACKAGE_ROOT%',
19-
'/test/io/test_files,rewritten', 'MyComponent3']);
17+
Platform.environment['DART_SDK'],
18+
'${tmpDir.path}/generated.dart', 'generated',
19+
'%SYSTEM_PACKAGE_ROOT%',
20+
'/test/io/test_files,rewritten', 'MyComponent3']);
2021
} catch(_) {
2122
tmpDir.deleteSync(recursive: true);
2223
rethrow;
2324
}
2425
return flush.then((_) {
2526
expect(new File('${tmpDir.path}/generated.dart').readAsStringSync(),
2627
'// GENERATED, DO NOT EDIT!\n'
27-
'library generated;\n'
28-
'\n'
29-
'import \'package:angular/angular.dart\';\n'
30-
'\n'
31-
'primeTemplateCache(TemplateCache tc) {\n'
32-
'tc.put("rewritten/templates/main.html", new HttpResponse(200, r"""Hello World!"""));\n'
33-
'}');
28+
'library generated;\n'
29+
'\n'
30+
'import \'package:angular/angular.dart\';\n'
31+
'\n'
32+
'primeTemplateCache(TemplateCache tc) {\n'
33+
'tc.put("rewritten/templates/main.html", new HttpResponse(200, r"""Hello World!"""));\n'
34+
'}');
3435
}).whenComplete(() {
3536
tmpDir.deleteSync(recursive: true);
3637
});
3738
});
39+
40+
it('should correctly generate the templates cache file (css)', () {
41+
var tmpDir = Directory.systemTemp.createTempSync();
42+
Future flush;
43+
try {
44+
flush = generator.main(['test/io/test_files/cssUrls/main.dart',
45+
Platform.environment['DART_SDK'],
46+
'${tmpDir.path}/generated.dart', 'generated',
47+
'%SYSTEM_PACKAGE_ROOT%',
48+
'/test/io/test_files,rewritten', 'MyComponent3']);
49+
} catch(_) {
50+
tmpDir.deleteSync(recursive: true);
51+
rethrow;
52+
}
53+
return flush.then((_) {
54+
expect(new File('${tmpDir.path}/generated.dart').readAsStringSync(),
55+
'// GENERATED, DO NOT EDIT!\n'
56+
'library generated;\n'
57+
'\n'
58+
'import \'package:angular/angular.dart\';\n'
59+
'\n'
60+
'primeTemplateCache(TemplateCache tc) {\n'
61+
'tc.put("rewritten/cssUrls/one.css", new HttpResponse(200, r"""body {}"""));\n'
62+
'tc.put("rewritten/cssUrls/two.css", new HttpResponse(200, r"""body {}"""));\n'
63+
'tc.put("rewritten/cssUrls/three.css", new HttpResponse(200, r"""body {}"""));\n'
64+
'}');
65+
}).whenComplete(() {
66+
//tmpDir.deleteSync(recursive: true);
67+
});
68+
});
69+
70+
3871
});

test/io/test_files/cssUrls/main.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
library test_files.main;
2+
3+
import 'package:angular/core/module.dart';
4+
import 'package:angular/tools/template_cache_annotation.dart';
5+
6+
@NgComponent(
7+
selector: 'my-component',
8+
cssUrl: '/test/io/test_files/cssUrls/one.css')
9+
class MyComponent
10+
{
11+
}
12+
13+
@NgComponent(
14+
selector: 'my-component2',
15+
cssUrl: [
16+
'/test/io/test_files/cssUrls/two.css',
17+
'/test/io/test_files/cssUrls/three.css'])
18+
class MyComponent2
19+
{
20+
}
21+
22+
@NgComponent(
23+
selector: 'my-component3',
24+
cssUrl: '/test/io/test_files/cssUrls/four.css')
25+
class MyComponent3
26+
{
27+
}

test/io/test_files/cssUrls/one.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
body {}

test/io/test_files/cssUrls/three.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
body {}

test/io/test_files/cssUrls/two.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
body {}

test/io/test_files/main.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ class NgIfDirective {
1818
},
1919
template: '<div>{{ctrl.inline.template.expression}}</div>',
2020
exportExpressionAttrs: const ['exported-attr'],
21-
exportExpressions: const ['exported + expression']
22-
)
21+
exportExpressions: const ['exported + expression'])
2322
class MyComponent {
2423
@NgOneWay('another-expression')
2524
String anotherExpression;
@@ -30,4 +29,12 @@ class MyComponent {
3029
set twoWayStuff(String abc) {}
3130
@NgTwoWay('two-way-stuff')
3231
String get twoWayStuff => null;
33-
}
32+
}
33+
34+
class CssUrlsString {
35+
36+
}
37+
38+
class CssUrlsList {
39+
40+
}

test/io/test_files/templates/main.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ import 'package:angular/tools/template_cache_annotation.dart';
55

66
@NgComponent(
77
selector: 'my-component',
8-
templateUrl: '/test/io/test_files/templates/main.html'
9-
)
8+
templateUrl: '/test/io/test_files/templates/main.html')
109
@NgTemplateCache()
11-
class MyComponent {
10+
class MyComponent
11+
{
1212
}
1313

1414
@NgComponent(
1515
selector: 'my-component2',
16-
templateUrl: '/test/io/test_files/templates/dont.html'
17-
)
16+
templateUrl: '/test/io/test_files/templates/dont.html')
1817
@NgTemplateCache(cache: false)
19-
class MyComponent2 {
18+
class MyComponent2
19+
{
2020
}
2121

2222

2323
@NgComponent(
2424
selector: 'my-component3',
25-
templateUrl: '/test/io/test_files/templates/dont.html'
26-
)
25+
templateUrl: '/test/io/test_files/templates/dont.html')
2726
@NgTemplateCache(cache: true)
28-
class MyComponent3 {
27+
class MyComponent3
28+
{
2929
}

0 commit comments

Comments
 (0)