Просмотр исходного кода

Merge pull request #1911 from overleaf/bg-block-non-bmp-characters

block null and non-BMP characters in Ace

GitOrigin-RevId: cdeaffc88e128a62463229d5cb55143028f23112
Brian Gough 7 лет назад
Родитель
Сommit
30d8247d54
1 измененных файлов с 32 добавлено и 0 удалено
  1. 32 0
      services/web/public/src/ide/editor/directives/aceEditor.js

+ 32 - 0
services/web/public/src/ide/editor/directives/aceEditor.js

@@ -198,6 +198,38 @@ define([
           files
         )
 
+        // prevent user entering null and non-BMP unicode characters in Ace
+        const BAD_CHARS_REGEXP = /[\0\uD800-\uDFFF]/g
+        const BAD_CHARS_REPLACEMENT_CHAR = '\uFFFD'
+        // the 'exec' event fires for ace functions before they are executed.
+        // you can modify the input or reject the event with e.preventDefault()
+        editor.commands.on('exec', function(e) {
+          // replace bad characters in paste content
+          if (
+            e.command &&
+            e.command.name === 'paste' &&
+            e.args &&
+            BAD_CHARS_REGEXP.test(e.args.text)
+          ) {
+            e.args.text = e.args.text.replace(
+              BAD_CHARS_REGEXP,
+              BAD_CHARS_REPLACEMENT_CHAR
+            )
+          }
+          // replace bad characters in keyboard input
+          if (
+            e.command &&
+            e.command.name === 'insertstring' &&
+            e.args &&
+            BAD_CHARS_REGEXP.test(e.args)
+          ) {
+            e.args = e.args.replace(
+              BAD_CHARS_REGEXP,
+              BAD_CHARS_REPLACEMENT_CHAR
+            )
+          }
+        })
+
         /* eslint-enable no-unused-vars */
 
         scope.$watch('onSave', function(callback) {