diff --git a/components/script/dom/textdecoder.rs b/components/script/dom/textdecoder.rs index b695af32fe8..38efa977e7f 100644 --- a/components/script/dom/textdecoder.rs +++ b/components/script/dom/textdecoder.rs @@ -3,7 +3,8 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use dom::bindings::codegen::Bindings::TextDecoderBinding; -use dom::bindings::codegen::Bindings::TextDecoderBinding::TextDecoderMethods; +use dom::bindings::codegen::Bindings::TextDecoderBinding::{TextDecoderMethods, TextDecodeOptions}; +use dom::bindings::codegen::UnionTypes::ArrayBufferViewOrArrayBuffer; use dom::bindings::error::{Error, Fallible}; use dom::bindings::reflector::{Reflector, reflect_dom_object}; use dom::bindings::root::DomRoot; @@ -11,7 +12,6 @@ use dom::bindings::str::{DOMString, USVString}; use dom::globalscope::GlobalScope; use dom_struct::dom_struct; use encoding_rs::Encoding; -use js::jsapi::{JSContext, JSObject}; use std::borrow::ToOwned; #[dom_struct] @@ -65,32 +65,30 @@ impl TextDecoderMethods for TextDecoder { self.fatal } - #[allow(unsafe_code)] // https://encoding.spec.whatwg.org/#dom-textdecoder-decode - unsafe fn Decode(&self, _cx: *mut JSContext, input: Option<*mut JSObject>) - -> Fallible { - let input = match input { - Some(input) => input, - None => return Ok(USVString("".to_owned())), - }; - - typedarray!(in(_cx) let data_res: ArrayBufferView = input); - let mut data = match data_res { - Ok(data) => data, - Err(_) => { - return Err(Error::Type("Argument to TextDecoder.decode is not an ArrayBufferView".to_owned())); + fn Decode( + &self, + input: Option, + _options: &TextDecodeOptions + ) -> Fallible { + match input { + Some(arr) => { + let vec: Vec = match arr { + ArrayBufferViewOrArrayBuffer::ArrayBufferView(mut a) => a.to_vec(), + ArrayBufferViewOrArrayBuffer::ArrayBuffer(mut a) => a.to_vec() + }; + let s = if self.fatal { + match self.encoding.decode_without_bom_handling_and_without_replacement(&vec) { + Some(s) => s, + None => return Err(Error::Type("Decoding failed".to_owned())), + } + } else { + let (s, _has_errors) = self.encoding.decode_without_bom_handling(&vec); + s + }; + Ok(USVString(s.into_owned())) } - }; - - let s = if self.fatal { - match self.encoding.decode_without_bom_handling_and_without_replacement(data.as_slice()) { - Some(s) => s, - None => return Err(Error::Type("Decoding failed".to_owned())), - } - } else { - let (s, _has_errors) = self.encoding.decode_without_bom_handling(data.as_slice()); - s - }; - Ok(USVString(s.into_owned())) + None => Ok(USVString("".to_owned())) + } } } diff --git a/components/script/dom/webidls/TextDecoder.webidl b/components/script/dom/webidls/TextDecoder.webidl index e7292ed3061..b511fec3d79 100644 --- a/components/script/dom/webidls/TextDecoder.webidl +++ b/components/script/dom/webidls/TextDecoder.webidl @@ -5,15 +5,18 @@ // https://encoding.spec.whatwg.org/#interface-textdecoder dictionary TextDecoderOptions { boolean fatal = false; - //boolean ignoreBOM = false; + // boolean ignoreBOM = false; +}; + +dictionary TextDecodeOptions { + // boolean stream = false; }; [Constructor(optional DOMString label = "utf-8", optional TextDecoderOptions options), Exposed=(Window,Worker)] interface TextDecoder { readonly attribute DOMString encoding; readonly attribute boolean fatal; - //readonly attribute boolean ignoreBOM; - //USVString decode(optional BufferSource input, optional TextDecodeOptions options); + // readonly attribute boolean ignoreBOM; [Throws] - USVString decode(optional object input); + USVString decode(optional BufferSource input, optional TextDecodeOptions options); }; diff --git a/tests/wpt/metadata/encoding/api-basics.html.ini b/tests/wpt/metadata/encoding/api-basics.html.ini deleted file mode 100644 index 4d672211178..00000000000 --- a/tests/wpt/metadata/encoding/api-basics.html.ini +++ /dev/null @@ -1,15 +0,0 @@ -[api-basics.html] - type: testharness - bug: https://github.com/servo/servo/issues/13236 - [Encode/decode round trip: utf-8] - expected: FAIL - - [Decode sample: utf-16le] - expected: FAIL - - [Decode sample: utf-16be] - expected: FAIL - - [Decode sample: utf-16] - expected: FAIL -