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

Next

Implemented TextDecoder struct and some functions (partial #4769)

  • Loading branch information
KiChjang committed Feb 22, 2015
commit c0b2db68fddcf6b44360ce47dc71acd485361c28
@@ -309,6 +309,7 @@ pub mod screen;
pub mod servohtmlparser;
pub mod storage;
pub mod text;
pub mod textdecoder;
pub mod treewalker;
pub mod uievent;
pub mod urlhelper;
@@ -0,0 +1,80 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use dom::bindings::codegen::Bindings::TextDecoderBinding;
use dom::bindings::codegen::Bindings::TextDecoderBinding::TextDecoderMethods;
use dom::bindings::js::{JSRef, Temporary};
use dom::bindings::trace::JSTraceable;
use dom::bindings::global::GlobalRef;
use dom::bindings::error::Fallible;
use dom::bindings::error::Error::Syntax;
use dom::bindings::utils::{Reflector, reflect_dom_object};

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::jsapi::JSTracer;

use util::str::DOMString;

use std::borrow::ToOwned;

#[dom_struct]
pub struct TextDecoder {
reflector_: Reflector,
encoding: EncodingRef,
fatal: bool
}

no_jsmanaged_fields!(EncodingRef);

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

pub fn new(global: GlobalRef, encoding: EncodingRef, fatal: bool) -> Temporary<TextDecoder> {
reflect_dom_object(box TextDecoder::new_inherited(encoding, fatal),
global,
TextDecoderBinding::Wrap)
}

// Spec: https://encoding.spec.whatwg.org/#dom-textdecoder
pub fn Constructor(global: GlobalRef,
label: DOMString,
options: &TextDecoderBinding::TextDecoderOptions)
-> Fallible<Temporary<TextDecoder>> {
let encoding = match encoding_from_whatwg_label(label.as_slice()) {
Some(enc) => enc,
None => return Err(Syntax) // FIXME: Should throw a RangeError as per spec
};
Ok(TextDecoder::new(global, encoding, options.fatal))
}
}

impl<'a> TextDecoderMethods for JSRef<'a, TextDecoder> {
/*pub fn Decode(self,
input: *mut JSObject,
stream: &TextDecoderBinding::TextDecodeOptions) -> DOMString {
}*/

fn Encoding(self) -> DOMString {
self.encoding.whatwg_name().unwrap().to_owned()
}

fn Fatal(self) -> bool {
self.fatal
}

fn IgnoreBOM(self) -> bool {
false
}
}
@@ -0,0 +1,27 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* The origin of this file is https://encoding.spec.whatwg.org/#interface-textdecoder
*
*/

dictionary TextDecoderOptions {
boolean fatal = false;
boolean ignoreBOM = false;
};

dictionary TextDecodeOptions {
boolean stream = false;
};

[Constructor(optional DOMString label = "utf-8", optional TextDecoderOptions options)/*,
Exposed=Window,Worker*/]
interface TextDecoder {
readonly attribute DOMString encoding;
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);
};
@@ -65,6 +65,8 @@ skip: true
skip: false
[system-state-and-capabilities]
skip: true
[encoding]
skip: false
[workers]
skip: false
[constructors]
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.