ソースを参照

Merge pull request #6536 from overleaf/bg-cm6-size-check

Add editor max doc length checks to codemirror

GitOrigin-RevId: 0a458fc3aa23f22b078c6607ee4347bc76c371e9
Timothée Alby 4 年 前
親
コミット
74d9937816

+ 2 - 2
services/web/frontend/js/ide/editor/ShareJsDoc.js

@@ -368,7 +368,7 @@ export default ShareJsDoc = (function () {
 
 
     attachToCM(cm) {
     attachToCM(cm) {
       this._attachToEditor('CM', 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) {
     attachToCM6(cm6) {
       this._attachToEditor('CM6', cm6, () => {
       this._attachToEditor('CM6', cm6, () => {
-        cm6.attachShareJs(this._doc)
+        cm6.attachShareJs(this._doc, false, window.maxDocLength)
       })
       })
     }
     }
 
 

+ 6 - 2
services/web/frontend/js/vendor/libs/sharejs.js

@@ -1506,7 +1506,7 @@ define(['ace/ace','crypto-js/sha1'], function (_ignore, CryptoJSSHA1) {
         return;
         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'));
         doc.emit('error', new Error('document length is greater than maxDocLength'));
         return;
         return;
       }
       }
@@ -1629,7 +1629,7 @@ define(['ace/ace','crypto-js/sha1'], function (_ignore, CryptoJSSHA1) {
   //        a custom `origin: 'remote'` which may conflict.
   //        a custom `origin: 'remote'` which may conflict.
   //       Perma link of the docs at the time of writing this note:
   //       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
   // 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) {
     if (!this.provides.text) {
       throw new Error('Only text documents can be attached to CodeMirror2');
       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
         // this change has been injected via sharejs
         return;
         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')
       var fromUndo = (change.origin === 'undo')
       applyCMToShareJS(editorDoc, change, sharedoc, fromUndo);
       applyCMToShareJS(editorDoc, change, sharedoc, fromUndo);
       return check();
       return check();