Browse Source

Fix parser to take account of `parser.optionsHtml.keepEmptyTextNodes` option (#6571)

* Fix parser to take account of `configParser.optionsHtml.keepEmptyTextNodes`

* Delete unused `editorConfig.keepEmptyTextNodes`
release-v0.22.12
nanto_vi, TOYAMA Nao 7 months ago
committed by GitHub
parent
commit
a94cf2526b
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 7
      packages/core/src/editor/config/config.ts
  2. 2
      packages/core/src/parser/model/ParserHtml.ts
  3. 39
      packages/core/test/specs/parser/model/ParserHtml.ts

7
packages/core/src/editor/config/config.ts

@ -204,12 +204,6 @@ export interface EditorConfig {
*/ */
tagVarEnd?: string; tagVarEnd?: string;
/**
* When false, removes empty text nodes when parsed, unless they contain a space.
* @default false
*/
keepEmptyTextNodes?: boolean;
/** /**
* Return JS of components inside HTML from 'editor.getHtml()'. * Return JS of components inside HTML from 'editor.getHtml()'.
* @default true * @default true
@ -461,7 +455,6 @@ const config: () => EditorConfig = () => ({
mediaCondition: 'max-width', mediaCondition: 'max-width',
tagVarStart: '{[ ', tagVarStart: '{[ ',
tagVarEnd: ' ]}', tagVarEnd: ' ]}',
keepEmptyTextNodes: false,
jsInHtml: true, jsInHtml: true,
nativeDnD: true, nativeDnD: true,
multipleSelection: true, multipleSelection: true,

2
packages/core/src/parser/model/ParserHtml.ts

@ -325,7 +325,6 @@ const ParserHtml = (em?: EditorModel, config: ParserConfig & { returnArray?: boo
const conf = em?.get('Config') || {}; const conf = em?.get('Config') || {};
const Parser = em?.Parser; const Parser = em?.Parser;
const res: HTMLParseResult = { html: [] }; const res: HTMLParseResult = { html: [] };
const cf = { ...config, ...opts };
const preOptions = { const preOptions = {
...config.optionsHtml, ...config.optionsHtml,
// @ts-ignore Support previous `configParser.htmlType` option // @ts-ignore Support previous `configParser.htmlType` option
@ -336,6 +335,7 @@ const ParserHtml = (em?: EditorModel, config: ParserConfig & { returnArray?: boo
...preOptions, ...preOptions,
asDocument: this.__checkAsDocument(str, preOptions), asDocument: this.__checkAsDocument(str, preOptions),
}; };
const cf = { ...config, ...options };
const { preParser, asDocument } = options; const { preParser, asDocument } = options;
const inputOptions = { input: isFunction(preParser) ? preParser(str, { editor: em?.getEditor()! }) : str }; const inputOptions = { input: isFunction(preParser) ? preParser(str, { editor: em?.getEditor()! }) : str };
Parser?.__emitEvent(ParserEvents.htmlBefore, inputOptions); Parser?.__emitEvent(ParserEvents.htmlBefore, inputOptions);

39
packages/core/test/specs/parser/model/ParserHtml.ts

@ -983,4 +983,43 @@ describe('ParserHtml', () => {
expect(obj.parse(str).html).toEqual(result); expect(obj.parse(str).html).toEqual(result);
}); });
}); });
describe('with keepEmptyTextNodes ON', () => {
beforeEach(() => {
obj = ParserHtml(em, {
returnArray: true,
optionsHtml: { keepEmptyTextNodes: true },
});
obj.compTypes = em.Components.componentTypes;
});
test('Keep empty whitespaces', () => {
const str = `<div>
<p>TestText</p>
</div>`;
const result = [
{
tagName: 'div',
components: [
{
tagName: '',
type: 'textnode',
content: '\n ',
},
{
tagName: 'p',
components: { type: 'textnode', content: 'TestText' },
type: 'text',
},
{
tagName: '',
type: 'textnode',
content: '\n ',
},
],
},
];
expect(obj.parse(str).html).toEqual(result);
});
});
}); });

Loading…
Cancel
Save