diff --git a/components/script/dom/textdecoder.rs b/components/script/dom/textdecoder.rs index 714200f9a8f..4b3333234a3 100644 --- a/components/script/dom/textdecoder.rs +++ b/components/script/dom/textdecoder.rs @@ -39,6 +39,10 @@ impl TextDecoder { } } + fn make_range_error() -> Fallible> { + Err(Error::Range("The given encoding is not supported.".to_owned())) + } + pub fn new(global: GlobalRef, encoding: EncodingRef, fatal: bool) -> Temporary { reflect_dom_object(box TextDecoder::new_inherited(encoding, fatal), global, @@ -51,14 +55,23 @@ impl TextDecoder { options: &TextDecoderBinding::TextDecoderOptions) -> Fallible> { let encoding = match encoding_from_whatwg_label(&label) { - Some(enc) => enc, - // FIXME: Should throw a RangeError as per spec - None => return Err(Error::Syntax), + None => return TextDecoder::make_range_error(), + Some(enc) => enc + }; + // 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)) } } + impl<'a> TextDecoderMethods for JSRef<'a, TextDecoder> { fn Encoding(self) -> DOMString { self.encoding.whatwg_name().unwrap().to_owned() diff --git a/components/script/dom/textencoder.rs b/components/script/dom/textencoder.rs index e568a6dfa75..4f1045cc7ac 100644 --- a/components/script/dom/textencoder.rs +++ b/components/script/dom/textencoder.rs @@ -6,7 +6,7 @@ use dom::bindings::codegen::Bindings::TextEncoderBinding; use dom::bindings::codegen::Bindings::TextEncoderBinding::TextEncoderMethods; use dom::bindings::global::GlobalRef; 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::str::USVString; use dom::bindings::utils::{Reflector, reflect_dom_object}; @@ -54,8 +54,7 @@ impl TextEncoder { Some(enc) => enc, None => { debug!("Encoding Label Not Supported"); - // TODO: should throw RangeError - return Err(IndexSize) + return Err(Range("The given encoding is not supported.".to_owned())) } }; @@ -65,8 +64,7 @@ impl TextEncoder { } _ => { debug!("Encoding Not UTF"); - // TODO: should throw RangeError - Err(IndexSize) + return Err(Range("The encoding must be utf-8, utf-16le, or utf-16be.".to_owned())) } } } diff --git a/tests/wpt/metadata/MANIFEST.json b/tests/wpt/metadata/MANIFEST.json index 0c4dc0c1164..5584274dfb0 100644 --- a/tests/wpt/metadata/MANIFEST.json +++ b/tests/wpt/metadata/MANIFEST.json @@ -10951,6 +10951,10 @@ "path": "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", "url": "/encoding/api-replacement-encodings.html" diff --git a/tests/wpt/metadata/encoding/api-replacement-encodings.html.ini b/tests/wpt/metadata/encoding/api-replacement-encodings.html.ini index 94c7203d3c2..b890466bae1 100644 --- a/tests/wpt/metadata/encoding/api-replacement-encodings.html.ini +++ b/tests/wpt/metadata/encoding/api-replacement-encodings.html.ini @@ -1,20 +1,4 @@ [api-replacement-encodings.html] 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] 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 - diff --git a/tests/wpt/metadata/encoding/textencoder-constructor-non-utf.html.ini b/tests/wpt/metadata/encoding/textencoder-constructor-non-utf.html.ini index 4d846b4443c..4c84622faf8 100644 --- a/tests/wpt/metadata/encoding/textencoder-constructor-non-utf.html.ini +++ b/tests/wpt/metadata/encoding/textencoder-constructor-non-utf.html.ini @@ -1,113 +1,7 @@ [textencoder-constructor-non-utf.html] 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] 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] expected: FAIL - - [Non-UTF encodings supported only for decode, not encode: x-user-defined] - expected: FAIL - diff --git a/tests/wpt/web-platform-tests/encoding/api-invalid-label.html b/tests/wpt/web-platform-tests/encoding/api-invalid-label.html new file mode 100644 index 00000000000..d2fa6786cdc --- /dev/null +++ b/tests/wpt/web-platform-tests/encoding/api-invalid-label.html @@ -0,0 +1,14 @@ + +Encoding API: invalid label + + +