From 57a70893ebd25a2b2389c762369cc8b8d3d3611a Mon Sep 17 00:00:00 2001 From: bd339 Date: Mon, 19 Dec 2016 00:11:01 +0100 Subject: [PATCH] Implement MediaQueryListEvent interface --- components/script/dom/mediaquerylistevent.rs | 82 +++++++++++++++++++ components/script/dom/mod.rs | 1 + .../dom/webidls/MediaQueryListEvent.webidl | 15 ++++ 3 files changed, 98 insertions(+) create mode 100644 components/script/dom/mediaquerylistevent.rs create mode 100644 components/script/dom/webidls/MediaQueryListEvent.webidl diff --git a/components/script/dom/mediaquerylistevent.rs b/components/script/dom/mediaquerylistevent.rs new file mode 100644 index 00000000000..8965ba43f79 --- /dev/null +++ b/components/script/dom/mediaquerylistevent.rs @@ -0,0 +1,82 @@ +/* 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::codegen::Bindings::EventBinding::EventMethods; +use dom::bindings::codegen::Bindings::MediaQueryListEventBinding; +use dom::bindings::codegen::Bindings::MediaQueryListEventBinding::MediaQueryListEventInit; +use dom::bindings::codegen::Bindings::MediaQueryListEventBinding::MediaQueryListEventMethods; +use dom::bindings::error::Fallible; +use dom::bindings::inheritance::Castable; +use dom::bindings::js::Root; +use dom::bindings::reflector::reflect_dom_object; +use dom::bindings::str::DOMString; +use dom::event::Event; +use dom::globalscope::GlobalScope; +use dom::window::Window; +use servo_atoms::Atom; +use std::cell::Cell; + +// https://drafts.csswg.org/cssom-view/#dom-mediaquerylistevent-mediaquerylistevent +#[dom_struct] +pub struct MediaQueryListEvent { + event: Event, + media: DOMString, + matches: Cell +} + +impl MediaQueryListEvent { + pub fn new_uninitialized(global: &GlobalScope) -> Root { + MediaQueryListEvent::new_initialized(global, + DOMString::new(), + false) + } + + pub fn new_initialized(global: &GlobalScope, + media: DOMString, + matches: bool) -> Root { + let ev = box MediaQueryListEvent { + event: Event::new_inherited(), + media: media, + matches: Cell::new(matches) + }; + reflect_dom_object(ev, global, MediaQueryListEventBinding::Wrap) + } + + pub fn new(global: &GlobalScope, type_: Atom, + bubbles: bool, cancelable: bool, + media: DOMString, matches: bool) -> Root { + let ev = MediaQueryListEvent::new_initialized(global, media, matches); + { + let event = ev.upcast::(); + event.init_event(type_, bubbles, cancelable); + } + ev + } + + pub fn Constructor(window: &Window, type_: DOMString, + init: &MediaQueryListEventInit) + -> Fallible> { + let global = window.upcast::(); + Ok(MediaQueryListEvent::new(global, Atom::from(type_), + init.parent.bubbles, init.parent.cancelable, + init.media.clone(), init.matches)) + } +} + +impl MediaQueryListEventMethods for MediaQueryListEvent { + // https://drafts.csswg.org/cssom-view/#dom-mediaquerylistevent-media + fn Media(&self) -> DOMString { + self.media.clone() + } + + // https://drafts.csswg.org/cssom-view/#dom-mediaquerylistevent-matches + fn Matches(&self) -> bool { + self.matches.get() + } + + // https://dom.spec.whatwg.org/#dom-event-istrusted + fn IsTrusted(&self) -> bool { + self.upcast::().IsTrusted() + } +} diff --git a/components/script/dom/mod.rs b/components/script/dom/mod.rs index b432b5a12e5..8c074a48ba5 100644 --- a/components/script/dom/mod.rs +++ b/components/script/dom/mod.rs @@ -371,6 +371,7 @@ pub mod location; pub mod mediaerror; pub mod medialist; pub mod mediaquerylist; +pub mod mediaquerylistevent; pub mod messageevent; pub mod mimetype; pub mod mimetypearray; diff --git a/components/script/dom/webidls/MediaQueryListEvent.webidl b/components/script/dom/webidls/MediaQueryListEvent.webidl new file mode 100644 index 00000000000..877ca17b60b --- /dev/null +++ b/components/script/dom/webidls/MediaQueryListEvent.webidl @@ -0,0 +1,15 @@ +/* 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/. */ + +// https://drafts.csswg.org/cssom-view/#dom-mediaquerylistevent-mediaquerylistevent +[Constructor(DOMString type, optional MediaQueryListEventInit eventInitDict), Exposed=(Window)] +interface MediaQueryListEvent : Event { + readonly attribute DOMString media; + readonly attribute boolean matches; +}; + +dictionary MediaQueryListEventInit : EventInit { + DOMString media = ""; + boolean matches = false; +};