mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
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:
parent
f3aee90b06
commit
97301400a5
6 changed files with 37 additions and 130 deletions
|
@ -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()
|
||||
|
|
|
@ -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()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue