You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support HTML as a Clipboard format for StyledText #292
The current StyledText can copy / paste styled text to clipboard, but only in RTF format.
If you copy a piece of code (with syntax) and paste it in MS Word, or PowerPoint, the colors and font are preserved
(although the background color is lost).
But only plain text is pasted into Google GMail, Docs, Slides, or Yahoo Mail.
Describe the solution you'd like
With web-based applications as common as they are, it would be really handy to also support HTML as a clipboard format.
So that one can copy code and paste it in something like Google Slides, for example, while preserving the colors (and font).
The way it works today in PowerPoint.
Describe alternatives you've considered
Copy / paste in MS Word works, then copy / paste from MS Word to web-based apps works.
Opening the code in Visual Studio Code and copying from there also works.
But it would be nice to have that working from Eclipse.
Additional context
Not much, I think it is pretty obvious.
BUT: I offer to implement this, if you are interested.
Sorry, nothing new, but I just to make sure I manage expectations :-)
I did not drop out, but I think this is not something for 2022-09.
My guess is that it is a bit too late for that.
(I also don't have much time, so I would rather focus on the color support in the Console, for now)
But I can share a bit the big picture plan:
Optional: extract TextWriter and RTFWriter out of org.eclipse.swt.custom.StyledText
Add an HTMLWriter, very similar to the RTFWriter
Change the StyledText copy operation to also put the HTML in the clipboard
The HTML I would produce is very "primitive" for best compatibility.
Using <b> and <i> and <span style=...>, no stylesheets or anything fancy.
I've compared the kind of HTML put in the clipboard by all kinds of apps, from (the "ancient") Office 2010 to Google Slides/Docs/GMail.
And that kind of HTML seems to be the norm.
In fact that was my thinking was, to have HTMLWriter outside.
And also move RTFWriter and TextWriter out of StyledText.
Right now RTFWriter and TextWriter are inner classes to StyledText.
I think it would be cleaner to make them standalone classes.
They are not public, so that would not be a breaking change.
But this refactoring is not a change that is needed for the html clipboard functionality.
That is why I said "optional".
@mihnita If you want to start with a refactoring to extract the existing RTFWriter and TextWriter, to familiarize with the code and maybe factorize what seems really worth it, that would be welcome in a preliminary PR.
Right now I am a bit under pressure with a project at work.
I will have a bit more time in a week or two.
And Eclipse is close to release, too late to make it into that. So no rush...
I guess there is some development branch going on for after-release code?
I checked and there are are indeed paragraphs, not line breaks.
But I don't think this is legacy.
It follows exactly the RTF an (almost exactly) the plain text behavior).
I've prepared a Word document with 3 sections, for plain text, RTF, and HTML.
There paragraph markers that already exist are Arial, so it will be clear what comes from clipboard.
Then I've pasted the various formats, with paste special:
All 3 formats use paragraphs (<p>) to separate the lines, not line break (<br>)
The RTF and HTML are virtually identical, and there is indeed a paragraph after the else { which is missing in plain text.
We can debate if that is correct or not. But is is not a regression, the HTML behave the same as the RTF, which was untouched recently.
Now, I would also argue that the extra paragraph is indeed correct, because the editor selection (as shown in the screenshot in jukzi's comment) includes a new line.
Here are the results when the EOL after else { is not selected:
There is indeed a difference between HTML and RTF now (and it is clear what paragraph marker was there and what comes from the clipboard because of the font difference.
So I might agree that his is indeed a bug.
BUT!
There is a reason why is there.
The HTML clipboard contains a <div> with several <p>s. And I think that MS Words generates that paragraph from the div.
And the div is there to support background colors, which are not supported in RTF:
The html I generate is very "old style" (not style sheets, nothing fancy), as from the various apps I've tried many don't support styles, or background colors, and so on.
For example in Word you can do color paragraphs without the div, but Google Slides does not.
The div is also needed to control the line height. And for indents in Google Docs and Slides and
In your first screenshot you have overseen that there IS a difference of an additional line:
For us it more important to paste exactly the text that was copied (no additional line) over having any copied style.
When i copy/paste text from Word to outlook it does paste the backgroundcolor without adding any additional newline/paragraph. i.e. somehow it is possible- maybe you could investigate what pattern they use and use that?
With HTML the paragraphs look higher then a linebreak, while with RTF the paragraph looks like a ordinary linebreak hight:
You could test the result by copy text from IDE, paste it as RTF in word, then copy that pasted text from word an paste it as HTML to word (looks the same as the pasted RTF for me). When pasting from the IDE directly as HTML into Word it should look the same.
I've spent on this a lot of hours over the week-end.
And yes, there is an extra paragraph in the HTML format.
For line spacing, there is no visual difference between HTML and RTF.
It looks the same as the RTF, and when I inspecting the paragraph properties (same dialog as yours) the spacing before / after is zero.
I will still spend some time on it.
But what makes it hard is solving this for everybody.
It is easy to test Outlook vs Word. Both on Windows, both Microsoft, both Office, probably same version.
It looks like even the version of Office matters, as you see different results in line space than what I see.
But this has to work on all OSes, most desktop apps, most web based apps in most popular browsers.
And the copy should not only be from Eclipse code but also any SWT widget.
That means supporting styles that the Eclipse code does not use.
You will notice paragraph alignment (left / center / right), bullets, bidi support, justify text or not, enable / disable word wrap, padding (for the whole content), line spacing (from the SWT widget, not fixed), indent, margins.
And this only lists the paragraph attributes, not spans (supperscript, underscore, etc)
I also looked at what Word puts in the clipboard.
And it is far from clean, and not at all portable.
This simple code:
if (name != null) {
System.out.printf("Hello %s!", name);
}
Produces 1451 bytes of clipboard data when copied from Eclipse, and 23327 bytes when copied from Word.
And the Word HTML contains "non-standard tricks":
<o:p></o:p> tags from a proprietary namespace (xmlns:o="urn:schemas-microsoft-com:office:office")
Office specific comments (`<!--[if gte mso 9]>...<![endif]-->)
Replacing real tabs with several nbsp and wrapped in a <span style='mso-tab-count:3'> (that is for 3 tabs)
So when you paste that in word (again) the nbsp(s) are in fact replaced by real tabs. But that is a non-portable hack.
tons of MS styles (MsoNormal)
tons of proprietary attributes in style (mso-spacerun, mso-tab-count, in general tons of mso- attributes)
It also seems to produce different html depending on what I "grab" in the clipboard.
For plain (ASCII) text it produces fewer proprietary attributes and styles.
Grab some fancy symbol character and it adds mso-ascii-font-family, mso-ascii-theme-font, mso-hansi-font-family, mso-hansi-theme-font, mso-char-type, mso-symbol-font-family and many other attributes.
In all this is what I see in the body part. I've completely ignored non-standard stuff happening in the style definitions.
TLDR: what Word (and Office) produces can't be a good example for what we want to put in the clipboard.
Yes, copy Word - to - Word works, but it is not portable.
On the good side, I think I have a small improvement that would remove the extra newline when there was a newline selected in Eclipse.
But I have to test it on other platforms and applications.
@mihnita honestly HTML "format" was from my opinion always broken (regardless of application) and especially for microsoft as you have discovered now ;-)
So from my side, if we have basic support for this this is totally fine and things like an additional line break a really nothing to worry about...
Using "<span>"'s instead of "<div>" and "<p>" for formatting and only adding "<br>" or "<p>" where needed both Teams and Word don't add additional newlines (but sill increased paragraph hight).
These are narrow solutions that only solve this problem, only for Office, and only for code.
I am looking for something that works everywhere, and anything that SWT StyledText supports. <p> and <div> are block elements (similar to paragraph styles in Word), <span> is an inline element (similar to character style in Word)
This means that text in a <span> can be centered or justified, for example.
There are things that affect the whole widget (and belong in a div), for example setAlignment, setBackground, set*Margin, setIndent, setJustify, setOrientation, etc.
Then there are line attributes that work (most of them) on block elements only (setLineJustify, setLineBullet, setLineSpacing`,
The things that map to <span> are the StyleRange(s), (used in setStyleRanges)
At this point I am ready to bet that the HTML clipboard (as is is now) supports more of the StyledText style attributes than the RTF clipboard format.
Just give me a some of breathing space and I will prepare something, and test it to work properly everywhere.
Or you can do something your way and I will test it on all platforms and with all the styles (including non-code editor ones) and I will tell you what breaks.
You can see that the line spacing is identical for both formats.
And here is the final result in Photoshop, doubled, the top layer at 50% transparency, and shifted so that the RTF overlap with the HTML under.
You can see the RTF at the top and HTML at the bottom at 50%, and the RTF + HTML overlapping in the middle.
And there is a perfect match, including the final newline.
What I am trying to show is not that I don't believe that you see differences in line heights.
I believe that you do.
But I want to show that that two different versions of MS Word, on two machines, can behave differently.
Making things work the same is a lot harder than it seems.
Add to the mix different operating systems, applications, web apps vs desktop apps, and that's an order of magnitude harder than making tow instances / versions of MS Word look the same.
Activity
mickaelistria commentedon Aug 7, 2022
Such a contribution would be very welcome!
mihnita commentedon Aug 16, 2022
Hi,
Sorry, nothing new, but I just to make sure I manage expectations :-)
I did not drop out, but I think this is not something for 2022-09.
My guess is that it is a bit too late for that.
(I also don't have much time, so I would rather focus on the color support in the Console, for now)
But I can share a bit the big picture plan:
TextWriterandRTFWriterout oforg.eclipse.swt.custom.StyledTextHTMLWriter, very similar to theRTFWriterStyledTextcopy operation to also put the HTML in the clipboardThe HTML I would produce is very "primitive" for best compatibility.
Using
<b>and<i>and<span style=...>, no stylesheets or anything fancy.I've compared the kind of HTML put in the clipboard by all kinds of apps, from (the "ancient") Office 2010 to Google Slides/Docs/GMail.
And that kind of HTML seems to be the norm.
Regards,
Mihai
tmssngr commentedon Aug 22, 2022
Maybe such code can be placed outside the (already large)
StyledTextcode?mickaelistria commentedon Aug 22, 2022
@tmssngr I believe this is what was meant with "Optional: extract TextWriter and RTFWriter out of org.eclipse.swt.custom.StyledText"
mihnita commentedon Aug 22, 2022
Agree with
HTMLWriteroutsideStyledText.In fact that was my thinking was, to have
HTMLWriteroutside.And also move
RTFWriterandTextWriterout ofStyledText.Right now
RTFWriterandTextWriterare inner classes toStyledText.I think it would be cleaner to make them standalone classes.
They are not public, so that would not be a breaking change.
But this refactoring is not a change that is needed for the html clipboard functionality.
That is why I said "optional".
Mihai
mickaelistria commentedon Aug 29, 2022
@mihnita If you want to start with a refactoring to extract the existing RTFWriter and TextWriter, to familiarize with the code and maybe factorize what seems really worth it, that would be welcome in a preliminary PR.
mihnita commentedon Sep 1, 2022
Thank you.
Right now I am a bit under pressure with a project at work.
I will have a bit more time in a week or two.
And Eclipse is close to release, too late to make it into that. So no rush...
I guess there is some development branch going on for after-release code?
Thanks,
Mihai
akurtakov commentedon Sep 1, 2022
Branching for 4.26 will happen end of this week/early next week.
Extract TextWriter & RTFWriter from StyledText (issue #292)
Support HTML as a Clipboard format for StyledText (issue #292) (#395)
mihnita commentedon Sep 19, 2022
Since this has a "noteworthy" label I suppose I will need to prepare an entry for N&N
(https://github.com/eclipse-platform/www.eclipse.org-eclipse-news)
But there is no 4.26 folder yet, so I will put a reminder somewhere.
Meantime I will test on other OSes and applications.
Maybe find an MS Office from this century ;-)
M.
mickaelistria commentedon Sep 19, 2022
Yes, but no need to rush.
I'm creating one right now.
mickaelistria commentedon Sep 19, 2022
4.26 folder is ready.
I verified it by copy-pasting from StyledText to GMail, and can now see the styling is preserved.
jukzi commentedon Mar 17, 2023
please fix the regression of adding extra newlines in the htmlText #593
jukzi commentedon Mar 17, 2023
There is an additional newline at the end and every linebreak is formatted as an paragraph instead of linebreak

mihnita commentedon Mar 23, 2023
I checked and there are are indeed paragraphs, not line breaks.
But I don't think this is legacy.
It follows exactly the RTF an (almost exactly) the plain text behavior).
I've prepared a Word document with 3 sections, for plain text, RTF, and HTML.
There paragraph markers that already exist are Arial, so it will be clear what comes from clipboard.
Then I've pasted the various formats, with paste special:
All 3 formats use paragraphs (
<p>) to separate the lines, not line break (<br>)The RTF and HTML are virtually identical, and there is indeed a paragraph after the
else {which is missing in plain text.We can debate if that is correct or not. But is is not a regression, the HTML behave the same as the RTF, which was untouched recently.
Now, I would also argue that the extra paragraph is indeed correct, because the editor selection (as shown in the screenshot in jukzi's comment) includes a new line.
Here are the results when the EOL after

else {is not selected:There is indeed a difference between HTML and RTF now (and it is clear what paragraph marker was there and what comes from the clipboard because of the font difference.
So I might agree that his is indeed a bug.
BUT!
There is a reason why is there.
The HTML clipboard contains a

<div>with several<p>s. And I think that MS Words generates that paragraph from the div.And the div is there to support background colors, which are not supported in RTF:
mihnita commentedon Mar 23, 2023
The html I generate is very "old style" (not style sheets, nothing fancy), as from the various apps I've tried many don't support styles, or background colors, and so on.
For example in Word you can do color paragraphs without the div, but Google Slides does not.
The div is also needed to control the line height. And for indents in Google Docs and Slides and
Here is documented everything I've tried:
#469
(with a link to screenshots before / after: https://mihai-nita.net/eclipse_swt_467/)
The
divwas the only thing that I could find that seem to work everywhere and fixes everything(background colors, line spacing, indents)
With that price, an extra newline...
Which is very easy to delete after paste if one does not like it.
jukzi commentedon Mar 23, 2023
In your first screenshot you have overseen that there IS a difference of an additional line:

For us it more important to paste exactly the text that was copied (no additional line) over having any copied style.
When i copy/paste text from Word to outlook it does paste the backgroundcolor without adding any additional newline/paragraph. i.e. somehow it is possible- maybe you could investigate what pattern they use and use that?
With HTML the paragraphs look higher then a linebreak, while with RTF the paragraph looks like a ordinary linebreak hight:

You could test the result by copy text from IDE, paste it as RTF in word, then copy that pasted text from word an paste it as HTML to word (looks the same as the pasted RTF for me). When pasting from the IDE directly as HTML into Word it should look the same.
jukzi commentedon Mar 23, 2023
The pasted HTML results in an additional gap per paragraph in word (0 when using RTF):

mihnita commentedon Mar 27, 2023
I've spent on this a lot of hours over the week-end.
And yes, there is an extra paragraph in the HTML format.
For line spacing, there is no visual difference between HTML and RTF.
It looks the same as the RTF, and when I inspecting the paragraph properties (same dialog as yours) the spacing before / after is zero.
I will still spend some time on it.
But what makes it hard is solving this for everybody.
It is easy to test Outlook vs Word. Both on Windows, both Microsoft, both Office, probably same version.
It looks like even the version of Office matters, as you see different results in line space than what I see.
But this has to work on all OSes, most desktop apps, most web based apps in most popular browsers.
And the copy should not only be from Eclipse code but also any SWT widget.
That means supporting styles that the Eclipse code does not use.
See https://mihai-nita.net/eclipse_clip_html/
You will notice paragraph alignment (left / center / right), bullets, bidi support, justify text or not, enable / disable word wrap, padding (for the whole content), line spacing (from the SWT widget, not fixed), indent, margins.
And this only lists the paragraph attributes, not spans (supperscript, underscore, etc)
I also looked at what Word puts in the clipboard.
And it is far from clean, and not at all portable.
This simple code:
Produces 1451 bytes of clipboard data when copied from Eclipse, and 23327 bytes when copied from Word.
And the Word HTML contains "non-standard tricks":
<o:p></o:p>tags from a proprietary namespace (xmlns:o="urn:schemas-microsoft-com:office:office")<span style='mso-tab-count:3'>(that is for 3 tabs)So when you paste that in word (again) the nbsp(s) are in fact replaced by real tabs. But that is a non-portable hack.
mso-spacerun,mso-tab-count, in general tons ofmso-attributes)It also seems to produce different html depending on what I "grab" in the clipboard.
For plain (ASCII) text it produces fewer proprietary attributes and styles.
Grab some fancy symbol character and it adds
mso-ascii-font-family,mso-ascii-theme-font,mso-hansi-font-family,mso-hansi-theme-font,mso-char-type,mso-symbol-font-familyand many other attributes.In all this is what I see in the body part. I've completely ignored non-standard stuff happening in the style definitions.
TLDR: what Word (and Office) produces can't be a good example for what we want to put in the clipboard.
Yes, copy Word - to - Word works, but it is not portable.
On the good side, I think I have a small improvement that would remove the extra newline when there was a newline selected in Eclipse.
But I have to test it on other platforms and applications.
laeubi commentedon Mar 27, 2023
@mihnita honestly HTML "format" was from my opinion always broken (regardless of application) and especially for microsoft as you have discovered now ;-)
So from my side, if we have basic support for this this is totally fine and things like an additional line break a really nothing to worry about...
jukzi commentedon Mar 27, 2023
Using
"<span>"'s instead of"<div>"and"<p>"for formatting and only adding"<br>"or"<p>"where needed both Teams and Word don't add additional newlines (but sill increased paragraph hight).mihnita commentedon Mar 28, 2023
These are narrow solutions that only solve this problem, only for Office, and only for code.
I am looking for something that works everywhere, and anything that SWT StyledText supports.
<p>and<div>are block elements (similar to paragraph styles in Word),<span>is an inline element (similar to character style in Word)This means that text in a
<span>can be centered or justified, for example.Check the
StyledTextdocumentation.There are things that affect the whole widget (and belong in a
div), for examplesetAlignment,setBackground,set*Margin,setIndent,setJustify,setOrientation, etc.Then there are line attributes that work (most of them) on block elements only (
setLineJustify,setLineBullet,setLineSpacing`,The things that map to
<span>are theStyleRange(s), (used insetStyleRanges)At this point I am ready to bet that the HTML clipboard (as is is now) supports more of the
StyledTextstyle attributes than the RTF clipboard format.Just give me a some of breathing space and I will prepare something, and test it to work properly everywhere.
Or you can do something your way and I will test it on all platforms and with all the styles (including non-code editor ones) and I will tell you what breaks.
mihnita commentedon Mar 28, 2023
Well, HTML was not supported at all.
So "working better now" is a pretty low bar :-)
mihnita commentedon Mar 30, 2023
See here a screen recording showing RTF vs HTML after the fix (in PR now):
https://mihai-nita.net/eclipse_test/MSWord_LineHeight.gif
You can see that the line spacing is identical for both formats.
And here is the final result in Photoshop, doubled, the top layer at 50% transparency, and shifted so that the RTF overlap with the HTML under.
You can see the RTF at the top and HTML at the bottom at 50%, and the RTF + HTML overlapping in the middle.
And there is a perfect match, including the final newline.
What I am trying to show is not that I don't believe that you see differences in line heights.
I believe that you do.
But I want to show that that two different versions of MS Word, on two machines, can behave differently.
Making things work the same is a lot harder than it seems.
Add to the mix different operating systems, applications, web apps vs desktop apps, and that's an order of magnitude harder than making tow instances / versions of MS Word look the same.