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

Commit afeec23

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: #8722
1 parent 8237482 commit afeec23

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
//================================
@@ -2069,8 +2077,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
20692077
terminalPriority = directive.priority;
20702078
$template = $compileNode;
20712079
$compileNode = templateAttrs.$$element =
2072-
jqLite(document.createComment(' ' + directiveName + ': ' +
2073-
templateAttrs[directiveName] + ' '));
2080+
jqLite(compile.$$createComment(directiveName, templateAttrs[directiveName]));
20742081
compileNode = $compileNode[0];
20752082
replaceWith(jqCollection, sliceArgs($template), compileNode);
20762083

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
@@ -316,7 +316,7 @@
316316
</file>
317317
</example>
318318
*/
319-
var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
319+
var ngRepeatDirective = ['$parse', '$animate', '$compile', function($parse, $animate, $compile) {
320320
var NG_REMOVED = '$$NG_REMOVED';
321321
var ngRepeatMinErr = minErr('ngRepeat');
322322

@@ -351,7 +351,7 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
351351
$$tlb: true,
352352
compile: function ngRepeatCompile($element, $attr) {
353353
var expression = $attr.ngRepeat;
354-
var ngRepeatEndComment = document.createComment(' end ngRepeat: ' + expression + ' ');
354+
var ngRepeatEndComment = $compile.$$createComment('end ngRepeat', expression);
355355

356356
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*$/);
357357

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
@@ -10130,4 +10130,28 @@ describe('$compile', function() {
1013010130
});
1013110131
});
1013210132
});
10133+
10134+
describe('$$createComment', function() {
10135+
it('should create empty comments if `debugInfoEnabled` is false', function() {
10136+
module(function($compileProvider) {
10137+
$compileProvider.debugInfoEnabled(false);
10138+
});
10139+
10140+
inject(function($compile) {
10141+
var comment = $compile.$$createComment('foo', 'bar');
10142+
expect(comment.data).toBe('');
10143+
});
10144+
});
10145+
10146+
it('should create descriptive comments if `debugInfoEnabled` is true', function() {
10147+
module(function($compileProvider) {
10148+
$compileProvider.debugInfoEnabled(true);
10149+
});
10150+
10151+
inject(function($compile) {
10152+
var comment = $compile.$$createComment('foo', 'bar');
10153+
expect(comment.data).toBe(' foo: bar ');
10154+
});
10155+
});
10156+
});
1013310157
});

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)