]> git.proxmox.com Git - mirror_xterm.js.git/blame - src/Linkifier.test.ts
Merge pull request #619 from Tyriar/618_register_link_before_dom
[mirror_xterm.js.git] / src / Linkifier.test.ts
CommitLineData
26ebc3d9
DI
1/**
2 * @license MIT
3 */
4import jsdom = require('jsdom');
5import { assert } from 'chai';
6import { ITerminal, ILinkifier } from './Interfaces';
7import { Linkifier } from './Linkifier';
7ac4f1a9 8import { LinkMatcher } from './Types';
26ebc3d9 9
15d79143 10class 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
19describe('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
b0624cad
DI
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 });
26ebc3d9 45
b0624cad
DI
46 describe('after attachToDom', () => {
47 beforeEach(() => {
48 rows = [];
49 linkifier.attachToDom(document, rows);
50 container = document.createElement('div');
51 document.body.appendChild(container);
52 });
26ebc3d9 53
b0624cad
DI
54 function addRow(text: string) {
55 const element = document.createElement('div');
56 element.textContent = text;
57 container.appendChild(element);
58 rows.push(element);
59 }
b68180b9 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 }
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));
26ebc3d9
DI
79 });
80
b0624cad
DI
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);
26ebc3d9 92 });
7ac4f1a9 93
b0624cad
DI
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 });
7ac4f1a9
DI
107 });
108
b0624cad
DI
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 });
7ac4f1a9 115
b0624cad
DI
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 });
7ac4f1a9
DI
127 });
128 });
26ebc3d9 129});