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

Indentation is lost when pasting in the RTE #9264

Closed
laurent22 opened this issue Nov 10, 2023 · 2 comments · Fixed by #9828
Closed

Indentation is lost when pasting in the RTE #9264

laurent22 opened this issue Nov 10, 2023 · 2 comments · Fixed by #9828
Assignees
Labels
bug It's a bug desktop All desktop platforms editor high High priority issues

Comments

@laurent22
Copy link
Owner

When pasting the text below in the RTE, the indentation is lost:

Some text with indented by a tab:
	Indented
Some text with indented by four spaces:
    Indented
@laurent22 laurent22 added bug It's a bug desktop All desktop platforms high High priority issues editor labels Nov 10, 2023
@personalizedrefrigerator
Copy link
Collaborator

When I copy from GitHub with formatting, it seems to work (and code blocks are created).

When copying without formatting (e.g. using the "copy" button to the right of the code block), I can confirm that indentation is lost.

@pedr
Copy link
Collaborator

pedr commented Jan 29, 2024

The difference between the behavior from when you "select everything and copy" and using the copy button is that in the first action you are also copying HTML to the clipboard. When we have HTML in the clipboard, the plain/text source is ignored.

It is in the "format plain text to HTML" where the bug happens.


One fix would be to:

Change line.trim() to line.trimEnd()

export function plainTextToHtml(plainText: string): string {
const lines = plainText
.replace(/\r\n/g, '\n')
.split('\n');
if (lines.length === 1) return escapeHtml(lines[0]);
// Step 1: Merge adjacent lines into paragraphs, with each line separated by
// '<br/>'. So 'one\ntwo' will become '<p>one</br>two</p>'
const step1: string[] = [];
let currentLine = '';
for (let line of lines) {
line = line.trim();
if (!line) {
if (currentLine) {
step1.push(`<p>${currentLine}</p>`);
currentLine = '';
}
step1.push(line);
} else {
if (currentLine) {
currentLine += `<br/>${escapeHtml(line)}`;
} else {
currentLine = escapeHtml(line);
}
}
}

And since HTML doesn't know spaces we would need to change espapeHTML to something like this:

function escapeHtml(s) {
	return s
		.replace(/&/g, '&amp;')
		.replace(/</g, '&lt;')
		.replace(/>/g, '&gt;')
		.replace(/"/g, '&quot;')
		.replace(/'/g, '&#039;')
		.replace(/ /g, '&nbsp;')
		.replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;');
}

function escapeHtml(s) {

This would fix the problem, but I'm not sure if this is the correct approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It's a bug desktop All desktop platforms editor high High priority issues
Projects
None yet
3 participants