Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 0d608d0

Browse files
committed
refactor($compile): automatically append end comment nodes to all element-transclusion templates
Previously we would do it manually in all of our structural directives. BREAKING CHANGE: element-transcluded directives now have an extra comment automatically appended to their cloned DOM This comment is usually needed to keep track the end boundary in the event child directives modify the root node(s). If not used for this purpose it can be safely ignored.
1 parent b5714ce commit 0d608d0

File tree

5 files changed

+12
-10
lines changed

5 files changed

+12
-10
lines changed

src/ng/compile.js

+3
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,9 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
13051305
compileNode = $compileNode[0];
13061306
replaceWith(jqCollection, sliceArgs($template), compileNode);
13071307

1308+
$template[$template.length++] = document.createComment(' end ' + directiveName + ': ' +
1309+
templateAttrs[directiveName] + ' ');
1310+
13081311
childTranscludeFn = compile($template, transcludeFn, terminalPriority,
13091312
replaceDirective && replaceDirective.name, {
13101313
// Don't pass in:

src/ng/directive/ngIf.js

-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ var ngIfDirective = ['$animate', function($animate) {
9292
if (!childScope) {
9393
$transclude(function (clone, newScope) {
9494
childScope = newScope;
95-
clone[clone.length++] = document.createComment(' end ngIf: ' + $attr.ngIf + ' ');
9695
// Note: We only need the first/last node of the cloned nodes.
9796
// However, we need to keep the reference to the jqlite wrapper as it might be changed later
9897
// by a directive with templateUrl when its template arrives.

src/ng/directive/ngRepeat.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,6 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
243243
$$tlb: true,
244244
compile: function ngRepeatCompile($element, $attr) {
245245
var expression = $attr.ngRepeat;
246-
var ngRepeatEndComment = document.createComment(' end ngRepeat: ' + expression + ' ');
247-
248246
var match = expression.match(/^\s*([\s\S]+?)\s+in\s+([\s\S]+?)(?:\s+as\s+([\s\S]+?))?(?:\s+track\s+by\s+([\s\S]+?))?\s*$/);
249247

250248
if (!match) {
@@ -409,11 +407,8 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
409407
// new item which we don't know about
410408
$transclude(function ngRepeatTransclude(clone, scope) {
411409
block.scope = scope;
412-
// http://jsperf.com/clone-vs-createcomment
413-
var endNode = ngRepeatEndComment.cloneNode();
414-
clone[clone.length++] = endNode;
415410
$animate.enter(clone, null, jqLite(previousNode));
416-
previousNode = endNode;
411+
previousNode = clone[clone.length - 1];
417412
// Note: We only need the first/last node of the cloned nodes.
418413
// However, we need to keep the reference to the jqlite wrapper as it might be changed later
419414
// by a directive with templateUrl when its template arrives.

src/ng/directive/ngSwitch.js

-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ var ngSwitchDirective = ['$animate', function($animate) {
169169
selectedTransclude.transclude(function(caseElement, selectedScope) {
170170
selectedScopes.push(selectedScope);
171171
var anchor = selectedTransclude.element;
172-
caseElement[caseElement.length++] = document.createComment(' end ngSwitchWhen: ');
173172
var block = { clone: caseElement };
174173

175174
selectedElements.push(block);

test/ng/compileSpec.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -4805,7 +4805,12 @@ describe('$compile', function() {
48054805
return function(scope, element, attrs, ctrl) {
48064806
log('link');
48074807
var cursor = element;
4808-
template(scope.$new(), function(clone) {cursor.after(cursor = clone);});
4808+
4809+
template(scope.$new(), function(clone) {
4810+
var nextCursor = clone.eq(1);
4811+
cursor.after(clone);
4812+
cursor = nextCursor;
4813+
});
48094814
ctrl.$transclude(function(clone) {cursor.after(clone);});
48104815
};
48114816
}
@@ -5110,9 +5115,10 @@ describe('$compile', function() {
51105115
"inner:#comment:innerAgain:"
51115116
]);
51125117
expect(child.length).toBe(1);
5113-
expect(child.contents().length).toBe(2);
5118+
expect(child.contents().length).toBe(3);
51145119
expect(lowercase(nodeName_(child.contents().eq(0)))).toBe('#comment');
51155120
expect(lowercase(nodeName_(child.contents().eq(1)))).toBe('div');
5121+
expect(lowercase(nodeName_(child.contents().eq(2)))).toBe('#comment');
51165122
});
51175123
});
51185124
});

0 commit comments

Comments
 (0)