Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit

Permalink
Add html test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
sambecker committed Sep 21, 2024
1 parent b486972 commit bd187da
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
17 changes: 17 additions & 0 deletions __tests__/html.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { htmlHasBrParagraphBreaks, safelyParseFormattedHtml } from '@/utility/html';
import { parameterize } from '@/utility/string';

describe('HTML', () => {
it('safely parses', () => {
expect(safelyParseFormattedHtml('<p>TEXT</p>')).toBe('TEXT');
expect(safelyParseFormattedHtml('<b>TEXT</b>')).toBe('<b>TEXT</b>');
});
it('detects br-style paragraph breaks', () => {
expect(htmlHasBrParagraphBreaks('TEXT<br><br>')).toBeTruthy();
expect(htmlHasBrParagraphBreaks('TEXT<br /><br />')).toBeTruthy();
expect(htmlHasBrParagraphBreaks('TEXT<br><br />')).toBeTruthy();
expect(htmlHasBrParagraphBreaks('TEXT')).toBeFalsy();
expect(htmlHasBrParagraphBreaks('TEXT<br/>')).toBeFalsy();
expect(htmlHasBrParagraphBreaks('TEXT<br />')).toBeFalsy();
});
});
4 changes: 2 additions & 2 deletions src/photo/PhotoGridSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { useAppState } from '@/state/AppState';
import { useMemo } from 'react';
import HiddenTag from '@/tag/HiddenTag';
import { SITE_ABOUT } from '@/site/config';
import { htmlHasBrParagraphBreaks, safelyParseFormattedHTML } from '@/utility/html';
import { htmlHasBrParagraphBreaks, safelyParseFormattedHtml } from '@/utility/html';
import { clsx } from 'clsx/lite';

export default function PhotoGridSidebar({
Expand Down Expand Up @@ -50,7 +50,7 @@ export default function PhotoGridSidebar({
htmlHasBrParagraphBreaks(SITE_ABOUT) && 'pb-2',
)}
dangerouslySetInnerHTML={{
__html: safelyParseFormattedHTML(SITE_ABOUT),
__html: safelyParseFormattedHtml(SITE_ABOUT),
}}
/>]}
/>}
Expand Down
2 changes: 1 addition & 1 deletion src/utility/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import sanitizeHtml from 'sanitize-html';

const ALLOWED_FORMATTING_TAGS = ['b', 'strong', 'i', 'em', 'u', 'br'];

export const safelyParseFormattedHTML = (text: string) =>
export const safelyParseFormattedHtml = (text: string) =>
sanitizeHtml(text, {
allowedTags: ALLOWED_FORMATTING_TAGS,
});
Expand Down

0 comments on commit bd187da

Please sign in to comment.