michael@0: let doc, range, selection; michael@0: function setSelection(el1, el2, index1, index2) { michael@0: while (el1.nodeType != Node.TEXT_NODE) michael@0: el1 = el1.firstChild; michael@0: while (el2.nodeType != Node.TEXT_NODE) michael@0: el2 = el2.firstChild; michael@0: michael@0: selection.removeAllRanges(); michael@0: range.setStart(el1, index1); michael@0: range.setEnd(el2, index2); michael@0: selection.addRange(range); michael@0: } michael@0: michael@0: function initContextMenu(aNode) { michael@0: document.popupNode = aNode; michael@0: let contentAreaContextMenu = document.getElementById("contentAreaContextMenu"); michael@0: let contextMenu = new nsContextMenu(contentAreaContextMenu); michael@0: return contextMenu; michael@0: } michael@0: michael@0: function testExpected(expected, msg, aNode) { michael@0: let popupNode = aNode || doc.getElementsByTagName("DIV")[0]; michael@0: initContextMenu(popupNode); michael@0: let linkMenuItem = document.getElementById("context-openlinkincurrent"); michael@0: is(linkMenuItem.hidden, expected, msg); michael@0: } michael@0: michael@0: function testLinkExpected(expected, msg, aNode) { michael@0: let popupNode = aNode || doc.getElementsByTagName("DIV")[0]; michael@0: let contextMenu = initContextMenu(popupNode); michael@0: is(contextMenu.linkURL, expected, msg); michael@0: } michael@0: michael@0: function runSelectionTests() { michael@0: let mainDiv = doc.createElement("div"); michael@0: let div = doc.createElement("div"); michael@0: let div2 = doc.createElement("div"); michael@0: let span1 = doc.createElement("span"); michael@0: let span2 = doc.createElement("span"); michael@0: let span3 = doc.createElement("span"); michael@0: let span4 = doc.createElement("span"); michael@0: let p1 = doc.createElement("p"); michael@0: let p2 = doc.createElement("p"); michael@0: span1.textContent = "http://index."; michael@0: span2.textContent = "example.com example.com"; michael@0: span3.textContent = " - Test"; michael@0: span4.innerHTML = "http://www.example.com/example"; michael@0: p1.textContent = "mailto:test.com ftp.example.com"; michael@0: p2.textContent = "example.com -"; michael@0: div.appendChild(span1); michael@0: div.appendChild(span2); michael@0: div.appendChild(span3); michael@0: div.appendChild(span4); michael@0: div.appendChild(p1); michael@0: div.appendChild(p2); michael@0: let p3 = doc.createElement("p"); michael@0: p3.textContent = "main.example.com"; michael@0: div2.appendChild(p3); michael@0: mainDiv.appendChild(div); michael@0: mainDiv.appendChild(div2); michael@0: doc.body.appendChild(mainDiv); michael@0: setSelection(span1.firstChild, span2.firstChild, 0, 11); michael@0: testExpected(false, "The link context menu should show for http://www.example.com"); michael@0: setSelection(span1.firstChild, span2.firstChild, 7, 11); michael@0: testExpected(false, "The link context menu should show for www.example.com"); michael@0: setSelection(span1.firstChild, span2.firstChild, 8, 11); michael@0: testExpected(true, "The link context menu should not show for ww.example.com"); michael@0: setSelection(span2.firstChild, span2.firstChild, 0, 11); michael@0: testExpected(false, "The link context menu should show for example.com"); michael@0: testLinkExpected("http://example.com/", "url for example.com selection should not prepend www"); michael@0: setSelection(span2.firstChild, span2.firstChild, 11, 23); michael@0: testExpected(false, "The link context menu should show for example.com"); michael@0: setSelection(span2.firstChild, span2.firstChild, 0, 10); michael@0: testExpected(true, "Link options should not show for selection that's not at a word boundary"); michael@0: setSelection(span2.firstChild, span3.firstChild, 12, 7); michael@0: testExpected(true, "Link options should not show for selection that has whitespace"); michael@0: setSelection(span2.firstChild, span2.firstChild, 12, 19); michael@0: testExpected(true, "Link options should not show unless a url is selected"); michael@0: setSelection(p1.firstChild, p1.firstChild, 0, 15); michael@0: testExpected(true, "Link options should not show for mailto: links"); michael@0: setSelection(p1.firstChild, p1.firstChild, 16, 31); michael@0: testExpected(false, "Link options should show for ftp.example.com"); michael@0: testLinkExpected("ftp://ftp.example.com/", "ftp.example.com should be preceeded with ftp://"); michael@0: setSelection(p2.firstChild, p2.firstChild, 0, 14); michael@0: testExpected(false, "Link options should show for www.example.com "); michael@0: selection.selectAllChildren(div2); michael@0: testExpected(false, "Link options should show for triple-click selections"); michael@0: selection.selectAllChildren(span4); michael@0: testLinkExpected("http://www.example.com/", "Linkified text should open the correct link", span4.firstChild); michael@0: michael@0: mainDiv.innerHTML = "(open-suse.ru)"; michael@0: setSelection(mainDiv, mainDiv, 1, 13); michael@0: testExpected(false, "Link options should show for open-suse.ru"); michael@0: testLinkExpected("http://open-suse.ru/", "Linkified text should open the correct link"); michael@0: setSelection(mainDiv, mainDiv, 1, 14); michael@0: testExpected(true, "Link options should not show for 'open-suse.ru)'"); michael@0: michael@0: gBrowser.removeCurrentTab(); michael@0: finish(); michael@0: } michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: gBrowser.selectedTab = gBrowser.addTab(); michael@0: gBrowser.selectedBrowser.addEventListener("load", function() { michael@0: gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true); michael@0: doc = content.document; michael@0: range = doc.createRange(); michael@0: selection = content.getSelection(); michael@0: waitForFocus(runSelectionTests, content); michael@0: }, true); michael@0: michael@0: content.location = michael@0: "data:text/html;charset=UTF-8,Test For Non-Hyperlinked url selection"; michael@0: }