| 1 |
/** |
| 2 |
* Copyright (c) 2022 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { IBufferRange, ILink } from 'browser/Types'; |
| 7 |
import { ILinkProvider } from 'browser/services/Services'; |
| 8 |
import { CellData } from 'common/buffer/CellData'; |
| 9 |
import { IBufferService, IOptionsService, IOscLinkService } from 'common/services/Services'; |
| 10 |
|
| 11 |
export class OscLinkProvider implements ILinkProvider { |
| 12 |
constructor( |
| 13 |
@IBufferService private readonly _bufferService: IBufferService, |
| 14 |
@IOptionsService private readonly _optionsService: IOptionsService, |
| 15 |
@IOscLinkService private readonly _oscLinkService: IOscLinkService |
| 16 |
) { |
| 17 |
} |
| 18 |
|
| 19 |
public provideLinks(y: number, callback: (links: ILink[] | undefined) => void): void { |
| 20 |
const line = this._bufferService.buffer.lines.get(y - 1); |
| 21 |
if (!line) { |
| 22 |
callback(undefined); |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
const result: ILink[] = []; |
| 27 |
const linkHandler = this._optionsService.rawOptions.linkHandler; |
| 28 |
const cell = new CellData(); |
| 29 |
const lineLength = line.getTrimmedLength(); |
| 30 |
let currentLinkId = -1; |
| 31 |
let currentStart = -1; |
| 32 |
let finishLink = false; |
| 33 |
for (let x = 0; x < lineLength; x++) { |
| 34 |
// Minor optimization, only check for content if there isn't a link in case the link ends with |
| 35 |
// a null cell |
| 36 |
if (currentStart === -1 && !line.hasContent(x)) { |
| 37 |
continue; |
| 38 |
} |
| 39 |
|
| 40 |
line.loadCell(x, cell); |
| 41 |
if (cell.hasExtendedAttrs() && cell.extended.urlId) { |
| 42 |
if (currentStart === -1) { |
| 43 |
currentStart = x; |
| 44 |
currentLinkId = cell.extended.urlId; |
| 45 |
continue; |
| 46 |
} else { |
| 47 |
finishLink = cell.extended.urlId !== currentLinkId; |
| 48 |
} |
| 49 |
} else { |
| 50 |
if (currentStart !== -1) { |
| 51 |
finishLink = true; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if (finishLink || (currentStart !== -1 && x === lineLength - 1)) { |
| 56 |
const text = this._oscLinkService.getLinkData(currentLinkId)?.uri; |
| 57 |
if (text) { |
| 58 |
// These ranges are 1-based |
| 59 |
const range: IBufferRange = { |
| 60 |
start: { |
| 61 |
x: currentStart + 1, |
| 62 |
y |
| 63 |
}, |
| 64 |
end: { |
| 65 |
// Offset end x if it's a link that ends on the last cell in the line |
| 66 |
x: x + (!finishLink && x === lineLength - 1 ? 1 : 0), |
| 67 |
y |
| 68 |
} |
| 69 |
}; |
| 70 |
|
| 71 |
let ignoreLink = false; |
| 72 |
if (!linkHandler?.allowNonHttpProtocols) { |
| 73 |
try { |
| 74 |
const parsed = new URL(text); |
| 75 |
if (!['http:', 'https:'].includes(parsed.protocol)) { |
| 76 |
ignoreLink = true; |
| 77 |
} |
| 78 |
} catch (e) { |
| 79 |
// Ignore invalid URLs to prevent unexpected behaviors |
| 80 |
ignoreLink = true; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
if (!ignoreLink) { |
| 85 |
// OSC links always use underline and pointer decorations |
| 86 |
result.push({ |
| 87 |
text, |
| 88 |
range, |
| 89 |
activate: (e, text) => (linkHandler ? linkHandler.activate(e, text, range) : defaultActivate(e, text)), |
| 90 |
hover: (e, text) => linkHandler?.hover?.(e, text, range), |
| 91 |
leave: (e, text) => linkHandler?.leave?.(e, text, range) |
| 92 |
}); |
| 93 |
} |
| 94 |
} |
| 95 |
finishLink = false; |
| 96 |
|
| 97 |
// Clear link or start a new link if one starts immediately |
| 98 |
if (cell.hasExtendedAttrs() && cell.extended.urlId) { |
| 99 |
currentStart = x; |
| 100 |
currentLinkId = cell.extended.urlId; |
| 101 |
} else { |
| 102 |
currentStart = -1; |
| 103 |
currentLinkId = -1; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// TODO: Handle fetching and returning other link ranges to underline other links with the same |
| 109 |
// id |
| 110 |
callback(result); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
function defaultActivate(e: MouseEvent, uri: string): void { |
| 115 |
const answer = confirm(`Do you want to navigate to ${uri}?\n\nWARNING: This link could potentially be dangerous`); |
| 116 |
if (answer) { |
| 117 |
const newWindow = window.open(); |
| 118 |
if (newWindow) { |
| 119 |
try { |
| 120 |
newWindow.opener = null; |
| 121 |
} catch { |
| 122 |
// no-op, Electron can throw |
| 123 |
} |
| 124 |
newWindow.location.href = uri; |
| 125 |
} else { |
| 126 |
console.warn('Opening link blocked as opener could not be cleared'); |
| 127 |
} |
| 128 |
} |
| 129 |
} |