summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/editing/selection/script-tests/doubleclick-inline-first-last-contenteditable.js
blob: ee655dc072aff2b25fb72356950309095c1c2e39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
description("Test to check if last and first word can be selected when they double-clicked (Bug 36359)");

function toLiteral(textValue)
{
    return "'" + textValue + "'";
}

function getPositionOfNode(node)
{
    var n = node;
    var offsetX = 0, offsetY = 0;

    while (n) {
        offsetX += n.offsetLeft;
        offsetY += n.offsetTop;
        n = n.offsetParnet;
    }

    return {
      "x":  offsetX,
      "y":  offsetY
    };
}

function doubleClickPosition(pos)
{
    eventSender.mouseMoveTo(pos.x, pos.y);
    eventSender.mouseDown();
    eventSender.mouseUp();
    eventSender.mouseDown();
    eventSender.mouseUp();
}

function singleClickPosition(pos)
{
    eventSender.mouseMoveTo(pos.x, pos.y);
    eventSender.mouseDown();
    eventSender.mouseUp();
}

var targetRoot = document.createElement("div");
document.body.appendChild(targetRoot);

var scratchRoot = document.createElement("div");
document.body.appendChild(scratchRoot);

function computeTextSize(text)
{
    scratchRoot.innerHTML = ("<span id='scratch'>" + text + "</span>");
    var scratch = document.getElementById("scratch");
    return { width: scratch.offsetWidth, height: scratch.offsetHeight };
}

function testWithDoublleClick(targetInnerHTML, expectedText)
{
    var textSize = computeTextSize(expectedText);

    targetRoot.innerHTML = targetInnerHTML;
    var target = document.getElementById("target");
    var nodePosition = getPositionOfNode(target);
    doubleClickPosition({ x: nodePosition.x + textSize.width / 2, y: nodePosition.y + textSize.height / 2 });
    shouldBe("window.getSelection().toString()", toLiteral(expectedText));
}

function testWithClickAndModify(targetInnerHTML, expectedText)
{
    var selection = window.getSelection();
    targetRoot.innerHTML = targetInnerHTML;
    var target = document.getElementById("target");
    singleClickPosition(getPositionOfNode(target));

    selection.modify("extend", "forward", "word");
    var lastHalf = window.getSelection().toString();
    selection.modify("extend", "backward", "word");
    var firstHalf = window.getSelection().toString();

    window.selectedByModify = firstHalf + lastHalf;
    shouldBe("window.selectedByModify", toLiteral(expectedText));
}

testRunner.setSelectTrailingWhitespaceEnabled(false);

var shouldSelecteFirstWordInline = "<span id='target' contentEditable>selectme1</span> and not select us";
testWithDoublleClick(shouldSelecteFirstWordInline, "selectme1");
testWithClickAndModify(shouldSelecteFirstWordInline, "selectme1");

var shouldSelectLastWordInline = "you should ignore us but <span id='target' contentEditable>selectme2</span>";
testWithDoublleClick(shouldSelectLastWordInline, "selectme2");
testWithClickAndModify(shouldSelectLastWordInline, "selectme2");

var shouldSelectMiddleWordInline = "you should get <span id='target' contentEditable>selectme3</span> selected";
testWithDoublleClick(shouldSelectMiddleWordInline, "selectme3");
testWithClickAndModify(shouldSelectMiddleWordInline, "selectme3");

var shouldSelecteFirstWordBlock = "<div id='target' contentEditable>selectme4</div> and not select us";
testWithDoublleClick(shouldSelecteFirstWordBlock, "selectme4");
testWithClickAndModify(shouldSelecteFirstWordBlock, "selectme4");

var shouldSelectLastWordBlock = "you should ignore us but <div id='target' contentEditable>selectme5</div>";
testWithDoublleClick(shouldSelectLastWordBlock, "selectme5");
testWithClickAndModify(shouldSelectLastWordBlock, "selectme5");

var shouldSelectMiddleWordBlock = "you should get <div id='target' contentEditable>selectme6</div> selected";
testWithDoublleClick(shouldSelectMiddleWordBlock, "selectme6");
testWithClickAndModify(shouldSelectMiddleWordBlock, "selectme6");

targetRoot.style.display = "none";
scratchRoot.style.display = "none";

var successfullyParsed = true;