Closed Bug 1953174 Opened 1 year ago Closed 4 months ago

Copying selected data generates outer HTML into the clipboard

Categories

(Core :: DOM: Serializers, defect)

defect

Tracking

()

RESOLVED FIXED
148 Branch
Tracking Status
firefox148 --- fixed

People

(Reporter: edgar, Assigned: edgar)

References

(Blocks 7 open bugs, )

Details

Attachments

(2 files)

There is a difference on content serialization result between Firefox and Chrome. For example. if you select all content on https://software.hixie.ch/utilities/js/live-dom-viewer/?saved=13533 and press Ctrl+C, then check the clipboard content:

The data that Firefox put into clipboard:

<div>
  <span>First Line</span>
  <div>
    <span>Second Line</span>
  </div>
  <span>Third Line</span>
</div>

The data the Chrome put into clipboard:

<meta charset='utf-8'><span style="color: rgb(0, 0, 0); font-family: Times; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">First Line</span><span style="color: rgb(0, 0, 0); font-family: Times; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"></span><div style="color: rgb(0, 0, 0); font-family: Times; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><span>Second Line</span></div><span style="color: rgb(0, 0, 0); font-family: Times; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">Third Line</span>

A key difference is that Firefox includes the outer <div>, whereas Chrome does not. I think this is https://github.com/w3c/clipboard-apis/issues/49 where spec is verge about it.

Blocks: 1876887

Per discussion with the Editing WG, we should try to match the behavior of Chromium and WebKit here.

Severity: -- → S3
Component: Knowledge Base → DOM: Serializers
Product: Web Compatibility → Core
No longer blocks: 1876887
See Also: → 1876887
Blocks: 1876887
See Also: 1876887

Is this caused by the difference of the serializers? I wonder if the other browsers might shrink the selection range when "Select All" to first/last meaningful leaf node start/end. Actually, I got the following range when I do "Select All" on Chrome.

Range {commonAncestorContainer: div, startContainer: text, startOffset: 0, endContainer: text, endOffset: 10, …}
collapsed: false
commonAncestorContainer: div
endContainer: text
endOffset: 10
startContainer: text
startOffset: 0

"Select All" is a different case. This case is about select all text manually by mouse.
Chrome and us both has same range as below,

Range { commonAncestorContainer: div, startContainer: #text, startOffset: 0, endContainer: #text, endOffset: 10, collapsed: false }
​collapsed: false
​commonAncestorContainer: <div>
​endContainer: #text "Third Line"
​endOffset: 10
​startContainer: #text "First Line"
​startOffset: 0

And our serialized html content for clipboard copying includes the common ancestor element, whereas Chrome doesn't.

Oh, thank you for clarifying.

And it seems that this bug is more important on Android. I've not realized that I'm a victim of this bug. When I copy all text in a heading element on Firefox for Android and paste it to another Android app, the pasted text ends with 2 line breaks. I guess the outer heading element of the text in clipboard causes the trailing line breaks when it's pasted into another app's text field. (Although I'm not familiar with clipboard handling/API of Android.)

(In reply to Masayuki Nakano [:masayuki] (he/him)(JST, +0900) from comment #4)

When I copy all text in a heading element on Firefox for Android and paste it to another Android app, the pasted text ends with 2 line breaks. I guess the outer heading element of the text in clipboard causes the trailing line breaks when it's pasted into another app's text field. (Although I'm not familiar with clipboard handling/API of Android.)

Would you mind filing a separated bug with STR? I could take a look and do some investigation. Thanks!

Flags: needinfo?(masayuki)

Filed bug 2003716.

Flags: needinfo?(masayuki)
Assignee: nobody → echen
Blocks: 1284473
Blocks: 819266
Blocks: 1213686
Blocks: 1962633
Attachment #9533216 - Attachment description: Bug 1953174 - Stop including common ancestor when serializing HTML data for clipboard copy; → WIP: Bug 1953174 - Part 2: Stop always including common ancestor when serializing HTML data for clipboard copy;
Attachment #9534798 - Attachment description: WIP: Bug 1953174 - Part 1: Add more mochitest tests for HTMLCopyEncoder; → Bug 1953174 - Part 1: Add more mochitest tests for HTMLCopyEncoder; r?masayuki
Attachment #9533216 - Attachment description: WIP: Bug 1953174 - Part 2: Stop always including common ancestor when serializing HTML data for clipboard copy; → Bug 1953174 - Part 2: Stop always including common ancestor when serializing HTML data for clipboard copy; r?masayuki
Blocks: 2008931
Pushed by echen@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/942ef4cb8ece https://hg.mozilla.org/integration/autoland/rev/2c3c9a4f5113 Part 1: Add more mochitest tests for HTMLCopyEncoder; r=masayuki DONTBUILD https://github.com/mozilla-firefox/firefox/commit/f244579d668e https://hg.mozilla.org/integration/autoland/rev/8eba4813d61a Part 2: Stop always including common ancestor when serializing HTML data for clipboard copy; r=masayuki DONTBUILD
Status: NEW → RESOLVED
Closed: 4 months ago
Resolution: --- → FIXED
Target Milestone: --- → 148 Branch
QA Whiteboard: [qa-triage-done-c149/b148]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: