|
| 1 | +import {SanitizeOptions} from '@diplodoc/transform/lib/sanitize'; |
| 2 | + |
| 3 | +import {getSanitizeYfmHtmlBlock, getYfmHtmlBlockOptions} from './utils'; // update the path accordingly |
| 4 | + |
| 5 | +// remove all whitespaces and newline characters |
| 6 | +const normalizeWhitespace = (str: string) => str.replace(/\s+/g, ' ').trim(); |
| 7 | + |
| 8 | +describe('sanitize options functions', () => { |
| 9 | + const defaultOptions: SanitizeOptions = { |
| 10 | + allowedTags: ['b', 'i', 'strong', 'em'], |
| 11 | + allowedAttributes: { |
| 12 | + a: ['href', 'name', 'target'], |
| 13 | + }, |
| 14 | + cssWhiteList: { |
| 15 | + color: true, |
| 16 | + 'font-weight': true, |
| 17 | + }, |
| 18 | + }; |
| 19 | + |
| 20 | + it('should merge additional tags into default options', () => { |
| 21 | + const options = getYfmHtmlBlockOptions(defaultOptions); |
| 22 | + expect(options.allowedTags).toEqual(expect.arrayContaining(['link', 'base', 'style'])); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should merge additional attributes into default options', () => { |
| 26 | + const options = getYfmHtmlBlockOptions(defaultOptions); |
| 27 | + expect(options.allowedAttributes).toEqual({ |
| 28 | + ...defaultOptions.allowedAttributes, |
| 29 | + link: ['rel', 'href'], |
| 30 | + base: ['target'], |
| 31 | + style: [], |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should merge additional css properties into default options', () => { |
| 36 | + const options = getYfmHtmlBlockOptions(defaultOptions); |
| 37 | + expect(options.cssWhiteList).toEqual({ |
| 38 | + ...defaultOptions.cssWhiteList, |
| 39 | + 'align-content': true, |
| 40 | + 'align-items': true, |
| 41 | + 'align-self': true, |
| 42 | + color: true, |
| 43 | + 'column-count': true, |
| 44 | + 'column-fill': true, |
| 45 | + 'column-gap': true, |
| 46 | + 'column-rule': true, |
| 47 | + 'column-rule-color': true, |
| 48 | + 'column-rule-style': true, |
| 49 | + 'column-rule-width': true, |
| 50 | + 'column-span': true, |
| 51 | + 'column-width': true, |
| 52 | + columns: true, |
| 53 | + flex: true, |
| 54 | + 'flex-basis': true, |
| 55 | + 'flex-direction': true, |
| 56 | + 'flex-flow': true, |
| 57 | + 'flex-grow': true, |
| 58 | + 'flex-shrink': true, |
| 59 | + 'flex-wrap': true, |
| 60 | + 'font-weight': true, |
| 61 | + gap: true, |
| 62 | + grid: true, |
| 63 | + 'grid-area': true, |
| 64 | + 'grid-auto-columns': true, |
| 65 | + 'grid-auto-flow': true, |
| 66 | + 'grid-auto-rows': true, |
| 67 | + 'grid-column': true, |
| 68 | + 'grid-column-end': true, |
| 69 | + 'grid-column-start': true, |
| 70 | + 'grid-row': true, |
| 71 | + 'grid-row-end': true, |
| 72 | + 'grid-row-start': true, |
| 73 | + 'grid-template': true, |
| 74 | + 'grid-template-areas': true, |
| 75 | + 'grid-template-columns': true, |
| 76 | + 'grid-template-rows': true, |
| 77 | + 'justify-content': true, |
| 78 | + 'justify-items': true, |
| 79 | + 'justify-self': true, |
| 80 | + 'line-height': true, |
| 81 | + 'object-fit': true, |
| 82 | + 'object-position': true, |
| 83 | + order: true, |
| 84 | + orphans: true, |
| 85 | + 'row-gap': true, |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +describe('sanitize HTML function', () => { |
| 91 | + const options: SanitizeOptions = { |
| 92 | + allowedTags: ['b', 'i', 'strong', 'em'], |
| 93 | + allowedAttributes: { |
| 94 | + a: ['href', 'name', 'target'], |
| 95 | + }, |
| 96 | + cssWhiteList: { |
| 97 | + color: true, |
| 98 | + 'font-weight': true, |
| 99 | + }, |
| 100 | + }; |
| 101 | + |
| 102 | + it('should sanitize HTML content with additional options', () => { |
| 103 | + const htmlContent = ` |
| 104 | + <b>Bold</b> |
| 105 | + <i>Italic</i> |
| 106 | + <link href="styles.css" rel="stylesheet" /> |
| 107 | + <base target="_blank" /> |
| 108 | + <style>.example { flex: 1; columns: 1; }</style> |
| 109 | + `; |
| 110 | + |
| 111 | + const sanitizeYfmHtmlBlock = getSanitizeYfmHtmlBlock({options}); |
| 112 | + const sanitizedContent = normalizeWhitespace(sanitizeYfmHtmlBlock(htmlContent)); |
| 113 | + |
| 114 | + expect(sanitizedContent).toContain('<link href="styles.css" rel="stylesheet" />'); |
| 115 | + expect(sanitizedContent).toContain('<base target="_blank" />'); |
| 116 | + expect(sanitizedContent).toContain( |
| 117 | + normalizeWhitespace('<style>.example { flex: 1; columns: 1; }</style>'), |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should sanitize HTML content using a custom sanitize function', () => { |
| 122 | + // example of custom sanitize logic |
| 123 | + const customSanitize = (html: string, _?: SanitizeOptions): string => { |
| 124 | + return html.replace(/<style.*<\/style>/, '<style></style>'); |
| 125 | + }; |
| 126 | + |
| 127 | + const htmlContent = '<style>.example { flex: 1; columns: 1; }</style>'; |
| 128 | + const expectedSanitizedContent = '<style></style>'; |
| 129 | + |
| 130 | + const sanitizeYfmHtmlBlock = getSanitizeYfmHtmlBlock({options, sanitize: customSanitize}); |
| 131 | + const sanitizedContent = sanitizeYfmHtmlBlock(htmlContent); |
| 132 | + |
| 133 | + expect(sanitizedContent).toEqual(expectedSanitizedContent); |
| 134 | + }); |
| 135 | +}); |
0 commit comments