From 220a471b149c6dbeaeee4af44f3a122d66f719e9 Mon Sep 17 00:00:00 2001 From: cdeler Date: Sat, 5 Jan 2019 22:20:25 +0300 Subject: implemented missed constructor for DOMException; fixed the tests expectations --- components/script/dom/domexception.rs | 123 ++++++++++++++++------ components/script/dom/webidls/DOMException.webidl | 6 +- 2 files changed, 95 insertions(+), 34 deletions(-) (limited to 'components/script') diff --git a/components/script/dom/domexception.rs b/components/script/dom/domexception.rs index 6f35b23f7a8..b5b0ca5a8c4 100644 --- a/components/script/dom/domexception.rs +++ b/components/script/dom/domexception.rs @@ -5,6 +5,7 @@ use crate::dom::bindings::codegen::Bindings::DOMExceptionBinding; use crate::dom::bindings::codegen::Bindings::DOMExceptionBinding::DOMExceptionConstants; use crate::dom::bindings::codegen::Bindings::DOMExceptionBinding::DOMExceptionMethods; +use crate::dom::bindings::error::Error; use crate::dom::bindings::reflector::{reflect_dom_object, Reflector}; use crate::dom::bindings::root::DomRoot; use crate::dom::bindings::str::DOMString; @@ -38,43 +39,46 @@ pub enum DOMErrorName { NotReadableError = DOMExceptionConstants::NOT_READABLE_ERR, } +impl DOMErrorName { + pub fn from(s: &DOMString) -> Option { + match s.as_ref() { + "IndexSizeError" => Some(DOMErrorName::IndexSizeError), + "HierarchyRequestError" => Some(DOMErrorName::HierarchyRequestError), + "WrongDocumentError" => Some(DOMErrorName::WrongDocumentError), + "InvalidCharacterError" => Some(DOMErrorName::InvalidCharacterError), + "NoModificationAllowedError" => Some(DOMErrorName::NoModificationAllowedError), + "NotFoundError" => Some(DOMErrorName::NotFoundError), + "NotSupportedError" => Some(DOMErrorName::NotSupportedError), + "InUseAttributeError" => Some(DOMErrorName::InUseAttributeError), + "InvalidStateError" => Some(DOMErrorName::InvalidStateError), + "SyntaxError" => Some(DOMErrorName::SyntaxError), + "InvalidModificationError" => Some(DOMErrorName::InvalidModificationError), + "NamespaceError" => Some(DOMErrorName::NamespaceError), + "InvalidAccessError" => Some(DOMErrorName::InvalidAccessError), + "SecurityError" => Some(DOMErrorName::SecurityError), + "NetworkError" => Some(DOMErrorName::NetworkError), + "AbortError" => Some(DOMErrorName::AbortError), + "TypeMismatchError" => Some(DOMErrorName::TypeMismatchError), + "QuotaExceededError" => Some(DOMErrorName::QuotaExceededError), + "TimeoutError" => Some(DOMErrorName::TimeoutError), + "InvalidNodeTypeError" => Some(DOMErrorName::InvalidNodeTypeError), + "DataCloneError" => Some(DOMErrorName::DataCloneError), + "NotReadableError" => Some(DOMErrorName::NotReadableError), + _ => None, + } + } +} + #[dom_struct] pub struct DOMException { reflector_: Reflector, - code: DOMErrorName, + message: DOMString, + name: DOMString, } impl DOMException { - fn new_inherited(code: DOMErrorName) -> DOMException { - DOMException { - reflector_: Reflector::new(), - code: code, - } - } - - pub fn new(global: &GlobalScope, code: DOMErrorName) -> DomRoot { - reflect_dom_object( - Box::new(DOMException::new_inherited(code)), - global, - DOMExceptionBinding::Wrap, - ) - } -} - -impl DOMExceptionMethods for DOMException { - // https://heycam.github.io/webidl/#dfn-DOMException - fn Code(&self) -> u16 { - self.code as u16 - } - - // https://heycam.github.io/webidl/#idl-DOMException-error-names - fn Name(&self) -> DOMString { - DOMString::from(format!("{:?}", self.code)) - } - - // https://heycam.github.io/webidl/#error-names - fn Message(&self) -> DOMString { - let message = match self.code { + fn get_error_data_by_code(code: DOMErrorName) -> (DOMString, DOMString) { + let message = match &code { DOMErrorName::IndexSizeError => "The index is not in the allowed range.", DOMErrorName::HierarchyRequestError => { "The operation would yield an incorrect node tree." @@ -105,11 +109,64 @@ impl DOMExceptionMethods for DOMException { DOMErrorName::NotReadableError => "The I/O read operation failed.", }; - DOMString::from(message) + ( + DOMString::from(message), + DOMString::from(format!("{:?}", code)), + ) + } + + fn new_inherited(message_: DOMString, name_: DOMString) -> DOMException { + DOMException { + reflector_: Reflector::new(), + message: message_, + name: name_, + } + } + + pub fn new(global: &GlobalScope, code: DOMErrorName) -> DomRoot { + let (message, name) = DOMException::get_error_data_by_code(code); + + reflect_dom_object( + Box::new(DOMException::new_inherited(message, name)), + global, + DOMExceptionBinding::Wrap, + ) + } + + pub fn Constructor( + global: &GlobalScope, + message: DOMString, + name: DOMString, + ) -> Result, Error> { + Ok(reflect_dom_object( + Box::new(DOMException::new_inherited(message, name)), + global, + DOMExceptionBinding::Wrap, + )) + } +} + +impl DOMExceptionMethods for DOMException { + // https://heycam.github.io/webidl/#dfn-DOMException + fn Code(&self) -> u16 { + match DOMErrorName::from(&self.name) { + Some(code) => code as u16, + None => 0 as u16, + } + } + + // https://heycam.github.io/webidl/#idl-DOMException-error-names + fn Name(&self) -> DOMString { + self.name.clone() + } + + // https://heycam.github.io/webidl/#error-names + fn Message(&self) -> DOMString { + self.message.clone() } // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-error.prototype.tostring fn Stringifier(&self) -> DOMString { - DOMString::from(format!("{}: {}", self.Name(), self.Message())) + DOMString::from(format!("{}: {}", self.name, self.message)) } } diff --git a/components/script/dom/webidls/DOMException.webidl b/components/script/dom/webidls/DOMException.webidl index e2f95cbdda1..d9ec6f4dc84 100644 --- a/components/script/dom/webidls/DOMException.webidl +++ b/components/script/dom/webidls/DOMException.webidl @@ -7,7 +7,11 @@ * https://heycam.github.io/webidl/#es-DOMException-constructor-object */ -[ExceptionClass, Exposed=(Window,Worker)] +[ + ExceptionClass, + Exposed=(Window,Worker), + Constructor(optional DOMString message="", optional DOMString name="Error") +] interface DOMException { const unsigned short INDEX_SIZE_ERR = 1; const unsigned short DOMSTRING_SIZE_ERR = 2; // historical -- cgit v1.2.3