import { LinkMatcher } from './Types';
class TestLinkifier extends Linkifier {
- constructor(document: Document, rows: HTMLElement[]) {
+ constructor() {
Linkifier.TIME_BEFORE_LINKIFY = 0;
- super(document, rows);
+ super();
}
public get linkMatchers(): LinkMatcher[] { return this._linkMatchers; }
let linkifier: TestLinkifier;
beforeEach(done => {
- rows = [];
jsdom.env('', (err, w) => {
window = w;
document = window.document;
- linkifier = new TestLinkifier(document, rows);
- container = document.createElement('div');
- document.body.appendChild(container);
+ linkifier = new TestLinkifier();
done();
});
});
- function addRow(text: string) {
- const element = document.createElement('div');
- element.textContent = text;
- container.appendChild(element);
- rows.push(element);
- }
+ describe('before attachToDom', () => {
+ it('should allow link matcher registration', done => {
+ assert.doesNotThrow(() => {
+ const linkMatcherId = linkifier.registerLinkMatcher(/foo/, () => {});
+ assert.isTrue(linkifier.deregisterLinkMatcher(linkMatcherId));
+ done();
+ });
+ });
+ });
- function clickElement(element: Node) {
- const event = document.createEvent('MouseEvent');
- event.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
- element.dispatchEvent(event);
- }
+ describe('after attachToDom', () => {
+ beforeEach(() => {
+ rows = [];
+ linkifier.attachToDom(document, rows);
+ container = document.createElement('div');
+ document.body.appendChild(container);
+ });
- 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);
- }
+ function addRow(text: string) {
+ const element = document.createElement('div');
+ element.textContent = text;
+ container.appendChild(element);
+ rows.push(element);
+ }
- describe('http links', () => {
- it('should allow ~ character in URI path', done => assertLinkifiesEntireRow('http://foo.com/a~b#c~d?e~f', done));
- });
+ function clickElement(element: Node) {
+ const event = document.createEvent('MouseEvent');
+ event.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
+ element.dispatchEvent(event);
+ }
- describe('validationCallback', () => {
- it('should enable link if true', done => {
- addRow('test');
- linkifier.registerLinkMatcher(/test/, () => done(), {
- validationCallback: (url, cb) => {
- cb(true);
+ function assertLinkifiesEntireRow(uri: string, done: MochaDone) {
+ addRow(uri);
+ linkifier.linkifyRow(0);
+ setTimeout(() => {
assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
- setTimeout(() => clickElement(rows[0].firstChild), 0);
- }
- });
- linkifier.linkifyRow(0);
+ 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));
});
- it('should disable link if false', done => {
- addRow('test');
- linkifier.registerLinkMatcher(/test/, () => assert.fail(), {
- validationCallback: (url, cb) => {
- cb(false);
- assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
- setTimeout(() => clickElement(rows[0].firstChild), 0);
- }
+ describe('validationCallback', () => {
+ it('should enable link if true', done => {
+ addRow('test');
+ linkifier.registerLinkMatcher(/test/, () => done(), {
+ validationCallback: (url, cb) => {
+ cb(true);
+ assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
+ setTimeout(() => clickElement(rows[0].firstChild), 0);
+ }
+ });
+ linkifier.linkifyRow(0);
});
- linkifier.linkifyRow(0);
- // Allow time for the click to be performed
- setTimeout(() => done(), 10);
- });
- });
- 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 disable link if false', done => {
+ addRow('test');
+ linkifier.registerLinkMatcher(/test/, () => assert.fail(), {
+ validationCallback: (url, cb) => {
+ cb(false);
+ assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
+ setTimeout(() => clickElement(rows[0].firstChild), 0);
+ }
+ });
+ linkifier.linkifyRow(0);
+ // Allow time for the click to be performed
+ setTimeout(() => done(), 10);
+ });
});
- 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]);
- });
+ 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 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]);
+ 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]);
+ });
});
});
});