]>
Commit | Line | Data |
---|---|---|
26ebc3d9 DI |
1 | /** |
2 | * @license MIT | |
3 | */ | |
4 | import jsdom = require('jsdom'); | |
5 | import { assert } from 'chai'; | |
6 | import { ITerminal, ILinkifier } from './Interfaces'; | |
7 | import { Linkifier } from './Linkifier'; | |
7ac4f1a9 | 8 | import { LinkMatcher } from './Types'; |
26ebc3d9 | 9 | |
15d79143 | 10 | class TestLinkifier extends Linkifier { |
b0624cad | 11 | constructor() { |
15d79143 | 12 | Linkifier.TIME_BEFORE_LINKIFY = 0; |
b0624cad | 13 | super(); |
15d79143 | 14 | } |
7ac4f1a9 DI |
15 | |
16 | public get linkMatchers(): LinkMatcher[] { return this._linkMatchers; } | |
15d79143 | 17 | } |
26ebc3d9 DI |
18 | |
19 | describe('Linkifier', () => { | |
20 | let window: Window; | |
21 | let document: Document; | |
22 | ||
23 | let container: HTMLElement; | |
24 | let rows: HTMLElement[]; | |
7ac4f1a9 | 25 | let linkifier: TestLinkifier; |
26ebc3d9 DI |
26 | |
27 | beforeEach(done => { | |
26ebc3d9 DI |
28 | jsdom.env('', (err, w) => { |
29 | window = w; | |
30 | document = window.document; | |
b0624cad | 31 | linkifier = new TestLinkifier(); |
26ebc3d9 DI |
32 | done(); |
33 | }); | |
34 | }); | |
35 | ||
8c2db8dd | 36 | function addRow(html: string) { |
26ebc3d9 | 37 | const element = document.createElement('div'); |
8c2db8dd | 38 | element.innerHTML = html; |
26ebc3d9 DI |
39 | container.appendChild(element); |
40 | rows.push(element); | |
41 | } | |
42 | ||
b0624cad DI |
43 | describe('before attachToDom', () => { |
44 | it('should allow link matcher registration', done => { | |
45 | assert.doesNotThrow(() => { | |
46 | const linkMatcherId = linkifier.registerLinkMatcher(/foo/, () => {}); | |
47 | assert.isTrue(linkifier.deregisterLinkMatcher(linkMatcherId)); | |
b68180b9 | 48 | done(); |
26ebc3d9 | 49 | }); |
b0624cad | 50 | }); |
b68180b9 DI |
51 | }); |
52 | ||
b0624cad DI |
53 | describe('after attachToDom', () => { |
54 | beforeEach(() => { | |
55 | rows = []; | |
56 | linkifier.attachToDom(document, rows); | |
57 | container = document.createElement('div'); | |
58 | document.body.appendChild(container); | |
26ebc3d9 DI |
59 | }); |
60 | ||
b0624cad DI |
61 | function clickElement(element: Node) { |
62 | const event = document.createEvent('MouseEvent'); | |
63 | event.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); | |
64 | element.dispatchEvent(event); | |
65 | } | |
b68180b9 | 66 | |
b0624cad DI |
67 | function assertLinkifiesEntireRow(uri: string, done: MochaDone) { |
68 | addRow(uri); | |
69 | linkifier.linkifyRow(0); | |
70 | setTimeout(() => { | |
26ebc3d9 | 71 | assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A'); |
b0624cad DI |
72 | assert.equal((<HTMLElement>rows[0].firstChild).textContent, uri); |
73 | done(); | |
74 | }, 0); | |
75 | } | |
26ebc3d9 | 76 | |
b0624cad | 77 | describe('http links', () => { |
fe8334a8 DI |
78 | function assertLinkifiesEntireRow(uri: string, done: MochaDone) { |
79 | addRow(uri); | |
80 | linkifier.linkifyRow(0); | |
81 | setTimeout(() => { | |
26ebc3d9 | 82 | assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A'); |
fe8334a8 DI |
83 | assert.equal((<HTMLElement>rows[0].firstChild).textContent, uri); |
84 | done(); | |
85 | }, 0); | |
86 | } | |
b0624cad | 87 | it('should allow ~ character in URI path', done => assertLinkifiesEntireRow('http://foo.com/a~b#c~d?e~f', done)); |
26ebc3d9 | 88 | }); |
7ac4f1a9 | 89 | |
fe8334a8 DI |
90 | describe('link matcher', () => { |
91 | function assertLinkifiesRow(rowText: string, linkMatcherRegex: RegExp, expectedHtml: string, done: MochaDone) { | |
92 | addRow(rowText); | |
93 | linkifier.registerLinkMatcher(linkMatcherRegex, () => {}); | |
94 | linkifier.linkifyRow(0); | |
95 | // Allow linkify to happen | |
96 | setTimeout(() => { | |
97 | assert.equal(rows[0].innerHTML, expectedHtml); | |
98 | done(); | |
99 | }, 0); | |
100 | } | |
101 | it('should match a single link', done => { | |
102 | assertLinkifiesRow('foo', /foo/, '<a>foo</a>', done); | |
103 | }); | |
104 | it('should match a single link at the start of a text node', done => { | |
105 | assertLinkifiesRow('foo bar', /foo/, '<a>foo</a> bar', done); | |
106 | }); | |
107 | it('should match a single link in the middle of a text node', done => { | |
108 | assertLinkifiesRow('foo bar baz', /bar/, 'foo <a>bar</a> baz', done); | |
109 | }); | |
110 | it('should match a single link at the end of a text node', done => { | |
111 | assertLinkifiesRow('foo bar', /bar/, 'foo <a>bar</a>', done); | |
112 | }); | |
113 | it('should match a link after a link at the start of a text node', done => { | |
114 | assertLinkifiesRow('foo bar', /foo|bar/, '<a>foo</a> <a>bar</a>', done); | |
115 | }); | |
116 | it('should match a link after a link in the middle of a text node', done => { | |
117 | assertLinkifiesRow('foo bar baz', /bar|baz/, 'foo <a>bar</a> <a>baz</a>', done); | |
118 | }); | |
119 | it('should match a link immediately after a link at the end of a text node', done => { | |
120 | assertLinkifiesRow('<span>foo bar</span>baz', /bar|baz/, '<span>foo <a>bar</a></span><a>baz</a>', done); | |
26ebc3d9 | 121 | }); |
ee1da73a DI |
122 | it('should not duplicate text after a unicode character (wrapped in a span)', done => { |
123 | // This is a regression test for an issue that came about when using | |
124 | // an oh-my-zsh theme that added the large blue diamond unicode | |
125 | // character (U+1F537) which caused the path to be duplicated. See #642. | |
126 | assertLinkifiesRow('echo \'<span class="xterm-normal-char">🔷</span>foo\'', /foo/, 'echo \'<span class="xterm-normal-char">🔷</span><a>foo</a>\'', done); | |
127 | }); | |
7ac4f1a9 DI |
128 | }); |
129 | ||
b0624cad DI |
130 | describe('validationCallback', () => { |
131 | it('should enable link if true', done => { | |
132 | addRow('test'); | |
133 | linkifier.registerLinkMatcher(/test/, () => done(), { | |
584ec681 | 134 | validationCallback: (url, element, cb) => { |
b0624cad DI |
135 | cb(true); |
136 | assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A'); | |
137 | setTimeout(() => clickElement(rows[0].firstChild), 0); | |
d8140097 | 138 | } |
b0624cad DI |
139 | }); |
140 | linkifier.linkifyRow(0); | |
d8140097 | 141 | }); |
7ac4f1a9 | 142 | |
b0624cad DI |
143 | it('should disable link if false', done => { |
144 | addRow('test'); | |
145 | linkifier.registerLinkMatcher(/test/, () => assert.fail(), { | |
584ec681 | 146 | validationCallback: (url, element, cb) => { |
b0624cad DI |
147 | cb(false); |
148 | assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A'); | |
149 | setTimeout(() => clickElement(rows[0].firstChild), 0); | |
150 | } | |
151 | }); | |
152 | linkifier.linkifyRow(0); | |
153 | // Allow time for the click to be performed | |
154 | setTimeout(() => done(), 10); | |
155 | }); | |
7ac4f1a9 | 156 | |
fe8334a8 DI |
157 | it('should trigger for multiple link matches on one row', done => { |
158 | addRow('test test'); | |
159 | let count = 0; | |
160 | linkifier.registerLinkMatcher(/test/, () => assert.fail(), { | |
584ec681 | 161 | validationCallback: (url, element, cb) => { |
fe8334a8 DI |
162 | count += 1; |
163 | if (count === 2) { | |
164 | done(); | |
165 | } | |
166 | cb(false); | |
167 | } | |
168 | }); | |
169 | linkifier.linkifyRow(0); | |
170 | }); | |
7ac4f1a9 DI |
171 | }); |
172 | ||
b0624cad DI |
173 | describe('priority', () => { |
174 | it('should order the list from highest priority to lowest #1', () => { | |
175 | const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: 1 }); | |
176 | const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: -1 }); | |
177 | assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [aId, 0, bId]); | |
178 | }); | |
7ac4f1a9 | 179 | |
b0624cad DI |
180 | it('should order the list from highest priority to lowest #2', () => { |
181 | const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: -1 }); | |
182 | const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: 1 }); | |
183 | assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [bId, 0, aId]); | |
184 | }); | |
185 | ||
186 | it('should order items of equal priority in the order they are added', () => { | |
187 | const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: 0 }); | |
188 | const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: 0 }); | |
189 | assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [0, aId, bId]); | |
190 | }); | |
7ac4f1a9 DI |
191 | }); |
192 | }); | |
26ebc3d9 | 193 | }); |