Fire load event for external stylesheets

Do not load stylesheets with the wrong Content-Type

Fixes #11912, #11910
This commit is contained in:
Mike MacDonald (crazymykl) 2016-06-28 21:07:39 -04:00 committed by Mike MacDonald
parent 7a7bdf51ad
commit aed761509b
No known key found for this signature in database
GPG key ID: 88460A63323A15DD
4 changed files with 19 additions and 13 deletions

View file

@ -22,6 +22,7 @@ use dom::virtualmethods::VirtualMethods;
use encoding::EncodingRef; use encoding::EncodingRef;
use encoding::all::UTF_8; use encoding::all::UTF_8;
use hyper::header::ContentType; use hyper::header::ContentType;
use hyper::http::RawStatus;
use hyper::mime::{Mime, TopLevel, SubLevel}; use hyper::mime::{Mime, TopLevel, SubLevel};
use ipc_channel::ipc; use ipc_channel::ipc;
use ipc_channel::router::ROUTER; use ipc_channel::router::ROUTER;
@ -296,15 +297,18 @@ impl AsyncResponseListener for StylesheetContext {
fn response_complete(&mut self, status: Result<(), NetworkError>) { fn response_complete(&mut self, status: Result<(), NetworkError>) {
let elem = self.elem.root(); let elem = self.elem.root();
let document = document_from_node(&*elem); let document = document_from_node(&*elem);
let mut successful = false;
if status.is_err() { if status.is_ok() {
self.elem.root().upcast::<EventTarget>().fire_simple_event("error");
} else {
let data = mem::replace(&mut self.data, vec!());
let metadata = match self.metadata.take() { let metadata = match self.metadata.take() {
Some(meta) => meta, Some(meta) => meta,
None => return, None => return,
}; };
let is_css = metadata.content_type.map_or(false, |ContentType(Mime(top, sub, _))|
top == TopLevel::Text && sub == SubLevel::Css);
let data = if is_css { mem::replace(&mut self.data, vec!()) } else { vec!() };
// TODO: Get the actual value. http://dev.w3.org/csswg/css-syntax/#environment-encoding // TODO: Get the actual value. http://dev.w3.org/csswg/css-syntax/#environment-encoding
let environment_encoding = UTF_8 as EncodingRef; let environment_encoding = UTF_8 as EncodingRef;
let protocol_encoding_label = metadata.charset.as_ref().map(|s| &**s); let protocol_encoding_label = metadata.charset.as_ref().map(|s| &**s);
@ -328,6 +332,9 @@ impl AsyncResponseListener for StylesheetContext {
*elem.stylesheet.borrow_mut() = Some(sheet); *elem.stylesheet.borrow_mut() = Some(sheet);
document.invalidate_stylesheets(); document.invalidate_stylesheets();
// FIXME: Revisit once consensus is reached at: https://github.com/whatwg/html/issues/1142
successful = metadata.status.map_or(false, |RawStatus(code, _)| code == 200);
} }
if elem.parser_inserted.get() { if elem.parser_inserted.get() {
@ -335,6 +342,10 @@ impl AsyncResponseListener for StylesheetContext {
} }
document.finish_load(LoadType::Stylesheet(self.url.clone())); document.finish_load(LoadType::Stylesheet(self.url.clone()));
let event = if successful { "load" } else { "error" };
elem.upcast::<EventTarget>().fire_simple_event(event);
} }
} }

View file

@ -1,3 +0,0 @@
[content-type-001.htm]
type: reftest
expected: FAIL

View file

@ -10,12 +10,9 @@ var t = async_test("Check if the stylesheet's load event blocks the document loa
document.getElementById('style_test').onload = t.step_func(function() { document.getElementById('style_test').onload = t.step_func(function() {
saw_link_onload = true; saw_link_onload = true;
}); });
window.addEventListener('load', function() { window.addEventListener('load', t.step_func_done(function() {
t.step_func(function() { assert_true(saw_link_onload);
assert_true(saw_link_onload); }));
});
t.done();
}, false);
</script> </script>
</head> </head>
</html> </html>

View file

@ -40,6 +40,7 @@ tText.step(function() {
assert_true(true, "Got error event for 404 error.") assert_true(true, "Got error event for 404 error.")
tText.done() tText.done()
}) })
elt.onload = tText.unreached_func("load event should not be fired");
elt.rel = "stylesheet"; elt.rel = "stylesheet";
elt.href = "../../../../../common/css-red.txt"; elt.href = "../../../../../common/css-red.txt";
document.getElementsByTagName("head")[0].appendChild(elt); document.getElementsByTagName("head")[0].appendChild(elt);