drop-link.html 3.3 KB
Newer Older
ggaren's avatar
ggaren committed
1 2 3 4
<script>
if (window.layoutTestController)
     layoutTestController.dumpEditingCallbacks();
</script>
lweintraub's avatar
lweintraub committed
5 6 7
<div>This test checks that links dragged into editable regions preserve their title and URL.</div>

<div contenteditable="true" id="destination">Hello</div>
8
<div contenteditable="true" id="destination2">Goodbye</div>
lweintraub's avatar
lweintraub committed
9 10

<div><a href="http://apple.com" id="grabme">This is a link</a></div>
11
<div><a href="http://apple.com" id="grabme2">This &lt;/a&gt;&lt;a href="http://webkit.org/"&gt;is another link</a></div>
lweintraub's avatar
lweintraub committed
12 13 14

<ul id="console"></ul>

15 16
<div>To run this test manually, drag the first link into the box with 'Hello' in it,
drag the second link into the box with 'Goodbye' in it, and then click on the button.<br><input type="button" value="Verify" onClick="verifier()"></div>
lweintraub's avatar
lweintraub committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

<script>

function log(message) {
    var console = document.getElementById("console");
    var li = document.createElement("li");
    var pre = document.createElement("pre");
    pre.appendChild(document.createTextNode(message));
    li.appendChild(pre);
    console.appendChild(li);
}

function runTest() {
    if (!window.layoutTestController)
        return;
32 33 34 35 36 37 38 39 40 41 42 43 44
    layoutTestController.dumpAsText();

    dragLink('grabme', 'destination');
    dragLink('grabme2', 'destination2');

    verifier();
}

function dragLink(srcId, targetId)
{
    var anchorToDrag = document.getElementById(srcId);
    window.getSelection().setBaseAndExtent(anchorToDrag, 0, anchorToDrag, 0);

lweintraub's avatar
lweintraub committed
45 46 47 48 49 50 51 52
    var x = anchorToDrag.offsetLeft + anchorToDrag.offsetWidth / 2;
    var y = anchorToDrag.offsetTop + anchorToDrag.offsetHeight / 2;

    eventSender.mouseMoveTo(x, y);

    eventSender.mouseDown();
    // Wait a moment so that the mouseDown will kick off a drag instead of navigating to the link
    eventSender.leapForward(400);
53 54

    var destinationObject = document.getElementById(targetId);
lweintraub's avatar
lweintraub committed
55 56 57 58 59 60 61 62 63
    var x = destinationObject.offsetLeft + destinationObject.offsetWidth / 2;
    var y = destinationObject.offsetTop + destinationObject.offsetHeight / 2;

    eventSender.mouseMoveTo(x, y);
    eventSender.mouseUp();
}

function verifier() {
    var anchorToDrag = document.getElementById("grabme");
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    var anchorToDrag2 = document.getElementById("grabme2");

    var passed = verifyLink('grabme', 0) && verifyLink('grabme2', 1);

    var anchors = document.getElementsByTagName("a");
    if (anchors.length != 2) {
        log("Failure, incorrect number of anchor tags present");
        passed = false;
    }

    if (passed)
        log("Success!");
}

function verifyLink(linkId, linkNumber)
{
    var anchorToDrag = document.getElementById(linkId);
lweintraub's avatar
lweintraub committed
81 82 83 84 85

    var originalTarget = anchorToDrag.href;
    var originalText = anchorToDrag.innerText;
    var anchorParent = anchorToDrag.parentNode;
    anchorParent.removeChild(anchorToDrag);
86

lweintraub's avatar
lweintraub committed
87
    var anchors = document.getElementsByTagName("a");
88 89 90
    if (!anchors[linkNumber]) {
        log("Link " + linkNumber + " not found");
        passed = false;
lweintraub's avatar
lweintraub committed
91
    } else {
92
        log("Link is " + anchors[linkNumber].href);
lweintraub's avatar
lweintraub committed
93
        var passed = true;
94
        if (anchors[linkNumber].href != originalTarget) {
lweintraub's avatar
lweintraub committed
95 96 97
            log("Failure! Link targets differ");
            passed = false;
        }
98
        if (anchors[linkNumber].innerText != originalText) {
lweintraub's avatar
lweintraub committed
99 100 101 102
            log("Failure! Link texts differ");
            passed = false;
        }
    }
103
    return passed;
lweintraub's avatar
lweintraub committed
104 105 106 107
}

runTest();
</script>