Skip to content
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

frontend: improve simple markup parsing #3055

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion frontends/web/src/utils/markup.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,27 @@ import { MultilineMarkup, SimpleMarkup } from './markup';

describe('SimpleMarkup', () => {
it('contains a strong element with the text bar', () => {
const { container } = render(<SimpleMarkup tagName="p" markup="foo <strong>bar</strong> baz" />);
const { container } = render(<SimpleMarkup tagName="div" markup="foo <strong>bar</strong> baz" />);
// container is a div that wraps whatever element that's being rendered
expect(container.firstElementChild?.nodeName).toBe('DIV');
expect(container.querySelector('strong')).toHaveTextContent('bar');
});
it('should ignore newlines', () => {
const { container } = render(<SimpleMarkup tagName="p" markup="foo <strong>bar</strong> baz.\n" />);
// container is a paragraph element that wraps whatever element that's being rendered
expect(container.firstElementChild?.nodeName).toBe('P');
expect(container.querySelector('strong')).toHaveTextContent('bar');
});
it('should ignore newlines 2', () => {
const { container } = render(<SimpleMarkup tagName="p" markup="foo \n<strong>bar</strong>\n baz.\n" />);
expect(container.firstElementChild?.nodeName).toBe('P');
expect(container.querySelector('strong')).toHaveTextContent('bar');
});
it('should ignore newlines 3', () => {
const { container } = render(<SimpleMarkup tagName="p" markup="foo \n<strong>bar\n bar\n</strong>\n baz.\n" />);
expect(container.firstElementChild?.nodeName).toBe('P');
expect(container.querySelector('strong')).toHaveTextContent(/bar(.*)bar(.*)$/i);
});
it('should but doesnt support multiple strong elements', () => {
const { container } = render(<SimpleMarkup tagName="span" markup="<strong>foo</strong> <strong>bar</strong> <strong>baz</strong>" />);
expect(container.firstElementChild?.nodeName).toBe('SPAN');
Expand Down
2 changes: 1 addition & 1 deletion frontends/web/src/utils/markup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type TMarkupProps = {
key?: string;
} & React.HTMLAttributes<HTMLElement>;

const captureStrongElement = /^(.*)<strong>(.*)<\/strong>(.*)$/;
const captureStrongElement = /^(.*)<strong>(.*)<\/strong>(.*)$/m;

/**
* **SimpleMarkup** renders `foo <strong>bar</strong> baz` safely as
Expand Down
Loading