Skip to content

Commit

Permalink
Merge pull request #627 from Tyriar/591_element_on_validation
Browse files Browse the repository at this point in the history
Give API access to valid link elements
  • Loading branch information
Tyriar authored Apr 5, 2017
2 parents 0c5b5cc + 584ec68 commit df13469
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/Linkifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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((<HTMLElement>rows[0].firstChild).tagName, 'A');
setTimeout(() => clickElement(rows[0].firstChild), 0);
Expand All @@ -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((<HTMLElement>rows[0].firstChild).tagName, 'A');
setTimeout(() => clickElement(rows[0].firstChild), 0);
Expand All @@ -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();
Expand Down
16 changes: 13 additions & 3 deletions src/Linkifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
19 changes: 17 additions & 2 deletions src/xterm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit df13469

Please sign in to comment.