servo: Merge #11919 - Fire load event for external stylesheets, enforce Content-Type checks (from crazymykl:fix-style-load-events); r=Ms2ger

<!-- Please describe your changes on the following line: -->
Fire load event for external stylesheets

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #11912,  #11910

<!-- Either: -->
- [x] There are tests for these changes

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

Source-Repo: https://github.com/servo/servo
Source-Revision: 85536900885911fae58207f63e956b4148a4861a
This commit is contained in:
Mike MacDonald (crazymykl)
2016-08-03 17:47:45 -05:00
parent eb319caf89
commit 567f8a469c

View File

@@ -22,6 +22,7 @@ use dom::virtualmethods::VirtualMethods;
use encoding::EncodingRef;
use encoding::all::UTF_8;
use hyper::header::ContentType;
use hyper::http::RawStatus;
use hyper::mime::{Mime, TopLevel, SubLevel};
use ipc_channel::ipc;
use ipc_channel::router::ROUTER;
@@ -296,15 +297,18 @@ impl AsyncResponseListener for StylesheetContext {
fn response_complete(&mut self, status: Result<(), NetworkError>) {
let elem = self.elem.root();
let document = document_from_node(&*elem);
let mut successful = false;
if status.is_err() {
self.elem.root().upcast::<EventTarget>().fire_simple_event("error");
} else {
let data = mem::replace(&mut self.data, vec!());
if status.is_ok() {
let metadata = match self.metadata.take() {
Some(meta) => meta,
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
let environment_encoding = UTF_8 as EncodingRef;
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);
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() {
@@ -335,6 +342,10 @@ impl AsyncResponseListener for StylesheetContext {
}
document.finish_load(LoadType::Stylesheet(self.url.clone()));
let event = if successful { "load" } else { "error" };
elem.upcast::<EventTarget>().fire_simple_event(event);
}
}