mirror of
https://github.com/servo/servo.git
synced 2025-08-05 13:40:08 +01:00
Implement EventSource and update test expectations
This commit is contained in:
parent
e3dd36f0f3
commit
1d62db405e
58 changed files with 543 additions and 51 deletions
102
components/script/dom/eventsource.rs
Normal file
102
components/script/dom/eventsource.rs
Normal file
|
@ -0,0 +1,102 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use dom::bindings::cell::DOMRefCell;
|
||||
use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
|
||||
use dom::bindings::codegen::Bindings::EventSourceBinding::{EventSourceInit, EventSourceMethods, Wrap};
|
||||
use dom::bindings::error::{Error, Fallible};
|
||||
use dom::bindings::global::GlobalRef;
|
||||
use dom::bindings::js::Root;
|
||||
use dom::bindings::reflector::reflect_dom_object;
|
||||
use dom::eventtarget::EventTarget;
|
||||
use std::cell::Cell;
|
||||
use url::Url;
|
||||
use util::str::DOMString;
|
||||
|
||||
#[derive(JSTraceable, PartialEq, Copy, Clone, Debug, HeapSizeOf)]
|
||||
enum EventSourceReadyState {
|
||||
Connecting = 0,
|
||||
Open = 1,
|
||||
Closed = 2
|
||||
}
|
||||
|
||||
#[dom_struct]
|
||||
pub struct EventSource {
|
||||
eventtarget: EventTarget,
|
||||
url: Url,
|
||||
ready_state: Cell<EventSourceReadyState>,
|
||||
with_credentials: bool,
|
||||
last_event_id: DOMRefCell<DOMString>
|
||||
}
|
||||
|
||||
impl EventSource {
|
||||
fn new_inherited(url: Url, with_credentials: bool) -> EventSource {
|
||||
EventSource {
|
||||
eventtarget: EventTarget::new_inherited(),
|
||||
url: url,
|
||||
ready_state: Cell::new(EventSourceReadyState::Connecting),
|
||||
with_credentials: with_credentials,
|
||||
last_event_id: DOMRefCell::new(DOMString::from(""))
|
||||
}
|
||||
}
|
||||
|
||||
fn new(global: GlobalRef, url: Url, with_credentials: bool) -> Root<EventSource> {
|
||||
reflect_dom_object(box EventSource::new_inherited(url, with_credentials), global, Wrap)
|
||||
}
|
||||
|
||||
pub fn Constructor(global: GlobalRef,
|
||||
url_str: DOMString,
|
||||
event_source_init: &EventSourceInit) -> Fallible<Root<EventSource>> {
|
||||
// Steps 1-2
|
||||
let base_url = global.get_url();
|
||||
let url = match base_url.join(&*url_str) {
|
||||
Ok(u) => u,
|
||||
Err(_) => return Err(Error::Syntax)
|
||||
};
|
||||
// Step 3
|
||||
let event_source = EventSource::new(global, url, event_source_init.withCredentials);
|
||||
// Step 4
|
||||
// Step 5
|
||||
// Step 6
|
||||
// Step 7
|
||||
// Step 8
|
||||
// Step 9
|
||||
// Step 10
|
||||
// Step 11
|
||||
Ok(event_source)
|
||||
// Step 12
|
||||
}
|
||||
}
|
||||
|
||||
impl EventSourceMethods for EventSource {
|
||||
// https://html.spec.whatwg.org/multipage/#handler-eventsource-onopen
|
||||
event_handler!(open, GetOnopen, SetOnopen);
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#handler-eventsource-onmessage
|
||||
event_handler!(message, GetOnmessage, SetOnmessage);
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#handler-eventsource-onerror
|
||||
event_handler!(error, GetOnerror, SetOnerror);
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-eventsource-url
|
||||
fn Url(&self) -> DOMString {
|
||||
DOMString::from(self.url.serialize())
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-eventsource-withcredentials
|
||||
fn WithCredentials(&self) -> bool {
|
||||
self.with_credentials
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-eventsource-readystate
|
||||
fn ReadyState(&self) -> u16 {
|
||||
self.ready_state.get() as u16
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-eventsource-close
|
||||
fn Close(&self) {
|
||||
self.ready_state.set(EventSourceReadyState::Closed);
|
||||
// TODO: Terminate ongoing fetch
|
||||
}
|
||||
}
|
|
@ -246,6 +246,7 @@ pub mod element;
|
|||
pub mod errorevent;
|
||||
pub mod event;
|
||||
pub mod eventdispatcher;
|
||||
pub mod eventsource;
|
||||
pub mod eventtarget;
|
||||
pub mod file;
|
||||
pub mod filelist;
|
||||
|
|
31
components/script/dom/webidls/EventSource.webidl
Normal file
31
components/script/dom/webidls/EventSource.webidl
Normal file
|
@ -0,0 +1,31 @@
|
|||
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
/*
|
||||
* The origin of this IDL file is:
|
||||
* https://html.spec.whatwg.org/multipage/#eventsource
|
||||
*/
|
||||
|
||||
[Constructor(DOMString url, optional EventSourceInit eventSourceInitDict),
|
||||
/*Exposed=(Window,Worker)*/]
|
||||
interface EventSource : EventTarget {
|
||||
readonly attribute DOMString url;
|
||||
readonly attribute boolean withCredentials;
|
||||
|
||||
// ready state
|
||||
const unsigned short CONNECTING = 0;
|
||||
const unsigned short OPEN = 1;
|
||||
const unsigned short CLOSED = 2;
|
||||
readonly attribute unsigned short readyState;
|
||||
|
||||
// networking
|
||||
attribute EventHandler onopen;
|
||||
attribute EventHandler onmessage;
|
||||
attribute EventHandler onerror;
|
||||
void close();
|
||||
};
|
||||
|
||||
dictionary EventSourceInit {
|
||||
boolean withCredentials = false;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue