forked from burg/timelapse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
onchange-text-form-field.html
102 lines (90 loc) · 3.88 KB
/
onchange-text-form-field.html
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
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
<body>
<div id="test">
<input type="text">
<input type="text"value="foo">
<textarea></textarea>
<textarea>foo</textarea>
<input type="text">
</div>
<div id="console"></div>
</body>
<script>
var BEFORE = 1;
var AFTER = 2;
function simulateTextEntry(element, text, opt_clear)
{
if (!window.eventSender)
return null;
var firedEvent = false;
function listener(event) {
firedEvent = true;
}
element.addEventListener('change', listener, false);
element.focus();
if (opt_clear & BEFORE) {
element.select();
eventSender.keyDown('delete');
}
for (var i = 0; i < text.length; i++) {
eventSender.keyDown(text.charAt(i));
}
if (opt_clear & AFTER) {
element.select();
eventSender.keyDown('delete');
}
element.blur();
element.removeEventListener('change', listener, false);
return firedEvent;
}
function setTextValue(element, text)
{
var firedEvent = false;
function listener(event) {
firedEvent = true;
}
element.addEventListener('change', listener, false);
element.focus();
element.value = text;
element.blur();
element.removeEventListener('change', listener, false);
return firedEvent;
}
var elements = document.getElementById('test').getElementsByTagName('*');
shouldBe("simulateTextEntry(elements[0], '', BEFORE);", "false");
shouldBe("simulateTextEntry(elements[0], 'fo', BEFORE);", "true");
shouldBe("simulateTextEntry(elements[0], 'o');", "true");
shouldBe("simulateTextEntry(elements[0], 'foo', BEFORE);", "false");
shouldBe("simulateTextEntry(elements[0], 'foo', BEFORE);", "false");
shouldBe("simulateTextEntry(elements[0], ' ');", "true");
shouldBe("simulateTextEntry(elements[0], 'foo bar', BEFORE);", "true");
shouldBe("simulateTextEntry(elements[0], 'foo bar', BEFORE);", "false");
shouldBe("setTextValue(elements[0], 'foo');", "false");
shouldBe("simulateTextEntry(elements[0], 'foo bar', BEFORE);", "true");
shouldBe("simulateTextEntry(elements[1], '', BEFORE);", "true");
shouldBe("simulateTextEntry(elements[1], 'fo', BEFORE);", "true");
shouldBe("simulateTextEntry(elements[1], 'o');", "true");
shouldBe("simulateTextEntry(elements[1], 'foo', BEFORE);", "false");
shouldBe("simulateTextEntry(elements[2], 'wee', AFTER);", "false");
shouldBe("simulateTextEntry(elements[2], 'foo', BEFORE | AFTER);", "false");
shouldBe("simulateTextEntry(elements[2], 'fo', BEFORE);", "true");
shouldBe("simulateTextEntry(elements[2], 'o');", "true");
shouldBe("simulateTextEntry(elements[2], 'foo', BEFORE);", "false");
shouldBe("simulateTextEntry(elements[3], 'foo', BEFORE);", "false");
shouldBe("simulateTextEntry(elements[3], 'foo', BEFORE);", "false");
shouldBe("setTextValue(elements[3], '');", "false");
shouldBe("simulateTextEntry(elements[3], 'fo', BEFORE);", "true");
shouldBe("simulateTextEntry(elements[3], 'o');", "true");
shouldBe("simulateTextEntry(elements[3], 'foo', BEFORE);", "false");
shouldBe("simulateTextEntry(elements[4], 'foo', AFTER);", "false");
shouldBe("simulateTextEntry(elements[4], 'foo');", "true");
shouldBe("simulateTextEntry(elements[4], 'foo', BEFORE);", "false");
shouldBe("simulateTextEntry(elements[4], 'foo', BEFORE | AFTER);", "true");
shouldBe("simulateTextEntry(elements[4], '', AFTER);", "false");
shouldBe("simulateTextEntry(elements[4], 'foo', AFTER);", "false");
// Hide test elements after run to avoid whitespace differences across platforms in the results.
document.getElementById('test').style.display = 'none';
</script>
</html>