diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /editor/libeditor/tests/test_paste_redirect_focus_in_paste_event_listener.html | |
parent | Initial commit. (diff) | |
download | thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'editor/libeditor/tests/test_paste_redirect_focus_in_paste_event_listener.html')
-rw-r--r-- | editor/libeditor/tests/test_paste_redirect_focus_in_paste_event_listener.html | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/editor/libeditor/tests/test_paste_redirect_focus_in_paste_event_listener.html b/editor/libeditor/tests/test_paste_redirect_focus_in_paste_event_listener.html new file mode 100644 index 0000000000..b82938158e --- /dev/null +++ b/editor/libeditor/tests/test_paste_redirect_focus_in_paste_event_listener.html @@ -0,0 +1,143 @@ +<!doctype html> +<html> +<head> +<meta charset="utf-8"> +<title>Testing handling "paste" command when a "paste" event listener moves focus</title> +<script src="/tests/SimpleTest/SimpleTest.js"></script> +<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +<script> +"use strict"; + +SimpleTest.waitForExplicitFinish(); +SimpleTest.waitForFocus(async () => { + info("Waiting for initializing clipboard..."); + await SimpleTest.promiseClipboardChange( + "plain text", + () => SpecialPowers.clipboardCopyString("plain text") + ); + + const transferable = + SpecialPowers.Cc["@mozilla.org/widget/transferable;1"].createInstance(SpecialPowers.Ci.nsITransferable); + transferable.init( + SpecialPowers.wrap(window).docShell.QueryInterface(SpecialPowers.Ci.nsILoadContext) + ); + const supportString = + SpecialPowers.Cc["@mozilla.org/supports-string;1"].createInstance(SpecialPowers.Ci.nsISupportsString); + supportString.data = "plain text"; + transferable.setTransferData("text/plain", supportString); + + function getValue(aElement) { + if (aElement.tagName.toLowerCase() == "input" || + aElement.tagName.toLowerCase() == "textarea") { + return aElement.value; + } + return aElement.textContent; + } + function setValue(aElement, aValue) { + if (aElement.tagName.toLowerCase() == "input" || + aElement.tagName.toLowerCase() == "textarea") { + aElement.value = aValue; + return; + } + aElement.innerHTML = aValue === "" ? "<br>" : aValue; + } + + for (const command of [ + "cmd_paste", + "cmd_pasteNoFormatting", + "cmd_pasteQuote", + "cmd_pasteTransferable" + ]) { + for (const editableSelector of [ + "#src > input", + "#src > textarea", + "#src > div[contenteditable]" + ]) { + const editableElement = document.querySelector(editableSelector); + const editableElementDesc = `<${ + editableElement.tagName.toLocaleLowerCase() + }${editableElement.hasAttribute("contenteditable") ? " contenteditable" : ""}>`; + (test_from_editableElement_to_input => { + const input = document.querySelector("#dest > input"); + editableElement.focus(); + editableElement.addEventListener( + "paste", + () => input.focus(), + {once: true} + ); + SpecialPowers.doCommand(window, command, transferable); + is( + getValue(editableElement).replace(/\n/g, ""), + "", + `${command}: ${ + editableElementDesc + } should not have the pasted text because focus is redirected to <input> in a "paste" event listener` + ); + is( + input.value.replace("> ", ""), + "plain text", + `${command}: new focused <input> (moved from ${ + editableElementDesc + }) should have the pasted text` + ); + setValue(editableElement, ""); + input.value = ""; + })(); + + (test_from_editableElement_to_contenteditable => { + const contentEditable = document.querySelector("#dest > div[contenteditable]"); + editableElement.focus(); + editableElement.addEventListener( + "paste", + () => contentEditable.focus(), + {once: true} + ); + SpecialPowers.doCommand(window, command, transferable); + is( + getValue(editableElement).replace(/\n/g, ""), + "", + `${command}: ${ + editableElementDesc + } should not have the pasted text because focus is redirected to <div contenteditable> in a "paste" event listener` + ); + is( + contentEditable.textContent.replace(/\n/g, "").replace("> ", ""), + "plain text", + `${command}: new focused <div contenteditable> (moved from ${ + editableElementDesc + }) should have the pasted text` + ); + setValue(editableElement, ""); + contentEditable.innerHTML = "<br>"; + })(); + + (test_from_editableElement_to_non_editable => { + const button = document.querySelector("#dest > button"); + editableElement.focus(); + editableElement.addEventListener( + "paste", + () => button.focus(), + {once: true} + ); + SpecialPowers.doCommand(window, command, transferable); + is( + getValue(editableElement).replace(/\n/g, ""), + "", + `${command}: ${ + editableElementDesc + } should not have the pasted text because focus is redirected to <button> in a "paste" event listener` + ); + setValue(editableElement, ""); + })(); + } + } + + SimpleTest.finish(); +}); +</script> +</head> +<body> +<div id="src"><input><textarea></textarea><div contenteditable><br></div></div> +<div id="dest"><input><div contenteditable><br></div><button>button</button></div> +</body> +</html> |