Skip to content

Commit

Permalink
Implement TextEncoder
Browse files Browse the repository at this point in the history
Fixes #4768.
  • Loading branch information
yodalee authored and mbrubeck committed Apr 1, 2015
1 parent b79d06c commit 9cd976a
Show file tree
Hide file tree
Showing 23 changed files with 1,372 additions and 0 deletions.
1 change: 1 addition & 0 deletions components/script/dom/mod.rs
Expand Up @@ -312,6 +312,7 @@ pub mod servohtmlparser;
pub mod storage;
pub mod storageevent;
pub mod text;
pub mod textencoder;
pub mod treewalker;
pub mod uievent;
pub mod urlhelper;
Expand Down
94 changes: 94 additions & 0 deletions components/script/dom/textencoder.rs
@@ -0,0 +1,94 @@
/* 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::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::js::{JSRef, Temporary};
use dom::bindings::str::USVString;
use dom::bindings::utils::{Reflector, reflect_dom_object};

use util::str::DOMString;

use std::borrow::ToOwned;
use std::ascii::AsciiExt;
use std::ptr;

use encoding::types::EncodingRef;
use encoding::{Encoding, EncoderTrap};
use encoding::label::encoding_from_whatwg_label;

use libc::uint8_t;
use js::jsapi::{JSContext, JSObject};
use js::jsfriendapi::bindgen::{JS_NewUint8Array, JS_GetUint8ArrayData};

#[dom_struct]
pub struct TextEncoder {
reflector_: Reflector,
encoding: DOMString,
encoder: EncodingRef,
}

impl TextEncoder {
fn new_inherited(encoding: DOMString, encoder: EncodingRef) -> TextEncoder {
TextEncoder {
reflector_: Reflector::new(),
encoding: encoding,
encoder: encoder,
}
}

pub fn new(global: GlobalRef, encoding: DOMString, encoder: EncodingRef) -> Temporary<TextEncoder> {
reflect_dom_object(box TextEncoder::new_inherited(encoding, encoder),
global,
TextEncoderBinding::Wrap)
}

// https://encoding.spec.whatwg.org/#dom-textencoder
pub fn Constructor(global: GlobalRef,
label: DOMString) -> Fallible<Temporary<TextEncoder>> {
let encoding = match encoding_from_whatwg_label(label.trim().as_slice().to_ascii_lowercase().as_slice()) {
Some(enc) => enc,
None => {
debug!("Encoding Label Not Supported");
// TODO: should throw RangeError
return Err(IndexSize)
}
};

match encoding.name() {
"utf-8" | "utf-16be" | "utf-16le" => {
Ok(TextEncoder::new(global, encoding.name().to_owned(), encoding))
}
_ => {
debug!("Encoding Not UTF");
// TODO: should throw RangeError
Err(IndexSize)
}
}
}
}

impl<'a> TextEncoderMethods for JSRef<'a, TextEncoder> {
// https://encoding.spec.whatwg.org/#dom-textencoder-encoding
fn Encoding(self) -> DOMString {
self.encoding.clone()
}

// https://encoding.spec.whatwg.org/#dom-textencoder-encode
#[allow(unsafe_code)]
fn Encode(self, cx: *mut JSContext, input: USVString) -> *mut JSObject {
unsafe {
let output = self.encoder.encode(input.0.as_slice(), EncoderTrap::Strict).unwrap();
let length = output.len() as u32;
let js_object: *mut JSObject = JS_NewUint8Array(cx, length);

let js_object_data: *mut uint8_t = JS_GetUint8ArrayData(js_object, cx);
ptr::copy_nonoverlapping(js_object_data, output.as_ptr(), length as usize);
return js_object;
}
}
}
12 changes: 12 additions & 0 deletions components/script/dom/webidls/TextEncoder.webidl
@@ -0,0 +1,12 @@
/* -*- 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/. */

/* https://encoding.spec.whatwg.org/#interface-textencoder */
[Constructor(optional DOMString utfLabel = "utf-8")/*, Exposed=Window,Worker */]
interface TextEncoder {
readonly attribute DOMString encoding;
[NewObject]
Uint8Array encode(optional USVString input = "");
};
1 change: 1 addition & 0 deletions tests/content/test_interfaces.html
Expand Up @@ -166,6 +166,7 @@
"StorageEvent",
"TestBinding", // XXX
"Text",
"TextEncoder",
"TreeWalker",
"UIEvent",
"URLSearchParams",
Expand Down
2 changes: 2 additions & 0 deletions tests/wpt/include.ini
Expand Up @@ -95,3 +95,5 @@ skip: true
skip: true
[webstorage]
skip: false
[encoding]
skip: false
20 changes: 20 additions & 0 deletions tests/wpt/metadata/encoding/api-basics.html.ini
@@ -0,0 +1,20 @@
[api-basics.html]
type: testharness
[Default encodings]
expected: FAIL

[Default inputs]
expected: FAIL

[Encode/decode round trip: utf-8]
expected: FAIL

[Encode/decode round trip: utf-16le]
expected: FAIL

[Encode/decode round trip: utf-16be]
expected: FAIL

[Encode/decode round trip: utf-16]
expected: FAIL

20 changes: 20 additions & 0 deletions tests/wpt/metadata/encoding/api-replacement-encodings.html.ini
@@ -0,0 +1,20 @@
[api-replacement-encodings.html]
type: testharness
[The "replacement" label should not be a known encoding.]
expected: FAIL

[Label for "replacement" should be rejected by API: csiso2022kr]
expected: FAIL

[Label for "replacement" should be rejected by API: hz-gb-2312]
expected: FAIL

[Label for "replacement" should be rejected by API: iso-2022-cn]
expected: FAIL

[Label for "replacement" should be rejected by API: iso-2022-cn-ext]
expected: FAIL

[Label for "replacement" should be rejected by API: iso-2022-kr]
expected: FAIL

20 changes: 20 additions & 0 deletions tests/wpt/metadata/encoding/api-surrogates-utf8.html.ini
@@ -0,0 +1,20 @@
[api-surrogates-utf8.html]
type: testharness
[Invalid surrogates encoded into UTF-8: Sanity check]
expected: FAIL

[Invalid surrogates encoded into UTF-8: Surrogate half (low)]
expected: FAIL

[Invalid surrogates encoded into UTF-8: Surrogate half (high)]
expected: FAIL

[Invalid surrogates encoded into UTF-8: Surrogate half (low), in a string]
expected: FAIL

[Invalid surrogates encoded into UTF-8: Surrogate half (high), in a string]
expected: FAIL

[Invalid surrogates encoded into UTF-8: Wrong order]
expected: FAIL

20 changes: 20 additions & 0 deletions tests/wpt/metadata/encoding/gb18030-encoder.html.ini
@@ -0,0 +1,20 @@
[gb18030-encoder.html]
type: testharness
[gb18030 encoder: very basic]
expected: FAIL

[gb18030 encoder: Euro]
expected: FAIL

[gb18030 encoder: character]
expected: FAIL

[gb18030 encoder: PUA]
expected: FAIL

[gb18030 encoder: PUA #2]
expected: FAIL

[gb18030 encoder: poo]
expected: FAIL

20 changes: 20 additions & 0 deletions tests/wpt/metadata/encoding/gbk-encoder.html.ini
@@ -0,0 +1,20 @@
[gbk-encoder.html]
type: testharness
[gbk encoder: very basic]
expected: FAIL

[gbk encoder: Euro]
expected: FAIL

[gbk encoder: character]
expected: FAIL

[gbk encoder: PUA]
expected: FAIL

[gbk encoder: PUA #2]
expected: FAIL

[gbk encoder: poo]
expected: FAIL

52 changes: 52 additions & 0 deletions tests/wpt/metadata/encoding/idlharness.html.ini
@@ -0,0 +1,52 @@
[idlharness.html]
type: testharness
[TextDecoder interface: existence and properties of interface object]
expected: FAIL

[TextDecoder interface object length]
expected: FAIL

[TextDecoder interface: existence and properties of interface prototype object]
expected: FAIL

[TextDecoder interface: existence and properties of interface prototype object\'s "constructor" property]
expected: FAIL
[TextDecoder interface: attribute encoding]
expected: FAIL
[TextDecoder interface: attribute fatal]
expected: FAIL
[TextDecoder interface: attribute ignoreBOM]
expected: FAIL
[TextDecoder interface: operation decode(BufferSource,TextDecodeOptions)]
expected: FAIL
[TextDecoder must be primary interface of new TextDecoder()]
expected: FAIL
[Stringification of new TextDecoder()]
expected: FAIL
[TextDecoder interface: new TextDecoder() must inherit property "encoding" with the proper type (0)]
expected: FAIL
[TextDecoder interface: new TextDecoder() must inherit property "fatal" with the proper type (1)]
expected: FAIL
[TextDecoder interface: new TextDecoder() must inherit property "ignoreBOM" with the proper type (2)]
expected: FAIL
[TextDecoder interface: new TextDecoder() must inherit property "decode" with the proper type (3)]
expected: FAIL
[TextDecoder interface: calling decode(BufferSource,TextDecodeOptions) on new TextDecoder() with too few arguments must throw TypeError]
expected: FAIL
[TextEncoder interface object length]
expected: FAIL
[TextEncoder interface: operation encode(USVString)]
expected: FAIL
104 changes: 104 additions & 0 deletions tests/wpt/metadata/encoding/iso-2022-jp-decoder.html.ini
@@ -0,0 +1,104 @@
[iso-2022-jp-decoder.html]
type: testharness
[iso-2022-jp decoder: Error ESC]
expected: FAIL

[iso-2022-jp decoder: Error ESC, character]
expected: FAIL

[iso-2022-jp decoder: ASCII ESC, character]
expected: FAIL

[iso-2022-jp decoder: Double ASCII ESC, character]
expected: FAIL

[iso-2022-jp decoder: character, ASCII ESC, character]
expected: FAIL

[iso-2022-jp decoder: characters]
expected: FAIL

[iso-2022-jp decoder: SO / SI]
expected: FAIL

[iso-2022-jp decoder: Roman ESC, characters]
expected: FAIL

[iso-2022-jp decoder: Roman ESC, SO / SI]
expected: FAIL

[iso-2022-jp decoder: Roman ESC, error ESC, Katakana ESC]
expected: FAIL

[iso-2022-jp decoder: Katakana ESC, character]
expected: FAIL

[iso-2022-jp decoder: Katakana ESC, multibyte ESC, character]
expected: FAIL

[iso-2022-jp decoder: Katakana ESC, error ESC, character]
expected: FAIL

[iso-2022-jp decoder: Katakana ESC, error ESC #2, character]
expected: FAIL

[iso-2022-jp decoder: Katakana ESC, character, Katakana ESC, character]
expected: FAIL

[iso-2022-jp decoder: Katakana ESC, SO / SI]
expected: FAIL

[iso-2022-jp decoder: Multibyte ESC, character]
expected: FAIL

[iso-2022-jp decoder: Multibyte ESC #2, character]
expected: FAIL

[iso-2022-jp decoder: Multibyte ESC, error ESC, character]
expected: FAIL

[iso-2022-jp decoder: Double multibyte ESC]
expected: FAIL

[iso-2022-jp decoder: Double multibyte ESC, character]
expected: FAIL

[iso-2022-jp decoder: Double multibyte ESC #2, character]
expected: FAIL

[iso-2022-jp decoder: Multibyte ESC, error ESC #2, character]
expected: FAIL

[iso-2022-jp decoder: Multibyte ESC, single byte, multibyte ESC, character]
expected: FAIL

[iso-2022-jp decoder: Multibyte ESC, lead error byte]
expected: FAIL

[iso-2022-jp decoder: Multibyte ESC, trail error byte]
expected: FAIL

[iso-2022-jp decoder: character, error ESC]
expected: FAIL

[iso-2022-jp decoder: character, error ESC #2]
expected: FAIL

[iso-2022-jp decoder: character, error ESC #3]
expected: FAIL

[iso-2022-jp decoder: character, ASCII ESC]
expected: FAIL

[iso-2022-jp decoder: character, Roman ESC]
expected: FAIL

[iso-2022-jp decoder: character, Katakana ESC]
expected: FAIL

[iso-2022-jp decoder: character, Multibyte ESC]
expected: FAIL

[iso-2022-jp decoder: character, Multibyte ESC #2]
expected: FAIL

9 comments on commit 9cd976a

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from jdm
at mbrubeck@9cd976a

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging mbrubeck/servo/yodalee-issue4768-textencoder = 9cd976a into auto

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mbrubeck/servo/yodalee-issue4768-textencoder = 9cd976a merged ok, testing candidate = f86d18c

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from jdm
at mbrubeck@9cd976a

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging mbrubeck/servo/yodalee-issue4768-textencoder = 9cd976a into auto

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mbrubeck/servo/yodalee-issue4768-textencoder = 9cd976a merged ok, testing candidate = 4846607

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 4846607

Please sign in to comment.