Files
tubestation/servo/components/script/dom/textdecoder.rs
Christian Poveda d726461e64 servo: Merge #20413 - TextDecoder's Decode now receives a BufferSource as input (from christianpoveda:issue_20344); r=jdm
<!-- 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. -->

Source-Repo: https://github.com/servo/servo
Source-Revision: 2f94e0d2a8c79f999e64231d2dbce5582ef1a689
2018-03-24 17:46:46 -04:00

95 lines
3.4 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* 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, 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;
use dom::bindings::str::{DOMString, USVString};
use dom::globalscope::GlobalScope;
use dom_struct::dom_struct;
use encoding_rs::Encoding;
use std::borrow::ToOwned;
#[dom_struct]
pub struct TextDecoder {
reflector_: Reflector,
encoding: &'static Encoding,
fatal: bool,
}
impl TextDecoder {
fn new_inherited(encoding: &'static Encoding, fatal: bool) -> TextDecoder {
TextDecoder {
reflector_: Reflector::new(),
encoding: encoding,
fatal: fatal,
}
}
fn make_range_error() -> Fallible<DomRoot<TextDecoder>> {
Err(Error::Range("The given encoding is not supported.".to_owned()))
}
pub fn new(global: &GlobalScope, encoding: &'static Encoding, fatal: bool) -> DomRoot<TextDecoder> {
reflect_dom_object(Box::new(TextDecoder::new_inherited(encoding, fatal)),
global,
TextDecoderBinding::Wrap)
}
/// <https://encoding.spec.whatwg.org/#dom-textdecoder>
pub fn Constructor(global: &GlobalScope,
label: DOMString,
options: &TextDecoderBinding::TextDecoderOptions)
-> Fallible<DomRoot<TextDecoder>> {
let encoding = match Encoding::for_label_no_replacement(label.as_bytes()) {
None => return TextDecoder::make_range_error(),
Some(enc) => enc
};
Ok(TextDecoder::new(global, encoding, options.fatal))
}
}
impl TextDecoderMethods for TextDecoder {
// https://encoding.spec.whatwg.org/#dom-textdecoder-encoding
fn Encoding(&self) -> DOMString {
DOMString::from(self.encoding.name().to_ascii_lowercase())
}
// https://encoding.spec.whatwg.org/#dom-textdecoder-fatal
fn Fatal(&self) -> bool {
self.fatal
}
// https://encoding.spec.whatwg.org/#dom-textdecoder-decode
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()))
}
None => Ok(USVString("".to_owned()))
}
}
}