servo: Merge #13405 - Removed support for UTF-16 in TextEncoder (from ashrko619:textencoder-encoding); r=Ms2ger

<!-- Please describe your changes on the following line: -->

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #13231  (github issue number if applicable).

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

Source-Repo: https://github.com/servo/servo
Source-Revision: d03f0a20cc0f98ee55fbca65d71ff59026bee323
This commit is contained in:
Ashwin R 2016-09-26 22:19:40 -05:00
Родитель b13d5121e5
Коммит 97c0d731da
2 изменённых файлов: 11 добавлений и 32 удалений

Просмотреть файл

@ -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());

Просмотреть файл

@ -3,7 +3,7 @@
* 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)]
[Constructor, Exposed=(Window,Worker)]
interface TextEncoder {
readonly attribute DOMString encoding;
[NewObject]