Skip to content

Commit d4d5c22

Browse files
committed
test: cover ellipsis rule in code
1 parent 2bbc465 commit d4d5c22

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import {EditorState, TextSelection} from 'prosemirror-state';
2+
import {EditorView} from 'prosemirror-view';
3+
import {builders} from 'prosemirror-test-builder';
4+
5+
import {ExtensionsManager} from '../../../core';
6+
import {BaseSchemaSpecs, BaseNode} from '../specs';
7+
import {CodeSpecs, codeMarkName} from '../../markdown/Code/CodeSpecs';
8+
import {BaseInputRules} from './index';
9+
10+
const {
11+
schema,
12+
plugins,
13+
} = new ExtensionsManager({
14+
extensions: (builder) =>
15+
builder.use(BaseSchemaSpecs, {}).use(CodeSpecs).use(BaseInputRules),
16+
}).build();
17+
18+
const {doc, p, c} = builders<'doc' | 'p', 'c'>(schema, {
19+
doc: {nodeType: BaseNode.Doc},
20+
p: {nodeType: BaseNode.Paragraph},
21+
c: {markType: codeMarkName},
22+
});
23+
24+
describe('BaseInputRules ellipsis', () => {
25+
it('does not replace triple dots inside inline code', () => {
26+
const startDoc = doc(p(c('text<a>')));
27+
const state = EditorState.create({
28+
schema,
29+
doc: startDoc,
30+
selection: TextSelection.create(startDoc, startDoc.tag.a),
31+
plugins,
32+
});
33+
const view = new EditorView(document.createElement('div'), {state});
34+
35+
const handled = view.someProp('handleTextInput', (f: any) =>
36+
f(view, startDoc.tag.a, startDoc.tag.a, '...'),
37+
);
38+
39+
if (!handled) {
40+
view.dispatch(view.state.tr.insertText('...', startDoc.tag.a, startDoc.tag.a));
41+
}
42+
43+
expect(view.state.doc).toMatchNode(doc(p(c('text...'))));
44+
});
45+
46+
it('replaces triple dots in regular text', () => {
47+
const startDoc = doc(p('text<a>'));
48+
const state = EditorState.create({
49+
schema,
50+
doc: startDoc,
51+
selection: TextSelection.create(startDoc, startDoc.tag.a),
52+
plugins,
53+
});
54+
const view = new EditorView(document.createElement('div'), {state});
55+
56+
const handled = view.someProp('handleTextInput', (f: any) =>
57+
f(view, startDoc.tag.a, startDoc.tag.a, '...'),
58+
);
59+
60+
if (!handled) {
61+
view.dispatch(view.state.tr.insertText('...', startDoc.tag.a, startDoc.tag.a));
62+
}
63+
64+
expect(view.state.doc).toMatchNode(doc(p('text…')));
65+
});
66+
});
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
import {ellipsis} from 'prosemirror-inputrules';
1+
import {InputRule} from 'prosemirror-inputrules';
22

33
import type {ExtensionAuto} from '../../../core';
4+
import {hasCodeMark} from '../../../utils/inputrules';
5+
6+
const ellipsisInputRule = new InputRule(/\.\.\.$/, (state, match, start, end) => {
7+
if (hasCodeMark(state, match, start, end)) return null;
8+
return state.tr.insertText('…', start, end);
9+
});
410

511
export const BaseInputRules: ExtensionAuto = (builder) => {
6-
builder.addInputRules(() => ({rules: [ellipsis]}));
12+
builder.addInputRules(() => ({rules: [ellipsisInputRule]}));
713
};

0 commit comments

Comments
 (0)