Skip to content
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

Remove null character at end of pasted string #1219

Open
willplatt opened this issue Mar 15, 2024 · 0 comments
Open

Remove null character at end of pasted string #1219

willplatt opened this issue Mar 15, 2024 · 0 comments

Comments

@willplatt
Copy link

willplatt commented Mar 15, 2024

When copying text from Microsoft Word on Windows and pasting into a RichTextFX text field or text area there is always a null character (code point 0) at the end of the pasted text. getText().getBytes(UTF_8) shows a single 0 byte and using arrow keys in the text field/area suggests there is a single invisible character.

This doesn't happen when pasting into a standard JavaFX TextField or TextArea, or when pasting into various non-Java applications.

I have only observed this issue when copying from Microsoft Word, but according to this, it's Windows-related, and there may even be multiple null characters, although I haven't observed this.

Expected behaviour: When a string is pasted there should be no null characters at the end of the string.
Actual behaviour: There is a null character at the end of the string.
Environment info: RichTextFX 0.11.2, Windows 10, Java 17 and 22. Tested with StyleClassedTextArea and StyleClassedTextField.

Workaround: Is there a clean way to edit the text being pasted, preferably before it's inserted?
I figured out this way to do it by overriding the paste() method, but I'm not sure if it will cause problems for pasting formatted text:

@Override
public void paste() {
	Clipboard clipboard = Clipboard.getSystemClipboard();
	if (clipboard.hasString()) {
		String str = clipboard.getString();
		if (str != null && str.endsWith("\u0000")) {
			replaceSelection(str.replaceAll("\u0000+\\Z", ""));
			return;
		}
	}
	super.paste();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant