Skip to content

Paste as HTML (copied from a StyledText) collapses white spaces #467

Closed

Description

@mihnita

When StyledText saves HTML to clipboard and the result is pasted into some application, in some cases the multiple spaces are collapsed into one.

This happens in GMail, MS Word, and Powerpoint.
The spaces are preserved in Google Docs and Google Slides.

This is most obvious for code, which needs indent.

To Reproduce

Run this code in Eclipse:

public class Main {
	public static void main(String[] args) {
		System.out.println("\033[31mpublic class\033[m Main {");
		System.out.println("    \033[31mpublic static void\033[m main(String[] args) {");
		System.out.println("        \033[32m// TODO Auto-generated method stub\033[m");
		System.out.println("        System.\033[34mout\033[m.println(\033[34m\"Hello world!\"\033[m);");
		System.out.println("    }");
		System.out.println("}");
	}
}

The console should look like syntax colored "Hello world" app.
Select the text in console and copy to clipboard. You can use Ctrl+Insert in Win / Linux (Option+Insert on Mac) to copy without escape codes.

Paste the clipboard content in Gmail (create new email), or in MS Word / MS PowerPoint.

Expected behavior

The spaces should be preserved, the result when pasted into an application should look as close as possible to what we see in the StyledText (for example in Eclipse).

Screenshots

image

Environment:

  1. Select the platform(s) on which the behavior is seen:
    • All OS
    • Windows
    • Linux
    • macOS
  1. Additional OS info (e.g. OS version, Linux Desktop, etc)

Tried on Win 11, MacOS, Debian

  1. JRE/JDK version

11, but very likely not relevant.

Version since

Eclipse 2022-12 M3 (4.26.0 M3)
HTML was not available as clipboard format until recently (Sep 19, 2022)
This was discovered while testing eclipse-jdt/eclipse.jdt.ui#320.
Before it was harder to notice because I did not copy-paste text with multiple spaces, which is more common in code.

Workaround (or) Additional context

No workarounds.

But the cause is easy to see if we dump the clipboard and inspect the HTML (or look at the code).
The div wrapping the content specifies style=' ... white-space:nowrap;'

But white-space:nowrap "collapses white space as for normal, but suppresses line breaks (text wrapping) within the source."
https://developer.mozilla.org/en-US/docs/Web/CSS/white-space

Need to use white-space:pre
"Sequences of white space are preserved. Lines are only broken at newline characters in the source and at <br> elements."

The code is here:
https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/bundles/org.eclipse.swt/Eclipse%20SWT%20Custom%20Widgets/common/org/eclipse/swt/custom/HTMLWriter.java#L77

Activity

  1. self-assigned this
    on Nov 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions