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

perf(a): do not link when href or name exists in template #5362

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
18 changes: 10 additions & 8 deletions src/ng/directive/a.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ var htmlAnchorDirective = valueFn({
element.append(document.createComment('IE fix'));
}

return function(scope, element) {
element.on('click', function(event){
// if we have no href url, then don't navigate anywhere.
if (!element.attr('href')) {
event.preventDefault();
}
});
};
if (!attr.href && !attr.name) {
return function(scope, element) {
element.on('click', function(event){
// if we have no href url, then don't navigate anywhere.
if (!element.attr('href')) {
event.preventDefault();
}
});
};
}
}
});
26 changes: 26 additions & 0 deletions test/ng/directive/aSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,30 @@ describe('a', function() {

expect(element.text()).toBe('hello@you');
});


it('should not link and hookup an event if href is present at compile', function() {
var jq = jQuery || jqLite;
element = jq('<a href="//a.com">hello@you</a>');
var linker = $compile(element);

spyOn(jq.prototype, 'on');

linker($rootScope);

expect(jq.prototype.on).not.toHaveBeenCalled();
});


it('should not link and hookup an event if name is present at compile', function() {
var jq = jQuery || jqLite;
element = jq('<a name="bobby">hello@you</a>');
var linker = $compile(element);

spyOn(jq.prototype, 'on');

linker($rootScope);

expect(jq.prototype.on).not.toHaveBeenCalled();
});
});