5180 fichiers 38.5 Mo 0 téléchargements 199 vues mis à jour il y a 5 heures ui
nitro-v36-with-renderer-v36 / src / api / utils / InternalHtmlLinks.ts
InternalHtmlLinks.ts 1.3 Ko Télécharger le fichier
import { CreateLinkEvent } from '@octane/renderer';
import { MouseEvent } from 'react';
import { SanitizeHtml } from './SanitizeHtml';

/** Where an `event:` link waits after sanitising: a fragment link the sanitizer keeps. */
const INTERNAL_LINK_PREFIX = '#octane-event:';

/**
 * Sanitises page HTML whose links are AIR client links (`<a href="event:habbopages/...">`, an html window with
 * `link_target="internal"`). The sanitizer drops the unknown `event:` scheme, so those links are first turned into
 * fragment links; {@link handleInternalLinkClick} hands them back to the client's link events.
 */
export const SanitizeHtmlWithInternalLinks = (html: string): string =>
    SanitizeHtml((html ?? '').replace(/href\s*=\s*(["'])\s*event:/gi, (_match, quote: string) => `href=${quote}${INTERNAL_LINK_PREFIX}`));

/** The click handler for HTML from {@link SanitizeHtmlWithInternalLinks}: an internal link opens in the client. */
export const handleInternalLinkClick = (event: MouseEvent<HTMLElement>): void => {
    const anchor = (event.target as HTMLElement | null)?.closest?.('a');
    const href = anchor?.getAttribute('href') ?? '';

    if (!href.startsWith(INTERNAL_LINK_PREFIX)) return;

    event.preventDefault();

    const link = href.substring(INTERNAL_LINK_PREFIX.length).trim();

    if (link) CreateLinkEvent(link);
};
15Sujets
1Dépôts
20Membres
2En ligne
Profile
Inscrit
Dernière visite
Messages
0
Votes positifs
0
Votes négatifs
0

Nous utilisons des cookies pour assurer le bon fonctionnement du site et améliorer votre expérience. en savoir plus

Autoriser