aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/textdecoder.rs
diff options
context:
space:
mode:
authorAneesh Agrawal <aneeshusa@gmail.com>2015-04-10 02:42:35 -0400
committerAneesh Agrawal <aneeshusa@gmail.com>2015-04-14 09:41:57 -0400
commit97301400a5e69dc214beaf840cf8129d203dbf90 (patch)
tree9003a1e8405f3695f52f48deaa679255c0103c1e /components/script/dom/textdecoder.rs
parentf3aee90b06df39c220470881f1f90c12d8410c12 (diff)
downloadservo-97301400a5e69dc214beaf840cf8129d203dbf90.tar.gz
servo-97301400a5e69dc214beaf840cf8129d203dbf90.zip
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.
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()