summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/forms/the-input-element/type-change-state.html
blob: 5fb4cc9b56491b16b6c54da18aefa8fd22ea088a (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<!DOCTYPE html>
<meta charset=utf-8>
<title>Input element's type attribute changes state</title>
<link rel="author" title="Denis Ah-Kang" href="mailto:denis@w3.org">
<link rel=help href="https://html.spec.whatwg.org/multipage/#the-input-element">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>

  const INITIAL_VALUE = "  foo\rbar  ";

  // Sanitize algorithm implementations only for values used in this test.
  function sanitizeText(value) {
    switch (value) {
      case INITIAL_VALUE: return "  foobar  ";
      case "  foobar  ": return value;
      case "foobar": return value;
      case "50": return value;
      case "#000000": return value;
      case "": return value;
      default: throw new DOMException(`Internal Error: Should add support of "${value}"`, "NotSupportedError");
    }
  }
  function sanitizeEmailOrUrl(value) {
    switch (value) {
      case INITIAL_VALUE: return "foobar";
      case "  foobar  ": return "foobar";
      case "foobar": return value;
      case "50": return value;
      case "#000000": return value;
      case "": return value;
      default: throw new DOMException(`Internal Error: Should add support of "${value}"`, "NotSupportedError");
    }
  }
  function sanitizeTemporal(value) {
    // We have no test cases using valid temporal values.
    return "";
  }
  function sanitizeNumber(value) {
    switch (value) {
      case "50": return value;
      default:
      // We have no test cases using valid numbers other than "50".
        return "";
    }
  }
  function sanitizeRange(value) {
    // We have no test cases using valid numbers other than "50".
    return "50";
  }
  function sanitizeColor(value) {
    // We have no test cases using valid colors other than "#000000".
    return "#000000";
  }
  function browserSupportsInputTypeOf(inputType) {
    var inputTest = document.createElement("input");
    inputTest.type = inputType;
    return (inputTest.type === inputType);
  }


  var types = [
    { type: "hidden" },
    { type: "text", sanitizer: sanitizeText },
    { type: "search", sanitizer: sanitizeText },
    { type: "tel", sanitizer: sanitizeText },
    { type: "url", sanitizer: sanitizeEmailOrUrl },
    { type: "email", sanitizer: sanitizeEmailOrUrl },
    { type: "password", sanitizer: sanitizeText },
    { type: "datetime-local", sanitizer: sanitizeTemporal },
    { type: "date", sanitizer: sanitizeTemporal },
    { type: "time", sanitizer: sanitizeTemporal },
    { type: "number", sanitizer: sanitizeNumber },
    { type: "range", sanitizer: sanitizeRange },
    { type: "color", sanitizer: sanitizeColor },
    { type: "checkbox", defaultValue: "on" },
    { type: "radio", defaultValue: "on" },
    { type: "file" },
    { type: "submit" },
    { type: "image" },
    { type: "reset" },
    { type: "button" }
  ];

  const selectionStart = 2;
  const selectionEnd = 5;
  const selectionDirection = "backward";

  // Obtain selectionDirection after setting it to "none".
  // Some platforms don't support "none" direction, and "forward" is returned
  // in such platforms.
  // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#set-the-selection-direction
  function testNoneDirection() {
    const input = document.createElement("input");
    input.selectionDirection = "none";
    return input.selectionDirection;
  }
  const noneDirectionResult = testNoneDirection();

  for (var i = 0; i < types.length; i++) {
    for (var j = 0; j < types.length; j++) {
      if (types[i] != types[j]) {
        test(function() {
          assert_implements(browserSupportsInputTypeOf(types[i].type), "Support for input type " + types[i].type + " is required for this test.");
          assert_implements(browserSupportsInputTypeOf(types[j].type), "Support for input type " + types[j].type + " is required for this test.");
          var input = document.createElement("input");
          var expected = INITIAL_VALUE;
          input.type = types[i].type;
          if (types[i].type === "file") {
            assert_throws_dom("INVALID_STATE_ERR", function() {
              input.value = expected;
            });
            assert_equals(input.value, "");
          } else if (types[j].type === "file") {
            input.value = expected;
            input.type = types[j].type;  // change state
            assert_equals(input.value, "");
          } else {
            input.value = expected;
            expected = input.value;

            const previouslySelectable = (input.selectionStart !== null);

            if (previouslySelectable) {
              input.setSelectionRange(selectionStart, selectionEnd, selectionDirection);
            }

            input.type = types[j].type;  // change state

            var preSanitizeValue = expected;
            // type[j] sanitization
            if (types[j].sanitizer) {
              expected = types[j].sanitizer(expected);
            }

            // type[j] defaultValue
            if (expected === "" && types[j].defaultValue) {
              expected = types[j].defaultValue;
            }

            assert_equals(input.value, expected, "input.value should be '" + expected + "' after change of state");

            const nowSelectable = (input.selectionStart !== null);

            if (nowSelectable) {
              if (previouslySelectable) {
                // Value might change after sanitization. The following checks are only valid when the value stays the same.
                if (preSanitizeValue === expected) {
                  assert_equals(input.selectionStart, selectionStart, "selectionStart should be unchanged");
                  assert_equals(input.selectionEnd, selectionEnd, "selectionEnd should be unchanged");
                  assert_equals(input.selectionDirection, selectionDirection, "selectionDirection should be unchanged");
                }
              } else {
                assert_equals(input.selectionStart, 0, "selectionStart should be 0");
                assert_equals(input.selectionEnd, 0, "selectionEnd should be 0");
                assert_equals(input.selectionDirection, noneDirectionResult,
                              `selectionDirection should be '{noneDirectionResult}'`);
              }
            }
          }
        }, "change state from " + types[i].type + " to " + types[j].type);
      }
    }
  }
</script>