forked from elastic/kibana
/
_url.js
200 lines (163 loc) · 8.2 KB
/
_url.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import $ from 'jquery';
import expect from 'expect.js';
import ngMock from 'ng_mock';
import { RegistryFieldFormatsProvider } from 'ui/registry/field_formats';
describe('Url Format', function () {
let fieldFormats;
beforeEach(ngMock.module('kibana'));
beforeEach(ngMock.inject(function (Private) {
fieldFormats = Private(RegistryFieldFormatsProvider);
}));
const unwrap = $el => {
expect($el.is('span[ng-non-bindable]')).to.be.ok();
return $el.children();
};
describe('Url Format', function () {
let Url;
beforeEach(function () {
Url = fieldFormats.getType('url');
});
it('ouputs a simple <a> tab by default', function () {
const url = new Url();
const $a = unwrap($(url.convert('http://elastic.co', 'html')));
expect($a.is('a')).to.be(true);
expect($a.size()).to.be(1);
expect($a.attr('href')).to.be('http://elastic.co');
expect($a.attr('target')).to.be('_blank');
expect($a.children().size()).to.be(0);
});
it('outputs an <image> if type === "img"', function () {
const url = new Url({ type: 'img' });
const $img = unwrap($(url.convert('http://elastic.co', 'html')));
expect($img.is('img')).to.be(true);
expect($img.attr('src')).to.be('http://elastic.co');
});
describe('url template', function () {
it('accepts a template', function () {
const url = new Url({ urlTemplate: 'http://{{ value }}' });
const $a = unwrap($(url.convert('url', 'html')));
expect($a.is('a')).to.be(true);
expect($a.size()).to.be(1);
expect($a.attr('href')).to.be('http://url');
expect($a.attr('target')).to.be('_blank');
expect($a.children().size()).to.be(0);
});
it('only outputs the url if the contentType === "text"', function () {
const url = new Url();
expect(url.convert('url', 'text')).to.be('url');
});
});
describe('label template', function () {
it('accepts a template', function () {
const url = new Url({ labelTemplate: 'extension: {{ value }}', urlTemplate: 'http://www.{{value}}.com' });
const $a = unwrap($(url.convert('php', 'html')));
expect($a.is('a')).to.be(true);
expect($a.size()).to.be(1);
expect($a.attr('href')).to.be('http://www.php.com');
expect($a.html()).to.be('extension: php');
});
it('uses the label template for text formating', function () {
const url = new Url({ labelTemplate: 'external {{value }}' });
expect(url.convert('url', 'text')).to.be('external url');
});
it('can use the raw value', function () {
const url = new Url({
labelTemplate: 'external {{value}}'
});
expect(url.convert('url?', 'text')).to.be('external url?');
});
it('can use the url', function () {
const url = new Url({
urlTemplate: 'http://google.com/{{value}}',
labelTemplate: 'external {{url}}'
});
expect(url.convert('url?', 'text')).to.be('external http://google.com/url%3F');
});
});
describe('templating', function () {
it('ignores unknown variables', function () {
const url = new Url({ urlTemplate: '{{ not really a var }}' });
expect(url.convert('url', 'text')).to.be('');
});
it('does not allow executing code in variable expressions', function () {
window.SHOULD_NOT_BE_TRUE = false;
const url = new Url({ urlTemplate: '{{ (window.SHOULD_NOT_BE_TRUE = true) && value }}' });
expect(url.convert('url', 'text')).to.be('');
});
describe('', function () {
it('does not get values from the prototype chain', function () {
const url = new Url({ urlTemplate: '{{ toString }}' });
expect(url.convert('url', 'text')).to.be('');
});
});
});
describe('whitelist', function () {
it('should assume a relative url if the value is not in the whitelist without a base path', function () {
const url = new Url();
const parsedUrl = {
origin: 'http://kibana',
basePath: '',
};
const converter = url.getConverterFor('html');
expect(converter('www.elastic.co', null, null, parsedUrl))
.to.be('<span ng-non-bindable><a href="http://kibana/app/www.elastic.co" target="_blank" rel="noopener noreferrer">www.elastic.co</a></span>');
expect(converter('elastic.co', null, null, parsedUrl))
.to.be('<span ng-non-bindable><a href="http://kibana/app/elastic.co" target="_blank" rel="noopener noreferrer">elastic.co</a></span>');
expect(converter('elastic', null, null, parsedUrl))
.to.be('<span ng-non-bindable><a href="http://kibana/app/elastic" target="_blank" rel="noopener noreferrer">elastic</a></span>');
expect(converter('ftp://elastic.co', null, null, parsedUrl))
.to.be('<span ng-non-bindable><a href="http://kibana/app/ftp://elastic.co" target="_blank" rel="noopener noreferrer">ftp://elastic.co</a></span>');
});
it('should assume a relative url if the value is not in the whitelist with a basepath', function () {
const url = new Url();
const parsedUrl = {
origin: 'http://kibana',
basePath: '/xyz',
};
const converter = url.getConverterFor('html');
expect(converter('www.elastic.co', null, null, parsedUrl))
.to.be('<span ng-non-bindable><a href="http://kibana/xyz/app/www.elastic.co" target="_blank" rel="noopener noreferrer">www.elastic.co</a></span>');
expect(converter('elastic.co', null, null, parsedUrl))
.to.be('<span ng-non-bindable><a href="http://kibana/xyz/app/elastic.co" target="_blank" rel="noopener noreferrer">elastic.co</a></span>');
expect(converter('elastic', null, null, parsedUrl))
.to.be('<span ng-non-bindable><a href="http://kibana/xyz/app/elastic" target="_blank" rel="noopener noreferrer">elastic</a></span>');
expect(converter('ftp://elastic.co', null, null, parsedUrl))
.to.be('<span ng-non-bindable><a href="http://kibana/xyz/app/ftp://elastic.co" target="_blank" rel="noopener noreferrer">ftp://elastic.co</a></span>');
});
it('should rely on parsedUrl', function () {
const url = new Url();
const parsedUrl = {
origin: 'http://kibana.host.com',
basePath: '/abc',
};
const converter = url.getConverterFor('html');
expect(converter('../app/kibana', null, null, parsedUrl))
.to.be('<span ng-non-bindable><a href="http://kibana.host.com/abc/app/../app/kibana" target="_blank" rel="noopener noreferrer">../app/kibana</a></span>');
});
it('should fail gracefully if there are no parsedUrl provided', function () {
const url = new Url();
const parsedUrl = null;
const converter = url.getConverterFor('html');
expect(converter('../app/kibana', null, null, parsedUrl))
.to.be('<span ng-non-bindable>../app/kibana</span>');
expect(converter('http://www.elastic.co', null, null, parsedUrl))
.to.be('<span ng-non-bindable><a href="http://www.elastic.co" target="_blank" rel="noopener noreferrer">http://www.elastic.co</a></span>');
});
it('should support multiple types of relative urls', function () {
const url = new Url();
const parsedUrl = {
origin: 'http://kibana.host.com',
pathname: '/nbc/app/kibana#/discover',
basePath: '/nbc',
};
const converter = url.getConverterFor('html');
expect(converter('#/foo', null, null, parsedUrl))
.to.be('<span ng-non-bindable><a href="http://kibana.host.com/nbc/app/kibana#/discover#/foo" target="_blank" rel="noopener noreferrer">#/foo</a></span>');
expect(converter('/nbc/app/kibana#/discover', null, null, parsedUrl))
.to.be('<span ng-non-bindable><a href="http://kibana.host.com/nbc/app/kibana#/discover" target="_blank" rel="noopener noreferrer">/nbc/app/kibana#/discover</a></span>');
expect(converter('../foo/bar', null, null, parsedUrl))
.to.be('<span ng-non-bindable><a href="http://kibana.host.com/nbc/app/../foo/bar" target="_blank" rel="noopener noreferrer">../foo/bar</a></span>');
});
});
});
});