copied text does not include sufficient/correct HTML entity escaping #3845

Closed
opened 2026-01-30 23:31:24 +00:00 by claunia · 13 comments
Owner

Originally created by @0xabu on GitHub (Sep 12, 2019).

Environment

Windows build number: 10.0.18362.356
Windows Terminal version (if applicable): 0.4.2382.0
Copy-on-select is enabled (I doubt it matters)

Steps to reproduce

Select some text in the terminal containing a < character. Right-click to copy it if needed.

For example:
image

Expected behavior

Text is copied to the clipboard as both plain text and valid HTML.

Actual behavior

Pasting into notepad pastes the full selected plain text.
Pasting into an HTML-capable app (in this case the mail client) shows that entity-escaping hasn't been done correctly:
image

Originally created by @0xabu on GitHub (Sep 12, 2019). # Environment ```none Windows build number: 10.0.18362.356 Windows Terminal version (if applicable): 0.4.2382.0 Copy-on-select is enabled (I doubt it matters) ``` # Steps to reproduce Select some text in the terminal containing a `<` character. Right-click to copy it if needed. For example: ![image](https://user-images.githubusercontent.com/13037682/64804858-8f4a1480-d544-11e9-860b-5381ee83422a.png) # Expected behavior Text is copied to the clipboard as both plain text and valid HTML. # Actual behavior Pasting into notepad pastes the full selected plain text. Pasting into an HTML-capable app (in this case the mail client) shows that entity-escaping hasn't been done correctly: ![image](https://user-images.githubusercontent.com/13037682/64804930-c1f40d00-d544-11e9-8fbc-252ab5e5abe6.png)
Author
Owner

@zadjii-msft commented on GitHub (Sep 12, 2019):

(I'm marking this as a conhost bug even though this is really only exposed through the Terminal, the code lives in the shared TextBuffer IIRC)

@zadjii-msft commented on GitHub (Sep 12, 2019): (I'm marking this as a conhost bug even though this is really only exposed through the Terminal, the code lives in the shared TextBuffer IIRC)
Author
Owner

@carlos-zamora commented on GitHub (Sep 12, 2019):

Ok, the code should be easy to find by taking a look at PR #1224 (PR #2038 might provide an even smaller scope of what to change).

We'll have to replace the < and > characters with &lt; and &gt;. But then, if somebody puts those strings in the text buffer, how can we make sure HTML copies it properly?

@carlos-zamora commented on GitHub (Sep 12, 2019): Ok, the code should be easy to find by taking a look at PR #1224 (PR #2038 might provide an even smaller scope of what to change). We'll have to replace the `<` and `>` characters with `&lt;` and `&gt;`. But then, if somebody puts those strings in the text buffer, how can we make sure HTML copies it properly?
Author
Owner

@0xabu commented on GitHub (Sep 12, 2019):

You also need to escape & to &amp;.

@0xabu commented on GitHub (Sep 12, 2019): You also need to escape `&` to `&amp;`.
Author
Owner

@dlong11 commented on GitHub (Sep 13, 2019):

Good links for encoding and escaping.
Link
Link

@dlong11 commented on GitHub (Sep 13, 2019): Good links for encoding and escaping. [Link](https://www.w3.org/International/questions/qa-escapes#use) [Link](https://www.onwebsecurity.com/security/properly-encoding-and-escaping-for-the-web.html)
Author
Owner

@kojoru commented on GitHub (Sep 16, 2019):

Note this comment in PR #2038:

HTML text was unescaped, and it still is, becouse Word doesn't work with HTML entities, like &lt;. But it seems to accept the invalid HTML with > from prompts. So if there is no text like <someTextThatNowBecomesATag>, it should work.

@kojoru commented on GitHub (Sep 16, 2019): Note this comment in PR #2038: > HTML text was unescaped, and it still is, becouse Word doesn't work with HTML entities, like `&lt;`. But it seems to accept the invalid HTML with `>` from prompts. So if there is no text like `<someTextThatNowBecomesATag>`, it should work.
Author
Owner

@mcpiroman commented on GitHub (Sep 16, 2019):

Indeed, I left it unescaped only because MS Word wouldn't handle that (its HTML support is quite retarded actually), which is quite common target for such copy.
However, when RTF copy is supported (#2487), then Word might prioritize it over HTML (though if someone explicitly selects HTML paste, then it still wouldn't work).
It might be worthwhile to introduce escapeHtml setting maybe?

@mcpiroman commented on GitHub (Sep 16, 2019): Indeed, I left it unescaped only because MS Word wouldn't handle that (its HTML support is quite retarded actually), which is quite common target for such copy. However, when RTF copy is supported (#2487), then Word might prioritize it over HTML (though if someone explicitly selects HTML paste, then it still wouldn't work). It might be worthwhile to introduce `escapeHtml` setting maybe?
Author
Owner

@0xabu commented on GitHub (Sep 16, 2019):

Indeed, I left it unescaped only because MS Word wouldn't handle that (its HTML support is quite retarded actually), which is quite common target for such copy.

@mcpiroman how did you determine that Word couldn't handle it? Do you have a ref for that? I did some quick tests copying and pasting from mintty in HTML format, and whatever it emits for < > etc. appears to paste correctly into Word.

@0xabu commented on GitHub (Sep 16, 2019): > Indeed, I left it unescaped only because MS Word wouldn't handle that (its HTML support is quite retarded actually), which is quite common target for such copy. @mcpiroman how did you determine that Word couldn't handle it? Do you have a ref for that? I did some quick tests copying and pasting from mintty in HTML format, and whatever it emits for < > etc. appears to paste correctly into Word.
Author
Owner

@mcpiroman commented on GitHub (Sep 16, 2019):

@0xabu I've played with it a quite a bit when writing mentioned PR few month ago and it simply didn't work (they were just literally displayed as I can remember). However I've just tried this once again and it worked. So it looks like they've updated Word to handle that.

@mcpiroman commented on GitHub (Sep 16, 2019): @0xabu I've played with it a quite a bit when writing mentioned PR few month ago and it simply didn't work (they were just literally displayed as I can remember). However I've just tried this once again and it worked. So it looks like they've updated Word to handle that.
Author
Owner

@devoopes commented on GitHub (Sep 17, 2019):

I have a concern that this is not the right fix here. As mentioned by @DHowett-MSFT
(https://github.com/microsoft/terminal/pull/2777/files#r324902781)

I have been having issues with copy/paste from terminal but in this test above my copy worked.

Microsoft Windows NT 10.0.18362.0
Windows Terminal (Preview) Version: 0.4.2382.0

PS C:\> echo "<foo>" 
 <foo> 
PS C:\> echo "<bar"
 <bar 

Not sure if this is related but when I put the above commands in a script and output I get this weird spacing issue in Atom only:
output
output.log

Opening in Atom shows two unreadable characters. Notepad does not.

I have been having issues with coping JSON from Terminal and I plan on opening a separate issue for this. But for context on why I think its how I think Terminal handles symbols and characters.

When I copy JSON from Terminal to Atom/Notepad it strips the "Topics":[ completely out. Oddly enough, in Chrome when posting this it copied correctly, but still does not in Atom/Notepad.

{                                                                                                                                                                                                                                                                                 "Topics": [
    {
      "TopicArn": "arn:aws:sns:"
    },
    {
      "TopicArn": "arn:aws:sns:"
    },
@devoopes commented on GitHub (Sep 17, 2019): I have a concern that this is not the right fix here. As mentioned by @DHowett-MSFT (https://github.com/microsoft/terminal/pull/2777/files#r324902781) I have been having issues with copy/paste from terminal but in this test above my copy worked. >Microsoft Windows NT 10.0.18362.0 >Windows Terminal (Preview) Version: 0.4.2382.0 ``` PS C:\> echo "<foo>" <foo> PS C:\> echo "<bar" <bar ``` Not sure if this is related but when I put the above commands in a script and output I get this weird spacing issue in Atom only: ![output](https://user-images.githubusercontent.com/5666862/65083375-3a9c0480-d95d-11e9-8757-87e72ecabc5c.png) [output.log](https://github.com/microsoft/terminal/files/3623622/output.log) Opening in Atom shows two unreadable characters. Notepad does not. I have been having issues with coping JSON from Terminal and I plan on opening a separate issue for this. But for context on why I think its how I think Terminal handles symbols and characters. When I copy JSON from Terminal to Atom/Notepad it strips the `"Topics":[` completely out. Oddly enough, in Chrome when posting this it copied correctly, but still does not in Atom/Notepad. ``` { "Topics": [ { "TopicArn": "arn:aws:sns:" }, { "TopicArn": "arn:aws:sns:" }, ```
Author
Owner

@DHowett-MSFT commented on GitHub (Sep 17, 2019):

Your first thing looks like you’re using UTF-16; don’t do that :P
The second thing: if you scroll all the way to the right in Atom/Notepad, do you see Formats on the right edge?

@DHowett-MSFT commented on GitHub (Sep 17, 2019): Your first thing looks like you’re using UTF-16; don’t do that :P The second thing: if you scroll all the way to the right in Atom/Notepad, do you see Formats on the right edge?
Author
Owner

@0xabu commented on GitHub (Sep 17, 2019):

Those unreadable characters are a BOM, it's probably unrelated.

This issue is only with HTML copy. You didn't say, but it sounds like you may have done the copy into an applciation (like a text editor) that pastes plain text.

@0xabu commented on GitHub (Sep 17, 2019): Those unreadable characters are a [BOM](https://en.wikipedia.org/wiki/Byte_order_mark), it's probably unrelated. This issue is only with HTML copy. You didn't say, but it sounds like you may have done the copy into an applciation (like a text editor) that pastes plain text.
Author
Owner

@devoopes commented on GitHub (Sep 17, 2019):

@DHowett-MSFT, I did switch Atom from Auto to UTF-8 and that is when I get the odd spacing, UTF-16 LE displays properly. Thanks for the tip.
@0xabu I see how the issues differ I apologize for hijacking your issue.
Could the escaping characters be only limited to the HTML tags or expand to [ and {?

@devoopes commented on GitHub (Sep 17, 2019): @DHowett-MSFT, I did switch Atom from Auto to UTF-8 and that is when I get the odd spacing, UTF-16 LE displays properly. Thanks for the tip. @0xabu I see how the issues differ I apologize for hijacking your issue. Could the escaping characters be only limited to the HTML tags or expand to `[` and `{`?
Author
Owner

@ghost commented on GitHub (Sep 24, 2019):

:tada:This issue was addressed in #2777, which has now been successfully released as Windows Terminal Preview v0.5.2661.0.🎉

Handy links:

@ghost commented on GitHub (Sep 24, 2019): :tada:This issue was addressed in #2777, which has now been successfully released as `Windows Terminal Preview v0.5.2661.0`.:tada: Handy links: * [Release Notes](https://github.com/microsoft/terminal/releases/tag/v0.5.2661.0) * [Store Download](https://www.microsoft.com/store/apps/9n0dx20hk701?cid=storebadge&ocid=badge)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#3845