TextDecoder's Decode now receives a BufferSource as input

This commit is contained in:
Christian Poveda 2018-03-24 08:58:23 -05:00
parent e8fdc677f4
commit 2c47e7e1db
3 changed files with 32 additions and 46 deletions

View file

@ -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<USVString> {
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<ArrayBufferViewOrArrayBuffer>,
_options: &TextDecodeOptions
) -> Fallible<USVString> {
match input {
Some(arr) => {
let vec: Vec<u8> = 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()))
}
}
}

View file

@ -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);
};