]> git.proxmox.com Git - mirror_xterm.js.git/blame - src/Linkifier.test.ts
Support multiple link matches in a single row
[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
DI
10class TestLinkifier extends Linkifier {
11 constructor(document: Document, rows: HTMLElement[]) {
12 Linkifier.TIME_BEFORE_LINKIFY = 0;
13 super(document, rows);
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 => {
28 rows = [];
29 jsdom.env('', (err, w) => {
30 window = w;
31 document = window.document;
7ac4f1a9 32 linkifier = new TestLinkifier(document, rows);
26ebc3d9
DI
33 container = document.createElement('div');
34 document.body.appendChild(container);
35 done();
36 });
37 });
38
39 function addRow(text: string) {
40 const element = document.createElement('div');
41 element.textContent = text;
42 container.appendChild(element);
43 rows.push(element);
44 }
45
46 function clickElement(element: Node) {
47 const event = document.createEvent('MouseEvent');
48 event.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
49 element.dispatchEvent(event);
50 }
51
b68180b9
DI
52 function assertLinkifiesEntireRow(uri: string, done: MochaDone) {
53 addRow(uri);
54 linkifier.linkifyRow(0);
55 setTimeout(() => {
56 assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
57 assert.equal((<HTMLElement>rows[0].firstChild).textContent, uri);
58 done();
59 }, 0);
60 }
61
62 describe('http links', () => {
63 it('should allow ~ character in URI path', done => assertLinkifiesEntireRow('http://foo.com/a~b#c~d?e~f', done));
64 });
65
26ebc3d9
DI
66 describe('validationCallback', () => {
67 it('should enable link if true', done => {
68 addRow('test');
69 linkifier.registerLinkMatcher(/test/, () => done(), {
70 validationCallback: (url, cb) => {
71 cb(true);
72 assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
73 setTimeout(() => clickElement(rows[0].firstChild), 0);
74 }
75 });
76 linkifier.linkifyRow(0);
77 });
78
79 it('should disable link if false', done => {
80 addRow('test');
81 linkifier.registerLinkMatcher(/test/, () => assert.fail(), {
82 validationCallback: (url, cb) => {
83 cb(false);
84 assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
85 setTimeout(() => clickElement(rows[0].firstChild), 0);
86 }
87 });
88 linkifier.linkifyRow(0);
89 // Allow time for the click to be performed
90 setTimeout(() => done(), 10);
91 });
92 });
7ac4f1a9
DI
93
94 describe('priority', () => {
95 it('should order the list from highest priority to lowest #1', () => {
96 const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: 1 });
97 const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: -1 });
98 assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [aId, 0, bId]);
99 });
100
101 it('should order the list from highest priority to lowest #2', () => {
102 const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: -1 });
103 const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: 1 });
104 assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [bId, 0, aId]);
105 });
106
107 it('should order items of equal priority in the order they are added', () => {
108 const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: 0 });
109 const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: 0 });
110 assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [0, aId, bId]);
111 });
112 });
26ebc3d9 113});