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