Skip to content

Commit 8e55b78

Browse files
committed
fix(ngSanitize): call attribute setter in linky for all links
Fixes angular#14707 PR: angular#14710
1 parent 87eff27 commit 8e55b78

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/ngSanitize/filter/linky.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) {
141141
if (text == null || text === '') return text;
142142
if (!isString(text)) throw linkyMinErr('notstring', 'Expected string but received: {0}', text);
143143

144+
var attributesFn =
145+
angular.isFunction(attributes) ? attributes :
146+
angular.isObject(attributes) ? function getAttributesObject() {return attributes;} :
147+
function getEmptyAttributesObject() {return {};};
148+
144149
var match;
145150
var raw = text;
146151
var html = [];
@@ -169,19 +174,14 @@ angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) {
169174
}
170175

171176
function addLink(url, text) {
172-
var key;
177+
var key, linkAttributes = attributesFn(url);
173178
html.push('<a ');
174-
if (angular.isFunction(attributes)) {
175-
attributes = attributes(url);
176-
}
177-
if (angular.isObject(attributes)) {
178-
for (key in attributes) {
179-
html.push(key + '="' + attributes[key] + '" ');
180-
}
181-
} else {
182-
attributes = {};
179+
180+
for (key in linkAttributes) {
181+
html.push(key + '="' + linkAttributes[key] + '" ');
183182
}
184-
if (angular.isDefined(target) && !('target' in attributes)) {
183+
184+
if (angular.isDefined(target) && !('target' in linkAttributes)) {
185185
html.push('target="',
186186
target,
187187
'" ');

test/ngSanitize/filter/linkySpec.js

+7
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ describe('linky', function() {
119119
});
120120

121121

122+
it('should call the attribute function for all links in the input', function() {
123+
var attributeFn = jasmine.createSpy('attributeFn').and.returnValue({});
124+
linky("http://example.com and http://google.com", "_self", attributeFn);
125+
expect(attributeFn.calls.allArgs()).toEqual([['http://example.com'], ['http://google.com']]);
126+
});
127+
128+
122129
it('should strip unsafe attributes', function() {
123130
expect(linky("http://example.com", "_self", {"class": "blue", "onclick": "alert('Hi')"})).
124131
toBeOneOf('<a class="blue" target="_self" href="http://example.com">http://example.com</a>',

0 commit comments

Comments
 (0)