1 function runTestsShouldPass(tagName, attributes)
3 attributes = attributes || {};
4 window.element = document.createElement(tagName);
5 for (var key in attributes)
6 element.setAttribute(key, attributes[key]);
7 document.body.appendChild(element);
9 debug("Running tests on " + tagName + " with attributes: " + JSON.stringify(attributes) + "\n");
10 debug("setRangeText() with only one parameter.");
12 evalAndLog("element.value = '0123456789'");
13 evalAndLog("element.setSelectionRange(2, 5)");
14 evalAndLog("element.setRangeText('ABC')");
15 shouldBeEqualToString("element.value", "01ABC56789");
16 shouldBe("element.selectionStart", "2");
17 shouldBe("element.selectionEnd", "5");
18 evalAndLog("element.setRangeText('ABCD')");
19 shouldBeEqualToString("element.value", "01ABCD56789");
20 shouldBe("element.selectionStart", "2");
21 shouldBe("element.selectionEnd", "6");
22 evalAndLog("element.setRangeText('AB')");
23 shouldBeEqualToString("element.value", "01AB56789");
24 shouldBe("element.selectionStart", "2");
25 shouldBe("element.selectionEnd", "4");
26 evalAndLog("element.setRangeText('')");
27 shouldBeEqualToString("element.value", "0156789");
28 shouldBe("element.selectionStart", "2");
29 shouldBe("element.selectionEnd", "2");
31 debug("\nsetRangeText() with 'select' as the selectMode.");
32 evalAndLog("element.value = '0123456789'");
33 evalAndLog("element.setSelectionRange(0, 0)");
34 evalAndLog("element.setRangeText('ABC', 2, 5, 'select')");
35 shouldBeEqualToString("element.value", "01ABC56789");
36 shouldBe("element.selectionStart", "2");
37 shouldBe("element.selectionEnd", "5");
39 evalAndLog("element.value = '0123456789'");
40 evalAndLog("element.setSelectionRange(0, 0)");
41 evalAndLog("element.setRangeText('ABC', 5, 10, 'select')");
42 shouldBeEqualToString("element.value", "01234ABC");
43 shouldBe("element.selectionStart", "5");
44 shouldBe("element.selectionEnd", "8");
46 evalAndLog("element.value = '0123456789'");
47 evalAndLog("element.setSelectionRange(0, 0)");
48 evalAndLog("element.setRangeText('ABC', 1, 2, 'select')");
49 shouldBeEqualToString("element.value", "0ABC23456789");
50 shouldBe("element.selectionStart", "1");
51 shouldBe("element.selectionEnd", "4");
53 evalAndLog("element.value = '0123456789'");
54 evalAndLog("element.setSelectionRange(0, 0)");
55 evalAndLog("element.setRangeText('', 1, 9, 'select')");
56 shouldBeEqualToString("element.value", "09");
57 shouldBe("element.selectionStart", "1");
58 shouldBe("element.selectionEnd", "1");
60 debug("\nsetRangeText() with 'start' as the selectMode.");
61 evalAndLog("element.value = '0123456789'");
62 evalAndLog("element.setSelectionRange(0, 0)");
63 evalAndLog("element.setRangeText('ABC', 2, 6, 'start')");
64 shouldBeEqualToString("element.value", "01ABC6789");
65 shouldBe("element.selectionStart", "2");
66 shouldBe("element.selectionEnd", "2");
68 debug("\nsetRangeText() with 'end' as the selectMode.");
69 evalAndLog("element.value = '0123456789'");
70 evalAndLog("element.setSelectionRange(0, 0)");
71 evalAndLog("element.setRangeText('ABC', 10, 10, 'end')");
72 shouldBeEqualToString("element.value", "0123456789ABC");
73 shouldBe("element.selectionStart", "13");
74 shouldBe("element.selectionEnd", "13");
76 debug("\nsetRangeText() with 'preserve' as the selectMode.");
77 evalAndLog("element.value = '0123456789'");
78 evalAndLog("element.setSelectionRange(6, 9)");
79 evalAndLog("element.setRangeText('A', 1, 2)"); // selectMode is optional and defaults to preserve.
80 shouldBeEqualToString("element.value", "0A23456789");
81 shouldBe("element.selectionStart", "6");
82 shouldBe("element.selectionEnd", "9");
84 evalAndLog("element.value = '0123456789'");
85 evalAndLog("element.setSelectionRange(6, 9)");
86 shouldThrow("element.setRangeText('AB', 1, 1, 'invalid')"); // Invalid selectMode should throw TypeError
87 shouldBeEqualToString("element.value", "0123456789");
88 shouldBe("element.selectionStart", "6");
89 shouldBe("element.selectionEnd", "9");
91 evalAndLog("element.value = '0123456789'");
92 evalAndLog("element.setSelectionRange(6, 9)");
93 evalAndLog("element.setRangeText('AB', 1, 1, undefined)"); // Undefined selectMode also default to preserve.
94 shouldBeEqualToString("element.value", "0AB123456789");
95 shouldBe("element.selectionStart", "8");
96 shouldBe("element.selectionEnd", "11");
98 evalAndLog("element.value = '0123456789'");
99 evalAndLog("element.setSelectionRange(6, 9)");
100 evalAndLog("element.setRangeText('A', 1, 3, 'preserve')");
101 shouldBeEqualToString("element.value", "0A3456789");
102 shouldBe("element.selectionStart", "5");
103 shouldBe("element.selectionEnd", "8");
105 evalAndLog("element.value = '0123456789'");
106 evalAndLog("element.setSelectionRange(2, 6)");
107 evalAndLog("element.setRangeText('A', 1, 4, 'preserve')");
108 shouldBeEqualToString("element.value", "0A456789");
109 shouldBe("element.selectionStart", "1");
110 shouldBe("element.selectionEnd", "4");
112 evalAndLog("element.value = '0123456789'");
113 evalAndLog("element.setSelectionRange(2, 6)");
114 evalAndLog("element.setRangeText('A', 4, 6, 'preserve')");
115 shouldBeEqualToString("element.value", "0123A6789");
116 shouldBe("element.selectionStart", "2");
117 shouldBe("element.selectionEnd", "5");
119 evalAndLog("element.value = '0123456789'");
120 evalAndLog("element.setSelectionRange(2, 6)");
121 evalAndLog("element.setRangeText('ABCDEF', 4, 7, 'preserve')");
122 shouldBeEqualToString("element.value", "0123ABCDEF789");
123 shouldBe("element.selectionStart", "2");
124 shouldBe("element.selectionEnd", "10");
126 debug("\nsetRangeText() with various start/end values.");
127 evalAndLog("element.value = '0123456789'");
128 evalAndLog("element.setSelectionRange(0, 0)");
129 evalAndLog("element.setRangeText('A', 100, 100, 'select')");
130 shouldBeEqualToString("element.value", "0123456789A");
131 shouldBe("element.selectionStart", "10");
132 shouldBe("element.selectionEnd", "11");
134 evalAndLog("element.value = '0123456789'");
135 evalAndLog("element.setSelectionRange(0, 0)");
136 evalAndLog("element.setRangeText('A', 8, 100, 'select')");
137 shouldBeEqualToString("element.value", "01234567A");
138 shouldBe("element.selectionStart", "8");
139 shouldBe("element.selectionEnd", "9");
141 evalAndLog("element.value = '0123456789'");
142 shouldThrow("element.setRangeText('A', 7, 3)");
145 function runTestsShouldFail(tagName, attributes)
147 attributes = attributes || {};
148 window.element = document.createElement(tagName);
149 for (var key in attributes)
150 element.setAttribute(key, attributes[key]);
152 document.body.appendChild(element);
154 debug("Running tests on " + tagName + " with attributes: " + JSON.stringify(attributes) + "\n");
155 if (element.getAttribute("type") == "file")
156 shouldThrow("element.value = '0123456789XYZ'");
158 evalAndLog("element.value = '0123456789XYZ'");
159 var initialValue = element.value;
160 shouldThrow("element.setRangeText('ABC', 0, 0)");
161 // setRangeText() shouldn't do anything on non-text form controls.
162 shouldBeEqualToString("element.value", initialValue);