summaryrefslogtreecommitdiffstats
path: root/dom/html/test/test_bug590363.html
blob: 3e72b542ca498d8ef0a5654ed4964ec768382170 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=590363
-->
<head>
  <title>Test for Bug 590363</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=590363">Mozilla Bug 590363</a>
<p id="display"></p>
<div id="content" style="display: none">
  
</div>
<pre id="test">
<script type="application/javascript">

/** Test for Bug 590363 **/

var testData = [
  /* type to test | is the value reset when changing to file then reverting */
  [ "button",   false ],
  [ "checkbox", false ],
  [ "hidden",   false ],
  [ "reset",    false ],
  [ "image",    false ],
  [ "radio",    false ],
  [ "submit",   false ],
  [ "tel",      true ],
  [ "text",     true ],
  [ "url",      true ],
  [ "email",    true ],
  [ "search",   true ],
  [ "password", true ],
  [ "number",   true ],
  [ "date",     true ],
  [ "time",     true ],
  [ "range",    true ],
  [ "color",    true ],
  [ 'month',    true ],
  [ 'week',     true ],
  [ 'datetime-local', true ]
  // 'file' is treated separatly.
];

var nonTrivialSanitizing = [ 'number', 'date', 'time', 'color', 'month', 'week',
                             'datetime-local' ];

var length = testData.length;
for (var i=0; i<length; ++i) {
  for (var j=0; j<length; ++j) {
    var e = document.createElement('input');
    e.type = testData[i][0];

    var expectedValue;

    // range will sanitize its value to 50 (the default) if it isn't a valid
    // number. We need to handle that specially.
    if (testData[j][0] == 'range' || testData[i][0] == 'range') {
      if (testData[j][0] == 'date' || testData[j][0] == 'time' ||
          testData[j][0] == 'month' || testData[j][0] == 'week' ||
          testData[j][0] == 'datetime-local') {
        expectedValue = '';
      } else if (testData[j][0] == 'color') {
        expectedValue = '#000000';
      } else {
        expectedValue = '50';
      }
    } else if (testData[i][0] == 'color' || testData[j][0] == 'color') {
        if (testData[j][0] == 'number' || testData[j][0] == 'date' ||
            testData[j][0] == 'time' || testData[j][0] == 'month' ||
            testData[j][0] == 'week' || testData[j][0] == 'datetime-local') {
          expectedValue = ''
        } else {
          expectedValue = '#000000';
        }
    } else if (nonTrivialSanitizing.indexOf(testData[i][0]) != -1 &&
               nonTrivialSanitizing.indexOf(testData[j][0]) != -1) {
      expectedValue = '';
    } else if (testData[i][0] == 'number' || testData[j][0] == 'number') {
      expectedValue = '42';
    } else if (testData[i][0] == 'date' || testData[j][0] == 'date') {
      expectedValue = '2012-12-21';
    } else if (testData[i][0] == 'time' || testData[j][0] == 'time') {
      expectedValue = '21:21';
    } else if (testData[i][0] == 'month' || testData[j][0] == 'month') {
      expectedValue = '2013-03';
    } else if (testData[i][0] == 'week' || testData[j][0] == 'week') {
      expectedValue = '2016-W35';
    } else if (testData[i][0] == 'datetime-local' ||
               testData[j][0] == 'datetime-local') {
      expectedValue = '2016-11-07T16:40';
    } else {
      expectedValue = "foo";
    }
    e.value = expectedValue;

    e.type = testData[j][0];
    is(e.value, expectedValue, ".value should still return the same value after " +
       "changing type from " + testData[i][0] + " to " + testData[j][0]);
  }
}

// For type='file' .value doesn't behave the same way.
// We are just going to check that we do not loose the value.
for (var data of testData) {
  var e = document.createElement('input');
  e.type = data[0];
  e.value = 'foo';
  e.type = 'file';
  e.type = data[0];

  if (data[0] == 'range') {
    is(e.value, '50', ".value should still return the same value after " +
       "changing type from " + data[0] + " to 'file' then reverting to " + data[0]);
  } else if (data[0] == 'color') {
    is(e.value, '#000000', ".value should have been reset to the default color after " +
       "changing type from " + data[0] + " to 'file' then reverting to " + data[0]);
  } else if (data[1]) {
    is(e.value, '', ".value should have been reset to the empty string after " +
       "changing type from " + data[0] + " to 'file' then reverting to " + data[0]);
  } else {
    is(e.value, 'foo', ".value should still return the same value after " +
       "changing type from " + data[0] + " to 'file' then reverting to " + data[0]);
  }
}

</script>
</pre>
</body>
</html>