8 ms·
> Except in Safari, whose maxlength implementation seems to treat all emoji as length 1. This means that the maxlength attribute is not fully interoperable betw
by SquareWheel 4y ago
> Except in Safari, whose maxlength implementation seems to treat all emoji as length 1. This means that the maxlength attribute is not fully interoperable between browsers.
No, it's definitely not. You can read the byte length more directly in JS, and use that to inform if more text is allowed or not.
const encoder = new TextEncoder();
const currentBytes = encoder.encode(inputStr).byteLength;
But the maxlength attribute is at best an approximation. Don't rely on it for things like limiting length for database fields (not that you should trust the client anyway).
- RobotToaster 4y ago>Don't rely on it for things like limiting length for database fields (not that you should trust the client anyway). Do database engines all agree on edge cases like this?
- Manfred 4y agoNo, they don't even agree between engine implementations within the same database server. Generally limits in the database are defined as storage limits for however a character is defined. That usually means bytes or codepoints.
- jonhohle 4y agoOr act like MySQL and define a code point as fixed to 3-bytes when choosing UTF-8 .
- bonsaibilly 4y agoThankfully MySQL also offers a non-gimped version of UTF-8 that one should always use in preference to the 3-byte version, but yeah it sucks that it's not the "obvious" version of UTF-8.
- tragomaskhalos 4y agoIs this part of MySQL's policy of "do the thing I've always done, no matter how daft or broken that may be, unless I see an obscure setting telling me to do the new correct thing" ?
- bonsaibilly 4y agoThat'd be my guess, but I don't really know. They just left the "utf8" type as broken 3-byte gibbled UTF-8, and added the "utf8mb4" type and "utf8mb4_unicode_ci" collation for "no, actually, I want UTF-8 for real".
- jonhohle 4y agoIt will be a fun day when Unicode crosses the 5-byte UTF-8 encoding threshold :/
- otabdeveloper4 4y agoIt won't. We settled on using stateful combining characters instead. (Remember when the selling point of switching the world to Unicode was "represent all writing systems with a single stateless 16 bit encoding"? Yeah, well, lol.)
- bonsaibilly 4y agoAnything beyond four bytes is composed of multiple code points, happily
- WJW 4y agoNo the default these days is the saner utf8mb4, if you create a new database on a modern MySQL version. But if you have an old database using the old encoding then upgrading databases doesn't magically update the encoding because some people take backwards compatibility serious.
- bruce343434 4y agoto be fair, there's utfmb4
- ASalazarMX 4y agoApple's take seems more reasonable. When a user uses an emoji, they think of it as a single symbol, they don't care of the Unicode implementation, or its length in bytes. IMO this should be the standard, and all other interpretations are a repeat of the transition from ASCII to Unicode.
- Sharlin 4y agoIndeed Swift is one of the very few languages that even have a stdlib API for working with this intuitive definition of a "character" (the technical Unicode term is "(extended) grapheme cluster" and the division of text into EGCs is called Unicode text segmentation, described by [uax29] – spoiler: definitely not trivial). [uax29]: https://unicode.org/reports/tr29/ https://unicode.org/reports/tr29/
- alwaysbeconsing 4y agoThere's no Swift involved here; this is in WebKit.
- alpaca128 4y agoAgreed, I'm surprised the linked OP filed a bug for Webkit when I'd say it's the only correct implementation. HTML is for UIs and I don't think many users would expect an emoji to be counted as 11 symbols when it looks like just one. If this wasn't an emoji but a symbol from a more complex writing system splitting that up would clearly be unacceptable, so I don't see why one should make an exception here just because this one known example of a family emoji still mostly survives the truncation.
- scatters 4y agoThe number of graphemes an emoji displays as depends on the platform and font. How many "characters" does Safari think Ninja Cat[1] is? It displays as a single grapheme on Windows 10. 1. https://emojipedia.org/ninja-cat/ https://emojipedia.org/ninja-cat/
- ASalazarMX 4y ago