test_text_selection.html (4725B)
1 <!DOCTYPE html> 2 <html> 3 <meta charset=utf-8> 4 <!-- 5 https://bugzilla.mozilla.org/show_bug.cgi?id=655877 6 --> 7 <head> 8 <title>Test for Bug 655877</title> 9 <script src="/tests/SimpleTest/SimpleTest.js"></script> 10 <script src="/tests/SimpleTest/EventUtils.js"></script> 11 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 12 </head> 13 <body> 14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=655877">Mozilla Bug 655877</a> 15 <p id="display"></p> 16 <div id="content" style="display: none"></div> 17 18 <iframe src="text-helper-selection.svg" width="400" height="300"></iframe> 19 20 <pre id="test"> 21 <script class="testbody" type="application/javascript"> 22 SimpleTest.waitForExplicitFinish(); 23 24 var svg, doc, win, dragstart, dragend; 25 26 function drag(fromX, fromY, toX, toY, show) { 27 synthesizeMouse(doc.documentElement, fromX, fromY, { type: "mousemove" }, win); 28 synthesizeMouse(doc.documentElement, fromX, fromY, { type: "mousedown" }, win); 29 synthesizeMouse(doc.documentElement, toX, toY, { type: "mousemove" }, win); 30 synthesizeMouse(doc.documentElement, toX, toY, { type: "mouseup" }, win); 31 32 if (show) { 33 dragstart.setAttribute("cx", fromX); 34 dragstart.setAttribute("cy", fromY); 35 dragstart.setAttribute("r", "4"); 36 dragend.setAttribute("cx", toX); 37 dragend.setAttribute("cy", toY); 38 dragend.setAttribute("r", "4"); 39 } 40 } 41 42 function click(x, y) { 43 synthesizeMouse(doc.documentElement, x, y, { type: "mousemove" }, win); 44 synthesizeMouse(doc.documentElement, x, y, { type: "mousedown" }, win); 45 synthesizeMouse(doc.documentElement, x, y, { type: "mouseup" }, win); 46 } 47 48 function selection_is(s, text) { 49 is(win.getSelection().toString(), s, text); 50 } 51 52 function deselect() { 53 // Click outside text (and outside all <rect> elements>) to deselect. 54 click(15, 15); 55 selection_is("", "deselecting by clicking outside text"); 56 } 57 58 function testSelection() { 59 svg = document.getElementsByTagName("iframe")[0]; 60 doc = svg.contentDocument; 61 win = svg.contentWindow; 62 dragstart = doc.getElementById("dragstart"); 63 dragend = doc.getElementById("dragend"); 64 65 var text = doc.getElementsByTagName("text"); 66 67 // Drag to select the entire text element. 68 drag(101, 50, 99 + text[0].getComputedTextLength(), 50); 69 selection_is("hello there", "selecting entire simple text"); 70 71 // Click within the text to deselect. 72 click(101, 50); 73 selection_is("", "deselecting by clicking on text"); 74 75 // Drag to select part of a text element. 76 drag(101, 50, 99 + text[0].getSubStringLength(0, 5), 50); 77 selection_is("hello", "selecting part of simple text"); 78 deselect(); 79 80 // Drag from left of the text to the right of the text to select it. 81 drag(101, 50, 99 + text[0].getComputedTextLength(), 50); 82 selection_is("hello there", "selecting entire simple text by dragging around it"); 83 deselect(); 84 85 // Drag above the text to select part of it. 86 var bbox1 = text[0].getBBox(); 87 drag(101 + text[0].getSubStringLength(0, 6), bbox1.y - 10, 101 + text[0].getSubStringLength(0, 9), bbox1.y - 10); 88 selection_is("the", "selecting part of simple text by dragging above it"); 89 deselect(); 90 91 // Drag between the first and second texts, but closer to the first. 92 var bbox2 = text[1].getBBox(); 93 var mid = (bbox1.y + bbox1.height + bbox2.y) / 2; 94 drag(101, mid - 10, 99 + text[0].getSubStringLength(0, 2), mid - 10); 95 selection_is("he", "selecting closer text above"); 96 deselect(); 97 98 // Drag between the first and second texts, but closer to the second. 99 drag(101, mid + 10, 99 + text[1].getSubStringLength(0, 2), mid + 10); 100 selection_is("to", "selecting closer text below"); 101 deselect(); 102 103 // Drag starting in the first text and ending in the second. 104 drag(101 + text[0].getSubStringLength(0, 6), 50, 99 + text[1].getSubStringLength(0, 2), 100); 105 selection_is("there to", "selecting from first to second text"); 106 deselect(); 107 108 // Select across positioned glyphs. 109 drag(99 + text[2].getSubStringLength(3, 1), 150, 201, 150); 110 selection_is("abcd", "selecting across positioned glyphs"); 111 deselect(); 112 113 // Select bidi text, from the left of the "א" to the left of the "b". 114 drag(text[3].getExtentOfChar(0).x + 1, 200, text[3].getExtentOfChar(4).x + 1, 200); 115 selection_is("בגa", "selecting bidi text"); 116 deselect(); 117 118 // Select transformed text. 119 drag(101, 250, 99 + text[4].getSubStringLength(0, 6) / 2, 250); 120 selection_is("squash"); 121 deselect(); 122 123 SimpleTest.finish(); 124 } 125 126 function runTest() { 127 SimpleTest.executeSoon(testSelection); 128 } 129 130 if (/Android/.test(navigator.userAgent)) { 131 ok(true, "No need to test text selection with the mouse on Android."); 132 SimpleTest.finish(); 133 } else { 134 window.addEventListener("load", runTest); 135 } 136 </script> 137 </pre> 138 </body> 139 </html>