From 74d9937816bec5d8ab1371db7c10f39796cf0854 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Alby?= Date: Wed, 2 Feb 2022 11:25:06 +0100 Subject: [PATCH] Merge pull request #6536 from overleaf/bg-cm6-size-check Add editor max doc length checks to codemirror GitOrigin-RevId: 0a458fc3aa23f22b078c6607ee4347bc76c371e9 --- services/web/frontend/js/ide/editor/ShareJsDoc.js | 4 ++-- services/web/frontend/js/vendor/libs/sharejs.js | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/services/web/frontend/js/ide/editor/ShareJsDoc.js b/services/web/frontend/js/ide/editor/ShareJsDoc.js index 0842dd11c2..ef2397ef9b 100644 --- a/services/web/frontend/js/ide/editor/ShareJsDoc.js +++ b/services/web/frontend/js/ide/editor/ShareJsDoc.js @@ -368,7 +368,7 @@ export default ShareJsDoc = (function () { attachToCM(cm) { this._attachToEditor('CM', cm, () => { - this._doc.attach_cm(cm, false) + this._doc.attach_cm(cm, false, window.maxDocLength) }) } @@ -381,7 +381,7 @@ export default ShareJsDoc = (function () { attachToCM6(cm6) { this._attachToEditor('CM6', cm6, () => { - cm6.attachShareJs(this._doc) + cm6.attachShareJs(this._doc, false, window.maxDocLength) }) } diff --git a/services/web/frontend/js/vendor/libs/sharejs.js b/services/web/frontend/js/vendor/libs/sharejs.js index 682ccdef75..7af70303fd 100644 --- a/services/web/frontend/js/vendor/libs/sharejs.js +++ b/services/web/frontend/js/vendor/libs/sharejs.js @@ -1506,7 +1506,7 @@ define(['ace/ace','crypto-js/sha1'], function (_ignore, CryptoJSSHA1) { return; } - if (maxDocLength != null && editorDoc.getValue().length > maxDocLength) { + if (maxDocLength != null && editorDoc.getValue().length >= maxDocLength) { doc.emit('error', new Error('document length is greater than maxDocLength')); return; } @@ -1629,7 +1629,7 @@ define(['ace/ace','crypto-js/sha1'], function (_ignore, CryptoJSSHA1) { // a custom `origin: 'remote'` which may conflict. // Perma link of the docs at the time of writing this note: // https://web.archive.org/web/20201029163528/https://codemirror.net/doc/manual.html#selection_origin - window.sharejs.extendDoc('attach_cm', function (editor, keepEditorContents) { + window.sharejs.extendDoc('attach_cm', function (editor, keepEditorContents, maxDocLength) { if (!this.provides.text) { throw new Error('Only text documents can be attached to CodeMirror2'); } @@ -1666,6 +1666,10 @@ define(['ace/ace','crypto-js/sha1'], function (_ignore, CryptoJSSHA1) { // this change has been injected via sharejs return; } + if (maxDocLength != null && editorDoc.getValue().length >= maxDocLength) { + sharedoc.emit('error', new Error('document length is greater than maxDocLength')); + return; + } var fromUndo = (change.origin === 'undo') applyCMToShareJS(editorDoc, change, sharedoc, fromUndo); return check();