Auto merge of #11919 - crazymykl:fix-style-load-events, r=Ms2ger

Fire load event for external stylesheets, enforce Content-Type checks

<!-- 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. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/11919)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-08-03 17:47:45 -05:00 committed by GitHub
commit 8553690088
4 changed files with 19 additions and 13 deletions

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);
}
}

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() {
saw_link_onload = true;
});
window.addEventListener('load', function() {
t.step_func(function() {
assert_true(saw_link_onload);
});
t.done();
}, false);
window.addEventListener('load', t.step_func_done(function() {
assert_true(saw_link_onload);
}));
</script>
</head>
</html>

View file

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