summaryrefslogtreecommitdiffstats
path: root/layout/style/test/test_load_events_on_stylesheets.html
blob: 34d73f9ad6fcc60e0a0181aa9972562beb093352 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=185236
-->
<head>
  <title>Test for Bug 185236</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script>
    var pendingEventCounter = 0;
    var messagePosted = false;
    SimpleTest.waitForExplicitFinish();
    addLoadEvent(function() {
      is(messagePosted, true, "Should have gotten onmessage event");
      is(pendingEventCounter, 0,
         "How did onload for the page fire before onload for all the stylesheets?");
      SimpleTest.finish();
    });
    // Count the link we're about to parse
    pendingEventCounter = 1;
  </script>
  <link rel="stylesheet" href="data:text/css,*{}"
        onload="--pendingEventCounter;
                ok(true, 'Load event firing on basic stylesheet')"
        onerror="--pendingEventCounter;
                 ok(false, 'Error event firing on basic stylesheet')">
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=185236">Mozilla Bug 185236</a>
<p id="display"></p>
<div id="content" style="display: none">
  
</div>
<pre id="test">
<script type="application/javascript">
/** Test for Bug 185236 **/
// Verify that there are no in-flight sheet loads right now; we should have
// waited for them when we hit the script tag
is(pendingEventCounter, 0, "There should be no pending events");

// Test sheet that will already be complete when we write it out
++pendingEventCounter;

// Make sure that a postMessage we do right now fires after the onload handler
// for the stylesheet.  If we ever change the timing of sheet onload, we will
// need to change that.
window.onmessage = function() {
  messagePosted = true;
  // There are 6 pending events: two from the two direct example.com loads,
  // and 4 from the two data:text/css loads that import things
  is(pendingEventCounter, 6, "Load event for sheet should have fired");
}
window.postMessage("", "*");

function checkSheetComplete(sheet, length) {
  try {
    is(sheet.cssRules.length, length, `Should be loaded with ${length} rule(s)`);
  } catch (e) {
    ok(false, "Sheet has not been loaded completely");
  }
}

document.write('<link rel="stylesheet" href="data:text/css,*{}"\
                onload="--pendingEventCounter;\
                        ok(true, \'Load event firing on basic stylesheet\')"\
                onerror="--pendingEventCounter;\
                        ok(false, \'Error event firing on basic stylesheet\')">');

// Make sure we have that second stylesheet
is(document.styleSheets.length, 3, "Should have three stylesheets");

// Make sure that the second stylesheet is all loaded
// If we ever switch away from sync loading of already-complete sheets, this
// test will need adjusting
checkSheetComplete(document.styleSheets[2], 1);

// Make sure the load event for that stylesheet has not fired yet
is(pendingEventCounter, 1, "There should be one pending event");

++pendingEventCounter;
document.write('<style\
                onload="--pendingEventCounter;\
                        ok(true, \'Load event firing on inline stylesheet\')"\
                onerror="--pendingEventCounter;\
                        ok(false, \'Error event firing on inline stylesheet\')"></style>');

// Make sure the load event for that second stylesheet has not fired yet
is(pendingEventCounter, 2, "There should be two pending events");

++pendingEventCounter;
document.write('<link rel="stylesheet" href="http://www.example.com"\
                onload="--pendingEventCounter;\
                        ok(false, \'Load event firing on broken stylesheet 1\')"\
                onerror="--pendingEventCounter;\
                        ok(true, \'Error event firing on broken stylesheet 1\')">');

++pendingEventCounter;
var link = document.createElement("link");
link.rel = "stylesheet";
link.href = "http://www.example.com";
link.onload = function() { --pendingEventCounter;
  ok(false, 'Load event firing on broken stylesheet 2');
};
link.onerror = function() { --pendingEventCounter;
  ok(true, 'Error event firing on broken stylesheet 2');
}
document.body.appendChild(link);

++pendingEventCounter;
link = document.createElement("link");
link.rel = "stylesheet";
link.href = "data:text/css,*{}";
link.onload = function() { --pendingEventCounter;
  ok(true, 'Load event firing on external stylesheet');
};
link.onerror = function() { --pendingEventCounter;
  ok(false, 'Error event firing on external stylesheet');
}
document.body.appendChild(link);

// If we ever switch away from sync loading of already-complete sheets, this
// test will need adjusting
checkSheetComplete(link.sheet, 1);

++pendingEventCounter;
link = document.createElement("link");
link.rel = "stylesheet";
link.href = "data:text/css,@import url('data:text/css,*{}')";
link.onload = function() { --pendingEventCounter;
  ok(true, 'Load event firing on external stylesheet');
};
link.onerror = function() { --pendingEventCounter;
  ok(false, 'Error event firing on external stylesheet');
}
document.body.appendChild(link);

++pendingEventCounter;
link = document.createElement("link");
link.rel = "stylesheet";
link.href = "data:text/css,@import url('http://www.example.com')";
link.onload = function() { --pendingEventCounter;
  ok(false, 'Load event firing on broken stylesheet 3');
};
link.onerror = function() { --pendingEventCounter;
  ok(true, 'Error event firing on broken stylesheet 3');
}
document.body.appendChild(link);

function absoluteURL(relativeURL) {
  return new URL(relativeURL, location.href).href;
}

++pendingEventCounter;
link = document.createElement("link");
link.rel = "stylesheet";
link.href = `data:text/css,@import url('http://www.example.com'); @import url(${absoluteURL('slow_ok_sheet.sjs')});`;
link.onload = function() { --pendingEventCounter;
  ok(false, 'Load event firing on broken stylesheet 4');
};
link.onerror = function() { --pendingEventCounter;
  ok(true, 'Error event firing on broken stylesheet 4');
}
document.body.appendChild(link);

++pendingEventCounter;
link = document.createElement("link");
link.rel = "stylesheet";
link.href = `data:text/css,@import url(${absoluteURL('slow_broken_sheet.sjs')}); @import url('data:text/css,');`;
link.onload = function() { --pendingEventCounter;
  ok(false, 'Load event firing on broken stylesheet 5');
};
link.onerror = function() { --pendingEventCounter;
  ok(true, 'Error event firing on broken stylesheet 5');
}
document.body.appendChild(link);

// Make sure the load events for all those stylesheets have not fired yet
is(pendingEventCounter, 9, "There should be nine pending events");

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