aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/textencoder.rs
diff options
context:
space:
mode:
Diffstat (limited to 'components/script/dom/textencoder.rs')
-rw-r--r--components/script/dom/textencoder.rs41
1 files changed, 10 insertions, 31 deletions
diff --git a/components/script/dom/textencoder.rs b/components/script/dom/textencoder.rs
index 674243dded8..996e5d28e98 100644
--- a/components/script/dom/textencoder.rs
+++ b/components/script/dom/textencoder.rs
@@ -5,75 +5,54 @@
use core::nonzero::NonZero;
use dom::bindings::codegen::Bindings::TextEncoderBinding;
use dom::bindings::codegen::Bindings::TextEncoderBinding::TextEncoderMethods;
-use dom::bindings::error::{Error, Fallible};
+use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::Root;
use dom::bindings::reflector::{Reflector, reflect_dom_object};
use dom::bindings::str::{DOMString, USVString};
use encoding::EncoderTrap;
-use encoding::label::encoding_from_whatwg_label;
-use encoding::types::EncodingRef;
+use encoding::Encoding;
+use encoding::all::UTF_8;
use js::jsapi::{JSContext, JSObject};
use js::jsapi::{JS_GetUint8ArrayData, JS_NewUint8Array};
use libc::uint8_t;
-use std::borrow::ToOwned;
use std::ptr;
#[dom_struct]
pub struct TextEncoder {
reflector_: Reflector,
- #[ignore_heap_size_of = "Defined in rust-encoding"]
- encoder: EncodingRef,
}
impl TextEncoder {
- fn new_inherited(encoder: EncodingRef) -> TextEncoder {
+ fn new_inherited() -> TextEncoder {
TextEncoder {
reflector_: Reflector::new(),
- encoder: encoder,
}
}
- pub fn new(global: GlobalRef, encoder: EncodingRef) -> Root<TextEncoder> {
- reflect_dom_object(box TextEncoder::new_inherited(encoder),
+ pub fn new(global: GlobalRef) -> Root<TextEncoder> {
+ reflect_dom_object(box TextEncoder::new_inherited(),
global,
TextEncoderBinding::Wrap)
}
// https://encoding.spec.whatwg.org/#dom-textencoder
- pub fn Constructor(global: GlobalRef,
- label: DOMString) -> Fallible<Root<TextEncoder>> {
- let encoding = match encoding_from_whatwg_label(&label) {
- Some(enc) => enc,
- None => {
- debug!("Encoding Label Not Supported");
- return Err(Error::Range("The given encoding is not supported.".to_owned()))
- }
- };
-
- match encoding.name() {
- "utf-8" | "utf-16be" | "utf-16le" => {
- Ok(TextEncoder::new(global, encoding))
- }
- _ => {
- debug!("Encoding Not UTF");
- Err(Error::Range("The encoding must be utf-8, utf-16le, or utf-16be.".to_owned()))
- }
- }
+ pub fn Constructor(global: GlobalRef) -> Fallible<Root<TextEncoder>> {
+ Ok(TextEncoder::new(global))
}
}
impl TextEncoderMethods for TextEncoder {
// https://encoding.spec.whatwg.org/#dom-textencoder-encoding
fn Encoding(&self) -> DOMString {
- DOMString::from(self.encoder.name())
+ DOMString::from(UTF_8.name())
}
#[allow(unsafe_code)]
// https://encoding.spec.whatwg.org/#dom-textencoder-encode
fn Encode(&self, cx: *mut JSContext, input: USVString) -> NonZero<*mut JSObject> {
unsafe {
- let encoded = self.encoder.encode(&input.0, EncoderTrap::Strict).unwrap();
+ let encoded = UTF_8.encode(&input.0, EncoderTrap::Strict).unwrap();
let length = encoded.len() as u32;
rooted!(in(cx) let js_object = JS_NewUint8Array(cx, length));
assert!(!js_object.is_null());