Closed Bug 1650720 Opened 5 years ago Closed 6 months ago

Copying \r\n inside <pre> converts to \n\n

Categories

(Core :: DOM: Serializers, defect)

defect

Tracking

()

RESOLVED FIXED
147 Branch
Tracking Status
firefox-esr115 --- wontfix
firefox-esr128 --- wontfix
firefox-esr140 --- wontfix
firefox130 --- wontfix
firefox131 --- wontfix
firefox132 --- wontfix
firefox145 --- wontfix
firefox146 --- wontfix
firefox147 --- fixed

People

(Reporter: saschanaz, Assigned: edgar)

References

(Regression)

Details

(4 keywords)

User Story

platform-scheduled:2025-09-30
user-impact-score:600

Attachments

(6 files, 3 obsolete files)

Attached file bug1528442-2.html
  1. Open the attachment
  2. Copy the content
  3. Paste it somewhere

Expected: Single newline per line
Actual: Double newlines per line

Assignee: nobody → krosylight
Summary: Copying \r\n inside <pre> converts to \n\n → Copying \r\n inside <pre> converts to \r\n\n

This is confusing.

Summary: Copying \r\n inside <pre> converts to \r\n\n → Copying \r\n inside <pre> converts to \r\r\n

This is confusing, EncodeForTextUnicode() tries encoding the text with the mime type text/html instead of text/plain. The comment says:

  // note that we assign text/unicode as mime type, but in fact
  // nsHTMLCopyEncoder ignore it and use text/html or text/plain depending where
  // the selection is. if it is a selection into input/textarea element or in a
  // html content with pre-wrap style : text/plain. Otherwise text/html. see
  // nsHTMLCopyEncoder::SetSelection

I'm not sure why the copy would ever be text/html even when it's not pre-wrap style. This comment is wrong anyway, as it only becomes text/plain for <input> and <textarea> but not for pre-wrap style. That means, copying <pre> actually uses text/html.

The second confusing part is that EncodeForTextUnicode() tries encoding in raw-mode first (which does not affect newlines) and then retries in non-raw-mode, which causes the bug by incorrectly replacing \n with \r\n, making \r\n to \r\r\n. Why try twice?

Emilio, do you happen to have some idea about this, as one of the relevant functions has your FIXME comment?

Flags: needinfo?(emilio)

Not really, that FIXME is about assuming that all the selection is in the first selection range, which is not true for table selection for example.

Flags: needinfo?(emilio)

Oops, okay. Keeping pinging semi-randomly based on commit history...

Mirko, it seems you refactored some of the code, do you have some idea about the mime type thing?

Flags: needinfo?(mbrodesser)

(In reply to Kagami :saschanaz from comment #2)

This is confusing, EncodeForTextUnicode() tries encoding the text with the mime type text/html instead of text/plain. The comment says:

  // note that we assign text/unicode as mime type, but in fact
  // nsHTMLCopyEncoder ignore it and use text/html or text/plain depending where
  // the selection is. if it is a selection into input/textarea element or in a
  // html content with pre-wrap style : text/plain. Otherwise text/html. see
  // nsHTMLCopyEncoder::SetSelection

I'm not sure why the copy would ever be text/html even when it's not pre-wrap style. This comment is wrong anyway, as it only becomes text/plain for <input> and <textarea> but not for pre-wrap style.

I'm not sure how relevant it is, but in another case text/plain is set too.

That means, copying <pre> actually uses text/html.

The second confusing part is that EncodeForTextUnicode() tries encoding in raw-mode first (which does not affect newlines) and then retries in non-raw-mode, which causes the bug by incorrectly replacing \n with \r\n, making \r\n to \r\r\n. Why try twice?

I'm unfamiliar with this detail, it needs investigation.

Emilio, do you happen to have some idea about this, as one of the relevant functions has your FIXME comment?

Flags: needinfo?(mbrodesser)
Severity: -- → S3

May be this behavior is also related to this bug.

Steps to reproduce

  • Go to IEEE Explore page and open any research paper. For example: https://ieeexplore.ieee.org/document/7919538
  • Click on Cite this button
  • Select BibTex option
  • Copy BibTex content using mouse and keyboard that's select text using mouse and the press Ctrl+C
  • Paste it in any text editor

Actual ouput

Every sentence ends with an extra new line.

Expected output

There shouldn't be any extra new line after each sentence.

Additional

Like 152844#c4 example, I cannot reproduce this bug in HTML based editors like Librewriter; seems like problem is with text serialization.

No immediate plan to work on this, please feel free to take it.

Assignee: krosylight → nobody
See Also: → 1917398
Duplicate of this bug: 1917398
See Also: 1917398

This page ( https://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q12303673327 ) is also affected .

Chrome works as expected.

Set release status flags based on info from the regressing bug 731896

:hsivonen, since you are the author of the regressor, bug 731896, could you take a look?

For more information, please visit BugBot documentation.

You are supposed to use only LF in the DOM. On Windows, in-DOM LF gets converted to CRLF to match Windows convention. Putting a CR in the DOM is bad, and the outcome follows logically from the previous sentence, since CR stays as-is and LF gets replaced with CRLF.

From a logical point of view, the behavior described in comment 0 is as expected. However, if Web pages run into this in a way that users perceive as a problem that does not occur in Chrome (per comment 9), it makes sense to find out what Chrome does and start doing that.

Flags: needinfo?(hsivonen)
Blocks: 1966955
User Story: (updated)

On macOS, the \r\n is replaced to \n\n on Firefox, whereas it is replaced to \n on Chromium. So it worth to test each platform, they could have different behavior.

There are two main actions involved,

  1. Serialize selection into plaintext and put into system clipboard
  2. Text editor read the clipboard plaintext data and insert the text.

For the second action, all browsers behave the same on each platform to turn the platform line break into DOM line break, e.g.

  • \r\n ---> \n
  • \r ---> \n

But on the first steps, Firefox behave differently than other browsers,

Mac:

Firefox Chrome Safari
\r \n \r \r
\n \n \n \n
\r\r \n\n \r\r \r\r
\r\n \n\n \r\n \r\n
\n\r \n\n \n\r \n\r
\n\n \n\n \n\n \n\n
\r\r\r \n\n\n \r\r\r \r\r\r
\r\r\n \n\n\n \r\r\n \r\r\n
\r\n\r \n\n\n \r\n\r \r\n\r
\r\n\n \n\n\n \r\n\n \r\n\n
\n\r\r \n\n\n \n\r\r \n\r\r
\n\r\n \n\n\n \n\r\n \n\r\n
\n\n\r \n\n\n \n\n\r \n\n\r
\n\n\n \n\n\n \n\n\n \n\n\n

Linux:

Firefox Chrome
\r \n \r
\n \n \n
\r\r \n\n \r\r
\r\n \n\n \r\n
\n\r \n\n \n\r
\n\n \n\n \n\n
\r\r\r \n\n\n \r\r\r
\r\r\n \n\n\n \r\r\n
\r\n\r \n\n\n \r\n\r
\r\n\n \n\n\n \r\n\n
\n\r\r \n\n\n \n\r\r
\n\r\n \n\n\n \n\r\n
\n\n\r \n\n\n \n\n\r
\n\n\n \n\n\n \n\n\n

Windows:

Firefox Chrome
\r \n \r
\n \n \r\n
\r\r \n\n \r\r
\r\n \n\n \r\n
\n\r \n\n \r\n\r
\n\n \n\n \r\n\r\n
\r\r\r \n\n\n \r\r\r
\r\r\n \n\n\n \r\r\n
\r\n\r \n\n\n \r\n\r
\r\n\n \n\n\n \r\n\r\n
\n\r\r \n\n\n \r\n\r\r
\n\r\n \n\n\n \r\n\r\n
\n\n\r \n\n\n \r\n\r\n\r
\n\n\n \n\n\n \r\n\r\n\r\n

So on Linux and Mac, Safari and Chrome doesn't change the DOM text while serializing. And on Windows, Chrome convert LF to CRLF for Windows convention.

Firefox always covert CR to LF. It is handled in nsPlainTextSerializer.

selection.toString() has similar issue, but Chrome doesn't convert LF to CRLF even on Windows.

Summary: Copying \r\n inside <pre> converts to \r\r\n → Copying \r\n inside <pre> converts to \n\n
Assignee: nobody → echen
OS: Windows 10 → All
Hardware: x86_64 → All
Attachment #9513581 - Attachment description: Bug 1650720 - Rename DoAddText() to DoAddLineBreak(); → WIP: Bug 1650720 - Rename DoAddText() to DoAddLineBreak();
Attachment #9513581 - Attachment description: WIP: Bug 1650720 - Rename DoAddText() to DoAddLineBreak(); → Bug 1650720 - Rename DoAddText() to DoAddLineBreak();

Comment on attachment 9513584 [details]
Bug 1650720 - Make nsIContentSerializer::AppendText() and AppendCDATASection() take Text;

Revision D265012 was moved to bug 1989141. Setting attachment 9513584 [details] to obsolete.

Attachment #9513584 - Attachment is obsolete: true
User Story: (updated)
Attachment #9513581 - Attachment is obsolete: true
Depends on: 1992067
Attachment #9519423 - Attachment description: Bug 1650720 - Part 1: Move some serializer mochitest tests to dom/serializers; → Bug 1650720 - Part 1: Move some serializer mochitest tests to dom/serializers/tests/;
Attachment #9519423 - Attachment description: Bug 1650720 - Part 1: Move some serializer mochitest tests to dom/serializers/tests/; → Bug 1650720 - Part 1: Move some serializer mochitest tests to dom/serializers/tests/; r?masayuki
Attachment #9522781 - Attachment description: Bug 1650720 - Part 2: Add serializer tests for linebreak; → Bug 1650720 - Part 2: Add plaintext serializer tests for linebreak; r?masayuki
Attachment #9522782 - Attachment description: Bug 1650720 - Part 3: Simplify nsPlainTextSerializer::ConvertToLinesAndOutput; → Bug 1650720 - Part 3: Simplify nsPlainTextSerializer::ConvertToLinesAndOutput; r?masayuki
Attachment #9523269 - Attachment description: Bug 1650720 - Part 4: Align link break coversion for clipboard copy with other browsers; → Bug 1650720 - Part 4: Align link break coversion for clipboard copy with other browsers; r?masayuki

Comment on attachment 9519423 [details]
Bug 1650720 - Part 1: Move some serializer mochitest tests to dom/serializers/tests/; r?masayuki

Revision D268242 was moved to bug 1997661. Setting attachment 9519423 [details] to obsolete.

Attachment #9519423 - Attachment is obsolete: true
Attachment #9522781 - Attachment description: Bug 1650720 - Part 2: Add plaintext serializer tests for linebreak; r?masayuki → Bug 1650720 - Part 1: Add plaintext serializer tests for linebreak; r?masayuki
Attachment #9522782 - Attachment description: Bug 1650720 - Part 3: Simplify nsPlainTextSerializer::ConvertToLinesAndOutput; r?masayuki → Bug 1650720 - Part 2: Simplify nsPlainTextSerializer::ConvertToLinesAndOutput; r?masayuki
Attachment #9523269 - Attachment description: Bug 1650720 - Part 4: Align link break coversion for clipboard copy with other browsers; r?masayuki → Bug 1650720 - Part 3: Align link break coversion for clipboard copy with other browsers; r?masayuki
Attachment #9523892 - Attachment description: Bug 1650720 - Part 4: Add line break tests for copying selection; → Bug 1650720 - Part 4: Add line break tests for copying selection; r?masayuki
Pushed by amarc@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/920ab7c17333 https://hg.mozilla.org/integration/autoland/rev/9fa4d47ced08 Revert "Bug 1650720 - Part 4: Add line break tests for copying selection; r=masayuki" for causing mochitest failures @ test_aboutmemory3.xhtml

Backed out for causing mochitest failures @ test_aboutmemory3.xhtml

Flags: needinfo?(echen)
Created web-platform-tests PR https://github.com/web-platform-tests/wpt/pull/55882 for changes under testing/web-platform/tests
Upstream PR merged by moz-wptsync-bot
Created web-platform-tests PR https://github.com/web-platform-tests/wpt/pull/55885 for changes under testing/web-platform/tests
Upstream PR merged by moz-wptsync-bot
Flags: needinfo?(echen)
Created web-platform-tests PR https://github.com/web-platform-tests/wpt/pull/55963 for changes under testing/web-platform/tests
Upstream PR merged by moz-wptsync-bot
Duplicate of this bug: 1789825
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: