Skip to content

Commit

Permalink
BC-5863 - Remove tiptap from news (#4592)
Browse files Browse the repository at this point in the history
* removed unused input-types

* added type richTextCk5Simple
  • Loading branch information
wolfganggreschus authored Dec 6, 2023
1 parent 092c2f9 commit 3960c61
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ export class CreateNewsParams {
title!: string;

@IsString()
// TODO add correct validation for input format
@SanitizeHtml(InputFormat.RICH_TEXT)
@SanitizeHtml(InputFormat.RICH_TEXT_CK5_SIMPLE)
@ApiProperty({
description: 'Content of the News entity',
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class UpdateNewsParams {

@IsOptional()
@IsString()
@SanitizeHtml(InputFormat.RICH_TEXT)
@SanitizeHtml(InputFormat.RICH_TEXT_CK5_SIMPLE)
@ApiPropertyOptional({
description: 'Content of the News entity',
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,17 @@ describe('SanitizeHtmlTransformer Decorator', () => {
@SanitizeHtml(InputFormat.PLAIN_TEXT)
title2!: string;

@SanitizeHtml(InputFormat.RICH_TEXT_SIMPLE)
@SanitizeHtml(InputFormat.PLAIN_TEXT)
excerpt?: string;

@SanitizeHtml(InputFormat.RICH_TEXT)
content!: string;

@SanitizeHtml(InputFormat.RICH_TEXT_CK5)
contentCk5!: string;

@SanitizeHtml(InputFormat.RICH_TEXT_CK5_SIMPLE)
contentCk5Simple!: string;

@SanitizeHtml(InputFormat.RICH_TEXT_CK4)
contentCk4!: string;

@SanitizeHtml(InputFormat.RICH_TEXT_CK5_SIMPLE)
contentRichTextCk5Simple!: string;
}

describe('when fully sanitizing an input string', () => {
Expand All @@ -40,22 +37,6 @@ describe('SanitizeHtmlTransformer Decorator', () => {
});
});

describe('when sanitizing inline formatting', () => {
it('should remove all html but inline tags', () => {
const plainString = { excerpt: '<h1><b>html text</b></h1>' };
const instance = plainToClass(WithHtmlDto, plainString);
expect(instance.excerpt).toEqual('<b>html text</b>');
});
});

describe('when sanitizing rich text formatting', () => {
it('should remove all html but rich text tags', () => {
const plainString = { content: '<h1><b>html text</b></h1><scriPT>alert("foobar");</sCript><stYle></style>' };
const instance = plainToClass(WithHtmlDto, plainString);
expect(instance.content).toEqual('<h1><b>html text</b></h1>');
});
});

describe('when sanitizing rich text ck5 formatting', () => {
it('should remove all html but rich text ck5 tags', () => {
const plainString = {
Expand All @@ -69,22 +50,26 @@ describe('SanitizeHtmlTransformer Decorator', () => {
});
});

describe('when sanitizing rich text ck5 simple formatting', () => {
it('should remove all html but rich text ck5 simple tags', () => {
describe('when sanitizing rich text ck4 formatting', () => {
it('should remove all html but rich text ck4 tags', () => {
const plainString = {
contentCk5Simple:
'<p><b>strong</b><br />text</p><h2></h2><scriPT>alert("foobar");</sCript><stYle></style><img src="some.png" />',
contentCk4: '<h1><b>html text</b></h1><scriPT>alert("foobar");</sCript><stYle></style><a href></a>',
};
const instance = plainToClass(WithHtmlDto, plainString);
expect(instance.contentCk5Simple).toEqual('<p><b>strong</b><br />text</p>');
expect(instance.contentCk4).toEqual('<h1><b>html text</b></h1><a href></a>');
});
});

describe('when sanitizing rich text ck4 formatting', () => {
it('should remove all html but rich text ck4 tags', () => {
const plainString = { contentCk4: '<h1><b>html text</b></h1><scriPT>alert("foobar");</sCript><stYle></style>' };
describe('when sanitizing rich text Ck5 simple formatting', () => {
it('should remove all html but rich text ck5 simple tags', () => {
const plainString = {
contentRichTextCk5Simple:
'<h1></h1><h2><b><mark>html <h4>text</h4></mark></b></h2><span class="math-tex">[x=\frac{-bpmsqrt{b^2-4ac}}{2a}]</span><scriPT>alert("foobar");</sCript><stYle></style><img src="some.png" />',
};
const instance = plainToClass(WithHtmlDto, plainString);
expect(instance.contentCk4).toEqual('<h1><b>html text</b></h1>');
expect(instance.contentRichTextCk5Simple).toEqual(
'<h2>html <h4>text</h4></h2>[x= rac{-bpmsqrt{b^2-4ac}}{2a}]<img src="some.png" />'
);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,6 @@ const inputFormatsSanitizeConfig: Record<string, IInputFormatsConfig> = {
allowedAttributes: {},
},

RichTextSimple: {
allowedTags: ['b', 'i', 'em', 'strong', 'small', 's', 'u'],
allowedAttributes: {},
},

RichText: {
allowedTags: [
'b',
'i',
'em',
'strong',
'small',
's',
'u',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'ul',
'li',
'ol',
'dl',
'dt',
'dd',
'p',
'pre',
'br',
'hr',
'table',
'tbody',
'td',
'tfoot',
'th',
'thead',
'tr',
'tr',
'td',
'a',
'img',
],
allowedAttributes: {
a: ['href', 'name', 'target'],
img: ['src', 'srcset', 'alt', 'title', 'width', 'height', 'loading'],
},
},

// TODO
RichTextCk4: {
allowedTags: [
'b',
Expand Down Expand Up @@ -146,23 +97,21 @@ const inputFormatsSanitizeConfig: Record<string, IInputFormatsConfig> = {
},

RichTextCk5Simple: {
allowedTags: ['p', 'br', 'b', 'strong', 'i', 'em', 'u'],
allowedAttributes: {},
allowedTags: ['p', 'br', 'strong', 'em', 'u', 's', 'h2', 'h3', 'h4', 'ul', 'ol', 'li', 'img', 'src'],
allowedAttributes: {
img: ['src', 'srcset', 'alt', 'title', 'width', 'height', 'loading'],
},
},
};

export const getSanitizeHtmlOptions = (inputFormat?: InputFormat): IInputFormatsConfig => {
switch (inputFormat) {
case InputFormat.RICH_TEXT_SIMPLE:
return inputFormatsSanitizeConfig.RichTextSimple;
case InputFormat.RICH_TEXT:
return inputFormatsSanitizeConfig.RichText;
case InputFormat.RICH_TEXT_CK5_SIMPLE:
return inputFormatsSanitizeConfig.RichTextCk5Simple;
case InputFormat.RICH_TEXT_CK4:
return inputFormatsSanitizeConfig.RichTextCk4;
case InputFormat.RICH_TEXT_CK5:
return inputFormatsSanitizeConfig.RichTextCk5;
case InputFormat.RICH_TEXT_CK5_SIMPLE:
return inputFormatsSanitizeConfig.RichTextCk5Simple;
case InputFormat.PLAIN_TEXT:
default:
return inputFormatsSanitizeConfig.PlainText;
Expand Down
4 changes: 1 addition & 3 deletions apps/server/src/shared/domain/types/input-format.types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export enum InputFormat {
PLAIN_TEXT = 'plainText',
RICH_TEXT = 'richText',
RICH_TEXT_SIMPLE = 'inline',
RICH_TEXT_CK5_SIMPLE = 'richTextCk5Simple',
RICH_TEXT_CK4 = 'richTextCk4',
RICH_TEXT_CK5 = 'richTextCk5',
RICH_TEXT_CK5_SIMPLE = 'richTextCk5Inline',
}

0 comments on commit 3960c61

Please sign in to comment.