Implement TextDecoder#decode.

This commit is contained in:
Keith Yeung 2015-04-08 14:26:42 +02:00 committed by Ms2ger
parent c0d4e27d79
commit e2929403ef
8 changed files with 37 additions and 142 deletions

View file

@ -7,16 +7,21 @@ use dom::bindings::codegen::Bindings::TextDecoderBinding::TextDecoderMethods;
use dom::bindings::error::{Error, Fallible};
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JSRef, Temporary};
use dom::bindings::str::USVString;
use dom::bindings::trace::JSTraceable;
use dom::bindings::utils::{Reflector, reflect_dom_object};
use util::str::DOMString;
use encoding::Encoding;
use encoding::types::EncodingRef;
use encoding::types::{EncodingRef, DecoderTrap};
use encoding::label::encoding_from_whatwg_label;
use js::jsapi::{JSContext, JSObject};
use js::jsfriendapi::bindgen::JS_GetObjectAsArrayBufferView;
use std::borrow::ToOwned;
use std::ptr;
use std::slice;
#[dom_struct]
pub struct TextDecoder {
@ -62,4 +67,33 @@ impl<'a> TextDecoderMethods for JSRef<'a, TextDecoder> {
fn Fatal(self) -> bool {
self.fatal
}
#[allow(unsafe_code)]
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())),
};
let mut length = 0;
let mut data = ptr::null_mut();
if unsafe { JS_GetObjectAsArrayBufferView(cx, input, &mut length, &mut data).is_null() } {
return Err(Error::Type("Argument to TextDecoder.decode is not an ArrayBufferView".to_owned()));
}
let buffer = unsafe {
slice::from_raw_parts(data as *const _, length as usize)
};
let trap = if self.fatal {
DecoderTrap::Strict
} else {
DecoderTrap::Replace
};
match self.encoding.decode(buffer, trap) {
Ok(s) => Ok(USVString(s)),
Err(_) => Err(Error::Type("Decoding failed".to_owned())),
}
}
}

View file

@ -16,4 +16,6 @@ interface TextDecoder {
readonly attribute boolean fatal;
//readonly attribute boolean ignoreBOM;
//USVString decode(optional BufferSource input, optional TextDecodeOptions options);
[Throws]
USVString decode(optional object input);
};

View file

@ -1,20 +0,0 @@
[api-surrogates-utf8.html]
type: testharness
[Invalid surrogates encoded into UTF-8: Sanity check]
expected: FAIL
[Invalid surrogates encoded into UTF-8: Surrogate half (low)]
expected: FAIL
[Invalid surrogates encoded into UTF-8: Surrogate half (high)]
expected: FAIL
[Invalid surrogates encoded into UTF-8: Surrogate half (low), in a string]
expected: FAIL
[Invalid surrogates encoded into UTF-8: Surrogate half (high), in a string]
expected: FAIL
[Invalid surrogates encoded into UTF-8: Wrong order]
expected: FAIL

View file

@ -12,12 +12,6 @@
[TextDecoder interface: new TextDecoder() must inherit property "ignoreBOM" with the proper type (2)]
expected: FAIL
[TextDecoder interface: new TextDecoder() must inherit property "decode" with the proper type (3)]
expected: FAIL
[TextDecoder interface: calling decode(BufferSource,TextDecodeOptions) on new TextDecoder() with too few arguments must throw TypeError]
expected: FAIL
[TextEncoder interface object length]
expected: FAIL

View file

@ -3,21 +3,12 @@
[iso-2022-jp decoder: Error ESC]
expected: FAIL
[iso-2022-jp decoder: Error ESC, character]
expected: FAIL
[iso-2022-jp decoder: ASCII ESC, character]
expected: FAIL
[iso-2022-jp decoder: Double ASCII ESC, character]
expected: FAIL
[iso-2022-jp decoder: character, ASCII ESC, character]
expected: FAIL
[iso-2022-jp decoder: characters]
expected: FAIL
[iso-2022-jp decoder: SO / SI]
expected: FAIL
@ -27,12 +18,6 @@
[iso-2022-jp decoder: Roman ESC, SO / SI]
expected: FAIL
[iso-2022-jp decoder: Roman ESC, error ESC, Katakana ESC]
expected: FAIL
[iso-2022-jp decoder: Katakana ESC, character]
expected: FAIL
[iso-2022-jp decoder: Katakana ESC, multibyte ESC, character]
expected: FAIL
@ -42,18 +27,6 @@
[iso-2022-jp decoder: Katakana ESC, error ESC #2, character]
expected: FAIL
[iso-2022-jp decoder: Katakana ESC, character, Katakana ESC, character]
expected: FAIL
[iso-2022-jp decoder: Katakana ESC, SO / SI]
expected: FAIL
[iso-2022-jp decoder: Multibyte ESC, character]
expected: FAIL
[iso-2022-jp decoder: Multibyte ESC #2, character]
expected: FAIL
[iso-2022-jp decoder: Multibyte ESC, error ESC, character]
expected: FAIL
@ -75,30 +48,6 @@
[iso-2022-jp decoder: Multibyte ESC, lead error byte]
expected: FAIL
[iso-2022-jp decoder: Multibyte ESC, trail error byte]
expected: FAIL
[iso-2022-jp decoder: character, error ESC]
expected: FAIL
[iso-2022-jp decoder: character, error ESC #2]
expected: FAIL
[iso-2022-jp decoder: character, error ESC #3]
expected: FAIL
[iso-2022-jp decoder: character, ASCII ESC]
expected: FAIL
[iso-2022-jp decoder: character, Roman ESC]
expected: FAIL
[iso-2022-jp decoder: character, Katakana ESC]
expected: FAIL
[iso-2022-jp decoder: character, Multibyte ESC]
expected: FAIL
[iso-2022-jp decoder: character, Multibyte ESC #2]
expected: FAIL

View file

@ -1,47 +1,20 @@
[textdecoder-streaming.html]
type: testharness
[Streaming decode: utf-8, 1 byte window]
expected: FAIL
[Streaming decode: utf-8, 2 byte window]
expected: FAIL
[Streaming decode: utf-8, 3 byte window]
expected: FAIL
[Streaming decode: utf-8, 4 byte window]
expected: FAIL
[Streaming decode: utf-8, 5 byte window]
expected: FAIL
[Streaming decode: utf-16le, 1 byte window]
expected: FAIL
[Streaming decode: utf-16le, 2 byte window]
expected: FAIL
[Streaming decode: utf-16le, 3 byte window]
expected: FAIL
[Streaming decode: utf-16le, 4 byte window]
expected: FAIL
[Streaming decode: utf-16le, 5 byte window]
expected: FAIL
[Streaming decode: utf-16be, 1 byte window]
expected: FAIL
[Streaming decode: utf-16be, 2 byte window]
expected: FAIL
[Streaming decode: utf-16be, 3 byte window]
expected: FAIL
[Streaming decode: utf-16be, 4 byte window]
expected: FAIL
[Streaming decode: utf-16be, 5 byte window]
expected: FAIL

View file

@ -1,17 +0,0 @@
[textdecoder-utf16-surrogates.html]
type: testharness
[utf-16le - lone surrogate lead]
expected: FAIL
[utf-16le - lone surrogate trail]
expected: FAIL
[utf-16le - unmatched surrogate lead]
expected: FAIL
[utf-16le - unmatched surrogate trail]
expected: FAIL
[utf-16le - swapped surrogate pair]
expected: FAIL

View file

@ -1,20 +0,0 @@
[textencoder-utf16-surrogates.html]
type: testharness
[USVString handling: lone surrogate lead]
expected: FAIL
[USVString handling: lone surrogate trail]
expected: FAIL
[USVString handling: unmatched surrogate lead]
expected: FAIL
[USVString handling: unmatched surrogate trail]
expected: FAIL
[USVString handling: swapped surrogate pair]
expected: FAIL
[USVString handling: properly encoded MUSICAL SYMBOL G CLEF (U+1D11E)]
expected: FAIL