servo: Merge #5375 - Add comments linking XHR code to XHR specification (from frewsxcv:xhr-docs); r=jdm

Also adding some newlines for visual separation between
functions/methods

Source-Repo: https://github.com/servo/servo
Source-Revision: 699ace844c939ab97180acad67a984dc71108a12
This commit is contained in:
Corey Farwell 2015-03-26 06:33:50 -06:00
Родитель 9a2592c2ae
Коммит c1770f9cb8
1 изменённых файлов: 42 добавлений и 1 удалений

Просмотреть файл

@ -199,6 +199,8 @@ impl XMLHttpRequest {
global, global,
XMLHttpRequestBinding::Wrap) XMLHttpRequestBinding::Wrap)
} }
// https://xhr.spec.whatwg.org/#constructors
pub fn Constructor(global: GlobalRef) -> Fallible<Temporary<XMLHttpRequest>> { pub fn Constructor(global: GlobalRef) -> Fallible<Temporary<XMLHttpRequest>> {
Ok(XMLHttpRequest::new(global)) Ok(XMLHttpRequest::new(global))
} }
@ -344,10 +346,12 @@ impl XMLHttpRequest {
impl<'a> XMLHttpRequestMethods for JSRef<'a, XMLHttpRequest> { impl<'a> XMLHttpRequestMethods for JSRef<'a, XMLHttpRequest> {
event_handler!(readystatechange, GetOnreadystatechange, SetOnreadystatechange); event_handler!(readystatechange, GetOnreadystatechange, SetOnreadystatechange);
// https://xhr.spec.whatwg.org/#dom-xmlhttprequest-readystate
fn ReadyState(self) -> u16 { fn ReadyState(self) -> u16 {
self.ready_state.get() as u16 self.ready_state.get() as u16
} }
// https://xhr.spec.whatwg.org/#the-open()-method
fn Open(self, method: ByteString, url: DOMString) -> ErrorResult { fn Open(self, method: ByteString, url: DOMString) -> ErrorResult {
//FIXME(seanmonstar): use a Trie instead? //FIXME(seanmonstar): use a Trie instead?
let maybe_method = method.as_str().and_then(|s| { let maybe_method = method.as_str().and_then(|s| {
@ -411,11 +415,15 @@ impl<'a> XMLHttpRequestMethods for JSRef<'a, XMLHttpRequest> {
_ => Err(Syntax), // Step 3 _ => Err(Syntax), // Step 3
} }
} }
// https://xhr.spec.whatwg.org/#the-open()-method
fn Open_(self, method: ByteString, url: DOMString, async: bool, fn Open_(self, method: ByteString, url: DOMString, async: bool,
_username: Option<DOMString>, _password: Option<DOMString>) -> ErrorResult { _username: Option<DOMString>, _password: Option<DOMString>) -> ErrorResult {
self.sync.set(!async); self.sync.set(!async);
self.Open(method, url) self.Open(method, url)
} }
// https://xhr.spec.whatwg.org/#the-setrequestheader()-method
fn SetRequestHeader(self, name: ByteString, mut value: ByteString) -> ErrorResult { fn SetRequestHeader(self, name: ByteString, mut value: ByteString) -> ErrorResult {
if self.ready_state.get() != XMLHttpRequestState::Opened || self.send_flag.get() { if self.ready_state.get() != XMLHttpRequestState::Opened || self.send_flag.get() {
return Err(InvalidState); // Step 1, 2 return Err(InvalidState); // Step 1, 2
@ -464,9 +472,13 @@ impl<'a> XMLHttpRequestMethods for JSRef<'a, XMLHttpRequest> {
headers.set_raw(name_str.to_owned(), vec![value.as_slice().to_vec()]); headers.set_raw(name_str.to_owned(), vec![value.as_slice().to_vec()]);
Ok(()) Ok(())
} }
// https://xhr.spec.whatwg.org/#the-timeout-attribute
fn Timeout(self) -> u32 { fn Timeout(self) -> u32 {
self.timeout.get() self.timeout.get()
} }
// https://xhr.spec.whatwg.org/#the-timeout-attribute
fn SetTimeout(self, timeout: u32) -> ErrorResult { fn SetTimeout(self, timeout: u32) -> ErrorResult {
if self.sync.get() { if self.sync.get() {
// FIXME: Not valid for a worker environment // FIXME: Not valid for a worker environment
@ -489,10 +501,13 @@ impl<'a> XMLHttpRequestMethods for JSRef<'a, XMLHttpRequest> {
Ok(()) Ok(())
} }
} }
// https://xhr.spec.whatwg.org/#the-withcredentials-attribute
fn WithCredentials(self) -> bool { fn WithCredentials(self) -> bool {
self.with_credentials.get() self.with_credentials.get()
} }
// Spec for SetWithCredentials: https://xhr.spec.whatwg.org/#dom-xmlhttprequest-withcredentials
// https://xhr.spec.whatwg.org/#dom-xmlhttprequest-withcredentials
fn SetWithCredentials(self, with_credentials: bool) -> ErrorResult { fn SetWithCredentials(self, with_credentials: bool) -> ErrorResult {
match self.ready_state.get() { match self.ready_state.get() {
XMLHttpRequestState::HeadersReceived | XMLHttpRequestState::HeadersReceived |
@ -508,9 +523,13 @@ impl<'a> XMLHttpRequestMethods for JSRef<'a, XMLHttpRequest> {
}, },
} }
} }
// https://xhr.spec.whatwg.org/#the-upload-attribute
fn Upload(self) -> Temporary<XMLHttpRequestUpload> { fn Upload(self) -> Temporary<XMLHttpRequestUpload> {
Temporary::new(self.upload) Temporary::new(self.upload)
} }
// https://xhr.spec.whatwg.org/#the-send()-method
fn Send(self, data: Option<SendParam>) -> ErrorResult { fn Send(self, data: Option<SendParam>) -> ErrorResult {
if self.ready_state.get() != XMLHttpRequestState::Opened || self.send_flag.get() { if self.ready_state.get() != XMLHttpRequestState::Opened || self.send_flag.get() {
return Err(InvalidState); // Step 1, 2 return Err(InvalidState); // Step 1, 2
@ -656,6 +675,8 @@ impl<'a> XMLHttpRequestMethods for JSRef<'a, XMLHttpRequest> {
} }
Ok(()) Ok(())
} }
// https://xhr.spec.whatwg.org/#the-abort()-method
fn Abort(self) { fn Abort(self) {
self.terminate_ongoing_fetch(); self.terminate_ongoing_fetch();
let state = self.ready_state.get(); let state = self.ready_state.get();
@ -672,17 +693,25 @@ impl<'a> XMLHttpRequestMethods for JSRef<'a, XMLHttpRequest> {
} }
self.ready_state.set(XMLHttpRequestState::Unsent); self.ready_state.set(XMLHttpRequestState::Unsent);
} }
// https://xhr.spec.whatwg.org/#the-responseurl-attribute
fn ResponseURL(self) -> DOMString { fn ResponseURL(self) -> DOMString {
self.response_url.clone() self.response_url.clone()
} }
// https://xhr.spec.whatwg.org/#the-status-attribute
fn Status(self) -> u16 { fn Status(self) -> u16 {
self.status.get() self.status.get()
} }
// https://xhr.spec.whatwg.org/#the-statustext-attribute
fn StatusText(self) -> ByteString { fn StatusText(self) -> ByteString {
// FIXME(https://github.com/rust-lang/rust/issues/23338) // FIXME(https://github.com/rust-lang/rust/issues/23338)
let status_text = self.status_text.borrow(); let status_text = self.status_text.borrow();
status_text.clone() status_text.clone()
} }
// https://xhr.spec.whatwg.org/#the-getresponseheader()-method
fn GetResponseHeader(self, name: ByteString) -> Option<ByteString> { fn GetResponseHeader(self, name: ByteString) -> Option<ByteString> {
self.filter_response_headers().iter().find(|h| { self.filter_response_headers().iter().find(|h| {
name.eq_ignore_case(&FromStr::from_str(h.name()).unwrap()) name.eq_ignore_case(&FromStr::from_str(h.name()).unwrap())
@ -690,12 +719,18 @@ impl<'a> XMLHttpRequestMethods for JSRef<'a, XMLHttpRequest> {
ByteString::new(h.value_string().into_bytes()) ByteString::new(h.value_string().into_bytes())
}) })
} }
// https://xhr.spec.whatwg.org/#the-getallresponseheaders()-method
fn GetAllResponseHeaders(self) -> ByteString { fn GetAllResponseHeaders(self) -> ByteString {
ByteString::new(self.filter_response_headers().to_string().into_bytes()) ByteString::new(self.filter_response_headers().to_string().into_bytes())
} }
// https://xhr.spec.whatwg.org/#the-responsetype-attribute
fn ResponseType(self) -> XMLHttpRequestResponseType { fn ResponseType(self) -> XMLHttpRequestResponseType {
self.response_type.get() self.response_type.get()
} }
// https://xhr.spec.whatwg.org/#the-responsetype-attribute
fn SetResponseType(self, response_type: XMLHttpRequestResponseType) -> ErrorResult { fn SetResponseType(self, response_type: XMLHttpRequestResponseType) -> ErrorResult {
match self.global.root() { match self.global.root() {
GlobalRoot::Worker(_) if response_type == XMLHttpRequestResponseType::Document GlobalRoot::Worker(_) if response_type == XMLHttpRequestResponseType::Document
@ -711,6 +746,8 @@ impl<'a> XMLHttpRequestMethods for JSRef<'a, XMLHttpRequest> {
} }
} }
} }
// https://xhr.spec.whatwg.org/#the-response-attribute
#[allow(unsafe_code)] #[allow(unsafe_code)]
fn Response(self, cx: *mut JSContext) -> JSVal { fn Response(self, cx: *mut JSContext) -> JSVal {
match self.response_type.get() { match self.response_type.get() {
@ -741,6 +778,8 @@ impl<'a> XMLHttpRequestMethods for JSRef<'a, XMLHttpRequest> {
} }
} }
} }
// https://xhr.spec.whatwg.org/#the-responsetext-attribute
fn GetResponseText(self) -> Fallible<DOMString> { fn GetResponseText(self) -> Fallible<DOMString> {
match self.response_type.get() { match self.response_type.get() {
_empty | Text => { _empty | Text => {
@ -752,6 +791,8 @@ impl<'a> XMLHttpRequestMethods for JSRef<'a, XMLHttpRequest> {
_ => Err(InvalidState) _ => Err(InvalidState)
} }
} }
// https://xhr.spec.whatwg.org/#the-responsexml-attribute
fn GetResponseXML(self) -> Option<Temporary<Document>> { fn GetResponseXML(self) -> Option<Temporary<Document>> {
self.response_xml.get() self.response_xml.get()
} }