Bug 85686 - Mimic Chromium's getSelection().toString() behaviour r=masayuki,dom-core,webidl,smaug

Basically when getSelection().toString() is called, Chromium may
return an serialization on a different content than Firefox.
This patch tries to address this webcompat issue by mimicing
the same behaviour.

We should still address the spec issues, but this seems to be
an acceptable compromise.

Differential Revision: https://phabricator.services.mozilla.com/D239657
This commit is contained in:
Sean Feng
2025-03-12 15:50:43 +00:00
parent 43d1c19842
commit afba09df01
26 changed files with 374 additions and 44 deletions

View File

@@ -773,6 +773,7 @@ bool PresShell::AccessibleCaretEnabled(nsIDocShell* aDocShell) {
PresShell::PresShell(Document* aDocument)
: mDocument(aDocument),
mViewManager(nullptr),
mLastSelectionForToString(nullptr),
mAutoWeakFrames(nullptr),
#ifdef ACCESSIBILITY
mDocAccessible(nullptr),
@@ -1539,7 +1540,8 @@ bool PresShell::FixUpFocus() {
void PresShell::SelectionWillTakeFocus() {
if (mSelection) {
FrameSelectionWillTakeFocus(*mSelection);
FrameSelectionWillTakeFocus(*mSelection,
CanMoveLastSelectionForToString::No);
}
}
@@ -1584,11 +1586,20 @@ void PresShell::FrameSelectionWillLoseFocus(nsFrameSelection& aFrameSelection) {
}
if (mSelection) {
FrameSelectionWillTakeFocus(*mSelection);
FrameSelectionWillTakeFocus(*mSelection,
CanMoveLastSelectionForToString::No);
}
}
void PresShell::FrameSelectionWillTakeFocus(nsFrameSelection& aFrameSelection) {
void PresShell::FrameSelectionWillTakeFocus(
nsFrameSelection& aFrameSelection,
CanMoveLastSelectionForToString aCanMoveLastSelectionForToString) {
if (StaticPrefs::dom_selection_mimic_chrome_tostring_enabled()) {
if (aCanMoveLastSelectionForToString ==
CanMoveLastSelectionForToString::Yes) {
UpdateLastSelectionForToString(&aFrameSelection);
}
}
if (mFocusedFrameSelection == &aFrameSelection) {
#ifdef XP_MACOSX
// FIXME: Mac needs to update the global selection cache, even if the
@@ -1616,6 +1627,14 @@ void PresShell::FrameSelectionWillTakeFocus(nsFrameSelection& aFrameSelection) {
}
}
void PresShell::UpdateLastSelectionForToString(
const nsFrameSelection* aFrameSelection) {
MOZ_ASSERT(StaticPrefs::dom_selection_mimic_chrome_tostring_enabled());
if (mLastSelectionForToString != aFrameSelection) {
mLastSelectionForToString = aFrameSelection;
}
}
NS_IMETHODIMP
PresShell::SetDisplaySelection(int16_t aToggle) {
mSelection->SetDisplaySelection(aToggle);