Skip to content

Commit

Permalink
Fix misplaced '@' in RTL post meta. (#4531)
Browse files Browse the repository at this point in the history
Co-authored-by: Joel <[email protected]>
Co-authored-by: Hailey <[email protected]>
  • Loading branch information
3 people authored Jul 7, 2024
1 parent ea37298 commit ea7afec
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
6 changes: 1 addition & 5 deletions src/lib/moderation/create-sanitized-display-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ export function createSanitizedDisplayName(
if (profile.displayName != null && profile.displayName !== '') {
return sanitizeDisplayName(profile.displayName)
} else {
let sanitizedHandle = sanitizeHandle(profile.handle)
if (!noAt) {
sanitizedHandle = `@${sanitizedHandle}`
}
return sanitizedHandle
return sanitizeHandle(profile.handle, noAt ? '' : '@')
}
}
10 changes: 10 additions & 0 deletions src/lib/strings/bidi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const LEFT_TO_RIGHT_EMBEDDING = '\u202A'
const POP_DIRECTIONAL_FORMATTING = '\u202C'

/*
* Force LTR directionality in a string.
* https://www.unicode.org/reports/tr9/#Directional_Formatting_Characters
*/
export function forceLTR(str: string) {
return LEFT_TO_RIGHT_EMBEDDING + str + POP_DIRECTIONAL_FORMATTING
}
6 changes: 5 additions & 1 deletion src/lib/strings/handles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Regex from the go implementation
// https://github.com/bluesky-social/indigo/blob/main/atproto/syntax/handle.go#L10
import {forceLTR} from 'lib/strings/bidi'

const VALIDATE_REGEX =
/^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/

Expand All @@ -22,7 +24,9 @@ export function isInvalidHandle(handle: string): boolean {
}

export function sanitizeHandle(handle: string, prefix = ''): string {
return isInvalidHandle(handle) ? '⚠Invalid Handle' : `${prefix}${handle}`
return isInvalidHandle(handle)
? '⚠Invalid Handle'
: forceLTR(`${prefix}${handle}`)
}

export interface IsValidHandle {
Expand Down
4 changes: 3 additions & 1 deletion src/view/com/util/PostMeta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ interface PostMetaOpts {
style?: StyleProp<ViewStyle>
}

const NON_BREAKING_SPACE = '\u00A0'

let PostMeta = (opts: PostMetaOpts): React.ReactNode => {
const pal = usePalette('default')
const displayName = opts.author.displayName || opts.author.handle
Expand Down Expand Up @@ -83,7 +85,7 @@ let PostMeta = (opts: PostMetaOpts): React.ReactNode => {
type="md"
disableMismatchWarning
style={[pal.textLight, {flexShrink: 4}]}
text={'\xa0' + sanitizeHandle(handle, '@')}
text={NON_BREAKING_SPACE + sanitizeHandle(handle, '@')}
href={profileLink}
onBeforePress={onBeforePressAuthor}
anchorNoUnderline
Expand Down

0 comments on commit ea7afec

Please sign in to comment.