-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Webdev 5735 use screenname #879
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,9 +79,24 @@ class PrimaryNav extends TrackedElement { | |
); | ||
} | ||
|
||
get isRTL() { | ||
// https://stackoverflow.com/questions/12006095/javascript-how-to-check-if-character-is-rtl | ||
const ltrChars = 'A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02B8\u0300-\u0590\u0800-\u1FFF'+'\u2C00-\uFB1C\uFDFE-\uFE6F\uFEFD-\uFFFF'; | ||
const rtlChars = '\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC'; | ||
const rtlCheck = new RegExp('^[^'+ltrChars+']*['+rtlChars+']'); | ||
|
||
return rtlCheck.test(this.screenName); | ||
} | ||
|
||
get truncatedScreenName() { | ||
if (this.screenName && this.screenName.length > 10) { | ||
return `${this.screenName.substr(0, 9)}…`; | ||
if (this.screenName && [...this.screenName].length > 10) { | ||
// Works with RTL and Unicode | ||
const truncated = [...this.screenName].slice(0, 9).join(''); | ||
Comment on lines
91
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for handling multibyte code points sensibly! I'm curious, are screen names always stored in NFC-normalized form? If we don't have that guarantee, one additional step that might help here is normalizing the screen name string before any length-checking or truncation, to collapse any combining characters down to their "combined" form if possible. If a name has multiple combining accents that don't contribute to the visual width of the string, for instance, we can avoid including those in the length budget. Notably, this also prevents us from accidentally truncating an accent off, at least in cases where the combined character exists.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi Laton, I'd like to do more tests, but I believe converting the string into an array is the simplest mechanism for splitting a multibyte string by grouped (in a single character) sequence of code points. If we use normalization to count, we might break the visual display of the original character -- or more complicated counting of normalized There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The array conversion you're doing is indeed a great, simple way to get the sequence of multibyte code points from the original string. I fully endorse that approach! Normalization here would accomplish a different goal: ensuring that sequences of separate code points that can be represented by an equivalent single one are always collapsed to the single-code-point version prior to the array conversion & truncation. To demonstrate what I mean, consider a screen name like So
If anything, this should preserve the visual display of the original characters in cases where they would otherwise be broken apart. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the detailed explanation. I'll use this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are, of course, further cases where even this normalization fails to help, and to address those we would need to consider entire grapheme clusters, which is substantially more involved. Consider tricky usernames like:
(I'm not advocating that we deal with cases like those within the scope of this ticket! Just pointing it out for a clear sense of where the edges are.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've tested many different in-browser truncations of multiple code point characters in Chrome, FF, Edge, and Brave. The browsers have never chopped a multi-point character (can't really tell with the Indian(?) script above -- even with non-normalized composition unicode. I've chosen to not normalize and rely on in-browser truncation. Code to follow after fixing problem with static top nav. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW: https://developer.mozilla.org/en-US/play
CSS
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good -- normalization would only be required if you're slicing up the string yourself. No need if we're going to use CSS truncation instead (which I assume is based on the width of rendered text, rather than the underlying text encoding). |
||
if (this.isRTL) { | ||
return `…${truncated}`; | ||
} else { | ||
return `${truncated}…`; | ||
} | ||
} | ||
return this.screenName; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,30 @@ describe('<primary-nav>', () => { | |
expect(usernameSpan.innerText).to.equal('somesuper…'); | ||
}); | ||
|
||
it('truncates a long screen name with extended characters', async () => { | ||
const el = await fixture(component({ | ||
baseHost: 'archive.org', | ||
username: '@foo', | ||
screenName: 'a😊b😊c😊d😊😊😊😊😊😊😊😊', | ||
})); | ||
|
||
const usernameSpan = el.shadowRoot.querySelector('.username'); | ||
|
||
expect(usernameSpan.innerText).to.equal('a😊b😊c😊d😊😊…'); | ||
}); | ||
|
||
it('truncates a long screen name with RTL extended characters', async () => { | ||
const el = await fixture(component({ | ||
baseHost: 'archive.org', | ||
username: '@foo', | ||
screenName: ' الدكتور محمالدكتور محمد العجوز', | ||
})); | ||
|
||
const usernameSpan = el.shadowRoot.querySelector('.username'); | ||
|
||
expect(usernameSpan.innerText).to.equal('… الدكتور '); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
}); | ||
|
||
it('opens a slot with `secondIdentitySlotMode`', async () => { | ||
const el = await fixture(component({ | ||
baseHost: 'archive.org', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just noting that browsers can also be made to perform this RTL character check natively, by adding a
dir="auto"
attribute to the enclosing element. The first strongly-directional character found in the element's text determines its overall directionality, placing nondirectional characters (like the ellipsis) on the corresponding side automatically: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/dir(Maybe that wouldn't apply to this use case, but just figured I'd point it out as an option to explore)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Laton, yes, aware of
dir="auto"
and that, if I'm not mistaken, requires an rendered element to work on.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's true that the RTL checks would take place at render time rather than during JS execution, and the result would not be readily available to our code.
My thinking was that if we can get the browser to do all the RTL detection & rendering work for us natively, then we may not need JS-based RTL checks in the first place. Using
dir="auto"
, the browser can determine which side to visually render the ellipsis on without our JS having to know anything about the directionality of the text.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your considered discussion.
I checked with Jim Shelton about having truncation/ellipsis by
max-width
and not 9 characters. Gets rid of a good deal of code, which makes sense to me.