Skip to content

Commit 32feb2b

Browse files
committed
refactor($compile): Create non-descriptive comments when debugInfoEnabled is false
When debugInfoEnabled is `false` when comments generated by transclusions, ngIf, ngRepeat and ngSwitch will not contain any information about the directive nor the expression associated with it. Closes: angular#8722
1 parent 5e37b2a commit 32feb2b

File tree

7 files changed

+46
-10
lines changed

7 files changed

+46
-10
lines changed

src/ng/compile.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,14 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
15101510
safeAddClass($element, isolated ? 'ng-isolate-scope' : 'ng-scope');
15111511
} : noop;
15121512

1513+
compile.$$createComment = function(directiveName, comment) {
1514+
var content = '';
1515+
if (debugInfoEnabled) {
1516+
content = ' ' + (directiveName || '') + ': ' + (comment || '') + ' ';
1517+
}
1518+
return document.createComment(content);
1519+
};
1520+
15131521
return compile;
15141522

15151523
//================================
@@ -2056,8 +2064,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
20562064
terminalPriority = directive.priority;
20572065
$template = $compileNode;
20582066
$compileNode = templateAttrs.$$element =
2059-
jqLite(document.createComment(' ' + directiveName + ': ' +
2060-
templateAttrs[directiveName] + ' '));
2067+
jqLite(compile.$$createComment(directiveName, templateAttrs[directiveName]));
20612068
compileNode = $compileNode[0];
20622069
replaceWith(jqCollection, sliceArgs($template), compileNode);
20632070

src/ng/directive/ngIf.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
</file>
7979
</example>
8080
*/
81-
var ngIfDirective = ['$animate', function($animate) {
81+
var ngIfDirective = ['$animate', '$compile', function($animate, $compile) {
8282
return {
8383
multiElement: true,
8484
transclude: 'element',
@@ -94,7 +94,7 @@ var ngIfDirective = ['$animate', function($animate) {
9494
if (!childScope) {
9595
$transclude(function(clone, newScope) {
9696
childScope = newScope;
97-
clone[clone.length++] = document.createComment(' end ngIf: ' + $attr.ngIf + ' ');
97+
clone[clone.length++] = $compile.$$createComment('end ngIf', $attr.ngIf);
9898
// Note: We only need the first/last node of the cloned nodes.
9999
// However, we need to keep the reference to the jqlite wrapper as it might be changed later
100100
// by a directive with templateUrl when its template arrives.

src/ng/directive/ngRepeat.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@
322322
</file>
323323
</example>
324324
*/
325-
var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
325+
var ngRepeatDirective = ['$parse', '$animate', '$compile', function($parse, $animate, $compile) {
326326
var NG_REMOVED = '$$NG_REMOVED';
327327
var ngRepeatMinErr = minErr('ngRepeat');
328328

@@ -357,7 +357,7 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
357357
$$tlb: true,
358358
compile: function ngRepeatCompile($element, $attr) {
359359
var expression = $attr.ngRepeat;
360-
var ngRepeatEndComment = document.createComment(' end ngRepeat: ' + expression + ' ');
360+
var ngRepeatEndComment = $compile.$$createComment('end ngRepeat', expression);
361361

362362
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*$/);
363363

src/ng/directive/ngSwitch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
</file>
130130
</example>
131131
*/
132-
var ngSwitchDirective = ['$animate', function($animate) {
132+
var ngSwitchDirective = ['$animate', '$compile', function($animate, $compile) {
133133
return {
134134
require: 'ngSwitch',
135135

@@ -170,7 +170,7 @@ var ngSwitchDirective = ['$animate', function($animate) {
170170
selectedTransclude.transclude(function(caseElement, selectedScope) {
171171
selectedScopes.push(selectedScope);
172172
var anchor = selectedTransclude.element;
173-
caseElement[caseElement.length++] = document.createComment(' end ngSwitchWhen: ');
173+
caseElement[caseElement.length++] = $compile.$$createComment('end ngSwitchWhen');
174174
var block = { clone: caseElement };
175175

176176
selectedElements.push(block);

src/ngMessages/messages.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,10 @@ angular.module('ngMessages', [])
543543
element.after(contents);
544544

545545
// the anchor is placed for debugging purposes
546-
var anchor = jqLite($document[0].createComment(' ngMessagesInclude: ' + src + ' '));
546+
var comment = $compile.$$createComment ?
547+
$compile.$$createComment('ngMessagesInclude', src) :
548+
$document[0].createComment(' ngMessagesInclude: ' + src + ' ');
549+
var anchor = jqLite(comment);
547550
element.after(anchor);
548551

549552
// we don't want to pollute the DOM anymore by keeping an empty directive element

test/ng/compileSpec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10162,4 +10162,28 @@ describe('$compile', function() {
1016210162
});
1016310163
});
1016410164
});
10165+
10166+
describe('$$createComment', function() {
10167+
it('should create empty comments if `debugInfoEnabled` is false', function() {
10168+
module(function($compileProvider) {
10169+
$compileProvider.debugInfoEnabled(false);
10170+
});
10171+
10172+
inject(function($compile) {
10173+
var comment = $compile.$$createComment('foo', 'bar');
10174+
expect(comment.data).toBe('');
10175+
});
10176+
});
10177+
10178+
it('should create descriptive comments if `debugInfoEnabled` is true', function() {
10179+
module(function($compileProvider) {
10180+
$compileProvider.debugInfoEnabled(true);
10181+
});
10182+
10183+
inject(function($compile) {
10184+
var comment = $compile.$$createComment('foo', 'bar');
10185+
expect(comment.data).toBe(' foo: bar ');
10186+
});
10187+
});
10188+
});
1016510189
});

test/ng/directive/ngIncludeSpec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,9 @@ describe('ngInclude', function() {
401401
it('should not compile template if original scope is destroyed', function() {
402402
module(function($provide) {
403403
$provide.decorator('$compile', function($delegate) {
404-
return jasmine.createSpy('$compile').andCallFake($delegate);
404+
var result = jasmine.createSpy('$compile').andCallFake($delegate);
405+
result.$$createComment = $delegate.$$createComment;
406+
return result;
405407
});
406408
});
407409
inject(function($rootScope, $httpBackend, $compile) {

0 commit comments

Comments
 (0)