Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement TextDecoder #5058

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Added ignoreBOM field and implemented Decode

  • Loading branch information
KiChjang committed Feb 24, 2015
commit 314e9f36d5bf811fefb465792e338ba505802fd2
@@ -15,7 +15,7 @@ use encoding::types::EncodingRef;
use encoding::{Encoding, DecoderTrap};
use encoding::label::encoding_from_whatwg_label;

// use js::jsfriendapi::bindgen::{JS_NewUint8Array, JS_GetUint8ArrayData};
use js::jsfriendapi::bindgen::{JS_NewUint8Array, JS_GetUint8ArrayData};
use js::jsapi::JSTracer;

use util::str::DOMString;
@@ -26,22 +26,24 @@ use std::borrow::ToOwned;
pub struct TextDecoder {
reflector_: Reflector,
encoding: EncodingRef,
fatal: bool
fatal: bool,
ignoreBOM: bool
}

no_jsmanaged_fields!(EncodingRef);

impl TextDecoder {
fn new_inherited(encoding: EncodingRef, fatal: bool) -> TextDecoder {
fn new_inherited(encoding: EncodingRef, fatal: bool, ignoreBOM: bool) -> TextDecoder {
TextDecoder {
reflector_: Reflector::new(),
encoding: encoding,
fatal: fatal
fatal: fatal,
ignoreBOM: ignoreBOM
}
}

pub fn new(global: GlobalRef, encoding: EncodingRef, fatal: bool) -> Temporary<TextDecoder> {
reflect_dom_object(box TextDecoder::new_inherited(encoding, fatal),
pub fn new(global: GlobalRef, encoding: EncodingRef, fatal: bool, ignoreBOM: bool) -> Temporary<TextDecoder> {
reflect_dom_object(box TextDecoder::new_inherited(encoding, fatal, ignoreBOM),
global,
TextDecoderBinding::Wrap)
}
@@ -55,16 +57,19 @@ impl TextDecoder {
Some(enc) => enc,
None => return Err(Syntax) // FIXME: Should throw a RangeError as per spec
};
Ok(TextDecoder::new(global, encoding, options.fatal))
Ok(TextDecoder::new(global, encoding, options.fatal, options.ignoreBOM))
}
}

impl<'a> TextDecoderMethods for JSRef<'a, TextDecoder> {
/*pub fn Decode(self,
pub fn Decode(self,
cx: *mut JSContext,
input: *mut JSObject,
stream: &TextDecoderBinding::TextDecodeOptions) -> DOMString {
}*/
options: &TextDecoderBinding::TextDecodeOptions) -> DOMString {
let stream: *const uint8_t = JS_GetUint8ArrayData(input, cx) as *const uint8_t;
let trap = if fatal { DecoderTrap::Strict } else { DecoderTrap::Replace };
unsafe { self.encoding.decode(stream as &[u8], trap).unwrap() };
}

fn Encoding(self) -> DOMString {
self.encoding.whatwg_name().unwrap().to_owned()
@@ -75,6 +80,6 @@ impl<'a> TextDecoderMethods for JSRef<'a, TextDecoder> {
}

fn IgnoreBOM(self) -> bool {
false
self.ignoreBOM
}
}
@@ -23,5 +23,5 @@ interface TextDecoder {
readonly attribute boolean fatal;
readonly attribute boolean ignoreBOM;
// FIXME: decode should return a USVString instead, and ArrayBuffer should really be BufferSource
// DOMString decode(optional ArrayBuffer input, optional TextDecodeOptions options);
DOMString decode(optional ArrayBuffer input, optional TextDecodeOptions options);
};
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.