Skip to content

Commit 196f9f6

Browse files
authored
fix(BaseSchema): fix serialization of empty paragraphs inside other blocks (#846)
1 parent 1c0005d commit 196f9f6

File tree

2 files changed

+53
-18
lines changed

2 files changed

+53
-18
lines changed

src/extensions/base/BaseSchema/BaseSchema.test.ts

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,62 @@ import {builders} from 'prosemirror-test-builder';
22

33
import {createMarkupChecker} from '../../../../tests/sameMarkup';
44
import {ExtensionsManager} from '../../../core';
5+
import {BlockquoteSpecs, blockquoteNodeName} from '../../markdown/Blockquote/BlockquoteSpecs';
56

67
import {BaseNode, BaseSchemaSpecs} from './BaseSchemaSpecs';
78

8-
const {
9-
schema,
10-
markupParser: parser,
11-
serializer,
12-
} = new ExtensionsManager({
13-
extensions: (builder) => builder.use(BaseSchemaSpecs, {}),
14-
}).buildDeps();
15-
16-
const {doc, p} = builders<'doc' | 'p'>(schema, {
17-
doc: {nodeType: BaseNode.Doc},
18-
p: {nodeType: BaseNode.Paragraph},
19-
});
9+
describe('BaseSchema extension', () => {
10+
describe('common', () => {
11+
const {
12+
schema,
13+
markupParser: parser,
14+
serializer,
15+
} = new ExtensionsManager({
16+
extensions: (builder) => builder.use(BaseSchemaSpecs, {}),
17+
}).buildDeps();
2018

21-
const {same} = createMarkupChecker({parser, serializer});
19+
const {doc, p} = builders<'doc' | 'p'>(schema, {
20+
doc: {nodeType: BaseNode.Doc},
21+
p: {nodeType: BaseNode.Paragraph},
22+
});
2223

23-
describe('BaseSchema extension', () => {
24-
it('should parse a paragraph', () => same('hello!', doc(p('hello!'))));
24+
const {same} = createMarkupChecker({parser, serializer});
25+
26+
it('should parse a paragraph', () => same('hello!', doc(p('hello!'))));
27+
28+
it('should parse a few paragraphs', () => {
29+
same(['hello', '', 'world!'].join('\n'), doc(p('hello'), p('world!')));
30+
});
31+
});
32+
33+
describe('preserveEmptyRows=true', () => {
34+
const {
35+
schema,
36+
markupParser: parser,
37+
serializer,
38+
} = new ExtensionsManager({
39+
extensions: (builder) =>
40+
builder.use(BaseSchemaSpecs, {preserveEmptyRows: true}).use(BlockquoteSpecs),
41+
}).buildDeps();
42+
43+
const {doc, p, bq} = builders<'doc' | 'p' | 'bq'>(schema, {
44+
doc: {nodeType: BaseNode.Doc},
45+
p: {nodeType: BaseNode.Paragraph},
46+
bq: {nodeType: blockquoteNodeName},
47+
});
48+
49+
const {same} = createMarkupChecker({parser, serializer});
50+
51+
it('should serialize empty paragraph as &nbsp;', () =>
52+
same(
53+
'hello!\n\n&nbsp;\n\nworld!',
54+
doc(p('hello!'), p(String.fromCharCode(160 /* &nbsp; */)), p('world!')),
55+
));
2556

26-
it('should parse a few paragraphs', () => {
27-
same(['hello', '', 'world!'].join('\n'), doc(p('hello'), p('world!')));
57+
it('should correct insert &nbsp; inside other nodes', () =>
58+
same(
59+
'> hello!\n>\n> &nbsp;\n> \n> world!',
60+
doc(bq(p('hello!'), p(String.fromCharCode(160 /* &nbsp; */)), p('world!'))),
61+
));
2862
});
2963
});

src/extensions/base/BaseSchema/BaseSchemaSpecs/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ export const BaseSchemaSpecs: ExtensionAuto<BaseSchemaSpecsOptions> = (builder,
8585
}
8686

8787
if (!isParentEmpty) {
88-
state.write('&nbsp;\n\n');
88+
state.write('&nbsp;\n');
89+
state.write('\n');
8990
}
9091
} else {
9192
state.renderInline(node);

0 commit comments

Comments
 (0)