From b66776a047bf45a2f6ecb57cd70636f1ce849176 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 9 Mar 2017 10:23:59 -0800 Subject: [PATCH 1/3] Include element in validationCallback Fixes #591 --- src/Linkifier.test.ts | 4 ++-- src/Linkifier.ts | 2 +- src/Types.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Linkifier.test.ts b/src/Linkifier.test.ts index 012b1b68..97750c94 100644 --- a/src/Linkifier.test.ts +++ b/src/Linkifier.test.ts @@ -53,7 +53,7 @@ describe('Linkifier', () => { it('should enable link if true', done => { addRow('test'); linkifier.registerLinkMatcher(/test/, () => done(), { - validationCallback: (url, cb) => { + validationCallback: (url, element, cb) => { cb(true); assert.equal((rows[0].firstChild).tagName, 'A'); setTimeout(() => clickElement(rows[0].firstChild), 0); @@ -65,7 +65,7 @@ describe('Linkifier', () => { it('should disable link if false', done => { addRow('test'); linkifier.registerLinkMatcher(/test/, () => assert.fail(), { - validationCallback: (url, cb) => { + validationCallback: (url, element, cb) => { cb(false); assert.equal((rows[0].firstChild).tagName, 'A'); setTimeout(() => clickElement(rows[0].firstChild), 0); diff --git a/src/Linkifier.ts b/src/Linkifier.ts index a34edbc2..77cd16c1 100644 --- a/src/Linkifier.ts +++ b/src/Linkifier.ts @@ -160,7 +160,7 @@ export class Linkifier { const linkElement = this._doLinkifyRow(rowIndex, uri, matcher.handler, matcher.id === HYPERTEXT_LINK_MATCHER_ID); // Fire validation callback if (linkElement && matcher.validationCallback) { - matcher.validationCallback(uri, isValid => { + matcher.validationCallback(uri, linkElement, isValid => { if (!isValid) { linkElement.classList.add(INVALID_LINK_CLASS); } diff --git a/src/Types.ts b/src/Types.ts index 9e8cd762..896b729a 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -11,4 +11,4 @@ export type LinkMatcher = { priority?: number }; export type LinkMatcherHandler = (event: MouseEvent, uri: string) => boolean | void; -export type LinkMatcherValidationCallback = (uri: string, callback: (isValid: boolean) => void) => void; +export type LinkMatcherValidationCallback = (uri: string, element: HTMLElement, callback: (isValid: boolean) => void) => void; From 11f62bab342475b178abded7c1a1dbf2af96e431 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 9 Mar 2017 10:38:21 -0800 Subject: [PATCH 2/3] Expose setHypertextValidationCallback --- src/Linkifier.ts | 11 ++++++++++- src/xterm.js | 17 ++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Linkifier.ts b/src/Linkifier.ts index 77cd16c1..e7fbcb50 100644 --- a/src/Linkifier.ts +++ b/src/Linkifier.ts @@ -75,10 +75,19 @@ export class Linkifier { * @param {LinkHandler} handler The handler to use, this can be cleared with * null. */ - public attachHypertextLinkHandler(handler: LinkMatcherHandler): void { + public setHypertextLinkHandler(handler: LinkMatcherHandler): void { this._linkMatchers[HYPERTEXT_LINK_MATCHER_ID].handler = handler; } + /** + * Attaches a validation callback for hypertext links. + * @param {LinkMatcherValidationCallback} callback The callback to use, this + * can be cleared with null. + */ + public setHypertextValidationCallback(callback: LinkMatcherValidationCallback): void { + this._linkMatchers[HYPERTEXT_LINK_MATCHER_ID].validationCallback = callback; + } + /** * Registers a link matcher, allowing custom link patterns to be matched and * handled. diff --git a/src/xterm.js b/src/xterm.js index 37c00ed8..86f648b6 100644 --- a/src/xterm.js +++ b/src/xterm.js @@ -1286,7 +1286,7 @@ Terminal.prototype.attachCustomKeydownHandler = function(customKeydownHandler) { * reconstructed. Calling this with null will remove the handler. * @param {LinkHandler} handler The handler callback function. */ -Terminal.prototype.attachHypertextLinkHandler = function(handler) { +Terminal.prototype.setHypertextLinkHandler = function(handler) { if (!this.linkifier) { throw new Error('Cannot attach a hypertext link handler before Terminal.open is called'); } @@ -1295,6 +1295,21 @@ Terminal.prototype.attachHypertextLinkHandler = function(handler) { this.refresh(0, this.rows - 1); } +/** + * Attaches a validation callback for hypertext links. This is useful to use + * validation logic or to do something with the link's element and url. + * @param {LinkMatcherValidationCallback} callback The callback to use, this can + * be cleared with null. + */ +Terminal.prototype.setHypertextValidationCallback = function(handler) { + if (!this.linkifier) { + throw new Error('Cannot attach a hypertext validation callback before Terminal.open is called'); + } + this.linkifier.setHypertextValidationCallback(handler); + // Refresh to force links to refresh + this.refresh(0, this.rows - 1); +} + /** * Registers a link matcher, allowing custom link patterns to be matched and * handled. From bef7137c925f4e113cd35d26179aeea9b4b2f5c3 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 9 Mar 2017 10:47:09 -0800 Subject: [PATCH 3/3] Fix bug --- src/xterm.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xterm.js b/src/xterm.js index 86f648b6..f139bbc9 100644 --- a/src/xterm.js +++ b/src/xterm.js @@ -1290,7 +1290,7 @@ Terminal.prototype.setHypertextLinkHandler = function(handler) { if (!this.linkifier) { throw new Error('Cannot attach a hypertext link handler before Terminal.open is called'); } - this.linkifier.attachHypertextLinkHandler(handler); + this.linkifier.setHypertextLinkHandler(handler); // Refresh to force links to refresh this.refresh(0, this.rows - 1); }