mirror of
https://github.com/servo/servo.git
synced 2025-06-20 23:28:59 +01:00
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:
commit
2f94e0d2a8
3 changed files with 32 additions and 46 deletions
|
@ -3,7 +3,8 @@
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use dom::bindings::codegen::Bindings::TextDecoderBinding;
|
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::error::{Error, Fallible};
|
||||||
use dom::bindings::reflector::{Reflector, reflect_dom_object};
|
use dom::bindings::reflector::{Reflector, reflect_dom_object};
|
||||||
use dom::bindings::root::DomRoot;
|
use dom::bindings::root::DomRoot;
|
||||||
|
@ -11,7 +12,6 @@ use dom::bindings::str::{DOMString, USVString};
|
||||||
use dom::globalscope::GlobalScope;
|
use dom::globalscope::GlobalScope;
|
||||||
use dom_struct::dom_struct;
|
use dom_struct::dom_struct;
|
||||||
use encoding_rs::Encoding;
|
use encoding_rs::Encoding;
|
||||||
use js::jsapi::{JSContext, JSObject};
|
|
||||||
use std::borrow::ToOwned;
|
use std::borrow::ToOwned;
|
||||||
|
|
||||||
#[dom_struct]
|
#[dom_struct]
|
||||||
|
@ -65,32 +65,30 @@ impl TextDecoderMethods for TextDecoder {
|
||||||
self.fatal
|
self.fatal
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unsafe_code)]
|
|
||||||
// https://encoding.spec.whatwg.org/#dom-textdecoder-decode
|
// https://encoding.spec.whatwg.org/#dom-textdecoder-decode
|
||||||
unsafe fn Decode(&self, _cx: *mut JSContext, input: Option<*mut JSObject>)
|
fn Decode(
|
||||||
-> Fallible<USVString> {
|
&self,
|
||||||
let input = match input {
|
input: Option<ArrayBufferViewOrArrayBuffer>,
|
||||||
Some(input) => input,
|
_options: &TextDecodeOptions
|
||||||
None => return Ok(USVString("".to_owned())),
|
) -> Fallible<USVString> {
|
||||||
};
|
match input {
|
||||||
|
Some(arr) => {
|
||||||
typedarray!(in(_cx) let data_res: ArrayBufferView = input);
|
let vec: Vec<u8> = match arr {
|
||||||
let mut data = match data_res {
|
ArrayBufferViewOrArrayBuffer::ArrayBufferView(mut a) => a.to_vec(),
|
||||||
Ok(data) => data,
|
ArrayBufferViewOrArrayBuffer::ArrayBuffer(mut a) => a.to_vec()
|
||||||
Err(_) => {
|
};
|
||||||
return Err(Error::Type("Argument to TextDecoder.decode is not an ArrayBufferView".to_owned()));
|
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()))
|
||||||
}
|
}
|
||||||
};
|
None => Ok(USVString("".to_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()))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,15 +5,18 @@
|
||||||
// https://encoding.spec.whatwg.org/#interface-textdecoder
|
// https://encoding.spec.whatwg.org/#interface-textdecoder
|
||||||
dictionary TextDecoderOptions {
|
dictionary TextDecoderOptions {
|
||||||
boolean fatal = false;
|
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)]
|
[Constructor(optional DOMString label = "utf-8", optional TextDecoderOptions options), Exposed=(Window,Worker)]
|
||||||
interface TextDecoder {
|
interface TextDecoder {
|
||||||
readonly attribute DOMString encoding;
|
readonly attribute DOMString encoding;
|
||||||
readonly attribute boolean fatal;
|
readonly attribute boolean fatal;
|
||||||
//readonly attribute boolean ignoreBOM;
|
// readonly attribute boolean ignoreBOM;
|
||||||
//USVString decode(optional BufferSource input, optional TextDecodeOptions options);
|
|
||||||
[Throws]
|
[Throws]
|
||||||
USVString decode(optional object input);
|
USVString decode(optional BufferSource input, optional TextDecodeOptions options);
|
||||||
};
|
};
|
||||||
|
|
|
@ -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
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue