]> git.proxmox.com Git - mirror_xterm.js.git/blobdiff - src/Linkifier.test.ts
Fix edge case
[mirror_xterm.js.git] / src / Linkifier.test.ts
index b1c1541df1ad7d7a5e08ce31ca2ad00a411c69da..f70718ea6eb97fea9cbd25bd615177bbbe91bd33 100644 (file)
@@ -5,12 +5,15 @@ import jsdom = require('jsdom');
 import { assert } from 'chai';
 import { ITerminal, ILinkifier } from './Interfaces';
 import { Linkifier } from './Linkifier';
+import { LinkMatcher } from './Types';
 
 class TestLinkifier extends Linkifier {
   constructor(document: Document, rows: HTMLElement[]) {
     Linkifier.TIME_BEFORE_LINKIFY = 0;
     super(document, rows);
   }
+
+  public get linkMatchers(): LinkMatcher[] { return this._linkMatchers; }
 }
 
 describe('Linkifier', () => {
@@ -19,14 +22,14 @@ describe('Linkifier', () => {
 
   let container: HTMLElement;
   let rows: HTMLElement[];
-  let linkifier: ILinkifier;
+  let linkifier: TestLinkifier;
 
   beforeEach(done => {
     rows = [];
     jsdom.env('', (err, w) => {
       window = w;
       document = window.document;
-      linkifier = new Linkifier(document, rows);
+      linkifier = new TestLinkifier(document, rows);
       container = document.createElement('div');
       document.body.appendChild(container);
       done();
@@ -46,6 +49,20 @@ describe('Linkifier', () => {
     element.dispatchEvent(event);
   }
 
+  function assertLinkifiesEntireRow(uri: string, done: MochaDone) {
+      addRow(uri);
+      linkifier.linkifyRow(0);
+      setTimeout(() => {
+        assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
+        assert.equal((<HTMLElement>rows[0].firstChild).textContent, uri);
+        done();
+      }, 0);
+  }
+
+  describe('http links', () => {
+    it('should allow ~ character in URI path', done => assertLinkifiesEntireRow('http://foo.com/a~b#c~d?e~f', done));
+  });
+
   describe('validationCallback', () => {
     it('should enable link if true', done => {
       addRow('test');
@@ -72,5 +89,40 @@ describe('Linkifier', () => {
       // Allow time for the click to be performed
       setTimeout(() => done(), 10);
     });
+
+    it('should trigger for multiple link matches on one row', done => {
+      addRow('test test');
+      let count = 0;
+      linkifier.registerLinkMatcher(/test/, () => assert.fail(), {
+        validationCallback: (url, cb) => {
+          count += 1;
+          if (count === 2) {
+            done();
+          }
+          cb(false);
+        }
+      });
+      linkifier.linkifyRow(0);
+    });
+  });
+
+  describe('priority', () => {
+    it('should order the list from highest priority to lowest #1', () => {
+      const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: 1 });
+      const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: -1 });
+      assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [aId, 0, bId]);
+    });
+
+    it('should order the list from highest priority to lowest #2', () => {
+      const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: -1 });
+      const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: 1 });
+      assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [bId, 0, aId]);
+    });
+
+    it('should order items of equal priority in the order they are added', () => {
+      const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: 0 });
+      const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: 0 });
+      assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [0, aId, bId]);
+    });
   });
 });