mirror of
https://github.com/microsoft/vscode.git
synced 2025-12-24 20:26:08 +00:00
don't create links when they won't be rendered (#248353)
dont create links when they wont be rendered
This commit is contained in:
@@ -190,7 +190,7 @@ function renderError(
|
||||
const minimalError = ctx.settings.minimalError && !!headerMessage?.length;
|
||||
outputElement.classList.add('traceback');
|
||||
|
||||
const { formattedStack, errorLocation } = formatStackTrace(err.stack);
|
||||
const { formattedStack, errorLocation } = formatStackTrace(err.stack, trustHtml);
|
||||
|
||||
const outputScrolling = !minimalError && scrollingEnabled(outputInfo, ctx.settings);
|
||||
const lineLimit = minimalError ? 1000 : ctx.settings.lineLimit;
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
export function formatStackTrace(stack: string): { formattedStack: string; errorLocation?: string } {
|
||||
export function formatStackTrace(stack: string, trustHtml: boolean): { formattedStack: string; errorLocation?: string } {
|
||||
let cleaned: string;
|
||||
// Ansi colors are described here:
|
||||
// https://en.wikipedia.org/wiki/ANSI_escape_code under the SGR section
|
||||
@@ -23,7 +23,7 @@ export function formatStackTrace(stack: string): { formattedStack: string; error
|
||||
return `${prefix}${num}${suffix}\n`;
|
||||
});
|
||||
|
||||
if (isIpythonStackTrace(cleaned)) {
|
||||
if (isIpythonStackTrace(cleaned) && trustHtml) {
|
||||
return linkifyStack(cleaned);
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ suite('StackTraceHelper', () => {
|
||||
'@Main c:\\src\\test\\3\\otherlanguages\\julia.ipynb: 3\n' +
|
||||
'[2] top - level scope\n' +
|
||||
'@c:\\src\\test\\3\\otherlanguages\\julia.ipynb: 1; ';
|
||||
assert.equal(formatStackTrace(stack).formattedStack, stack);
|
||||
assert.equal(formatStackTrace(stack, true).formattedStack, stack);
|
||||
});
|
||||
|
||||
const formatSequence = /\u001b\[.+?m/g;
|
||||
@@ -37,7 +37,7 @@ suite('StackTraceHelper', () => {
|
||||
'\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m\n\n' +
|
||||
'\u001b[1;31mException\u001b[0m\n:';
|
||||
|
||||
const { formattedStack, errorLocation } = formatStackTrace(stack);
|
||||
const { formattedStack, errorLocation } = formatStackTrace(stack, true);
|
||||
const cleanStack = stripAsciiFormatting(formattedStack);
|
||||
assert.ok(cleanStack.indexOf('Cell In[3], <a href=\'vscode-notebook-cell:?execution_count=3&line=2\'>line 2</a>') > 0, 'Missing line link in ' + cleanStack);
|
||||
assert.ok(cleanStack.indexOf('<a href=\'vscode-notebook-cell:?execution_count=3&line=2\'>2</a>') > 0, 'Missing frame link in ' + cleanStack);
|
||||
@@ -56,13 +56,28 @@ suite('StackTraceHelper', () => {
|
||||
'\n' +
|
||||
'\u001b[31mTypeError\u001b[39m: unsupported operand type(s) for +: "NoneType" and "int"\n';
|
||||
|
||||
const { formattedStack, errorLocation } = formatStackTrace(stack);
|
||||
const { formattedStack, errorLocation } = formatStackTrace(stack, true);
|
||||
const cleanStack = stripAsciiFormatting(formattedStack);
|
||||
assert.ok(cleanStack.indexOf('Cell In[3], <a href=\'vscode-notebook-cell:?execution_count=3&line=2\'>line 2</a>') > 0, 'Missing line link in ' + cleanStack);
|
||||
assert.ok(cleanStack.indexOf('<a href=\'vscode-notebook-cell:?execution_count=3&line=2\'>2</a>') > 0, 'Missing frame link in ' + cleanStack);
|
||||
assert.equal(errorLocation, '<a href=\'vscode-notebook-cell:?execution_count=3&line=2\'>line 2</a>');
|
||||
});
|
||||
|
||||
test('Stack trace is not linkified when HTML is not trusted', () => {
|
||||
const stack =
|
||||
'\u001b[31m---------------------------------------------------------------------------\u001b[39m\n' +
|
||||
'\u001b[31mTypeError\u001b[39m Traceback (most recent call last)\n' +
|
||||
'\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[3]\u001b[39m\u001b[32m, line 2\u001b[39m\n' +
|
||||
'\u001b[32m 1\u001b[39m x = firstItem((\u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m, \u001b[32m5\u001b[39m))\n' +
|
||||
'\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m y = \u001b[43mx\u001b[49m\u001b[43m \u001b[49m\u001b[43m+\u001b[49m\u001b[43m \u001b[49m\u001b[32;43m1\u001b[39;49m\n' +
|
||||
'\u001b[32m 3\u001b[39m \u001b[38;5;28mprint\u001b[39m(y)\n' +
|
||||
'\n' +
|
||||
'\u001b[31mTypeError\u001b[39m: unsupported operand type(s) for +: "NoneType" and "int"\n';
|
||||
|
||||
const formattedLines = formatStackTrace(stack, false).formattedStack.split('\n');
|
||||
formattedLines.forEach(line => assert.ok(!/<a href=.*>/.test(line), 'line should not contain a link: ' + line));
|
||||
});
|
||||
|
||||
test('IPython stack line numbers are linkified for IPython 8.3', () => {
|
||||
// stack frames within functions do not list the line number, i.e.
|
||||
// 'Input In [1], in myfunc()' vs
|
||||
@@ -85,7 +100,7 @@ suite('StackTraceHelper', () => {
|
||||
'\n' +
|
||||
'\u001b[1;31mException\u001b[0m:\n';
|
||||
|
||||
const { formattedStack } = formatStackTrace(stack);
|
||||
const { formattedStack } = formatStackTrace(stack, true);
|
||||
const formatted = stripAsciiFormatting(formattedStack);
|
||||
assert.ok(formatted.indexOf('Input <a href=\'vscode-notebook-cell:?execution_count=2\'>In [2]</a>, in <cell line: 5>') > 0, 'Missing cell link in ' + formatted);
|
||||
assert.ok(formatted.indexOf('Input <a href=\'vscode-notebook-cell:?execution_count=1\'>In [1]</a>, in myfunc()') > 0, 'Missing cell link in ' + formatted);
|
||||
@@ -103,7 +118,7 @@ suite('StackTraceHelper', () => {
|
||||
'\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m\n\n' +
|
||||
'\u001b[1;31mException\u001b[0m\n:';
|
||||
|
||||
const formatted = formatStackTrace(stack).formattedStack;
|
||||
const formatted = formatStackTrace(stack, true).formattedStack;
|
||||
assert.ok(!/<a href=.*>\d<\/a>/.test(formatted), formatted);
|
||||
});
|
||||
|
||||
@@ -118,7 +133,7 @@ suite('StackTraceHelper', () => {
|
||||
'a 1 print(\n' +
|
||||
' 1a print(\n';
|
||||
|
||||
const formattedLines = formatStackTrace(stack).formattedStack.split('\n');
|
||||
const formattedLines = formatStackTrace(stack, true).formattedStack.split('\n');
|
||||
assert.ok(/<a href='vscode-notebook-cell.*>/.test(formattedLines[0]), 'line should contain a link: ' + formattedLines[0]);
|
||||
formattedLines.slice(1).forEach(line => assert.ok(!/<a href=.*>/.test(line), 'line should not contain a link: ' + line));
|
||||
});
|
||||
@@ -127,7 +142,7 @@ suite('StackTraceHelper', () => {
|
||||
const stack =
|
||||
'open\u001b[39;49m\u001b[43m(\u001b[49m\u001b[33;43m\'\u001b[39;49m\u001b[33;43minput.txt\u001b[39;49m\u001b[33;43m\'\u001b[39;49m\u001b[43m)\u001b[49m;';
|
||||
|
||||
const formattedLines = formatStackTrace(stack).formattedStack.split('\n');
|
||||
const formattedLines = formatStackTrace(stack, true).formattedStack.split('\n');
|
||||
assert.ok(!/4\d/.test(formattedLines[0]), 'should not contain background colors ' + formattedLines[0]);
|
||||
formattedLines.slice(1).forEach(line => assert.ok(!/<a href=.*>/.test(line), 'line should not contain a link: ' + line));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user