linkify tests

This commit is contained in:
Aaron Munger 2023-09-12 10:41:50 -07:00
parent fb8e51346f
commit ab09263c52
3 changed files with 63 additions and 5 deletions

View file

@ -27,9 +27,21 @@ type LinkPart = {
};
export class LinkDetector {
constructor(
) {
// noop
// used by unit tests
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));
break;
case 'html':
if (ttPolicy && trustHtml) {
if (this.shouldGenerateHtml(!!trustHtml)) {
const span = document.createElement('span');
span.innerHTML = ttPolicy.createHTML(part.value).toString();
span.innerHTML = this.createHtml(part.value)!;
container.appendChild(span);
} else {
container.appendChild(document.createTextNode(part.value));

View 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 &lt;a href="file.py"&gt;link&lt;/a&gt; something');
assert.equal(htmlWithLinks.textContent, 'something <a href="file.py">link</a> something');
});
});

View file

@ -8,6 +8,7 @@ import { activate } from '..';
import { RendererApi } from 'vscode-notebook-renderer';
import { IDisposable, IRichRenderContext, OutputWithAppend, RenderOptions } from '../rendererTypes';
import { JSDOM } from "jsdom";
import { LinkDetector } from '../linkify';
const dom = new JSDOM();
global.document = dom.window.document;
@ -273,6 +274,7 @@ suite('Notebook builtin output renderer', () => {
});
test(`Render with wordwrap and scrolling for error output`, async () => {
LinkDetector.injectedHtmlCreator = (value: string) => value;
const context = createContext({ outputWordWrap: true, outputScrolling: true });
const renderer = await activate(context);
assert.ok(renderer, 'Renderer not created');