This repository has been archived by the owner on Sep 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 231
/
view_compiler_utils.dart
602 lines (570 loc) · 17.6 KB
/
view_compiler_utils.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
import 'dart:collection';
import 'package:angular/src/core/linker/view_type.dart';
import '../compile_metadata.dart'
show
CompileTokenMetadata,
CompileDirectiveMetadata,
CompileIdentifierMetadata;
import '../expression_parser/ast.dart' as ast;
import '../identifiers.dart';
import '../output/convert.dart' show typeArgumentsFrom;
import '../output/output_ast.dart' as o;
import '../template_ast.dart' show AttrAst;
import 'compile_view.dart' show CompileView;
import 'constants.dart';
// List of supported namespaces.
const namespaceUris = {
'xlink': 'http://www.w3.org/1999/xlink',
'svg': 'http://www.w3.org/2000/svg',
'xhtml': 'http://www.w3.org/1999/xhtml'
};
/// Creating outlines for faster builds is preventing auto input change
/// detection for now. The following flag should be removed to reenable in the
/// future.
const bool outlinerDeprecated = false;
/// Variable name used to read viewData.parentIndex in build functions.
const String cachedParentIndexVarName = 'parentIdx';
// Creates method parameters list for AppView set attribute calls.
List<o.Expression> createSetAttributeParams(o.Expression renderNode,
String attrNs, String attrName, o.Expression valueExpr) {
if (attrNs != null) {
return [renderNode, o.literal(attrNs), o.literal(attrName), valueExpr];
} else {
return [renderNode, o.literal(attrName), valueExpr];
}
}
final _unsafeCastFn = o.importExpr(Identifiers.unsafeCast);
/// Returns `unsafeCast<{Cast}>(expression)`.
o.Expression _unsafeCast(o.Expression expression, [o.OutputType cast]) {
return _unsafeCastFn.callFn(
[expression],
typeArguments: cast != null ? [cast] : const [],
);
}
o.Expression getPropertyInView(
o.Expression property,
CompileView callingView,
CompileView definedView, {
bool forceCast = false,
}) {
if (identical(callingView, definedView)) {
return property;
} else {
o.Expression viewProp;
CompileView currView = callingView;
while (!identical(currView, definedView) &&
currView.declarationElement.view != null) {
currView = currView.declarationElement.view;
viewProp = viewProp == null
? o.ReadClassMemberExpr('parentView')
: viewProp.prop('parentView');
}
if (!identical(currView, definedView)) {
throw StateError('Internal error: Could not calculate a property '
'in a parent view: $property');
}
o.ReadClassMemberExpr readMemberExpr = unwrapDirective(property);
if (readMemberExpr != null) {
// Note: Don't cast for members of the AppView base class...
if (definedView.storage.fields
.any((field) => field.name == readMemberExpr.name) ||
definedView.getters
.any((field) => field.name == readMemberExpr.name)) {
viewProp = _unsafeCast(viewProp, definedView.classType);
}
} else if (forceCast) {
viewProp = _unsafeCast(viewProp, definedView.classType);
}
return o.replaceReadClassMemberInExpression(viewProp, property);
}
}
o.Expression injectFromViewParentInjector(
CompileView view,
CompileTokenMetadata token,
bool optional,
) {
o.Expression viewExpr = (view.viewType == ViewType.host)
? o.THIS_EXPR
: o.ReadClassMemberExpr('parentView');
var args = [
createDiTokenExpression(token),
o.ReadClassMemberExpr('viewData').prop('parentIndex')
];
if (optional) {
args.add(o.NULL_EXPR);
}
return viewExpr.callMethod('injectorGet', args);
}
/// Returns the name of a [component] view factory for [index].
///
/// Each generated view of [component], be it component, host, or embedded has
/// an associated [index] that is used to distinguish between embedded views.
String getViewFactoryName(CompileDirectiveMetadata component, int index) =>
'viewFactory_${component.type.name}$index';
/// Returns a callable expression for the [component] view factory named [name].
///
/// If [component] is generic, the view factory will flow the type parameters of
/// the parent view as type arguments to the embedded view.
///
/// **Note:** It's assumed that [name] originates from an invocation of
/// [getViewFactoryName] with the same [component].
o.Expression getViewFactory(
CompileDirectiveMetadata component,
String name,
) {
final viewFactoryVar = o.variable(name);
if (component.originType.typeParameters.isEmpty) {
return viewFactoryVar;
}
final parameters = [o.FnParam('parentView'), o.FnParam('parentIndex')];
final arguments = parameters.map((p) => o.variable(p.name)).toList();
return o.FunctionExpr(parameters, [
o.ReturnStatement(o.InvokeFunctionExpr(
viewFactoryVar,
arguments,
typeArgumentsFrom(component.originType.typeParameters),
)),
]);
}
o.Expression createDiTokenExpression(CompileTokenMetadata token) {
if (token.identifierIsInstance) {
return o.importExpr(token.identifier).instantiate(
// If there is also a value, assume it is the first argument.
//
// i.e. const OpaqueToken('literalValue')
token.value != null ? [o.literal(token.value)] : const <o.Expression>[],
type: o.importType(token.identifier, [], [o.TypeModifier.Const]),
// Add any generic types attached to the type.
//
// Only a value of `null` precisely means "no generic types", not [].
genericTypes: token.identifier.typeArguments.isNotEmpty
? token.identifier.typeArguments
: null);
} else if (token.value != null) {
return o.literal(token.value);
} else {
return o.importExpr(token.identifier);
}
}
o.Expression createDebugInfoTokenExpression(CompileTokenMetadata token) {
if (token.value != null) {
return o.literal(token.value);
} else if (token.identifierIsInstance) {
return o
.importExpr(token.identifier)
.instantiate([], type: o.importType(token.identifier, []));
} else {
return o.importExpr(token.identifier);
}
}
o.Expression createFlatArray(List<o.Expression> expressions,
{bool constForEmpty = true}) {
// Simplify: No items.
if (expressions.isEmpty) {
return o.literalArr(
const [],
o.ArrayType(
null, constForEmpty ? const [o.TypeModifier.Const] : const []),
);
}
// Check for [].addAll([x,y,z]) case and optimize.
if (expressions.length == 1) {
if (expressions[0].type is o.ArrayType) {
return expressions[0];
} else {
return o.literalArr([expressions[0]]);
}
}
var lastNonArrayExpressions = <o.Expression>[];
o.Expression result = o.literalArr([]);
bool initialEmptyArray = true;
for (var i = 0; i < expressions.length; i++) {
var expr = expressions[i];
if (expr.type is o.ArrayType) {
if (lastNonArrayExpressions.length > 0) {
if (initialEmptyArray) {
result = o.literalArr(lastNonArrayExpressions, o.DYNAMIC_TYPE);
initialEmptyArray = false;
} else {
result = result.callMethod(o.BuiltinMethod.ConcatArray,
[o.literalArr(lastNonArrayExpressions)]);
}
lastNonArrayExpressions = [];
}
result = initialEmptyArray
? o.literalArr([expr], o.DYNAMIC_TYPE)
: result.callMethod(o.BuiltinMethod.ConcatArray, [expr]);
initialEmptyArray = false;
} else {
lastNonArrayExpressions.add(expr);
}
}
if (lastNonArrayExpressions.length > 0) {
if (initialEmptyArray) {
result = o.literalArr(lastNonArrayExpressions);
} else {
result = result.callMethod(
o.BuiltinMethod.ConcatArray, [o.literalArr(lastNonArrayExpressions)]);
}
}
return result;
}
/// Converts a reference, literal or existing expression to provider value.
o.Expression convertValueToOutputAst(dynamic value) {
if (value is CompileIdentifierMetadata) {
return o.importExpr(value);
} else if (value is CompileTokenMetadata) {
return createDiTokenExpression(value);
} else if (value is o.Expression) {
return value;
} else {
return o.literal(value);
}
}
CompileDirectiveMetadata componentFromDirectives(
List<CompileDirectiveMetadata> directives) {
for (CompileDirectiveMetadata directive in directives) {
if (directive.isComponent) return directive;
}
return null;
}
// Detect _PopupSourceDirective_0_6.instance for directives that have
// change detectors and unwrap to change detector.
o.Expression unwrapDirectiveInstance(o.Expression directiveInstance) {
if (directiveInstance is o.ReadPropExpr &&
directiveInstance.name == 'instance' &&
(directiveInstance.receiver is o.ReadClassMemberExpr ||
directiveInstance.receiver is o.ReadPropExpr)) {
return directiveInstance.receiver;
}
return null;
}
// Return instance of directive for both regular directives and directives
// with ChangeDetector class.
o.Expression unwrapDirective(o.Expression directiveInstance) {
var instance = unwrapDirectiveInstance(directiveInstance);
if (instance != null) {
return instance;
} else if (directiveInstance is o.ReadClassMemberExpr) {
// Non change detector directive read.
return directiveInstance;
}
return null;
}
String toTemplateExtension(String moduleUrl) {
if (!moduleUrl.endsWith('.dart')) return moduleUrl;
return moduleUrl.substring(0, moduleUrl.length - 5) + '.template.dart';
}
Map<String, AttrAst> astAttribListToMap(List<AttrAst> attrs) {
Map<String, AttrAst> htmlAttrs = {};
for (AttrAst attr in attrs) {
htmlAttrs[attr.name] = attr;
}
return htmlAttrs;
}
ast.AST _mergeAttributeValue(
String attrName, ast.AST attrValue1, ast.AST attrValue2) {
if (attrName == classAttrName || attrName == styleAttrName) {
// attrValue1 can be a literal string, an expression, or an interpolation
// attrValue2 can be a literal string or an expression, it CANNOT be an
// interpolate because attrValue2 represents the "new" attribute value
// we need to merge in, which must always be a literal or property access.
// Only the "previous" attrValue can be an interpolation because we are
// constructing the interpolation here.
if (attrValue1 is ast.LiteralPrimitive &&
attrValue2 is ast.LiteralPrimitive) {
return ast.LiteralPrimitive('${attrValue1.value} ${attrValue2.value}');
} else if (attrValue1 is ast.Interpolation) {
if (attrValue2 is ast.LiteralPrimitive) {
attrValue1.strings.last += ' ${attrValue2.value}';
return attrValue1;
} else {
attrValue1.expressions.add(attrValue2);
attrValue1.strings.add(' ');
return attrValue1;
}
} else {
return ast.Interpolation(['', ' ', ''], [attrValue1, attrValue2]);
}
} else {
return attrValue2;
}
}
o.Statement createSetAttributeStatement(String astNodeName,
o.Expression renderNode, String attrName, o.Expression attrValue) {
String attrNs;
// Copy of the logic in property_binder.dart.
//
// Unfortunately host attributes are treated differently (for historic
// reasons), and they do run through property_binder. We should eventually
// refactor and not have duplicate code here.
if (attrName.endsWith('.if')) {
attrName = attrName.substring(0, attrName.length - 3);
attrValue = attrValue.conditional(o.literal(''), o.NULL_EXPR);
}
if (attrName.startsWith('@') && attrName.contains(':')) {
var nameParts = attrName.substring(1).split(':');
attrNs = namespaceUris[nameParts[0]];
attrName = nameParts[1];
}
/// Optimization for common attributes. Call dart:html directly without
/// going through setAttr wrapper.
if (attrNs == null) {
switch (attrName) {
case 'class':
// Remove check below after SVGSVGElement DDC bug is fixed b2/32931607
bool hasNamespace =
astNodeName.startsWith('@') || astNodeName.contains(':');
if (!hasNamespace) {
return renderNode.prop('className').set(attrValue).toStmt();
}
break;
case 'tabindex':
case 'tabIndex':
try {
if (attrValue is o.LiteralExpr) {
final value = attrValue.value;
if (value is String) {
final tabValue = int.parse(value);
return renderNode
.prop('tabIndex')
.set(o.literal(tabValue))
.toStmt();
}
} else {
// Assume it's an int field
return renderNode.prop('tabIndex').set(attrValue).toStmt();
}
} catch (_) {
// fallthrough to default handler since index is not int.
}
break;
default:
break;
}
}
var params =
createSetAttributeParams(renderNode, attrNs, attrName, attrValue);
return o.InvokeMemberMethodExpr(
attrNs == null ? "createAttr" : "setAttrNS", params)
.toStmt();
}
Map<String, ast.AST> _toSortedMap(Map<String, ast.AST> data) {
var result = SplayTreeMap<String, ast.AST>();
return result..addAll(data);
}
// Reads hostAttributes from each directive and merges with declaredHtmlAttrs
// to return a single map from name to value(expression).
Map<String, ast.AST> mergeHtmlAndDirectiveAttrs(
Map<String, AttrAst> declaredHtmlAttrs,
List<CompileDirectiveMetadata> directives) {
var result = <String, ast.AST>{};
var mergeCount = <String, int>{};
declaredHtmlAttrs.forEach((name, attrAst) {
result[name] = ast.LiteralPrimitive(attrAst.value);
if (mergeCount.containsKey(name)) {
mergeCount[name]++;
} else {
mergeCount[name] = 1;
}
});
for (CompileDirectiveMetadata directiveMeta in directives) {
directiveMeta.hostAttributes.forEach((name, value) {
if (mergeCount.containsKey(name)) {
mergeCount[name]++;
} else {
mergeCount[name] = 1;
}
});
}
for (CompileDirectiveMetadata directiveMeta in directives) {
bool isComponent = directiveMeta.isComponent;
for (String name in directiveMeta.hostAttributes.keys) {
var value = directiveMeta.hostAttributes[name];
if (isComponent &&
!((name == classAttrName || name == styleAttrName) &&
mergeCount[name] > 1)) {
continue;
}
var prevValue = result[name];
result[name] = prevValue != null
? _mergeAttributeValue(name, prevValue, value)
: value;
}
}
return _toSortedMap(result);
}
Map<String, CompileIdentifierMetadata> _tagNameToIdentifier;
/// Returns strongly typed html elements to improve code generation.
CompileIdentifierMetadata identifierFromTagName(String name) {
_tagNameToIdentifier ??= {
'a': Identifiers.HTML_ANCHOR_ELEMENT,
'area': Identifiers.HTML_AREA_ELEMENT,
'audio': Identifiers.HTML_AUDIO_ELEMENT,
'button': Identifiers.HTML_BUTTON_ELEMENT,
'canvas': Identifiers.HTML_CANVAS_ELEMENT,
'div': Identifiers.HTML_DIV_ELEMENT,
'form': Identifiers.HTML_FORM_ELEMENT,
'iframe': Identifiers.HTML_IFRAME_ELEMENT,
'input': Identifiers.HTML_INPUT_ELEMENT,
'image': Identifiers.HTML_IMAGE_ELEMENT,
'media': Identifiers.HTML_MEDIA_ELEMENT,
'menu': Identifiers.HTML_MENU_ELEMENT,
'ol': Identifiers.HTML_OLIST_ELEMENT,
'option': Identifiers.HTML_OPTION_ELEMENT,
'col': Identifiers.HTML_TABLE_COL_ELEMENT,
'row': Identifiers.HTML_TABLE_ROW_ELEMENT,
'select': Identifiers.HTML_SELECT_ELEMENT,
'table': Identifiers.HTML_TABLE_ELEMENT,
'text': Identifiers.HTML_TEXT_NODE,
'textarea': Identifiers.HTML_TEXTAREA_ELEMENT,
'ul': Identifiers.HTML_ULIST_ELEMENT,
'svg': Identifiers.SVG_SVG_ELEMENT,
};
String tagName = name.toLowerCase();
var elementType = _tagNameToIdentifier[tagName];
elementType ??= Identifiers.HTML_ELEMENT;
// TODO: classify as HtmlElement or SvgElement to improve further.
return elementType;
}
Set<String> _tagNameSet;
/// Returns true if tag name is HtmlElement.
///
/// Returns false if tag name is svg element or other. Used for optimizations.
/// Should not generate false positives but returning false when unknown is
/// fine since code will fallback to general Element case.
bool detectHtmlElementFromTagName(String tagName) {
const htmlTagNames = <String>[
'a',
'abbr',
'acronym',
'address',
'applet',
'area',
'article',
'aside',
'audio',
'b',
'base',
'basefont',
'bdi',
'bdo',
'bgsound',
'big',
'blockquote',
'body',
'br',
'button',
'canvas',
'caption',
'center',
'cite',
'code',
'col',
'colgroup',
'command',
'data',
'datalist',
'dd',
'del',
'details',
'dfn',
'dialog',
'dir',
'div',
'dl',
'dt',
'element',
'em',
'embed',
'fieldset',
'figcaption',
'figure',
'font',
'footer',
'form',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'head',
'header',
'hr',
'i',
'iframe',
'img',
'input',
'ins',
'kbd',
'keygen',
'label',
'legend',
'li',
'link',
'listing',
'main',
'map',
'mark',
'menu',
'menuitem',
'meta',
'meter',
'nav',
'object',
'ol',
'optgroup',
'option',
'output',
'p',
'param',
'picture',
'pre',
'progress',
'q',
'rp',
'rt',
'rtc',
'ruby',
's',
'samp',
'script',
'section',
'select',
'shadow',
'small',
'source',
'span',
'strong',
'style',
'sub',
'summary',
'sup',
'table',
'tbody',
'td',
'template',
'textarea',
'tfoot',
'th',
'thead',
'time',
'title',
'tr',
'track',
'tt',
'u',
'ul',
'var',
'video',
'wbr'
];
if (_tagNameSet == null) {
_tagNameSet = Set<String>();
for (String name in htmlTagNames) {
_tagNameSet.add(name);
}
}
return _tagNameSet.contains(tagName);
}