]> git.proxmox.com Git - mirror_xterm.js.git/blob - src/Linkifier.test.ts
Merge pull request #619 from Tyriar/618_register_link_before_dom
[mirror_xterm.js.git] / src / Linkifier.test.ts
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';
8 import { LinkMatcher } from './Types';
9
10 class TestLinkifier extends Linkifier {
11 constructor() {
12 Linkifier.TIME_BEFORE_LINKIFY = 0;
13 super();
14 }
15
16 public get linkMatchers(): LinkMatcher[] { return this._linkMatchers; }
17 }
18
19 describe('Linkifier', () => {
20 let window: Window;
21 let document: Document;
22
23 let container: HTMLElement;
24 let rows: HTMLElement[];
25 let linkifier: TestLinkifier;
26
27 beforeEach(done => {
28 jsdom.env('', (err, w) => {
29 window = w;
30 document = window.document;
31 linkifier = new TestLinkifier();
32 done();
33 });
34 });
35
36 describe('before attachToDom', () => {
37 it('should allow link matcher registration', done => {
38 assert.doesNotThrow(() => {
39 const linkMatcherId = linkifier.registerLinkMatcher(/foo/, () => {});
40 assert.isTrue(linkifier.deregisterLinkMatcher(linkMatcherId));
41 done();
42 });
43 });
44 });
45
46 describe('after attachToDom', () => {
47 beforeEach(() => {
48 rows = [];
49 linkifier.attachToDom(document, rows);
50 container = document.createElement('div');
51 document.body.appendChild(container);
52 });
53
54 function addRow(text: string) {
55 const element = document.createElement('div');
56 element.textContent = text;
57 container.appendChild(element);
58 rows.push(element);
59 }
60
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 }
66
67 function assertLinkifiesEntireRow(uri: string, done: MochaDone) {
68 addRow(uri);
69 linkifier.linkifyRow(0);
70 setTimeout(() => {
71 assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
72 assert.equal((<HTMLElement>rows[0].firstChild).textContent, uri);
73 done();
74 }, 0);
75 }
76
77 describe('http links', () => {
78 it('should allow ~ character in URI path', done => assertLinkifiesEntireRow('http://foo.com/a~b#c~d?e~f', done));
79 });
80
81 describe('validationCallback', () => {
82 it('should enable link if true', done => {
83 addRow('test');
84 linkifier.registerLinkMatcher(/test/, () => done(), {
85 validationCallback: (url, cb) => {
86 cb(true);
87 assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
88 setTimeout(() => clickElement(rows[0].firstChild), 0);
89 }
90 });
91 linkifier.linkifyRow(0);
92 });
93
94 it('should disable link if false', done => {
95 addRow('test');
96 linkifier.registerLinkMatcher(/test/, () => assert.fail(), {
97 validationCallback: (url, cb) => {
98 cb(false);
99 assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
100 setTimeout(() => clickElement(rows[0].firstChild), 0);
101 }
102 });
103 linkifier.linkifyRow(0);
104 // Allow time for the click to be performed
105 setTimeout(() => done(), 10);
106 });
107 });
108
109 describe('priority', () => {
110 it('should order the list from highest priority to lowest #1', () => {
111 const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: 1 });
112 const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: -1 });
113 assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [aId, 0, bId]);
114 });
115
116 it('should order the list from highest priority to lowest #2', () => {
117 const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: -1 });
118 const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: 1 });
119 assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [bId, 0, aId]);
120 });
121
122 it('should order items of equal priority in the order they are added', () => {
123 const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: 0 });
124 const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: 0 });
125 assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [0, aId, bId]);
126 });
127 });
128 });
129 });