213 points · by robin_reala · 3y ago · 101 comments
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).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.
[uax29]: https://unicode.org/reports/tr29/
I don't actually know whether ninja cat would count as one or two though. The spec for calculating grapheme boundaries is actually several pages long.
And the OS might not obey the Unicode standard. Ninja cat appears to be proprietary to Microsoft.
I understand that technical limitations will sometimes cause a combined character to be displayed as more than one character, as it happens in Safari with that ninja cat, and in those cases, it's preferable to decompose the emoji, or use a tofu character, than changing it for another.
Edit: Looks like HN stripped out the character codes. The effect can be reproduced by copy-pasting the symbols from https://en.wikipedia.org/wiki/Regional_indicator_symbol
A string length is now the number grapheme clusters, or just the width.
Do database engines all agree on edge cases like this?
> Constraint validation: If an element has a maximum allowed value length, its dirty value flag is true, its value was last changed by a user edit (as opposed to a change made by a script), and the length of the element's API value is greater than the element's maximum allowed value length, then the element is suffering from being too long.
Where "the length of" is a link to [2]:
> A string’s length is the number of code units it contains.
And "code units" is a link to [3]:
> A string is a sequence of unsigned 16-bit integers, also known as code units.
I agree with your implied point that this is a questionable definition, though!
[1] https://html.spec.whatwg.org/multipage/form-control-infrastr...
“User agents _may_ prevent the user from causing the element's API value to be set to a value whose length is greater than the element's maximum allowed value length.”
and we have “MAY This word, or the adjective "OPTIONAL", mean that an item is truly optional” [2]
[1] https://html.spec.whatwg.org/multipage/form-control-infrastr...
Choosing 16-bit code points as the base keeps consistency with the naive string length counting algorithm of JS, in particular, which has always returned length in 16-bit code points.
Sure, it's slightly to the detriment of user experience in terms of how a user expects graphemes to be counted, but it avoids later storage problems.
The length in code units is unambiguous and constant across versions.
A good starting place is UAX #29: https://www.unicode.org/reports/tr29/tr29-41.html
However, the gold standard in UI implementations is that you never break the user's input.
It starts by breaking down common Unicode assumptions folks have
People don't think about code points and they definitely don't think about code units.
What does "character" mean?
Grapheme clusters aren't perfect but they're far ahead of code whatevers.
Edit: Oops. I was trying to type emoji into HN comment. apparently it is not supported.
I never knew that [emoji of family of 4] takes 5 backspace to eliminate. It goes from [emoji of family of 4] to [emoji of family of 3] to [father and mother] to [father] to [father]. Somehow [father] can take double the (key)punch than the rest of his family.
I miss when Unicode was just a simple list of codepoints. (Get off my lawn)
[1]: https://seriot.ch/resources/talks_papers/20171027_brainfuck_...
It wouldn't be "universal", but enough to write in the most commonly used languages, and maybe support a few, single codepoint special characters and emoji.
This, for reasons stated in already posted responses, and to put it mildly, does not work in international context (e.g. the vast majority of software written every day).
If you want to eliminate edge cases you would need to introduce new incompatible code points and a 32 or 64 bit fixed length encoding depending on how many languages you want to support.
As for encoding, 32 bits fixed length should be sufficient, I can't believe that we would need billions of symbols, combinations included, in order to write in most common languages, though I may be wrong.
Also, "limiting" doesn't not necessarily means "single code point only", but more like "only one diacritic, only from this list, and only for these characters", so that combination fits in a certain size limit (ex: a 32 bit word), and that the engine only has to process a limited number of use cases.
Plenty of software already does that-- HN itself doesn't allow emoji, for example.
Make a study over a wide range of documents about the characters that are used the most, see if there are alternatives to the characters that don't make it, etc...
This is unlike the HN ban on emoji, which I think is more of a political decision than a technical one. Most people on HN use systems that can read emoji just fine, but they decided that the site would be better without it. This would be more technical, a way to balance inclusivity and technical constraints, something between ASCII and full Unicode.
We already know client side limits aren’t enough and server validation is required anyway. Trying to cleverly “help” entry usually just causes headaches for users and devs. Dynamically showing/hiding, enabling/disabling, focus jumping, auto formatting, updating other fields based on other values, is usually confusing for the user and more difficult to program correctly. Just show it all, allow all entry and catch everything in the validation code, everyone will be happier.
But I can also imagine this is problematic when setting validation rules else where and now there’s a subtle foot gun buried in most web forms.
I guess the thing to learn here is to not rely on maxlength=
Which is weird
1: https://www.joelonsoftware.com/2003/10/08/the-absolute-minim...
https://adraffy.github.io/ens-normalize.js/test/resolver.htm...
Also:
https://adraffy.github.io/ens-normalize.js/test/emoji.html
https://adraffy.github.io/ens-normalize.js/test/chars.html
https://adraffy.github.io/ens-normalize.js/test/confused.htm...
https://adraffy.github.io/punycode.js/test/demo.html
Here is a video of another failure case: