summaryrefslogtreecommitdiffstats
path: root/dom/html/test/test_bug1472426.html
blob: 6f891184b8c588ab4adc7e2ed347c0f25010555a (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1472426
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 1472426</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="application/javascript">

  /** Test for Bug 1472426 **/

  var shadowIframe;
  var targetIframe;
  var form;
  var sr;

  function checkMPSubmission(sub, expected, test) {
    function getPropCount(o) {
      var x, l = 0;
      for (x in o) ++l;
      return l;
    }
    function mpquote(s) {
      return s.replace(/\r\n/g, " ")
              .replace(/\r/g, " ")
              .replace(/\n/g, " ")
              .replace(/\"/g, "\\\"");
    }

    is(sub.length, expected.length,
       "Correct number of multipart items in " + test);

    if (sub.length != expected.length) {
      alert(JSON.stringify(sub));
    }

    var i;
    for (i = 0; i < expected.length; ++i) {
      if (!("fileName" in expected[i])) {
        is(sub[i].headers["Content-Disposition"],
           "form-data; name=\"" + mpquote(expected[i].name) + "\"",
           "Correct name in " + test);
        is (getPropCount(sub[i].headers), 1,
            "Wrong number of headers in " + test);
        is(sub[i].body,
           expected[i].value.replace(/\r\n|\r|\n/, "\r\n"),
           "Correct value in " + test);
      }
      else {
        is(sub[i].headers["Content-Disposition"],
           "form-data; name=\"" + mpquote(expected[i].name) + "\"; filename=\"" +
             mpquote(expected[i].fileName) + "\"",
           "Correct name in " + test);
        is(sub[i].headers["Content-Type"],
           expected[i].contentType,
           "Correct content type in " + test);
        is (getPropCount(sub[i].headers), 2,
            "Wrong number of headers in " + test);
        is(sub[i].body,
           expected[i].value,
           "Correct value in " + test);
      }
    }
  }

  function testFormSubmissionInShadowDOM() {
    targetIframe = document.getElementById("target_iframe");
    shadowIframe = document.createElement("iframe");
    shadowIframe.src = "about:blank";
    shadowIframe.onload = shadowFrameCreated;
    document.body.appendChild(shadowIframe);
  }

  function shadowFrameCreated() {
    var doc = shadowIframe.contentDocument;
    var body = doc.body;
    var host = doc.createElement("div");
    body.appendChild(host);
    sr = host.attachShadow({ mode: "open" });
    sr.appendChild(document.getElementById('template').content.cloneNode(true));
    targetIframe.onload = checkSubmitValues;
    sr.getElementById("form").submit();
  }

  function checkSubmitValues() {
    submission = JSON.parse(targetIframe.contentDocument.documentElement.textContent);
    var expected = [
       { name: "text", value: "textvalue" },
       { name: "hidden", value: "hiddenvalue" },
       { name: "select", value: "selectvalue" },
       { name: "textarea", value: "textareavalue" }
      ];
    checkMPSubmission(submission, expected, "form submission inside shadow DOM");
    SimpleTest.finish();
  }

  window.onload = function() {
    SimpleTest.waitForExplicitFinish();
    testFormSubmissionInShadowDOM();
  }

  </script>
  <template id="template">
    <form action="form_submit_server.sjs" target="target_iframe" id="form"
          method="POST" enctype="multipart/form-data">
      <input name="text" value="textvalue">
      <input name="hidden" value="hiddenvalue" type="hidden">
      <select name="select"><option selected>selectvalue</option></select>
      <textarea name="textarea">textareavalue</textarea>
    </form>
  </template>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1472426">Mozilla Bug 1472426</a>
<iframe name="target_iframe" id="target_iframe"></iframe>
</body>
</html>