mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
Move DOMString back to script
This entirely removes the 'non-geckolib' feature of the util crate.
This commit is contained in:
parent
7b467ee52d
commit
cdc7bca944
188 changed files with 501 additions and 529 deletions
|
@ -8,15 +8,6 @@ publish = false
|
|||
name = "util"
|
||||
path = "lib.rs"
|
||||
|
||||
[features]
|
||||
|
||||
# This feature allows us to avoid depending on various things we don't need for
|
||||
# GeckoLib builds. Conceptually, it would make more sense to have a "geckolib"
|
||||
# feature, but Cargo is generally set up for features to add dependencies, not
|
||||
# remove them. So we do it this way, and request that all non-GeckoLib builds
|
||||
# set this feature.
|
||||
non-geckolib = ["js"]
|
||||
|
||||
[dependencies]
|
||||
app_units = {version = "0.2.3", features = ["plugins"]}
|
||||
backtrace = "0.2.1"
|
||||
|
@ -27,7 +18,6 @@ getopts = "0.2.11"
|
|||
heapsize = "0.3.0"
|
||||
heapsize_plugin = "0.1.2"
|
||||
ipc-channel = {git = "https://github.com/servo/ipc-channel"}
|
||||
js = {git = "https://github.com/servo/rust-mozjs", optional = true}
|
||||
lazy_static = "0.2"
|
||||
libc = "0.2"
|
||||
log = "0.3.5"
|
||||
|
@ -39,7 +29,6 @@ rustc-serialize = "0.3"
|
|||
serde = "0.7"
|
||||
serde_macros = "0.7"
|
||||
smallvec = "0.1"
|
||||
string_cache = {version = "0.2.17", features = ["heap_size"]}
|
||||
url = {version = "1.0.0", features = ["heap_size", "serde"]}
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
#![feature(core_intrinsics)]
|
||||
#![feature(custom_derive)]
|
||||
#![feature(fnbox)]
|
||||
#![feature(optin_builtin_traits)]
|
||||
#![feature(plugin)]
|
||||
#![feature(panic_handler)]
|
||||
#![feature(reflect_marker)]
|
||||
|
@ -26,8 +25,6 @@ extern crate euclid;
|
|||
extern crate getopts;
|
||||
extern crate heapsize;
|
||||
extern crate ipc_channel;
|
||||
#[cfg(feature = "non-geckolib")]
|
||||
extern crate js;
|
||||
#[allow(unused_extern_crates)]
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
@ -40,7 +37,6 @@ extern crate rand;
|
|||
extern crate rustc_serialize;
|
||||
extern crate serde;
|
||||
extern crate smallvec;
|
||||
extern crate string_cache;
|
||||
extern crate url;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
@ -52,9 +48,6 @@ pub mod geometry;
|
|||
#[allow(unsafe_code)]
|
||||
pub mod ipc;
|
||||
pub mod linked_list;
|
||||
#[cfg(feature = "non-geckolib")]
|
||||
#[allow(unsafe_code)]
|
||||
pub mod non_geckolib;
|
||||
#[allow(unsafe_code)]
|
||||
pub mod opts;
|
||||
pub mod panicking;
|
||||
|
|
|
@ -1,91 +0,0 @@
|
|||
/* 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/. */
|
||||
|
||||
///! Miscellaneous Code which depends on large libraries that we don't
|
||||
/// depend on in GeckoLib builds.
|
||||
use js::conversions::{FromJSValConvertible, ToJSValConvertible, latin1_to_string};
|
||||
use js::jsapi::{JSContext, JSString, HandleValue, MutableHandleValue};
|
||||
use js::jsapi::{JS_GetTwoByteStringCharsAndLength, JS_StringHasLatin1Chars};
|
||||
use js::rust::ToString;
|
||||
use opts;
|
||||
use std::char;
|
||||
use std::ptr;
|
||||
use std::slice;
|
||||
use str::DOMString;
|
||||
|
||||
/// Behavior for stringification of `JSVal`s.
|
||||
#[derive(PartialEq, Clone)]
|
||||
pub enum StringificationBehavior {
|
||||
/// Convert `null` to the string `"null"`.
|
||||
Default,
|
||||
/// Convert `null` to the empty string.
|
||||
Empty,
|
||||
}
|
||||
|
||||
// https://heycam.github.io/webidl/#es-DOMString
|
||||
impl ToJSValConvertible for DOMString {
|
||||
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
|
||||
(**self).to_jsval(cx, rval);
|
||||
}
|
||||
}
|
||||
|
||||
// https://heycam.github.io/webidl/#es-DOMString
|
||||
impl FromJSValConvertible for DOMString {
|
||||
type Config = StringificationBehavior;
|
||||
unsafe fn from_jsval(cx: *mut JSContext,
|
||||
value: HandleValue,
|
||||
null_behavior: StringificationBehavior)
|
||||
-> Result<DOMString, ()> {
|
||||
if null_behavior == StringificationBehavior::Empty &&
|
||||
value.get().is_null() {
|
||||
Ok(DOMString::new())
|
||||
} else {
|
||||
let jsstr = ToString(cx, value);
|
||||
if jsstr.is_null() {
|
||||
debug!("ToString failed");
|
||||
Err(())
|
||||
} else {
|
||||
Ok(jsstring_to_str(cx, jsstr))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert the given `JSString` to a `DOMString`. Fails if the string does not
|
||||
/// contain valid UTF-16.
|
||||
pub unsafe fn jsstring_to_str(cx: *mut JSContext, s: *mut JSString) -> DOMString {
|
||||
let latin1 = JS_StringHasLatin1Chars(s);
|
||||
DOMString::from_string(if latin1 {
|
||||
latin1_to_string(cx, s)
|
||||
} else {
|
||||
let mut length = 0;
|
||||
let chars = JS_GetTwoByteStringCharsAndLength(cx, ptr::null(), s, &mut length);
|
||||
assert!(!chars.is_null());
|
||||
let potentially_ill_formed_utf16 = slice::from_raw_parts(chars, length as usize);
|
||||
let mut s = String::with_capacity(length as usize);
|
||||
for item in char::decode_utf16(potentially_ill_formed_utf16.iter().cloned()) {
|
||||
match item {
|
||||
Ok(c) => s.push(c),
|
||||
Err(_) => {
|
||||
// FIXME: Add more info like document URL in the message?
|
||||
macro_rules! message {
|
||||
() => {
|
||||
"Found an unpaired surrogate in a DOM string. \
|
||||
If you see this in real web content, \
|
||||
please comment on https://github.com/servo/servo/issues/6564"
|
||||
}
|
||||
}
|
||||
if opts::get().replace_surrogates {
|
||||
error!(message!());
|
||||
s.push('\u{FFFD}');
|
||||
} else {
|
||||
panic!(concat!(message!(), " Use `-Z replace-surrogates` \
|
||||
on the command line to make this non-fatal."));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
s
|
||||
})
|
||||
}
|
|
@ -8,119 +8,9 @@ use num_traits::ToPrimitive;
|
|||
use std::borrow::ToOwned;
|
||||
use std::convert::AsRef;
|
||||
use std::ffi::CStr;
|
||||
use std::fmt;
|
||||
use std::iter::{Filter, Peekable};
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::str::{Bytes, Split, from_utf8};
|
||||
use string_cache::Atom;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Eq, Hash, HeapSizeOf, Ord, PartialEq, PartialOrd, Serialize)]
|
||||
pub struct DOMString(String);
|
||||
|
||||
impl !Send for DOMString {}
|
||||
|
||||
impl DOMString {
|
||||
pub fn new() -> DOMString {
|
||||
DOMString(String::new())
|
||||
}
|
||||
pub fn from_string(s: String) -> DOMString {
|
||||
DOMString(s)
|
||||
}
|
||||
// FIXME(ajeffrey): implement more of the String methods on DOMString?
|
||||
pub fn push_str(&mut self, string: &str) {
|
||||
self.0.push_str(string)
|
||||
}
|
||||
pub fn clear(&mut self) {
|
||||
self.0.clear()
|
||||
}
|
||||
|
||||
pub fn bytes(&self) -> Bytes {
|
||||
self.0.bytes()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for DOMString {
|
||||
fn default() -> Self {
|
||||
DOMString(String::new())
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for DOMString {
|
||||
type Target = str;
|
||||
|
||||
#[inline]
|
||||
fn deref(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for DOMString {
|
||||
#[inline]
|
||||
fn deref_mut(&mut self) -> &mut str {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<str> for DOMString {
|
||||
fn as_ref(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for DOMString {
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt::Display::fmt(&**self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<str> for DOMString {
|
||||
fn eq(&self, other: &str) -> bool {
|
||||
&**self == other
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> PartialEq<&'a str> for DOMString {
|
||||
fn eq(&self, other: &&'a str) -> bool {
|
||||
&**self == *other
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for DOMString {
|
||||
fn from(contents: String) -> DOMString {
|
||||
DOMString(contents)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a str> for DOMString {
|
||||
fn from(contents: &str) -> DOMString {
|
||||
DOMString::from(String::from(contents))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DOMString> for Atom {
|
||||
fn from(contents: DOMString) -> Atom {
|
||||
Atom::from(contents.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DOMString> for String {
|
||||
fn from(contents: DOMString) -> String {
|
||||
contents.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<Vec<u8>> for DOMString {
|
||||
fn into(self) -> Vec<u8> {
|
||||
self.0.into()
|
||||
}
|
||||
}
|
||||
|
||||
impl Extend<char> for DOMString {
|
||||
fn extend<I>(&mut self, iterable: I) where I: IntoIterator<Item=char> {
|
||||
self.0.extend(iterable)
|
||||
}
|
||||
}
|
||||
use std::ops::Deref;
|
||||
use std::str::{Split, from_utf8};
|
||||
|
||||
pub type StaticCharVec = &'static [char];
|
||||
pub type StaticStringVec = &'static [&'static str];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue