Auto merge of #20413 - christianpoveda:issue_20344, r=jdm

TextDecoder's Decode now receives a BufferSource as input

<!-- Please describe your changes on the following line: -->

r? jdm

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #20344 (github issue number if applicable).

<!-- Either: -->
- [X] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/20413)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2018-03-24 17:46:46 -04:00 committed by GitHub
commit 2f94e0d2a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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);
};