I came across DOMString
while reading the HTML spec. On some research, found the following data regarding it:
From MDN:
DOMString is a UTF-16 String. As JavaScript already uses such strings, DOMString is mapped directly to a String.
From W3C spec:
A DOMString is a sequence of 16-bit units.
But I still have the following questions:
DOMString
actually and what is it used for?String
, does it become a DOMString
?It's an implementation-independent DOM interface for UTF-16 strings.
JavaScript strings are already UTF-16 strings, so any instance of a JavaScript String
is automatically also a DOMString
instance.
The interface is meant for implementations whose strings are not natively UTF-16 sequences, so they can implement a separate type to map to DOMString
if necessary. The reason an implementation-independent interface is necessary is, as the spec states, "[to] ensure interoperability".
Why is it called DOMString
? Presumably because it's related to the DOM. How does it relate to the DOM? Well, it's part of the DOM standard, for one.
Taking as example 3 different ways to use Element.append()
.
Element.append() allows you to also append DOMString objects
Those 3 elements are both DOMString(s)
document.body.append( Object.assign(document.createElement("h2"), {textContent: "Hello"}), new DOMParser().parseFromString(`<span> world</span>`, "text/html").body.firstChild, "!" )
H2, SPAN {display:inline; FONT-SIZE: X-LARGE}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With