mirror of
https://github.com/microsoft/vscode.git
synced 2025-12-20 02:08:47 +00:00
linkify tests
This commit is contained in:
@@ -27,9 +27,21 @@ type LinkPart = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export class LinkDetector {
|
export class LinkDetector {
|
||||||
constructor(
|
|
||||||
) {
|
// used by unit tests
|
||||||
// noop
|
static injectedHtmlCreator: (value: string) => string;
|
||||||
|
|
||||||
|
private shouldGenerateHtml(trustHtml: boolean) {
|
||||||
|
return trustHtml && (!!LinkDetector.injectedHtmlCreator || !!ttPolicy);
|
||||||
|
}
|
||||||
|
|
||||||
|
private createHtml(value: string) {
|
||||||
|
if (LinkDetector.injectedHtmlCreator) {
|
||||||
|
return LinkDetector.injectedHtmlCreator(value);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return ttPolicy?.createHTML(value).toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -71,9 +83,9 @@ export class LinkDetector {
|
|||||||
container.appendChild(this.createWebLink(part.value));
|
container.appendChild(this.createWebLink(part.value));
|
||||||
break;
|
break;
|
||||||
case 'html':
|
case 'html':
|
||||||
if (ttPolicy && trustHtml) {
|
if (this.shouldGenerateHtml(!!trustHtml)) {
|
||||||
const span = document.createElement('span');
|
const span = document.createElement('span');
|
||||||
span.innerHTML = ttPolicy.createHTML(part.value).toString();
|
span.innerHTML = this.createHtml(part.value)!;
|
||||||
container.appendChild(span);
|
container.appendChild(span);
|
||||||
} else {
|
} else {
|
||||||
container.appendChild(document.createTextNode(part.value));
|
container.appendChild(document.createTextNode(part.value));
|
||||||
|
|||||||
44
extensions/notebook-renderers/src/test/linkify.test.ts
Normal file
44
extensions/notebook-renderers/src/test/linkify.test.ts
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
/*---------------------------------------------------------------------------------------------
|
||||||
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||||
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||||
|
*--------------------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
import * as assert from 'assert';
|
||||||
|
import { JSDOM } from "jsdom";
|
||||||
|
import { LinkDetector, linkify } from '../linkify';
|
||||||
|
|
||||||
|
const dom = new JSDOM();
|
||||||
|
global.document = dom.window.document;
|
||||||
|
|
||||||
|
suite('Notebook builtin output link detection', () => {
|
||||||
|
|
||||||
|
LinkDetector.injectedHtmlCreator = (value: string) => value;
|
||||||
|
|
||||||
|
test('no links', () => {
|
||||||
|
const htmlWithLinks = linkify('hello', true, undefined, true);
|
||||||
|
assert.equal(htmlWithLinks.innerHTML, 'hello');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('web link detection', () => {
|
||||||
|
const htmlWithLinks = linkify('something www.example.com something', true, undefined, true);
|
||||||
|
|
||||||
|
assert.equal(htmlWithLinks.innerHTML, 'something <a href="www.example.com">www.example.com</a> something');
|
||||||
|
assert.equal(htmlWithLinks.textContent, 'something www.example.com something');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('html link detection', () => {
|
||||||
|
const htmlWithLinks = linkify('something <a href="www.example.com">link</a> something', true, undefined, true);
|
||||||
|
|
||||||
|
assert.equal(htmlWithLinks.innerHTML, 'something <span><a href="www.example.com">link</a></span> something');
|
||||||
|
assert.equal(htmlWithLinks.textContent, 'something link something');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('html link without trust', () => {
|
||||||
|
const trustHtml = false;
|
||||||
|
const htmlWithLinks = linkify('something <a href="file.py">link</a> something', true, undefined, trustHtml);
|
||||||
|
|
||||||
|
assert.equal(htmlWithLinks.innerHTML, 'something <a href="file.py">link</a> something');
|
||||||
|
assert.equal(htmlWithLinks.textContent, 'something <a href="file.py">link</a> something');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
@@ -8,6 +8,7 @@ import { activate } from '..';
|
|||||||
import { RendererApi } from 'vscode-notebook-renderer';
|
import { RendererApi } from 'vscode-notebook-renderer';
|
||||||
import { IDisposable, IRichRenderContext, OutputWithAppend, RenderOptions } from '../rendererTypes';
|
import { IDisposable, IRichRenderContext, OutputWithAppend, RenderOptions } from '../rendererTypes';
|
||||||
import { JSDOM } from "jsdom";
|
import { JSDOM } from "jsdom";
|
||||||
|
import { LinkDetector } from '../linkify';
|
||||||
|
|
||||||
const dom = new JSDOM();
|
const dom = new JSDOM();
|
||||||
global.document = dom.window.document;
|
global.document = dom.window.document;
|
||||||
@@ -273,6 +274,7 @@ suite('Notebook builtin output renderer', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test(`Render with wordwrap and scrolling for error output`, async () => {
|
test(`Render with wordwrap and scrolling for error output`, async () => {
|
||||||
|
LinkDetector.injectedHtmlCreator = (value: string) => value;
|
||||||
const context = createContext({ outputWordWrap: true, outputScrolling: true });
|
const context = createContext({ outputWordWrap: true, outputScrolling: true });
|
||||||
const renderer = await activate(context);
|
const renderer = await activate(context);
|
||||||
assert.ok(renderer, 'Renderer not created');
|
assert.ok(renderer, 'Renderer not created');
|
||||||
|
|||||||
Reference in New Issue
Block a user