mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-30 05:42:44 -08:00
Ignore malformed settings focus ids
This commit is contained in:
5 files changed
+56
-5
No files matched your search
@@ -855,4 +855,25 @@ describe('useSettingsFocus', () => {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it('ignores malformed focus ids without throwing', () => {
|
||||
expect(() =>
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/settings/appearance?focus=display-name%22%3ESettings']}>
|
||||
<ScreenSizeProvider value={ScreenSize.Mobile}>
|
||||
<LocationProbe />
|
||||
<FocusFixture />
|
||||
</ScreenSizeProvider>
|
||||
</MemoryRouter>
|
||||
)
|
||||
).not.toThrow();
|
||||
|
||||
const target = document.querySelector('[data-settings-focus="message-link-preview"]');
|
||||
const highlightTarget = target?.parentElement;
|
||||
|
||||
expect(highlightTarget).not.toHaveClass(focusedSettingTile);
|
||||
expect(screen.getByTestId('location-probe')).toHaveTextContent(
|
||||
'/settings/appearance?focus=display-name%22%3ESettings'
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -3,6 +3,7 @@ import { matchPath, useLocation, useNavigate } from 'react-router-dom';
|
||||
import { getSettingsPath } from '$pages/pathUtils';
|
||||
import { SETTINGS_PATH } from '$pages/paths';
|
||||
import type { SettingsSectionId } from './routes';
|
||||
import { normalizeSettingsFocusId } from './settingsLink';
|
||||
|
||||
export function useOpenSettings() {
|
||||
const navigate = useNavigate();
|
||||
@@ -14,7 +15,7 @@ export function useOpenSettings() {
|
||||
? undefined
|
||||
: { backgroundLocation: location };
|
||||
|
||||
navigate(getSettingsPath(section, focus), {
|
||||
navigate(getSettingsPath(section, normalizeSettingsFocusId(focus)), {
|
||||
state: settingsState,
|
||||
});
|
||||
},
|
||||
|
||||
@@ -5,6 +5,12 @@ import { focusedSettingTile } from './styles.css';
|
||||
const focusedSettingTileClasses = focusedSettingTile.split(' ').filter(Boolean);
|
||||
const getHighlightTarget = (target: HTMLElement): HTMLElement =>
|
||||
target.closest<HTMLElement>('[data-sequence-card="true"]') ?? target.parentElement ?? target;
|
||||
const getFocusTarget = (focusId: string): HTMLElement | null =>
|
||||
document.getElementById(focusId) ??
|
||||
Array.from(document.querySelectorAll<HTMLElement>('[data-settings-focus]')).find(
|
||||
(element) => element.getAttribute('data-settings-focus') === focusId
|
||||
) ??
|
||||
null;
|
||||
const SETTINGS_FOCUS_HANDLED_STATE_KEY = 'settingsFocusHandledKey';
|
||||
|
||||
type SettingsFocusRouteState = {
|
||||
@@ -39,9 +45,7 @@ export function useSettingsFocus() {
|
||||
return;
|
||||
}
|
||||
|
||||
const target =
|
||||
document.getElementById(focusId) ??
|
||||
document.querySelector<HTMLElement>(`[data-settings-focus="${focusId}"]`);
|
||||
const target = getFocusTarget(focusId);
|
||||
|
||||
if (!target) return;
|
||||
|
||||
|
||||
@@ -60,4 +60,26 @@ describe('useMentionClickHandler', () => {
|
||||
|
||||
expect(mockOpenSettings).toHaveBeenCalledWith('appearance', 'message-link-preview');
|
||||
});
|
||||
|
||||
it('drops malformed settings focus ids before calling openSettings', () => {
|
||||
const { result } = renderHook(() => useMentionClickHandler('!room:example.org'), {
|
||||
wrapper: Wrapper,
|
||||
});
|
||||
const malformedFocus = 'display-name">Settings';
|
||||
|
||||
const { getByRole } = render(
|
||||
<button
|
||||
type="button"
|
||||
data-settings-link-section="account"
|
||||
data-settings-link-focus={malformedFocus}
|
||||
onClick={result.current}
|
||||
>
|
||||
Open malformed settings link
|
||||
</button>
|
||||
);
|
||||
|
||||
fireEvent.click(getByRole('button', { name: 'Open malformed settings link' }));
|
||||
|
||||
expect(mockOpenSettings).toHaveBeenCalledWith('account', undefined);
|
||||
});
|
||||
});
|
||||
@@ -4,6 +4,7 @@ import { isRoomId, isUserId } from '$utils/matrix';
|
||||
import { getHomeRoomPath, withSearchParam } from '$pages/pathUtils';
|
||||
import { RoomSearchParams } from '$pages/paths';
|
||||
import { isSettingsSectionId } from '$features/settings/routes';
|
||||
import { normalizeSettingsFocusId } from '$features/settings/settingsLink';
|
||||
import { useOpenSettings } from '$features/settings/useOpenSettings';
|
||||
import { useOpenUserRoomProfile } from '$state/hooks/userRoomProfile';
|
||||
import { useMatrixClient } from './useMatrixClient';
|
||||
@@ -25,7 +26,9 @@ export const useMentionClickHandler = (roomId: string): ReactEventHandler<HTMLEl
|
||||
const target = evt.currentTarget;
|
||||
const settingsSection = target.getAttribute('data-settings-link-section') || undefined;
|
||||
if (isSettingsSectionId(settingsSection)) {
|
||||
const settingsFocus = target.getAttribute('data-settings-link-focus') || undefined;
|
||||
const settingsFocus = normalizeSettingsFocusId(
|
||||
target.getAttribute('data-settings-link-focus') || undefined
|
||||
);
|
||||
openSettings(settingsSection, settingsFocus);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user