browser_contextmenu_contenteditable.js (3374B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 let contextMenu; 7 8 const example_base = 9 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 10 "http://example.com/browser/browser/base/content/test/contextMenu/"; 11 const chrome_base = 12 "chrome://mochitests/content/browser/browser/base/content/test/contextMenu/"; 13 14 /* import-globals-from contextmenu_common.js */ 15 Services.scriptloader.loadSubScript( 16 chrome_base + "contextmenu_common.js", 17 this 18 ); 19 20 const askChatMenu = [ 21 "context-ask-chat", 22 true, 23 // Need a blank entry here because the Ask Chat submenu is dynamically built with no ids. 24 "", 25 null, 26 "---", 27 null, 28 ]; 29 30 async function openMenuAndPaste(browser, useFormatting) { 31 const kElementToUse = "test-contenteditable-spellcheck-false"; 32 let oldText = await SpecialPowers.spawn(browser, [kElementToUse], elemID => { 33 return content.document.getElementById(elemID).textContent; 34 }); 35 36 // Open context menu and paste 37 await test_contextmenu( 38 "#" + kElementToUse, 39 [ 40 "context-undo", 41 null, // whether we can undo changes mid-test. 42 "context-redo", 43 false, 44 "---", 45 null, 46 "context-cut", 47 false, 48 "context-copy", 49 false, 50 "context-paste", 51 true, 52 "context-paste-no-formatting", 53 true, 54 "context-delete", 55 false, 56 "context-selectall", 57 true, 58 "---", 59 null, 60 ...askChatMenu, 61 ], 62 { 63 keepMenuOpen: true, 64 awaitOnMenuBuilt: { 65 id: "context-ask-chat", 66 }, 67 } 68 ); 69 let popupHidden = BrowserTestUtils.waitForPopupEvent(contextMenu, "hidden"); 70 let menuID = "context-paste" + (useFormatting ? "" : "-no-formatting"); 71 contextMenu.activateItem(document.getElementById(menuID)); 72 await popupHidden; 73 await SpecialPowers.spawn( 74 browser, 75 [kElementToUse, oldText, useFormatting], 76 (elemID, textToReset, expectStrong) => { 77 let node = content.document.getElementById(elemID); 78 Assert.stringContains( 79 node.textContent, 80 "Bold text", 81 "Text should have been pasted" 82 ); 83 if (expectStrong) { 84 isnot( 85 node.querySelector("strong"), 86 null, 87 "Should be markup in the text." 88 ); 89 } else { 90 is( 91 node.querySelector("strong"), 92 null, 93 "Should be no markup in the text." 94 ); 95 } 96 node.textContent = textToReset; 97 } 98 ); 99 } 100 101 add_task(async function test_contenteditable() { 102 // Put some HTML on the clipboard: 103 const xferable = Cc["@mozilla.org/widget/transferable;1"].createInstance( 104 Ci.nsITransferable 105 ); 106 xferable.init(null); 107 xferable.addDataFlavor("text/html"); 108 xferable.setTransferData( 109 "text/html", 110 PlacesUtils.toISupportsString("<strong>Bold text</strong>") 111 ); 112 xferable.addDataFlavor("text/plain"); 113 xferable.setTransferData( 114 "text/plain", 115 PlacesUtils.toISupportsString("Bold text") 116 ); 117 Services.clipboard.setData( 118 xferable, 119 null, 120 Services.clipboard.kGlobalClipboard 121 ); 122 123 let url = example_base + "subtst_contextmenu.html"; 124 await BrowserTestUtils.withNewTab( 125 { 126 gBrowser, 127 url, 128 }, 129 async function (browser) { 130 await openMenuAndPaste(browser, false); 131 await openMenuAndPaste(browser, true); 132 } 133 ); 134 });