Merge pull request #627 from Tyriar/591_element_on_validation

Give API access to valid link elements
This commit is contained in:
Daniel Imms
2017-04-05 11:34:34 -07:00
committed by GitHub
4 changed files with 34 additions and 9 deletions
+3 -3
View File
@@ -125,7 +125,7 @@ describe('Linkifier', () => {
it('should enable link if true', done => { it('should enable link if true', done => {
addRow('test'); addRow('test');
linkifier.registerLinkMatcher(/test/, () => done(), { linkifier.registerLinkMatcher(/test/, () => done(), {
validationCallback: (url, cb) => { validationCallback: (url, element, cb) => {
cb(true); cb(true);
assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A'); assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
setTimeout(() => clickElement(rows[0].firstChild), 0); setTimeout(() => clickElement(rows[0].firstChild), 0);
@@ -137,7 +137,7 @@ describe('Linkifier', () => {
it('should disable link if false', done => { it('should disable link if false', done => {
addRow('test'); addRow('test');
linkifier.registerLinkMatcher(/test/, () => assert.fail(), { linkifier.registerLinkMatcher(/test/, () => assert.fail(), {
validationCallback: (url, cb) => { validationCallback: (url, element, cb) => {
cb(false); cb(false);
assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A'); assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
setTimeout(() => clickElement(rows[0].firstChild), 0); setTimeout(() => clickElement(rows[0].firstChild), 0);
@@ -152,7 +152,7 @@ describe('Linkifier', () => {
addRow('test test'); addRow('test test');
let count = 0; let count = 0;
linkifier.registerLinkMatcher(/test/, () => assert.fail(), { linkifier.registerLinkMatcher(/test/, () => assert.fail(), {
validationCallback: (url, cb) => { validationCallback: (url, element, cb) => {
count += 1; count += 1;
if (count === 2) { if (count === 2) {
done(); done();
+13 -3
View File
@@ -88,10 +88,19 @@ export class Linkifier {
* @param {LinkHandler} handler The handler to use, this can be cleared with * @param {LinkHandler} handler The handler to use, this can be cleared with
* null. * null.
*/ */
public attachHypertextLinkHandler(handler: LinkMatcherHandler): void { public setHypertextLinkHandler(handler: LinkMatcherHandler): void {
this._linkMatchers[HYPERTEXT_LINK_MATCHER_ID].handler = handler; 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 * Registers a link matcher, allowing custom link patterns to be matched and
* handled. * handled.
@@ -173,9 +182,10 @@ export class Linkifier {
// Fire validation callback // Fire validation callback
if (matcher.validationCallback) { if (matcher.validationCallback) {
for (let j = 0; j < linkElements.length; j++) { 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) { if (!isValid) {
linkElements[j].classList.add(INVALID_LINK_CLASS); element.classList.add(INVALID_LINK_CLASS);
} }
}); });
} }
+1 -1
View File
@@ -11,4 +11,4 @@ export type LinkMatcher = {
priority?: number priority?: number
}; };
export type LinkMatcherHandler = (event: MouseEvent, uri: string) => boolean | void; 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;
+17 -2
View File
@@ -1319,11 +1319,26 @@ Terminal.prototype.attachCustomKeydownHandler = function(customKeydownHandler) {
* reconstructed. Calling this with null will remove the handler. * reconstructed. Calling this with null will remove the handler.
* @param {LinkHandler} handler The handler callback function. * @param {LinkHandler} handler The handler callback function.
*/ */
Terminal.prototype.attachHypertextLinkHandler = function(handler) { Terminal.prototype.setHypertextLinkHandler = function(handler) {
if (!this.linkifier) { if (!this.linkifier) {
throw new Error('Cannot attach a hypertext link handler before Terminal.open is called'); 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 // Refresh to force links to refresh
this.refresh(0, this.rows - 1); this.refresh(0, this.rows - 1);
} }