summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/input-events/input-events-cut-paste.html
blob: f2ca5a0a654f270d33d8662f4e8b25369cd98df7 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<!DOCTYPE html>
<meta charset="utf-8">
<title>Cut and Paste should trigger corresponding InputEvent</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script type="text/javascript" src="pointerevent_support.js"></script>
<p>To manually run this test, please follow the steps below:<br/>
1. Select 'plain' => Cut (e.g. Ctrl/Cmd-X) => Paste (e.g. Ctrl/Cmd-V).<br/>
2. Select 'rich' => Cut => Paste.<br/>
3. Select 'prevent' => Paste.<br/>
4. Select 'prevent' => Cut => Select 'normal' => Paste.<br/>
<br/>
If a "PASS" result appears the test passes, otherwise it fails</p>
<textarea id="test1_plain">plain</textarea>
<p id="test2_editable" contenteditable><b>rich</b></p>
<p id="test3_editable_prevent" contenteditable>prevent</p>
<p id="test3_editable_normal" contenteditable>normal</p>
<script>

function resolveWhen(condition) {
  return new Promise((resolve, reject) => {
    function tick() {
      if (condition())
        resolve();
      else
        requestAnimationFrame(tick.bind(this));
    }
    tick();
  });
}

let modifier_key = "\uE009";
if(navigator.platform.includes('Mac'))
    modifier_key = "\uE03D";
const commands = {
    COPY: 'copy',
    CUT: 'cut',
    PASTE: 'paste',
}
function selectTextCommand(target, command) {
  let command_key = "";
  if(command == "copy")
    command_key = "c";
  else if (command == "cut")
    command_key = "x";
  else if (command == "paste")
    command_key = "v";
  return new test_driver.Actions()
      .pointerMove(0, 0, {origin: target})
      .pointerDown()
      .pointerUp()
      .addTick()
      .keyDown(modifier_key)
      .keyDown("a")
      .keyUp("a")
      .keyDown(command_key)
      .keyUp(command_key)
      .keyUp(modifier_key)
      .send();
}

function selectTextCutAndPaste(target1, target2) {
  return selectTextCommand(target1, commands.CUT).then(() => {
    return selectTextCommand(target2, commands.PASTE);
  })
}

promise_test(async test => {
  const expectedEventLog = [
        'cut-[null]', 'beforeinput-deleteByCut', 'input-deleteByCut',
        'paste-[null]', 'beforeinput-insertFromPaste', 'input-insertFromPaste'];
  const actualEventLog = [];
  const text1 = document.getElementById("test1_plain");

  for (let eventType of ['beforeinput', 'input', 'cut', 'paste']) {
      text1.addEventListener(eventType, test.step_func(function() {
          if (event.type === 'beforeinput' && event.inputType === 'insertFromPaste') {
              assert_equals(event.data, 'plain');
              assert_equals(event.dataTransfer, null);
          }

          actualEventLog.push(`${event.type}-${event.inputType || '[null]'}`);
      }));
  }
  await selectTextCutAndPaste(text1, text1);
  await resolveWhen(() => { return actualEventLog.length == expectedEventLog.length });
  assert_array_equals(actualEventLog, expectedEventLog,
                      `Expected: ${expectedEventLog}; Actual: ${actualEventLog}.`);
}, 'Event order and data on textarea.');

promise_test(async test => {
    const expectedEventLog = [
        'cut-[null]', 'beforeinput-deleteByCut', 'input-deleteByCut',
        'paste-[null]', 'beforeinput-insertFromPaste', 'input-insertFromPaste'];
    const actualEventLog = [];
    const text2 = document.getElementById("test2_editable");

    for (let eventType of ['beforeinput', 'input', 'cut', 'paste']) {
        text2.addEventListener(eventType, test.step_func(function() {
            if (event.type === 'beforeinput' && event.inputType === 'insertFromPaste') {
                assert_equals(event.data, null);
                assert_equals(event.dataTransfer.getData('text/plain'), 'rich');
                assert_regexp_match(event.dataTransfer.getData('text/html'), /<b.*>rich<\/b>$/);
            }

            actualEventLog.push(`${event.type}-${event.inputType || '[null]'}`);
        }));
    }
    await selectTextCutAndPaste(text2, text2);
    await resolveWhen(() => { return actualEventLog.length == expectedEventLog.length });
    assert_array_equals(actualEventLog, expectedEventLog,
                        `Expected: ${expectedEventLog}; Actual: ${actualEventLog}.`);
}, 'Event order and dataTransfer on contenteditable.');

promise_test(async test => {
    const prevent = document.getElementById('test3_editable_prevent');
    const normal = document.getElementById('test3_editable_normal');
    prevent.addEventListener('beforeinput', test.step_func(function() {
        if (event.inputType === 'deleteByCut' ||
            event.inputType === 'insertFromPaste') {
            event.preventDefault();
        }
    }));

    normal.addEventListener('input', test.step_func(function() {
        if (event.inputType === 'insertFromPaste') {
            assert_equals(prevent.textContent, 'prevent');
            assert_equals(normal.textContent, 'prevent');
        }
    }));

    await selectTextCommand(prevent, commands.PASTE);
    await selectTextCutAndPaste(prevent, normal);
    await resolveWhen(() => { return normal.textContent == 'prevent' });
    assert_equals(prevent.textContent, 'prevent');
    assert_equals(normal.textContent, 'prevent');
}, 'preventDefault() should prevent DOM modification but allow clipboard updates.');
</script>