Skip to content

Commit

Permalink
Desktop,Mobile: Plugins: Legacy editor API: Fix delayed crash caused …
Browse files Browse the repository at this point in the history
…by out-of-bounds inputs

See laurent22#11710
  • Loading branch information
personalizedrefrigerator committed Jan 23, 2025
1 parent 877123b commit 9b3c3cc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,20 @@ describe('CodeMirror5Emulation', () => {

expect(lastThis).toBe(codeMirror);
});

it('.markText should support specifying ranges outside the document', () => {
const codeMirror = makeCodeMirrorEmulation('Test...');

const testClassName = 'out-of-range-test-mark';
codeMirror.markText(
// In range
{ line: 0, ch: 4 },
// Out of range
{ line: 0, ch: 1002 },
{ className: testClassName },
);

const dom = codeMirror.editor.dom;
expect(dom.querySelectorAll(`.${testClassName}`)).toHaveLength(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ interface DocumentPositionRange {
to: DocumentPosition;
}

const clampPositionToDoc = (doc: Text, pos: number) => {
return Math.min(Math.max(0, pos), doc.length);
};

const documentPositionFromPos = (doc: Text, pos: number): DocumentPosition => {
const line = doc.lineAt(pos);
return {
Expand Down Expand Up @@ -420,8 +424,8 @@ export default class CodeMirror5Emulation extends BaseCodeMirror5Emulation {
const doc = this.editor.state.doc;

return this._decorator.markText(
posFromDocumentPosition(doc, from),
posFromDocumentPosition(doc, to),
clampPositionToDoc(doc, posFromDocumentPosition(doc, from)),
clampPositionToDoc(doc, posFromDocumentPosition(doc, to)),
options,
);
}
Expand Down

0 comments on commit 9b3c3cc

Please sign in to comment.