From 06b663afd41af9039bb903c8f39778a72c8b7ae2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 27 Mar 2021 10:22:55 +0000 Subject: [PATCH] Bug 1697836 - Avoid throwing when asking for a selection. r=emalysz Selection may be null if the page got hidden in e.g., the afterprint event listener or somewhere else. Add a try catch for good measure, assuming there's no selection is always safe. I'll try to get an automated test-case for this working, though it might be tricky. Depends on D109714 Differential Revision: https://phabricator.services.mozilla.com/D109715 --- toolkit/actors/PrintingSelectionChild.jsm | 3 +-- toolkit/components/printing/content/printUtils.js | 8 +++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/toolkit/actors/PrintingSelectionChild.jsm b/toolkit/actors/PrintingSelectionChild.jsm index b13ced53ad89..e8e39293f03c 100644 --- a/toolkit/actors/PrintingSelectionChild.jsm +++ b/toolkit/actors/PrintingSelectionChild.jsm @@ -22,9 +22,8 @@ class PrintingSelectionChild extends JSWindowActorChild { let focusedWindow = Services.focus.focusedWindow; if (focusedWindow) { let selection = focusedWindow.getSelection(); - return selection.type == "Range"; + return selection && selection.type == "Range"; } - return false; } } diff --git a/toolkit/components/printing/content/printUtils.js b/toolkit/components/printing/content/printUtils.js index 920ff5b00fcd..9bd616d5b371 100644 --- a/toolkit/components/printing/content/printUtils.js +++ b/toolkit/components/printing/content/printUtils.js @@ -235,9 +235,11 @@ var PrintUtils = { let sourceActor = aBrowsingContext.currentWindowGlobal.getActor( "PrintingSelection" ); - hasSelection = await sourceActor.sendQuery( - "PrintingSelection:HasSelection" - ); + try { + hasSelection = await sourceActor.sendQuery( + "PrintingSelection:HasSelection" + ); + } catch (ex) {} } let sourceBrowser = aBrowsingContext.top.embedderElement;