Styled Unicode Text Can Silently Break Character Limits in Web Forms
Unicode characters from the Mathematical Alphanumeric Symbols block, used for styled text like bold or script fonts, occupy different amounts of space depending on whether they are measured in code points, UTF-16 units, or UTF-8 bytes — sometimes by a factor of four. A developer discovered this inconsistency while auditing a character counter built alongside a form field whose destination encoding was undocumented. For example, a 76-code-point bold-styled string occupies 134 UTF-16 units and 250 UTF-8 bytes, meaning a counter showing '76' may give a false 'safe' signal. Standard JavaScript string methods like .slice() can also corrupt styled characters by splitting surrogate pairs, producing replacement characters during rendering. The developer recommends always measuring text in all three units, truncating on code points or grapheme clusters rather than UTF-16 indices, and avoiding a green 'safe' indicator unless the destination platform's encoding unit is explicitly known.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in