Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: third_party/pkg/angular/lib/core_dom/compiler.dart

Issue 176943008: Update the Angular/DI tests to latest from github. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 part of angular.core.dom; 1 part of angular.core.dom;
2 2
3 @NgInjectableService() 3 @NgInjectableService()
4 class Compiler { 4 class Compiler {
5 final DirectiveMap directives;
6 final Profiler _perf; 5 final Profiler _perf;
7 final Parser _parser; 6 final Parser _parser;
8 final Expando _expando; 7 final Expando _expando;
9 8
10 DirectiveSelector selector; 9 Compiler(this._perf, this._parser, this._expando);
11
12 Compiler(this.directives, this._perf, this._parser, this._expando) {
13 selector = directiveSelectorFactory(directives);
14 }
15 10
16 _compileBlock(NodeCursor domCursor, NodeCursor templateCursor, 11 _compileBlock(NodeCursor domCursor, NodeCursor templateCursor,
17 List<DirectiveRef> useExistingDirectiveRefs) { 12 List<DirectiveRef> useExistingDirectiveRefs,
13 DirectiveMap directives) {
18 if (domCursor.nodeList().length == 0) return null; 14 if (domCursor.nodeList().length == 0) return null;
19 15
20 var directivePositions = null; // don't pre-create to create sparse tree and prevent GC pressure. 16 var directivePositions = null; // don't pre-create to create sparse tree and prevent GC pressure.
21 var cursorAlreadyAdvanced; 17 var cursorAlreadyAdvanced;
22 18
23 do { 19 do {
24 var declaredDirectiveRefs = useExistingDirectiveRefs == null 20 var declaredDirectiveRefs = useExistingDirectiveRefs == null
25 ? selector(domCursor.nodeList()[0]) 21 ? directives.selector(domCursor.nodeList()[0])
26 : useExistingDirectiveRefs; 22 : useExistingDirectiveRefs;
27 var children = NgAnnotation.COMPILE_CHILDREN; 23 var children = NgAnnotation.COMPILE_CHILDREN;
28 var childDirectivePositions = null; 24 var childDirectivePositions = null;
29 List<DirectiveRef> usableDirectiveRefs = null; 25 List<DirectiveRef> usableDirectiveRefs = null;
30 26
31 cursorAlreadyAdvanced = false; 27 cursorAlreadyAdvanced = false;
32 28
33 for (var j = 0, jj = declaredDirectiveRefs.length; j < jj; j++) { 29 for (var j = 0; j < declaredDirectiveRefs.length; j++) {
34 DirectiveRef directiveRef = declaredDirectiveRefs[j]; 30 DirectiveRef directiveRef = declaredDirectiveRefs[j];
35 NgAnnotation annotation = directiveRef.annotation; 31 NgAnnotation annotation = directiveRef.annotation;
36 var blockFactory = null; 32 var blockFactory = null;
37 33
38 if (annotation.children != children && 34 if (annotation.children != children &&
39 children == NgAnnotation.COMPILE_CHILDREN) { 35 children == NgAnnotation.COMPILE_CHILDREN) {
40 children = annotation.children; 36 children = annotation.children;
41 } 37 }
42 38
43 if (children == NgAnnotation.TRANSCLUDE_CHILDREN) { 39 if (children == NgAnnotation.TRANSCLUDE_CHILDREN) {
44 var remainingDirectives = declaredDirectiveRefs.sublist(j + 1); 40 var remainingDirectives = declaredDirectiveRefs.sublist(j + 1);
45 blockFactory = compileTransclusion( 41 blockFactory = compileTransclusion(
46 domCursor, templateCursor, 42 domCursor, templateCursor,
47 directiveRef, remainingDirectives); 43 directiveRef, remainingDirectives, directives);
48 44
49 j = jj; // stop processing further directives since they belong to tra nsclusion; 45 // stop processing further directives since they belong to
46 // transclusion
47 j = declaredDirectiveRefs.length;
50 } 48 }
51 if (usableDirectiveRefs == null) { 49 if (usableDirectiveRefs == null) usableDirectiveRefs = [];
52 usableDirectiveRefs = [];
53 }
54 directiveRef.blockFactory = blockFactory; 50 directiveRef.blockFactory = blockFactory;
55 createMappings(directiveRef); 51 createMappings(directiveRef);
56 usableDirectiveRefs.add(directiveRef); 52 usableDirectiveRefs.add(directiveRef);
57 } 53 }
58 54
59 if (children == NgAnnotation.COMPILE_CHILDREN && domCursor.descend()) { 55 if (children == NgAnnotation.COMPILE_CHILDREN && domCursor.descend()) {
60 templateCursor.descend(); 56 templateCursor.descend();
61 57
62 childDirectivePositions = _compileBlock(domCursor, templateCursor, null) ; 58 childDirectivePositions =
59 _compileBlock(domCursor, templateCursor, null, directives);
63 60
64 domCursor.ascend(); 61 domCursor.ascend();
65 templateCursor.ascend(); 62 templateCursor.ascend();
66 } 63 }
67 64
68 if (childDirectivePositions != null || usableDirectiveRefs != null) { 65 if (childDirectivePositions != null || usableDirectiveRefs != null) {
69 if (directivePositions == null) directivePositions = []; 66 if (directivePositions == null) directivePositions = [];
70 var directiveOffsetIndex = templateCursor.index; 67 var directiveOffsetIndex = templateCursor.index;
71 68
72 directivePositions 69 directivePositions
73 ..add(directiveOffsetIndex) 70 ..add(directiveOffsetIndex)
74 ..add(usableDirectiveRefs) 71 ..add(usableDirectiveRefs)
75 ..add(childDirectivePositions); 72 ..add(childDirectivePositions);
76 } 73 }
77 } while (templateCursor.microNext() && domCursor.microNext()); 74 } while (templateCursor.microNext() && domCursor.microNext());
78 75
79 return directivePositions; 76 return directivePositions;
80 } 77 }
81 78
82 BlockFactory compileTransclusion( 79 BlockFactory compileTransclusion(
83 NodeCursor domCursor, NodeCursor templateCursor, 80 NodeCursor domCursor, NodeCursor templateCursor,
84 DirectiveRef directiveRef, 81 DirectiveRef directiveRef,
85 List<DirectiveRef> transcludedDirectiveRefs) { 82 List<DirectiveRef> transcludedDirectiveRefs,
83 DirectiveMap directives) {
86 var anchorName = directiveRef.annotation.selector + (directiveRef.value != n ull ? '=' + directiveRef.value : ''); 84 var anchorName = directiveRef.annotation.selector + (directiveRef.value != n ull ? '=' + directiveRef.value : '');
87 var blockFactory; 85 var blockFactory;
88 var blocks; 86 var blocks;
89 87
90 var transcludeCursor = templateCursor.replaceWithAnchor(anchorName); 88 var transcludeCursor = templateCursor.replaceWithAnchor(anchorName);
91 var domCursorIndex = domCursor.index; 89 var domCursorIndex = domCursor.index;
92 var directivePositions = _compileBlock(domCursor, transcludeCursor, transclu dedDirectiveRefs); 90 var directivePositions =
91 _compileBlock(domCursor, transcludeCursor, transcludedDirectiveRefs, dir ectives);
93 if (directivePositions == null) directivePositions = []; 92 if (directivePositions == null) directivePositions = [];
94 93
95 blockFactory = new BlockFactory(transcludeCursor.elements, directivePosition s, _perf, _expando); 94 blockFactory = new BlockFactory(transcludeCursor.elements, directivePosition s, _perf, _expando);
96 domCursor.index = domCursorIndex; 95 domCursor.index = domCursorIndex;
97 96
98 if (domCursor.isInstance()) { 97 if (domCursor.isInstance()) {
99 domCursor.insertAnchorBefore(anchorName); 98 domCursor.insertAnchorBefore(anchorName);
100 blocks = [blockFactory(domCursor.nodeList())]; 99 blocks = [blockFactory(domCursor.nodeList())];
101 domCursor.macroNext(); 100 domCursor.macroNext();
102 templateCursor.macroNext(); 101 templateCursor.macroNext();
103 while (domCursor.isValid() && domCursor.isInstance()) { 102 while (domCursor.isValid() && domCursor.isInstance()) {
104 blocks.add(blockFactory(domCursor.nodeList())); 103 blocks.add(blockFactory(domCursor.nodeList()));
105 domCursor.macroNext(); 104 domCursor.macroNext();
106 templateCursor.remove(); 105 templateCursor.remove();
107 } 106 }
108 } else { 107 } else {
109 domCursor.replaceWithAnchor(anchorName); 108 domCursor.replaceWithAnchor(anchorName);
110 } 109 }
111 110
112 return blockFactory; 111 return blockFactory;
113 } 112 }
114 113
115 BlockFactory call(List<dom.Node> elements) { 114 BlockFactory call(List<dom.Node> elements, DirectiveMap directives) {
116 var timerId; 115 var timerId;
117 assert((timerId = _perf.startTimer('ng.compile', _html(elements))) != false) ; 116 assert((timerId = _perf.startTimer('ng.compile', _html(elements))) != false) ;
118 List<dom.Node> domElements = elements; 117 List<dom.Node> domElements = elements;
119 List<dom.Node> templateElements = cloneElements(domElements); 118 List<dom.Node> templateElements = cloneElements(domElements);
120 var directivePositions = _compileBlock( 119 var directivePositions = _compileBlock(
121 new NodeCursor(domElements), new NodeCursor(templateElements), 120 new NodeCursor(domElements), new NodeCursor(templateElements),
122 null); 121 null, directives);
123 122
124 var blockFactory = new BlockFactory(templateElements, 123 var blockFactory = new BlockFactory(templateElements,
125 directivePositions == null ? [] : directivePositions, _perf, _expando); 124 directivePositions == null ? [] : directivePositions, _perf, _expando);
126 125
127 assert(_perf.stopTimer(timerId) != false); 126 assert(_perf.stopTimer(timerId) != false);
128 return blockFactory; 127 return blockFactory;
129 } 128 }
130 129
131 static RegExp _MAPPING = new RegExp(r'^(\@|=\>\!|\=\>|\<\=\>|\&)\s*(.*)$'); 130 static RegExp _MAPPING = new RegExp(r'^(\@|=\>\!|\=\>|\<\=\>|\&)\s*(.*)$');
132 131
133 createMappings(DirectiveRef ref) { 132 createMappings(DirectiveRef ref) {
134 NgAnnotation annotation = ref.annotation; 133 NgAnnotation annotation = ref.annotation;
135 if (annotation.map != null) annotation.map.forEach((attrName, mapping) { 134 if (annotation.map != null) annotation.map.forEach((attrName, mapping) {
136 Match match = _MAPPING.firstMatch(mapping); 135 Match match = _MAPPING.firstMatch(mapping);
137 if (match == null) { 136 if (match == null) {
138 throw "Unknown mapping '$mapping' for attribute '$attrName'."; 137 throw "Unknown mapping '$mapping' for attribute '$attrName'.";
139 } 138 }
140 var mode = match[1]; 139 var mode = match[1];
141 var dstPath = match[2]; 140 var dstPath = match[2];
142 141
143 Expression dstPathFn = _parser(dstPath.isEmpty ? attrName : dstPath); 142 String dstExpression = dstPath.isEmpty ? attrName : dstPath;
143 Expression dstPathFn = _parser(dstExpression);
144 if (!dstPathFn.isAssignable) { 144 if (!dstPathFn.isAssignable) {
145 throw "Expression '$dstPath' is not assignable in mapping '$mapping' for attribute '$attrName'."; 145 throw "Expression '$dstPath' is not assignable in mapping '$mapping' for attribute '$attrName'.";
146 } 146 }
147 ApplyMapping mappingFn; 147 ApplyMapping mappingFn;
148 switch (mode) { 148 switch (mode) {
149 case '@': 149 case '@':
150 mappingFn = (NodeAttrs attrs, Scope scope, Object dst) { 150 mappingFn = (NodeAttrs attrs, Scope scope, Object controller, FilterMa p filters, notify()) {
151 attrs.observe(attrName, (value) => dstPathFn.assign(dst, value)); 151 attrs.observe(attrName, (value) {
152 dstPathFn.assign(controller, value);
153 notify();
154 });
152 }; 155 };
153 break; 156 break;
154 case '<=>': 157 case '<=>':
155 mappingFn = (NodeAttrs attrs, Scope scope, Object dst) { 158 mappingFn = (NodeAttrs attrs, Scope scope, Object controller, FilterMa p filters, notify()) {
156 if (attrs[attrName] == null) return; 159 if (attrs[attrName] == null) return notify();
157 Expression attrExprFn = _parser(attrs[attrName]); 160 String expression = attrs[attrName];
158 var shadowValue = null; 161 Expression expressionFn = _parser(expression);
159 scope.$watch( 162 var blockOutbound = false;
160 () => attrExprFn.eval(scope), 163 var blockInbound = false;
161 (v) => dstPathFn.assign(dst, shadowValue = v), 164 scope.watch(
162 attrs[attrName]); 165 expression,
163 if (attrExprFn.isAssignable) { 166 (inboundValue, _) {
164 scope.$watch( 167 if (!blockInbound) {
165 () => dstPathFn.eval(dst), 168 blockOutbound = true;
166 (v) { 169 scope.rootScope.runAsync(() => blockOutbound = false);
167 if (shadowValue != v) { 170 var value = dstPathFn.assign(controller, inboundValue);
168 shadowValue = v; 171 notify();
169 attrExprFn.assign(scope, v); 172 return value;
173 }
174 },
175 filters: filters
176 );
177 if (expressionFn.isAssignable) {
178 scope.watch(
179 dstExpression,
180 (outboundValue, _) {
181 if (!blockOutbound) {
182 blockInbound = true;
183 scope.rootScope.runAsync(() => blockInbound = false);
184 expressionFn.assign(scope.context, outboundValue);
185 notify();
170 } 186 }
171 }, 187 },
172 dstPath); 188 context: controller,
189 filters: filters
190 );
173 } 191 }
174 }; 192 };
175 break; 193 break;
176 case '=>': 194 case '=>':
177 mappingFn = (NodeAttrs attrs, Scope scope, Object dst) { 195 mappingFn = (NodeAttrs attrs, Scope scope, Object controller, FilterMa p filters, notify()) {
178 if (attrs[attrName] == null) return; 196 if (attrs[attrName] == null) return notify();
179 Expression attrExprFn = _parser(attrs[attrName]); 197 Expression attrExprFn = _parser(attrs[attrName]);
180 var shadowValue = null; 198 var shadowValue = null;
181 scope.$watch( 199 scope.watch(attrs[attrName],
182 () => attrExprFn.eval(scope), 200 (v, _) {
183 (v) => dstPathFn.assign(dst, shadowValue = v), 201 dstPathFn.assign(controller, shadowValue = v);
184 attrs[attrName]); 202 notify();
203 },
204 filters: filters);
185 }; 205 };
186 break; 206 break;
187 case '=>!': 207 case '=>!':
188 mappingFn = (NodeAttrs attrs, Scope scope, Object dst) { 208 mappingFn = (NodeAttrs attrs, Scope scope, Object controller, FilterMa p filters, notify()) {
189 if (attrs[attrName] == null) return; 209 if (attrs[attrName] == null) return notify();
190 Expression attrExprFn = _parser(attrs[attrName]); 210 Expression attrExprFn = _parser(attrs[attrName]);
191 var stopWatching; 211 var watch;
192 stopWatching = scope.$watch( 212 watch = scope.watch(
193 () => attrExprFn.eval(scope), 213 attrs[attrName],
194 (value) { 214 (value, _) {
195 if (dstPathFn.assign(dst, value) != null) { 215 if (dstPathFn.assign(controller, value) != null) {
196 stopWatching(); 216 watch.remove();
197 } 217 }
198 }, 218 },
199 attrs[attrName]); 219 filters: filters);
220 notify();
200 }; 221 };
201 break; 222 break;
202 case '&': 223 case '&':
203 mappingFn = (NodeAttrs attrs, Scope scope, Object dst) { 224 mappingFn = (NodeAttrs attrs, Scope scope, Object dst, FilterMap filte rs, notify()) {
204 dstPathFn.assign(dst, _parser(attrs[attrName]).bind(scope, ScopeLoca ls.wrapper)); 225 dstPathFn.assign(dst, _parser(attrs[attrName]).bind(scope.context, S copeLocals.wrapper));
226 notify();
205 }; 227 };
206 break; 228 break;
207 } 229 }
208 ref.mappings.add(mappingFn); 230 ref.mappings.add(mappingFn);
209 }); 231 });
210 } 232 }
211 } 233 }
212 234
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698