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';
10 class TestLinkifier extends Linkifier {
12 Linkifier.TIME_BEFORE_LINKIFY = 0;
16 public get linkMatchers(): LinkMatcher[] { return this._linkMatchers; }
19 describe('Linkifier', () => {
22 let document: Document;
24 let container: HTMLElement;
25 let rows: HTMLElement[];
26 let linkifier: TestLinkifier;
29 dom = new jsdom.JSDOM('')
31 document = window.document;
32 linkifier = new TestLinkifier();
35 function addRow(html: string) {
36 const element = document.createElement('div');
37 element.innerHTML = html;
38 container.appendChild(element);
42 describe('before attachToDom', () => {
43 it('should allow link matcher registration', done => {
44 assert.doesNotThrow(() => {
45 const linkMatcherId = linkifier.registerLinkMatcher(/foo/, () => {});
46 assert.isTrue(linkifier.deregisterLinkMatcher(linkMatcherId));
52 describe('after attachToDom', () => {
55 linkifier.attachToDom(document, rows);
56 container = document.createElement('div');
57 document.body.appendChild(container);
60 function clickElement(element: Node) {
61 const event = document.createEvent('MouseEvent');
62 event.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
63 element.dispatchEvent(event);
66 function assertLinkifiesEntireRow(uri: string, done: MochaDone) {
68 linkifier.linkifyRow(0);
70 assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
71 assert.equal((<HTMLElement>rows[0].firstChild).textContent, uri);
76 describe('http links', () => {
77 function assertLinkifiesEntireRow(uri: string, done: MochaDone) {
79 linkifier.linkifyRow(0);
81 assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
82 assert.equal((<HTMLElement>rows[0].firstChild).textContent, uri);
86 it('should allow ~ character in URI path', done => assertLinkifiesEntireRow('http://foo.com/a~b#c~d?e~f', done));
89 describe('link matcher', () => {
90 function assertLinkifiesRow(rowText: string, linkMatcherRegex: RegExp, expectedHtml: string, done: MochaDone) {
92 linkifier.registerLinkMatcher(linkMatcherRegex, () => {});
93 linkifier.linkifyRow(0);
94 // Allow linkify to happen
96 assert.equal(rows[0].innerHTML, expectedHtml);
100 it('should match a single link', done => {
101 assertLinkifiesRow('foo', /foo/, '<a>foo</a>', done);
103 it('should match a single link at the start of a text node', done => {
104 assertLinkifiesRow('foo bar', /foo/, '<a>foo</a> bar', done);
106 it('should match a single link in the middle of a text node', done => {
107 assertLinkifiesRow('foo bar baz', /bar/, 'foo <a>bar</a> baz', done);
109 it('should match a single link at the end of a text node', done => {
110 assertLinkifiesRow('foo bar', /bar/, 'foo <a>bar</a>', done);
112 it('should match a link after a link at the start of a text node', done => {
113 assertLinkifiesRow('foo bar', /foo|bar/, '<a>foo</a> <a>bar</a>', done);
115 it('should match a link after a link in the middle of a text node', done => {
116 assertLinkifiesRow('foo bar baz', /bar|baz/, 'foo <a>bar</a> <a>baz</a>', done);
118 it('should match a link immediately after a link at the end of a text node', done => {
119 assertLinkifiesRow('<span>foo bar</span>baz', /bar|baz/, '<span>foo <a>bar</a></span><a>baz</a>', done);
121 it('should not duplicate text after a unicode character (wrapped in a span)', done => {
122 // This is a regression test for an issue that came about when using
123 // an oh-my-zsh theme that added the large blue diamond unicode
124 // character (U+1F537) which caused the path to be duplicated. See #642.
125 assertLinkifiesRow('echo \'<span class="xterm-normal-char">🔷</span>foo\'', /foo/, 'echo \'<span class="xterm-normal-char">🔷</span><a>foo</a>\'', done);
129 describe('validationCallback', () => {
130 it('should enable link if true', done => {
132 linkifier.registerLinkMatcher(/test/, () => done(), {
133 validationCallback: (url, element, cb) => {
135 assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
136 setTimeout(() => clickElement(rows[0].firstChild), 0);
139 linkifier.linkifyRow(0);
142 it('should disable link if false', done => {
144 linkifier.registerLinkMatcher(/test/, () => assert.fail(), {
145 validationCallback: (url, element, cb) => {
147 assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
148 setTimeout(() => clickElement(rows[0].firstChild), 0);
151 linkifier.linkifyRow(0);
152 // Allow time for the click to be performed
153 setTimeout(() => done(), 10);
156 it('should trigger for multiple link matches on one row', done => {
159 linkifier.registerLinkMatcher(/test/, () => assert.fail(), {
160 validationCallback: (url, element, cb) => {
168 linkifier.linkifyRow(0);
172 describe('priority', () => {
173 it('should order the list from highest priority to lowest #1', () => {
174 const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: 1 });
175 const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: -1 });
176 assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [aId, 0, bId]);
179 it('should order the list from highest priority to lowest #2', () => {
180 const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: -1 });
181 const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: 1 });
182 assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [bId, 0, aId]);
185 it('should order items of equal priority in the order they are added', () => {
186 const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: 0 });
187 const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: 0 });
188 assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [0, aId, bId]);