aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/textdecoder.rs
diff options
context:
space:
mode:
Diffstat (limited to 'components/script/dom/textdecoder.rs')
-rw-r--r--components/script/dom/textdecoder.rs19
1 files changed, 16 insertions, 3 deletions
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<Temporary<TextDecoder>> {
+ Err(Error::Range("The given encoding is not supported.".to_owned()))
+ }
+
pub fn new(global: GlobalRef, encoding: EncodingRef, fatal: bool) -> Temporary<TextDecoder> {
reflect_dom_object(box TextDecoder::new_inherited(encoding, fatal),
global,
@@ -51,14 +55,23 @@ impl TextDecoder {
options: &TextDecoderBinding::TextDecoderOptions)
-> Fallible<Temporary<TextDecoder>> {
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()