forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkySpec.js
140 lines (111 loc) · 6.79 KB
/
linkySpec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'use strict';
describe('linky', function() {
var linky;
beforeEach(module('ngSanitize'));
beforeEach(inject(function($filter) {
linky = $filter('linky');
}));
it('should do basic filter', function() {
expect(linky('http://ab/ (http://a/) <http://a/> http://1.2/v:~-123. c “http://example.com” ‘http://me.com’')).
toEqual('<a href="http://ab/">http://ab/</a> ' +
'(<a href="http://a/">http://a/</a>) ' +
'<<a href="http://a/">http://a/</a>> ' +
'<a href="http://1.2/v:~-123">http://1.2/v:~-123</a>. c ' +
'“<a href="http://example.com">http://example.com</a>” ' +
'‘<a href="http://me.com">http://me.com</a>’');
expect(linky(undefined)).not.toBeDefined();
});
it('should return `undefined`/`null`/`""` values unchanged', function() {
expect(linky(undefined)).toBeUndefined();
expect(linky(null)).toBe(null);
expect(linky('')).toBe('');
});
it('should throw an error when used with a non-string value (other than `undefined`/`null`)',
function() {
expect(function() { linky(false); }).
toThrowMinErr('linky', 'notstring', 'Expected string but received: false');
expect(function() { linky(true); }).
toThrowMinErr('linky', 'notstring', 'Expected string but received: true');
expect(function() { linky(0); }).
toThrowMinErr('linky', 'notstring', 'Expected string but received: 0');
expect(function() { linky(42); }).
toThrowMinErr('linky', 'notstring', 'Expected string but received: 42');
expect(function() { linky({}); }).
toThrowMinErr('linky', 'notstring', 'Expected string but received: {}');
expect(function() { linky([]); }).
toThrowMinErr('linky', 'notstring', 'Expected string but received: []');
expect(function() { linky(noop); }).
toThrowMinErr('linky', 'notstring', 'Expected string but received: function noop()');
}
);
it('should be case-insensitive', function() {
expect(linky('WWW.example.com')).toEqual('<a href="http://WWW.example.com">WWW.example.com</a>');
expect(linky('WWW.EXAMPLE.COM')).toEqual('<a href="http://WWW.EXAMPLE.COM">WWW.EXAMPLE.COM</a>');
expect(linky('HTTP://www.example.com')).toEqual('<a href="HTTP://www.example.com">HTTP://www.example.com</a>');
expect(linky('HTTP://example.com')).toEqual('<a href="HTTP://example.com">HTTP://example.com</a>');
expect(linky('HTTPS://www.example.com')).toEqual('<a href="HTTPS://www.example.com">HTTPS://www.example.com</a>');
expect(linky('HTTPS://example.com')).toEqual('<a href="HTTPS://example.com">HTTPS://example.com</a>');
expect(linky('FTP://www.example.com')).toEqual('<a href="FTP://www.example.com">FTP://www.example.com</a>');
expect(linky('FTP://example.com')).toEqual('<a href="FTP://example.com">FTP://example.com</a>');
expect(linky('SFTP://www.example.com')).toEqual('<a href="SFTP://www.example.com">SFTP://www.example.com</a>');
expect(linky('SFTP://example.com')).toEqual('<a href="SFTP://example.com">SFTP://example.com</a>');
});
it('should handle www.', function() {
expect(linky('www.example.com')).toEqual('<a href="http://www.example.com">www.example.com</a>');
});
it('should handle mailto:', function() {
expect(linky('mailto:[email protected]')).
toEqual('<a href="mailto:[email protected]">[email protected]</a>');
expect(linky('[email protected]')).
toEqual('<a href="mailto:[email protected]">[email protected]</a>');
expect(linky('send email to [email protected], but')).
toEqual('send email to <a href="mailto:[email protected]">[email protected]</a>, but');
expect(linky('my email is "[email protected]"')).
toEqual('my email is "<a href="mailto:[email protected]">[email protected]</a>"');
});
it('should handle quotes in the email', function() {
expect(linky('foo@"bar".com')).toEqual('<a href="mailto:foo@"bar".com">foo@"bar".com</a>');
});
it('should handle target:', function() {
expect(linky('http://example.com', '_blank')).
toBeOneOf('<a target="_blank" href="http://example.com">http://example.com</a>',
'<a href="http://example.com" target="_blank">http://example.com</a>');
expect(linky('http://example.com', 'someNamedIFrame')).
toBeOneOf('<a target="someNamedIFrame" href="http://example.com">http://example.com</a>',
'<a href="http://example.com" target="someNamedIFrame">http://example.com</a>');
});
describe('custom attributes', function() {
it('should optionally add custom attributes', function() {
expect(linky('http://example.com', '_self', {rel: 'nofollow'})).
toBeOneOf('<a rel="nofollow" target="_self" href="http://example.com">http://example.com</a>',
'<a href="http://example.com" target="_self" rel="nofollow">http://example.com</a>');
});
it('should override target parameter with custom attributes', function() {
expect(linky('http://example.com', '_self', {target: '_blank'})).
toBeOneOf('<a target="_blank" href="http://example.com">http://example.com</a>',
'<a href="http://example.com" target="_blank">http://example.com</a>');
});
it('should optionally add custom attributes from function', function() {
expect(linky('http://example.com', '_self', function(url) {return {'class': 'blue'};})).
toBeOneOf('<a class="blue" target="_self" href="http://example.com">http://example.com</a>',
'<a href="http://example.com" target="_self" class="blue">http://example.com</a>',
'<a class="blue" href="http://example.com" target="_self">http://example.com</a>');
});
it('should pass url as parameter to custom attribute function', function() {
var linkParameters = jasmine.createSpy('linkParameters').and.returnValue({'class': 'blue'});
linky('http://example.com', '_self', linkParameters);
expect(linkParameters).toHaveBeenCalledWith('http://example.com');
});
it('should call the attribute function for all links in the input', function() {
var attributeFn = jasmine.createSpy('attributeFn').and.returnValue({});
linky('http://example.com and http://google.com', '_self', attributeFn);
expect(attributeFn.calls.allArgs()).toEqual([['http://example.com'], ['http://google.com']]);
});
it('should strip unsafe attributes', function() {
expect(linky('http://example.com', '_self', {'class': 'blue', 'onclick': 'alert(\'Hi\')'})).
toBeOneOf('<a class="blue" target="_self" href="http://example.com">http://example.com</a>',
'<a href="http://example.com" target="_self" class="blue">http://example.com</a>',
'<a class="blue" href="http://example.com" target="_self">http://example.com</a>');
});
});
});