Struct DOMString

Source
#[repr(transparent)]
pub struct DOMString(RefCell<DOMStringType>);
Expand description

This type corresponds to the DOMString type in WebIDL.

Conceptually, a DOMString has the same value space as a JavaScript String, i.e., an array of 16-bit code units representing UTF-16, potentially with unpaired surrogates present (also sometimes called WTF-16).

However, Rust Strings are guaranteed to be valid UTF-8, and as such have a smaller value space than WTF-16 (i.e., some JavaScript String values can not be represented as a Rust String). This introduces the question of what to do with values being passed from JavaScript to Rust that contain unpaired surrogates.

The hypothesis is that it does not matter much how exactly those values are transformed, because passing unpaired surrogates into the DOM is very rare. Instead Servo withh replace the unpaired surrogate by a U+FFFD replacement character.

Currently, the lack of crash reports about this issue provides some evidence to support the hypothesis. This evidence will hopefully be used to convince other browser vendors that it would be safe to replace unpaired surrogates at the boundary between JavaScript and native code. (This would unify the DOMString and USVString types, both in the WebIDL standard and in Servo.)

This string class will keep either the Reference to the mozjs object alive or will have an internal rust string. We currently default to doing most of the string operation on the rust side. You should use str() to get the Rust string (represented by a StringView which you can deref to a string). You should assume that this conversion costs. You should assume that all the functions incur the conversion cost.

Tuple Fields§

§0: RefCell<DOMStringType>

Implementations§

Source§

impl DOMString

Source

pub fn new() -> DOMString

Creates a new DOMString.

Source

pub fn from_js_string(cx: SafeJSContext, value: HandleValue<'_>) -> DOMString

Creates the string from js. If the string can be encoded in latin1, just take the reference to the JSString. Otherwise do the conversion to utf8 now.

Source

pub fn from_string(s: String) -> DOMString

Source

fn make_rust(&self)

Transforms the string into rust string if not yet a rust string.

Source

fn debug_js(&self)

Debug the current state of the string without modifying it.

Source

pub fn str(&self) -> StringView<'_>

Returns the underlying rust string.

Source

pub fn clear(&mut self)

Source

pub fn is_empty(&self) -> bool

Source

pub fn len(&self) -> usize

This length (as rust spec) is in bytes not chars.

Source

pub fn make_ascii_lowercase(&mut self)

Source

pub fn push_str(&mut self, s: &str)

Source

pub fn strip_leading_and_trailing_ascii_whitespace(&mut self)

Source

pub fn is_valid_floating_point_number_string(&self) -> bool

This is a dom spec

Source

pub fn parse<T: FromStr>(&self) -> Result<T, <T as FromStr>::Err>

Source

pub fn parse_floating_point_number(&self) -> Option<f64>

Source

pub fn set_best_representation_of_the_floating_point_number(&mut self)

This is a dom spec

Source

pub fn to_lowercase(&self) -> String

Source

pub fn to_uppercase(&self) -> String

Source

pub fn strip_newlines(&mut self)

Source

pub fn normalize_newlines(&mut self)

Normalize newlines according to https://infra.spec.whatwg.org/#normalize-newlines.

Source

pub fn replace(self, needle: &str, replace_char: &str) -> DOMString

Source

pub fn find(&self, c: char) -> Option<usize>

Source

pub fn starts_with(&self, c: char) -> bool

Pattern is not yet stable in rust, hence, we need different methods for str and char

Source

pub fn starts_with_str(&self, needle: &str) -> bool

Source

pub fn contains(&self, needle: &str) -> bool

Source

pub fn to_ascii_lowercase(&self) -> String

Source

pub fn contains_html_space_characters(&self) -> bool

Source

pub fn as_bytes(&self) -> BytesView<'_>

This returns the string in utf8 bytes, i.e., [u8].

Trait Implementations§

Source§

impl Clone for DOMString

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DOMString

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for DOMString

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for DOMString

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Extend<char> for DOMString

Source§

fn extend<T: IntoIterator<Item = char>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl From<&DOMString> for LocalName

Source§

fn from(contents: &DOMString) -> LocalName

Converts to this type from the input type.
Source§

impl From<&str> for DOMString

Source§

fn from(contents: &str) -> DOMString

Converts to this type from the input type.
Source§

impl From<Cow<'_, str>> for DOMString

Source§

fn from(value: Cow<'_, str>) -> Self

Converts to this type from the input type.
Source§

impl From<DOMString> for Atom

Source§

fn from(contents: DOMString) -> Atom

Converts to this type from the input type.
Source§

impl From<DOMString> for LocalName

Source§

fn from(contents: DOMString) -> LocalName

Converts to this type from the input type.
Source§

impl From<DOMString> for Namespace

Source§

fn from(contents: DOMString) -> Namespace

Converts to this type from the input type.
Source§

impl From<DOMString> for String

Source§

fn from(val: DOMString) -> Self

Converts to this type from the input type.
Source§

impl From<DOMString> for Vec<u8>

Source§

fn from(value: DOMString) -> Self

Converts to this type from the input type.
Source§

impl From<String> for DOMString

Source§

fn from(value: String) -> Self

Converts to this type from the input type.
Source§

impl FromJSValConvertible for DOMString

Source§

type Config = StringificationBehavior

Optional configurable behaviour switch; use () for no configuration.
Source§

unsafe fn from_jsval( cx: *mut JSContext, value: HandleValue<'_>, null_behavior: StringificationBehavior, ) -> Result<ConversionResult<DOMString>, ()>

Convert val to type Self. Optional configuration of type T can be passed as the option argument. If it returns Err(()), a JSAPI exception is pending. If it returns Ok(Failure(reason)), there is no pending JSAPI exception.
Source§

impl Hash for DOMString

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl MallocSizeOf for DOMString

Source§

fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize

Measure the heap usage of all descendant heap-allocated structures, but not the space taken up by the value itself.
Source§

impl Ord for DOMString

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq<&str> for DOMString

Source§

fn eq(&self, other: &&str) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<DOMString> for String

Source§

fn eq(&self, other: &DOMString) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<DOMString> for str

Source§

fn eq(&self, other: &DOMString) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<String> for DOMString

Source§

fn eq(&self, other: &String) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<str> for DOMString

Source§

fn eq(&self, other: &str) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for DOMString

Source§

fn eq(&self, other: &DOMString) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for DOMString

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl RecordKey for DOMString

Source§

fn to_utf16_vec(&self) -> Vec<u16>

Source§

unsafe fn from_id( cx: *mut JSContext, id: HandleId<'_>, ) -> Result<ConversionResult<Self>, ()>

Attempt to extract a key from a JS id. Read more
Source§

impl ToJSValConvertible for DOMString

Source§

unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue<'_>)

Convert self to a JSVal. JSAPI failure causes a panic.
Source§

impl Traceable for DOMString

Source§

unsafe fn trace(&self, tracer: *mut JSTracer)

Trace self.
Source§

impl Eq for DOMString

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsVoidPtr for T

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> Filterable for T

Source§

fn filterable( self, filter_name: &'static str, ) -> RequestFilterDataProvider<T, fn(DataRequest<'_>) -> bool>

Creates a filterable data provider with the given name for debugging. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> MaybeBoxed<Box<T>> for T

Source§

fn maybe_boxed(self) -> Box<T>

Convert
Source§

impl<T> MaybeBoxed<T> for T

Source§

fn maybe_boxed(self) -> T

Convert
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> SafeFromJSValConvertible for T

Source§

impl<T> SafeToJSValConvertible for T

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T