forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix HTML escaping to match what Jupyter does #14038
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix escaping of output to encode HTML chars correctly. |
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 |
---|---|---|
|
@@ -314,26 +314,34 @@ export class CellOutput extends React.Component<ICellOutputProps> { | |
input = JSON.stringify(output.data); | ||
renderWithScrollbars = true; | ||
isText = true; | ||
} else if (output.output_type === 'execute_result' && input && input.hasOwnProperty('text/plain')) { | ||
// Plain text should actually be shown as html so that escaped HTML shows up correctly | ||
mimeType = 'text/html'; | ||
isText = true; | ||
isError = false; | ||
renderWithScrollbars = true; | ||
// tslint:disable-next-line: no-any | ||
const text = (input as any)['text/plain']; | ||
input = { | ||
'text/html': text // XML tags should have already been escaped. | ||
}; | ||
} else if (output.output_type === 'stream') { | ||
// Stream output needs to be wrapped in xmp so it | ||
// show literally. Otherwise < chars start a new html element. | ||
mimeType = 'text/html'; | ||
isText = true; | ||
isError = false; | ||
renderWithScrollbars = true; | ||
// Sonar is wrong, TS won't compile without this AS | ||
const stream = output as nbformat.IStream; // NOSONAR | ||
const formatted = concatMultilineString(stream.text); | ||
const concatted = concatMultilineString(stream.text); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would do |
||
input = { | ||
'text/html': formatted.includes('<') ? `<xmp>${formatted}</xmp>` : `<div>${formatted}</div>` | ||
'text/html': concatted // XML tags should have already been escaped. | ||
}; | ||
|
||
// Output may have goofy ascii colorization chars in it. Try | ||
// colorizing if we don't have html that needs <xmp> around it (ex. <type ='string'>) | ||
// Output may have ascii colorization chars in it. | ||
try { | ||
if (ansiRegex().test(formatted)) { | ||
if (ansiRegex().test(concatted)) { | ||
const converter = new CellOutput.ansiToHtmlClass(CellOutput.getAnsiToHtmlOptions()); | ||
const html = converter.toHtml(formatted); | ||
const html = converter.toHtml(concatted); | ||
input = { | ||
'text/html': html | ||
}; | ||
|
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,47 @@ | ||
{ | ||
"metadata": { | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.6.6-final" | ||
}, | ||
"orig_nbformat": 2 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2, | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"output_type": "execute_result", | ||
"data": { | ||
"text/plain": "1" | ||
}, | ||
"metadata": {}, | ||
"execution_count": 1 | ||
} | ||
], | ||
"source": [ | ||
"a=1\n", | ||
"a" | ||
] | ||
} | ||
] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the lodash docs right now. Is there a possibility here of unescaping something that isn't intended to be escaped? Like say I have a jupyter cell that's scraping a website and I output a string with > in it. But I actually want that > as a string. It looks like this might convert it.
Worth worrying about? Or not a likely issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh github actually edited what I put in.
>
actually in the output string and intended as a string with>
not as HTML output.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This unescape here is for a specific case. Getting the sys info. In that situation the messages that return shouldn't be escaped and since we know what code is running we can control what the output is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh you're right. I thought this was handling general stream output.