-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1298 from Tyriar/1297_webLinks_addon
Pull web link support into an addon
- Loading branch information
Showing
12 changed files
with
124 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "xterm.weblinks", | ||
"main": "weblinks.js", | ||
"private": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"target": "es5", | ||
"rootDir": ".", | ||
"outDir": "../../../lib/addons/webLinks/", | ||
"sourceMap": true, | ||
"removeComments": true, | ||
"declaration": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Copyright (c) 2017 The xterm.js authors. All rights reserved. | ||
* @license MIT | ||
*/ | ||
|
||
import { assert, expect } from 'chai'; | ||
|
||
import * as webLinks from './webLinks'; | ||
|
||
class MockTerminal { | ||
public regex: RegExp; | ||
public handler: (event: MouseEvent, uri: string) => void; | ||
public options?: any; | ||
|
||
public registerLinkMatcher(regex: RegExp, handler: (event: MouseEvent, uri: string) => void, options?: any): number { | ||
this.regex = regex; | ||
this.handler = handler; | ||
this.options = options; | ||
return 0; | ||
} | ||
} | ||
|
||
describe('webLinks addon', () => { | ||
describe('apply', () => { | ||
it('should do register the `webLinksInit` method', () => { | ||
webLinks.apply(<any>MockTerminal); | ||
assert.equal(typeof (<any>MockTerminal).prototype.webLinksInit, 'function'); | ||
}); | ||
}); | ||
|
||
it('should allow ~ character in URI path', () => { | ||
const term = new MockTerminal(); | ||
webLinks.webLinksInit(<any>term); | ||
|
||
const row = ' http://foo.com/a~b#c~d?e~f '; | ||
|
||
let match = row.match(term.regex); | ||
let uri = match[term.options.matchIndex]; | ||
|
||
assert.equal(uri, 'http://foo.com/a~b#c~d?e~f'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Copyright (c) 2017 The xterm.js authors. All rights reserved. | ||
* @license MIT | ||
*/ | ||
|
||
/// <reference path="../../../typings/xterm.d.ts"/> | ||
|
||
import { Terminal, ILinkMatcherOptions } from 'xterm'; | ||
|
||
const protocolClause = '(https?:\\/\\/)'; | ||
const domainCharacterSet = '[\\da-z\\.-]+'; | ||
const negatedDomainCharacterSet = '[^\\da-z\\.-]+'; | ||
const domainBodyClause = '(' + domainCharacterSet + ')'; | ||
const tldClause = '([a-z\\.]{2,6})'; | ||
const ipClause = '((\\d{1,3}\\.){3}\\d{1,3})'; | ||
const localHostClause = '(localhost)'; | ||
const portClause = '(:\\d{1,5})'; | ||
const hostClause = '((' + domainBodyClause + '\\.' + tldClause + ')|' + ipClause + '|' + localHostClause + ')' + portClause + '?'; | ||
const pathClause = '(\\/[\\/\\w\\.\\-%~]*)*'; | ||
const queryStringHashFragmentCharacterSet = '[0-9\\w\\[\\]\\(\\)\\/\\?\\!#@$%&\'*+,:;~\\=\\.\\-]*'; | ||
const queryStringClause = '(\\?' + queryStringHashFragmentCharacterSet + ')?'; | ||
const hashFragmentClause = '(#' + queryStringHashFragmentCharacterSet + ')?'; | ||
const negatedPathCharacterSet = '[^\\/\\w\\.\\-%]+'; | ||
const bodyClause = hostClause + pathClause + queryStringClause + hashFragmentClause; | ||
const start = '(?:^|' + negatedDomainCharacterSet + ')('; | ||
const end = ')($|' + negatedPathCharacterSet + ')'; | ||
const strictUrlRegex = new RegExp(start + protocolClause + bodyClause + end); | ||
|
||
function handleLink(event: MouseEvent, uri: string): void { | ||
window.open(uri, '_blank'); | ||
} | ||
|
||
/** | ||
* Initialize the web links addon, registering the link matcher. | ||
* @param term The terminal to use web links within. | ||
* @param handler A custom handler to use. | ||
* @param options Custom options to use, matchIndex will always be ignored. | ||
*/ | ||
export function webLinksInit(term: Terminal, handler: (event: MouseEvent, uri: string) => void = handleLink, options: ILinkMatcherOptions = {}): void { | ||
options.matchIndex = 1; | ||
term.registerLinkMatcher(strictUrlRegex, handler, options); | ||
} | ||
|
||
export function apply(terminalConstructor: typeof Terminal): void { | ||
(<any>terminalConstructor.prototype).webLinksInit = function (handler?: (event: MouseEvent, uri: string) => void, options?: ILinkMatcherOptions): void { | ||
webLinksInit(this, handler, options); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "xterm.winptyCompat", | ||
"name": "xterm.winptycompat", | ||
"main": "winptyCompat.js", | ||
"private": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters