Skip to content
This repository was archived by the owner on Apr 11, 2018. It is now read-only.

Commit 26ebc3d

Browse files
committed
Use jsdom instead of phantomjs for linkifier test
1 parent d0f36e9 commit 26ebc3d

File tree

5 files changed

+84
-85
lines changed

5 files changed

+84
-85
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"gulp-sourcemaps": "1.9.1",
5555
"gulp-typescript": "^3.1.3",
5656
"jsdoc": "3.4.3",
57+
"jsdom": "^9.11.0",
5758
"merge-stream": "^1.0.1",
5859
"node-pty": "^0.4.1",
5960
"nodemon": "1.10.2",

src/Linkifier.phantom.ts

Lines changed: 0 additions & 76 deletions
This file was deleted.

src/Linkifier.test.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
9+
Linkifier.setTimeBeforeLinkifyForTest(0);
10+
11+
describe('Linkifier', () => {
12+
let window: Window;
13+
let document: Document;
14+
15+
let container: HTMLElement;
16+
let rows: HTMLElement[];
17+
let linkifier: ILinkifier;
18+
19+
beforeEach(done => {
20+
rows = [];
21+
jsdom.env('', (err, w) => {
22+
window = w;
23+
document = window.document;
24+
linkifier = new Linkifier(document, rows);
25+
container = document.createElement('div');
26+
document.body.appendChild(container);
27+
done();
28+
});
29+
});
30+
31+
function addRow(text: string) {
32+
const element = document.createElement('div');
33+
element.textContent = text;
34+
container.appendChild(element);
35+
rows.push(element);
36+
}
37+
38+
function clickElement(element: Node) {
39+
const event = document.createEvent('MouseEvent');
40+
event.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
41+
element.dispatchEvent(event);
42+
}
43+
44+
describe('validationCallback', () => {
45+
it('should enable link if true', done => {
46+
addRow('test');
47+
linkifier.registerLinkMatcher(/test/, () => done(), {
48+
validationCallback: (url, cb) => {
49+
cb(true);
50+
assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
51+
setTimeout(() => clickElement(rows[0].firstChild), 0);
52+
}
53+
});
54+
linkifier.linkifyRow(0);
55+
});
56+
57+
it('should disable link if false', done => {
58+
addRow('test');
59+
linkifier.registerLinkMatcher(/test/, () => assert.fail(), {
60+
validationCallback: (url, cb) => {
61+
cb(false);
62+
assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
63+
setTimeout(() => clickElement(rows[0].firstChild), 0);
64+
}
65+
});
66+
linkifier.linkifyRow(0);
67+
// Allow time for the click to be performed
68+
setTimeout(() => done(), 10);
69+
});
70+
});
71+
});

src/Linkifier.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ let TIME_BEFORE_LINKIFY = 200;
4747
* The Linkifier applies links to rows shortly after they have been refreshed.
4848
*/
4949
export class Linkifier {
50+
private _document: Document;
5051
private _rows: HTMLElement[];
5152
private _rowTimeoutIds: number[];
5253
private _linkMatchers: LinkMatcher[];
5354
private _nextLinkMatcherId = HYPERTEXT_LINK_MATCHER_ID;
5455

55-
constructor(rows: HTMLElement[]) {
56+
constructor(document: Document, rows: HTMLElement[]) {
57+
this._document = document;
5658
this._rows = rows;
5759
this._rowTimeoutIds = [];
5860
this._linkMatchers = [];
@@ -169,7 +171,8 @@ export class Linkifier {
169171
const linkElement = this._createAnchorElement(uri, handler);
170172
if (node.textContent.length === uri.length) {
171173
// Matches entire string
172-
if (node.nodeType === Node.TEXT_NODE) {
174+
175+
if (node.nodeType === 3 /*Node.TEXT_NODE*/) {
173176
this._replaceNode(node, linkElement);
174177
} else {
175178
const element = (<HTMLElement>node);
@@ -209,7 +212,7 @@ export class Linkifier {
209212
* @return {HTMLAnchorElement} The link.
210213
*/
211214
private _createAnchorElement(uri: string, handler: LinkMatcherHandler): HTMLAnchorElement {
212-
const element = document.createElement('a');
215+
const element = this._document.createElement('a');
213216
element.textContent = uri;
214217
if (handler) {
215218
element.addEventListener('click', () => {
@@ -249,7 +252,7 @@ export class Linkifier {
249252
*/
250253
private _replaceNodeSubstringWithNode(targetNode: Node, newNode: Node, substring: string, substringIndex: number): void {
251254
let node = targetNode;
252-
if (node.nodeType !== Node.TEXT_NODE) {
255+
if (node.nodeType !== 3/*Node.TEXT_NODE*/) {
253256
node = node.childNodes[0];
254257
}
255258

@@ -265,19 +268,19 @@ export class Linkifier {
265268
if (substringIndex === 0) {
266269
// Replace with <newNode><textnode>
267270
const rightText = fullText.substring(substring.length);
268-
const rightTextNode = document.createTextNode(rightText);
271+
const rightTextNode = this._document.createTextNode(rightText);
269272
this._replaceNode(node, newNode, rightTextNode);
270273
} else if (substringIndex === targetNode.textContent.length - substring.length) {
271274
// Replace with <textnode><newNode>
272275
const leftText = fullText.substring(0, substringIndex);
273-
const leftTextNode = document.createTextNode(leftText);
276+
const leftTextNode = this._document.createTextNode(leftText);
274277
this._replaceNode(node, leftTextNode, newNode);
275278
} else {
276279
// Replace with <textnode><newNode><textnode>
277280
const leftText = fullText.substring(0, substringIndex);
278-
const leftTextNode = document.createTextNode(leftText);
281+
const leftTextNode = this._document.createTextNode(leftText);
279282
const rightText = fullText.substring(substringIndex + substring.length);
280-
const rightTextNode = document.createTextNode(rightText);
283+
const rightTextNode = this._document.createTextNode(rightText);
281284
this._replaceNode(node, leftTextNode, newNode, rightTextNode);
282285
}
283286
}

src/xterm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ Terminal.prototype.open = function(parent) {
612612
this.rowContainer.classList.add('xterm-rows');
613613
this.element.appendChild(this.rowContainer);
614614
this.children = [];
615-
this.linkifier = new Linkifier(this.children);
615+
this.linkifier = new Linkifier(document, this.children);
616616

617617
// Create the container that will hold helpers like the textarea for
618618
// capturing DOM Events. Then produce the helpers.

0 commit comments

Comments
 (0)