diff --git a/src/Linkifier.test.ts b/src/Linkifier.test.ts index f4dc9f4d..2450ffb9 100644 --- a/src/Linkifier.test.ts +++ b/src/Linkifier.test.ts @@ -125,7 +125,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); @@ -137,7 +137,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); @@ -152,7 +152,7 @@ describe('Linkifier', () => { addRow('test test'); let count = 0; linkifier.registerLinkMatcher(/test/, () => assert.fail(), { - validationCallback: (url, cb) => { + validationCallback: (url, element, cb) => { count += 1; if (count === 2) { done(); diff --git a/src/Linkifier.ts b/src/Linkifier.ts index 6f93a897..f99cdaa4 100644 --- a/src/Linkifier.ts +++ b/src/Linkifier.ts @@ -88,10 +88,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. @@ -173,9 +182,10 @@ export class Linkifier { // Fire validation callback if (matcher.validationCallback) { for (let j = 0; j < linkElements.length; j++) { - matcher.validationCallback(linkElements[j].textContent, isValid => { + const element = linkElements[j]; + matcher.validationCallback(element.textContent, element, isValid => { if (!isValid) { - linkElements[j].classList.add(INVALID_LINK_CLASS); + element.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; diff --git a/src/xterm.js b/src/xterm.js index 2c6ad38e..09c03322 100644 --- a/src/xterm.js +++ b/src/xterm.js @@ -1319,11 +1319,26 @@ 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'); } - this.linkifier.attachHypertextLinkHandler(handler); + this.linkifier.setHypertextLinkHandler(handler); + // Refresh to force links to refresh + 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); }