Throw RangeErrors in TextEncoder/TextDecoder constructors.

Fixes #5620.
Fix the TODOs and FIXMEs to comply with the spec.
Add test case for passing invalid invalid labels.
Update test metadata; three test cases have been resolved upstream and
will be fixed whenever the rust-encoding dependency is sufficiently upgraded.
This commit is contained in:
Aneesh Agrawal 2015-04-10 02:42:35 -04:00
parent f3aee90b06
commit 97301400a5
6 changed files with 37 additions and 130 deletions

View file

@ -39,6 +39,10 @@ impl TextDecoder {
} }
} }
fn make_range_error() -> Fallible<Temporary<TextDecoder>> {
Err(Error::Range("The given encoding is not supported.".to_owned()))
}
pub fn new(global: GlobalRef, encoding: EncodingRef, fatal: bool) -> Temporary<TextDecoder> { pub fn new(global: GlobalRef, encoding: EncodingRef, fatal: bool) -> Temporary<TextDecoder> {
reflect_dom_object(box TextDecoder::new_inherited(encoding, fatal), reflect_dom_object(box TextDecoder::new_inherited(encoding, fatal),
global, global,
@ -51,14 +55,23 @@ impl TextDecoder {
options: &TextDecoderBinding::TextDecoderOptions) options: &TextDecoderBinding::TextDecoderOptions)
-> Fallible<Temporary<TextDecoder>> { -> Fallible<Temporary<TextDecoder>> {
let encoding = match encoding_from_whatwg_label(&label) { let encoding = match encoding_from_whatwg_label(&label) {
Some(enc) => enc, None => return TextDecoder::make_range_error(),
// FIXME: Should throw a RangeError as per spec Some(enc) => enc
None => return Err(Error::Syntax), };
// The rust-encoding crate has WHATWG compatibility, so we are
// guaranteed to have a whatwg_name because we successfully got
// the encoding from encoding_from_whatwg_label.
// Use match + panic! instead of unwrap for better error message
match encoding.whatwg_name() {
None => panic!("Label {} fits valid encoding without valid name", label),
Some("replacement") => return TextDecoder::make_range_error(),
_ => ()
}; };
Ok(TextDecoder::new(global, encoding, options.fatal)) Ok(TextDecoder::new(global, encoding, options.fatal))
} }
} }
impl<'a> TextDecoderMethods for JSRef<'a, TextDecoder> { impl<'a> TextDecoderMethods for JSRef<'a, TextDecoder> {
fn Encoding(self) -> DOMString { fn Encoding(self) -> DOMString {
self.encoding.whatwg_name().unwrap().to_owned() self.encoding.whatwg_name().unwrap().to_owned()

View file

@ -6,7 +6,7 @@ use dom::bindings::codegen::Bindings::TextEncoderBinding;
use dom::bindings::codegen::Bindings::TextEncoderBinding::TextEncoderMethods; use dom::bindings::codegen::Bindings::TextEncoderBinding::TextEncoderMethods;
use dom::bindings::global::GlobalRef; use dom::bindings::global::GlobalRef;
use dom::bindings::error::Fallible; use dom::bindings::error::Fallible;
use dom::bindings::error::Error::IndexSize; use dom::bindings::error::Error::Range;
use dom::bindings::js::{JSRef, Temporary}; use dom::bindings::js::{JSRef, Temporary};
use dom::bindings::str::USVString; use dom::bindings::str::USVString;
use dom::bindings::utils::{Reflector, reflect_dom_object}; use dom::bindings::utils::{Reflector, reflect_dom_object};
@ -54,8 +54,7 @@ impl TextEncoder {
Some(enc) => enc, Some(enc) => enc,
None => { None => {
debug!("Encoding Label Not Supported"); debug!("Encoding Label Not Supported");
// TODO: should throw RangeError return Err(Range("The given encoding is not supported.".to_owned()))
return Err(IndexSize)
} }
}; };
@ -65,8 +64,7 @@ impl TextEncoder {
} }
_ => { _ => {
debug!("Encoding Not UTF"); debug!("Encoding Not UTF");
// TODO: should throw RangeError return Err(Range("The encoding must be utf-8, utf-16le, or utf-16be.".to_owned()))
Err(IndexSize)
} }
} }
} }

View file

@ -10951,6 +10951,10 @@
"path": "encoding/api-basics.html", "path": "encoding/api-basics.html",
"url": "/encoding/api-basics.html" "url": "/encoding/api-basics.html"
}, },
{
"path": "encoding/api-invalid-label.html",
"url": "/encoding/api-invalid-label.html"
},
{ {
"path": "encoding/api-replacement-encodings.html", "path": "encoding/api-replacement-encodings.html",
"url": "/encoding/api-replacement-encodings.html" "url": "/encoding/api-replacement-encodings.html"

View file

@ -1,20 +1,4 @@
[api-replacement-encodings.html] [api-replacement-encodings.html]
type: testharness type: testharness
[The "replacement" label should not be a known encoding.]
expected: FAIL
[Label for "replacement" should be rejected by API: csiso2022kr]
expected: FAIL
[Label for "replacement" should be rejected by API: hz-gb-2312] [Label for "replacement" should be rejected by API: hz-gb-2312]
expected: FAIL expected: FAIL
[Label for "replacement" should be rejected by API: iso-2022-cn]
expected: FAIL
[Label for "replacement" should be rejected by API: iso-2022-cn-ext]
expected: FAIL
[Label for "replacement" should be rejected by API: iso-2022-kr]
expected: FAIL

View file

@ -1,113 +1,7 @@
[textencoder-constructor-non-utf.html] [textencoder-constructor-non-utf.html]
type: testharness type: testharness
[Non-UTF encodings supported only for decode, not encode: ibm866]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: iso-8859-2]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: iso-8859-3]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: iso-8859-4]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: iso-8859-5]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: iso-8859-6]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: iso-8859-7]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: iso-8859-8]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: iso-8859-8-i]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: iso-8859-10]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: iso-8859-13]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: iso-8859-14]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: iso-8859-15]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: iso-8859-16]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: koi8-r]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: koi8-u]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: macintosh]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: windows-874]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: windows-1250]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: windows-1251]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: windows-1252]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: windows-1253]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: windows-1254]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: windows-1255]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: windows-1256]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: windows-1257]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: windows-1258]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: x-mac-cyrillic]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: gbk] [Non-UTF encodings supported only for decode, not encode: gbk]
expected: FAIL expected: FAIL
[Non-UTF encodings supported only for decode, not encode: gb18030]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: big5]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: euc-jp]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: iso-2022-jp]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: shift_jis]
expected: FAIL
[Non-UTF encodings supported only for decode, not encode: euc-kr]
expected: FAIL
[UTF encodings are supported for encode and decode: utf-16le] [UTF encodings are supported for encode and decode: utf-16le]
expected: FAIL expected: FAIL
[Non-UTF encodings supported only for decode, not encode: x-user-defined]
expected: FAIL

View file

@ -0,0 +1,14 @@
<!DOCTYPE html>
<title>Encoding API: invalid label</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
var invalidLabel = "invalid-invalidLabel"
test(function() {
assert_throws({name: 'RangeError'}, function() { new TextEncoder(invalidLabel); });
assert_throws({name: 'RangeError'}, function() { new TextDecoder(invalidLabel); });
}, 'Invalid label "' + invalidLabel + '" should be rejected by API.');
</script>