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

refactor($compile): Create non-descriptive comments when debugInfoEnabled is false #14132

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1508,6 +1508,14 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
safeAddClass($element, isolated ? 'ng-isolate-scope' : 'ng-scope');
} : noop;

compile.$$createComment = function(directiveName, comment) {
var content = '';
if (debugInfoEnabled) {
content = ' ' + (directiveName || '') + ': ' + (comment || '') + ' ';
}
return document.createComment(content);
};

return compile;

//================================
Expand Down Expand Up @@ -2054,8 +2062,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
terminalPriority = directive.priority;
$template = $compileNode;
$compileNode = templateAttrs.$$element =
jqLite(document.createComment(' ' + directiveName + ': ' +
templateAttrs[directiveName] + ' '));
jqLite(compile.$$createComment(directiveName, templateAttrs[directiveName]));
compileNode = $compileNode[0];
replaceWith(jqCollection, sliceArgs($template), compileNode);

Expand Down
4 changes: 2 additions & 2 deletions src/ng/directive/ngIf.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
</file>
</example>
*/
var ngIfDirective = ['$animate', function($animate) {
var ngIfDirective = ['$animate', '$compile', function($animate, $compile) {
return {
multiElement: true,
transclude: 'element',
Expand All @@ -94,7 +94,7 @@ var ngIfDirective = ['$animate', function($animate) {
if (!childScope) {
$transclude(function(clone, newScope) {
childScope = newScope;
clone[clone.length++] = document.createComment(' end ngIf: ' + $attr.ngIf + ' ');
clone[clone.length++] = $compile.$$createComment('end ngIf', $attr.ngIf);
// Note: We only need the first/last node of the cloned nodes.
// However, we need to keep the reference to the jqlite wrapper as it might be changed later
// by a directive with templateUrl when its template arrives.
Expand Down
4 changes: 2 additions & 2 deletions src/ng/directive/ngRepeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@
</file>
</example>
*/
var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
var ngRepeatDirective = ['$parse', '$animate', '$compile', function($parse, $animate, $compile) {
var NG_REMOVED = '$$NG_REMOVED';
var ngRepeatMinErr = minErr('ngRepeat');

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

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

Expand Down
4 changes: 2 additions & 2 deletions src/ng/directive/ngSwitch.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
</file>
</example>
*/
var ngSwitchDirective = ['$animate', function($animate) {
var ngSwitchDirective = ['$animate', '$compile', function($animate, $compile) {
return {
require: 'ngSwitch',

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

selectedElements.push(block);
Expand Down
5 changes: 4 additions & 1 deletion src/ngMessages/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,10 @@ angular.module('ngMessages', [])
element.after(contents);

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

// we don't want to pollute the DOM anymore by keeping an empty directive element
Expand Down
24 changes: 24 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10146,4 +10146,28 @@ describe('$compile', function() {
});
});
});

describe('$$createComment', function() {
it('should create empty comments if `debugInfoEnabled` is false', function() {
module(function($compileProvider) {
$compileProvider.debugInfoEnabled(false);
});

inject(function($compile) {
var comment = $compile.$$createComment('foo', 'bar');
expect(comment.data).toBe('');
});
});

it('should create descriptive comments if `debugInfoEnabled` is true', function() {
module(function($compileProvider) {
$compileProvider.debugInfoEnabled(true);
});

inject(function($compile) {
var comment = $compile.$$createComment('foo', 'bar');
expect(comment.data).toBe(' foo: bar ');
});
});
});
});
4 changes: 3 additions & 1 deletion test/ng/directive/ngIncludeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,9 @@ describe('ngInclude', function() {
it('should not compile template if original scope is destroyed', function() {
module(function($provide) {
$provide.decorator('$compile', function($delegate) {
return jasmine.createSpy('$compile').andCallFake($delegate);
var result = jasmine.createSpy('$compile').andCallFake($delegate);
result.$$createComment = $delegate.$$createComment;
return result;
});
});
inject(function($rootScope, $httpBackend, $compile) {
Expand Down