Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a DOMString really?

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:

  1. What is a DOMString actually and what is it used for?
  2. Why is it named as DOMString and not UTF16String? More specifically, how does it relate to DOM?
  3. If I UTF-16 encode a String, does it become a DOMString?
like image 452
chaudharyp Avatar asked Feb 01 '16 06:02

chaudharyp


2 Answers

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.

like image 110
BoltClock Avatar answered Oct 06 '22 06:10

BoltClock


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}
like image 34
NVRM Avatar answered Oct 06 '22 07:10

NVRM