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