Why does HTML treat 2 and 3-byte characters the same, but not 4-byte?

Viewed 382

I'm doing some GUI work for a website and using the "maxlength" attribute for some text inputs, some of which may contain Unicode characters.

Say I've got a text field with maxlength = 50 and I fill it full of 2-byte Unicode characters (UTF-16). I can get 50 characters in the text field.

I can also do the same with 3-byte characters. 50 of them.

I can only get 25 4-byte characters in the field, however. Stands to reason, since it's twice as many bytes, but why does it still respond normally when using 3-byte characters? How is the extra byte handled?

1 Answers

Unicode characters can generally be encoded in either UTF-8, UTF-16, or UTF-32 (see their faq). Your usage of 2, 3 & 4 byte characters tells me you're working from a UTF-8 perspective.

However, the maxlength attribute is defined as the maximum number of UTF-16 code units, not number of bytes. Each UTF-16 code unit is two bytes.

A 2-byte UTF-8 character will be a single UTF-16 code unit. A 3-byte UTF-8 character will also be a single UTF-16 code unit. However, a 4-byte UTF-8 character represents a Unicode character greater than 0xFFFF. UTF-16 represents this as two code units (called surrogate pairs, see faq linked above).

Related