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

Commit df90e22

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 2d6c218 commit df90e22

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
@@ -1508,6 +1508,14 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
15081508
safeAddClass($element, isolated ? 'ng-isolate-scope' : 'ng-scope');
15091509
} : noop;
15101510

1511+
compile.$createComment = function(directiveName, comment) {
1512+
var content = '';
1513+
if (debugInfoEnabled) {
1514+
content = ' ' + (directiveName || '') + ': ' + (comment || '') + ' ';
1515+
}
1516+
return document.createComment(content);
1517+
};
1518+
15111519
return compile;
15121520

15131521
//================================
@@ -2054,8 +2062,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
20542062
terminalPriority = directive.priority;
20552063
$template = $compileNode;
20562064
$compileNode = templateAttrs.$$element =
2057-
jqLite(document.createComment(' ' + directiveName + ': ' +
2058-
templateAttrs[directiveName] + ' '));
2065+
jqLite(compile.$createComment(directiveName, templateAttrs[directiveName]));
20592066
compileNode = $compileNode[0];
20602067
replaceWith(jqCollection, sliceArgs($template), compileNode);
20612068

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
@@ -522,7 +522,10 @@ angular.module('ngMessages', [])
522522
element.after(contents);
523523

524524
// the anchor is placed for debugging purposes
525-
var anchor = jqLite($document[0].createComment(' ngMessagesInclude: ' + src + ' '));
525+
var comment = $compile.$createComment ?
526+
$compile.$createComment('ngMessagesInclude', src) :
527+
$document[0].createComment(' ngMessagesInclude: ' + src + ' ');
528+
var anchor = jqLite(comment);
526529
element.after(anchor);
527530

528531
// 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
@@ -10146,4 +10146,28 @@ describe('$compile', function() {
1014610146
});
1014710147
});
1014810148
});
10149+
10150+
describe('$createComment', function() {
10151+
it('should create empty comments if `debugInfoEnabled` is false', function() {
10152+
module(function($compileProvider) {
10153+
$compileProvider.debugInfoEnabled(false);
10154+
});
10155+
10156+
inject(function($compile) {
10157+
var comment = $compile.$createComment('foo', 'bar');
10158+
expect(comment.data).toBe('');
10159+
});
10160+
});
10161+
10162+
it('should create descriptive comments if `debugInfoEnabled` is true', function() {
10163+
module(function($compileProvider) {
10164+
$compileProvider.debugInfoEnabled(true);
10165+
});
10166+
10167+
inject(function($compile) {
10168+
var comment = $compile.$createComment('foo', 'bar');
10169+
expect(comment.data).toBe(' foo: bar ');
10170+
});
10171+
});
10172+
});
1014910173
});

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)