1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/base/content/test/general/browser_plainTextLinks.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,115 @@ 1.4 +let doc, range, selection; 1.5 +function setSelection(el1, el2, index1, index2) { 1.6 + while (el1.nodeType != Node.TEXT_NODE) 1.7 + el1 = el1.firstChild; 1.8 + while (el2.nodeType != Node.TEXT_NODE) 1.9 + el2 = el2.firstChild; 1.10 + 1.11 + selection.removeAllRanges(); 1.12 + range.setStart(el1, index1); 1.13 + range.setEnd(el2, index2); 1.14 + selection.addRange(range); 1.15 +} 1.16 + 1.17 +function initContextMenu(aNode) { 1.18 + document.popupNode = aNode; 1.19 + let contentAreaContextMenu = document.getElementById("contentAreaContextMenu"); 1.20 + let contextMenu = new nsContextMenu(contentAreaContextMenu); 1.21 + return contextMenu; 1.22 +} 1.23 + 1.24 +function testExpected(expected, msg, aNode) { 1.25 + let popupNode = aNode || doc.getElementsByTagName("DIV")[0]; 1.26 + initContextMenu(popupNode); 1.27 + let linkMenuItem = document.getElementById("context-openlinkincurrent"); 1.28 + is(linkMenuItem.hidden, expected, msg); 1.29 +} 1.30 + 1.31 +function testLinkExpected(expected, msg, aNode) { 1.32 + let popupNode = aNode || doc.getElementsByTagName("DIV")[0]; 1.33 + let contextMenu = initContextMenu(popupNode); 1.34 + is(contextMenu.linkURL, expected, msg); 1.35 +} 1.36 + 1.37 +function runSelectionTests() { 1.38 + let mainDiv = doc.createElement("div"); 1.39 + let div = doc.createElement("div"); 1.40 + let div2 = doc.createElement("div"); 1.41 + let span1 = doc.createElement("span"); 1.42 + let span2 = doc.createElement("span"); 1.43 + let span3 = doc.createElement("span"); 1.44 + let span4 = doc.createElement("span"); 1.45 + let p1 = doc.createElement("p"); 1.46 + let p2 = doc.createElement("p"); 1.47 + span1.textContent = "http://index."; 1.48 + span2.textContent = "example.com example.com"; 1.49 + span3.textContent = " - Test"; 1.50 + span4.innerHTML = "<a href='http://www.example.com'>http://www.example.com/example</a>"; 1.51 + p1.textContent = "mailto:test.com ftp.example.com"; 1.52 + p2.textContent = "example.com -"; 1.53 + div.appendChild(span1); 1.54 + div.appendChild(span2); 1.55 + div.appendChild(span3); 1.56 + div.appendChild(span4); 1.57 + div.appendChild(p1); 1.58 + div.appendChild(p2); 1.59 + let p3 = doc.createElement("p"); 1.60 + p3.textContent = "main.example.com"; 1.61 + div2.appendChild(p3); 1.62 + mainDiv.appendChild(div); 1.63 + mainDiv.appendChild(div2); 1.64 + doc.body.appendChild(mainDiv); 1.65 + setSelection(span1.firstChild, span2.firstChild, 0, 11); 1.66 + testExpected(false, "The link context menu should show for http://www.example.com"); 1.67 + setSelection(span1.firstChild, span2.firstChild, 7, 11); 1.68 + testExpected(false, "The link context menu should show for www.example.com"); 1.69 + setSelection(span1.firstChild, span2.firstChild, 8, 11); 1.70 + testExpected(true, "The link context menu should not show for ww.example.com"); 1.71 + setSelection(span2.firstChild, span2.firstChild, 0, 11); 1.72 + testExpected(false, "The link context menu should show for example.com"); 1.73 + testLinkExpected("http://example.com/", "url for example.com selection should not prepend www"); 1.74 + setSelection(span2.firstChild, span2.firstChild, 11, 23); 1.75 + testExpected(false, "The link context menu should show for example.com"); 1.76 + setSelection(span2.firstChild, span2.firstChild, 0, 10); 1.77 + testExpected(true, "Link options should not show for selection that's not at a word boundary"); 1.78 + setSelection(span2.firstChild, span3.firstChild, 12, 7); 1.79 + testExpected(true, "Link options should not show for selection that has whitespace"); 1.80 + setSelection(span2.firstChild, span2.firstChild, 12, 19); 1.81 + testExpected(true, "Link options should not show unless a url is selected"); 1.82 + setSelection(p1.firstChild, p1.firstChild, 0, 15); 1.83 + testExpected(true, "Link options should not show for mailto: links"); 1.84 + setSelection(p1.firstChild, p1.firstChild, 16, 31); 1.85 + testExpected(false, "Link options should show for ftp.example.com"); 1.86 + testLinkExpected("ftp://ftp.example.com/", "ftp.example.com should be preceeded with ftp://"); 1.87 + setSelection(p2.firstChild, p2.firstChild, 0, 14); 1.88 + testExpected(false, "Link options should show for www.example.com "); 1.89 + selection.selectAllChildren(div2); 1.90 + testExpected(false, "Link options should show for triple-click selections"); 1.91 + selection.selectAllChildren(span4); 1.92 + testLinkExpected("http://www.example.com/", "Linkified text should open the correct link", span4.firstChild); 1.93 + 1.94 + mainDiv.innerHTML = "(open-suse.ru)"; 1.95 + setSelection(mainDiv, mainDiv, 1, 13); 1.96 + testExpected(false, "Link options should show for open-suse.ru"); 1.97 + testLinkExpected("http://open-suse.ru/", "Linkified text should open the correct link"); 1.98 + setSelection(mainDiv, mainDiv, 1, 14); 1.99 + testExpected(true, "Link options should not show for 'open-suse.ru)'"); 1.100 + 1.101 + gBrowser.removeCurrentTab(); 1.102 + finish(); 1.103 +} 1.104 + 1.105 +function test() { 1.106 + waitForExplicitFinish(); 1.107 + gBrowser.selectedTab = gBrowser.addTab(); 1.108 + gBrowser.selectedBrowser.addEventListener("load", function() { 1.109 + gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true); 1.110 + doc = content.document; 1.111 + range = doc.createRange(); 1.112 + selection = content.getSelection(); 1.113 + waitForFocus(runSelectionTests, content); 1.114 + }, true); 1.115 + 1.116 + content.location = 1.117 + "data:text/html;charset=UTF-8,Test For Non-Hyperlinked url selection"; 1.118 +}