-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only enable HTML5 dragging if default is not prevented on the mousedo…
…wn event
- Loading branch information
Showing
7 changed files
with
91 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
|
||
class Capybara::Selenium::Node | ||
module Html5Drag | ||
private | ||
|
||
def html5_drag_to(element) | ||
driver.execute_script MOUSEDOWN_TRACKER | ||
scroll_if_needed { browser_action.click_and_hold(native).perform } | ||
if driver.evaluate_script('window.capybara_mousedown_prevented') | ||
element.scroll_if_needed { browser_action.move_to(element.native).release.perform } | ||
else | ||
driver.execute_script HTML5_DRAG_DROP_SCRIPT, self, element | ||
end | ||
end | ||
|
||
def draggable? | ||
# Workaround https://github.com/SeleniumHQ/selenium/issues/6396 | ||
driver.evaluate_script('arguments[0]["draggable"]', self) == true | ||
end | ||
|
||
MOUSEDOWN_TRACKER = <<~JS | ||
if (!window.hasOwnProperty('capybara_mousedown_prevented')){ | ||
document.addEventListener('mousedown', function(ev){ | ||
window.capybara_mousedown_prevented = ev.defaultPrevented; | ||
}) | ||
} | ||
JS | ||
|
||
HTML5_DRAG_DROP_SCRIPT = <<~JS | ||
var source = arguments[0]; | ||
var target = arguments[1]; | ||
var dt = new DataTransfer(); | ||
var opts = { cancelable: true, bubbles: true, dataTransfer: dt }; | ||
if (source.tagName == 'A'){ | ||
dt.setData('text/uri-list', source.href); | ||
dt.setData('text', source.href); | ||
} | ||
if (source.tagName == 'IMG'){ | ||
dt.setData('text/uri-list', source.src); | ||
dt.setData('text', source.src); | ||
} | ||
var dragEvent = new DragEvent('dragstart', opts); | ||
source.dispatchEvent(dragEvent); | ||
target.scrollIntoView({behavior: 'instant', block: 'center', inline: 'center'}); | ||
var dragOverEvent = new DragEvent('dragover', opts); | ||
target.dispatchEvent(dragOverEvent); | ||
var dragLeaveEvent = new DragEvent('dragleave', opts); | ||
target.dispatchEvent(dragLeaveEvent); | ||
if (dragOverEvent.defaultPrevented) { | ||
var dropEvent = new DragEvent('drop', opts); | ||
target.dispatchEvent(dropEvent); | ||
} | ||
var dragEndEvent = new DragEvent('dragend', opts); | ||
source.dispatchEvent(dragEndEvent); | ||
JS | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters