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 all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -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,85 @@
/* 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_GetUint8ArrayData, JS_GetArrayBufferByteLength};
use js::jsapi::JSTracer;

use util::str::DOMString;

use std::borrow::ToOwned;
use std::slice::from_raw_parts;

#[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, cx: *mut JSContext, input: *mut JSObject) -> Fallible<DOMString> {
let length: usize = JS_GetArrayBufferByteLength(input, cx) as usize;
let stream: *const uint8_t = JS_GetUint8ArrayData(input, cx) as *const uint8_t;
let trap = if self.fatal { DecoderTrap::Strict } else { DecoderTrap::Replace };
unsafe { self.encoding.decode(from_raw_parts(stream, length), trap) }
}

fn Encoding(self) -> DOMString {
match self.encoding.whatwg_name() {
Some(enc) => enc.to_owned(),
None => "Unknown"
}
}

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

fn IgnoreBOM(self) -> bool {
false
}
}
@@ -0,0 +1,28 @@
/* -*- 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
[Throws]
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.