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

fix: cannot convert details, summary block using useDefaultHTMLSanitizer: false option #1472

Merged
merged 3 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion apps/editor/src/js/convertor.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const attribute = `(?:\\s+${attrName}(?:\\s*=\\s*${attrValue})?)*\\s*`;
const openingTag = `(\\\\<|<)([A-Za-z][A-Za-z0-9\\-]*${attribute})(\\/?>)`;
const HTML_TAG_RX = new RegExp(openingTag, 'g');
const FRONT_MATTER_RX = /^\s?\\-\\-\\-([\s\S]+?)\\-\\-\\-/;
const NBSP_RX = /&nbsp;/g;

/**
* Class Convertor
Expand Down Expand Up @@ -180,7 +181,7 @@ class Convertor {
result[index] = line;
});

return result.join('\n');
return result.join('\n').replace(NBSP_RX, ' ');
}

_removeNewlinesBeforeAfterAndBlockElement(markdown) {
Expand Down
4 changes: 2 additions & 2 deletions apps/editor/src/js/mdPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,14 @@ class MarkdownPreview extends Preview {
startEl.insertAdjacentHTML('beforebegin', newHtml);
let el = startEl;

while (el !== endEl) {
while (el && el !== endEl) {
const nextEl = el.nextElementSibling;

el.parentNode.removeChild(el);
removeOffsetInfoByNode(el);
el = nextEl;
}
if (el.parentNode) {
if (el && el.parentNode) {
js87zz marked this conversation as resolved.
Show resolved Hide resolved
domUtils.remove(el);
removeOffsetInfoByNode(el);
}
Expand Down
4 changes: 3 additions & 1 deletion apps/editor/src/js/wysiwygEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import KeyMapper from './keyMapper';
import WwTextObject from './wwTextObject';
import ComponentManager from './componentManager';
import CodeBlockGadget from './ui/codeBlockGadget';
import htmlSanitizer from './htmlSanitizer';

const keyMapper = KeyMapper.getSharedInstance();

Expand Down Expand Up @@ -83,7 +84,8 @@ class WysiwygEditor {
leafNodeNames: {
HR: false
},
allowedBlocks: this._sanitizer ? [] : ['details', 'summary']
allowedBlocks:
this._sanitizer && this._sanitizer === htmlSanitizer ? [] : ['details', 'summary']
});
this.editor.blockCommandShortcuts();

Expand Down
19 changes: 19 additions & 0 deletions apps/editor/test/unit/convertor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -744,3 +744,22 @@ describe('Convertor', () => {
});
});
});

describe('Convertor without default sanitizer', () => {
let convertor, em;

beforeEach(() => {
em = new EventManager();
convertor = new Convertor(em);
convertor.initHtmlSanitizer(html => html);
});

it('should convert details block without default sanitizer', () => {
const markdown = '<details><summary>2021</summary>test</details>';
const html =
'<details data-tomark-pass=""><summary data-tomark-pass="">2021</summary>test</details>\n';

expect(convertor.toHTML(markdown)).toBe(html);
expect(convertor.toMarkdown(html)).toBe(markdown);
});
});
9 changes: 9 additions & 0 deletions apps/editor/test/unit/wysiwygEditor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -753,4 +753,13 @@ describe('WysiwygEditor', () => {
it('getSanitizer()', () => {
expect(wwe.getSanitizer()).toEqual(sanitizer);
});

it('should render details block without default sanitizer', () => {
const html = '<details><summary>2021</summary>test</details>';
const result = '<details><summary>2021</summary>test<br /></details><br />';

wwe.setValue(html);

expect(wwe.getValue()).toEqual(result);
});
});
6 changes: 4 additions & 2 deletions libs/toastmark/src/__sample__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ cm.on('change', (editor, changeObj) => {
if (startEl) {
startEl.insertAdjacentHTML('beforebegin', newHtml);
let el: Element = startEl;
while (el !== endEl) {
while (el && el !== endEl) {
const nextEl: Element | null = el.nextElementSibling;
el.remove();
el = nextEl!;
}
el.remove();
if (el) {
el.remove();
js87zz marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

Expand Down