Bug 1759604, check CanDropLink during the drop event as sites can cancel the dragover event causing the drop event to still fire, r=arai

Differential Revision: https://phabricator.services.mozilla.com/D154896
This commit is contained in:
Neil Deakin 2022-08-19 22:15:50 +00:00
Родитель 96460bcff8
Коммит 0de8be3ac6
7 изменённых файлов: 95 добавлений и 2 удалений

Просмотреть файл

@ -955,6 +955,14 @@ nsDocShellTreeOwner::HandleEvent(Event* aEvent) {
} else if (eventType.EqualsLiteral("drop")) {
nsIWebNavigation* webnav = static_cast<nsIWebNavigation*>(mWebBrowser);
// The page might have cancelled the dragover event itself, so check to
// make sure that the link can be dropped first.
bool canDropLink = false;
handler->CanDropLink(dragEvent, false, &canDropLink);
if (!canDropLink) {
return NS_OK;
}
nsTArray<RefPtr<nsIDroppedLinkItem>> links;
if (webnav && NS_SUCCEEDED(handler->DropLinks(dragEvent, true, links))) {
if (links.Length() >= 1) {

Просмотреть файл

@ -9,6 +9,10 @@ support-files =
../../../browser/base/content/test/general/head.js
[browser_bug1539497.js]
[browser_dragimage.js]
support-files =
dragimage.html
green.png
[browser_keyboard_event_init_key_event_enabled_in_contentscript.js]
support-files =
file_keyboard_event_init_key_event_enabled_in_contentscript.html

Просмотреть файл

@ -0,0 +1,67 @@
const TEST_URI = "dragimage.html";
// This test checks that dragging an image onto the same document
// does not drop it, even when the page cancels the dragover event.
add_task(async function dragimage_remote_tab() {
var tab = await BrowserTestUtils.openNewForegroundTab(
gBrowser,
"http://www.example.com/browser/dom/events/test/" + TEST_URI
);
let dropHappened = false;
let oldHandler = tab.linkedBrowser.droppedLinkHandler;
tab.linkedBrowser.droppedLinkHandler = () => {
dropHappened = true;
};
await SpecialPowers.spawn(tab.linkedBrowser, [], async () => {
let image = content.document.body.firstElementChild;
let target = content.document.body.lastElementChild;
const EventUtils = ContentTaskUtils.getEventUtils(content);
await EventUtils.synthesizePlainDragAndDrop({
srcElement: image,
destElement: target,
srcWindow: content,
destWindow: content,
id: content.windowUtils.DEFAULT_MOUSE_POINTER_ID,
});
});
tab.linkedBrowser.droppedLinkHandler = oldHandler;
ok(!dropHappened, "drop did not occur");
BrowserTestUtils.removeTab(tab);
});
// This test checks the same but with an in-process page.
add_task(async function dragimage_local_tab() {
var tab = await BrowserTestUtils.openNewForegroundTab(
gBrowser,
getRootDirectory(gTestPath) + TEST_URI
);
let dropHappened = false;
let oldHandler = tab.linkedBrowser.droppedLinkHandler;
tab.linkedBrowser.droppedLinkHandler = () => {
dropHappened = true;
};
let image = tab.linkedBrowser.contentDocument.body.firstElementChild;
let target = tab.linkedBrowser.contentDocument.body.lastElementChild;
await EventUtils.synthesizePlainDragAndDrop({
srcElement: image,
destElement: target,
srcWindow: tab.linkedBrowser.contentWindow,
destWindow: tab.linkedBrowser.contentWindow,
});
tab.linkedBrowser.droppedLinkHandler = oldHandler;
ok(!dropHappened, "drop did not occur");
BrowserTestUtils.removeTab(tab);
});

Просмотреть файл

@ -0,0 +1,10 @@
<html>
<head>
<title>Drag Image Test</title>
</head>
<body>
<img src="green.png" width=100 height=100>
<div id='over' style='width: 100px; height: 100px; border: 1px solid black;'
ondragover="event.preventDefault();"></div>
</body>
</html>

Двоичные данные
dom/events/test/green.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 255 B

Просмотреть файл

@ -3089,7 +3089,7 @@ async function synthesizePlainDragAndDrop(aParams) {
);
try {
_getDOMWindowUtils().disableNonTestMouseEvents(true);
_getDOMWindowUtils(srcWindow).disableNonTestMouseEvents(true);
await new Promise(r => setTimeout(r, 0));
@ -3414,7 +3414,7 @@ async function synthesizePlainDragAndDrop(aParams) {
srcWindow.removeEventListener("dragend", onDragEnd, { capture: true });
}
}
_getDOMWindowUtils().disableNonTestMouseEvents(false);
_getDOMWindowUtils(srcWindow).disableNonTestMouseEvents(false);
if (logFunc) {
logFunc("synthesizePlainDragAndDrop() -- END");
}

Просмотреть файл

@ -172,6 +172,10 @@
let linkHandler = Services.droppedLinkHandler;
try {
if (!linkHandler.canDropLink(event, false)) {
return;
}
// Pass true to prevent the dropping of javascript:/data: URIs
var links = linkHandler.dropLinks(event, true);
} catch (ex) {