Browse Source

escape son string to avoid parse exceptions for special characters in text

Artal Druk 8 years ago
parent
commit
f152ae1dc4
1 changed files with 15 additions and 1 deletions
  1. 15
    1
      src/RichTextEditor.js

+ 15
- 1
src/RichTextEditor.js View File

@@ -93,8 +93,22 @@ export default class RichTextEditor extends Component {
93 93
     );
94 94
   }
95 95
 
96
+  escapeJSONString = function(string) {
97
+    return string
98
+      .replace(/[\\]/g, '\\\\')
99
+      .replace(/[\"]/g, '\\\"')
100
+      .replace(/[\/]/g, '\\/')
101
+      .replace(/[\b]/g, '\\b')
102
+      .replace(/[\f]/g, '\\f')
103
+      .replace(/[\n]/g, '\\n')
104
+      .replace(/[\r]/g, '\\r')
105
+      .replace(/[\t]/g, '\\t');
106
+  };
107
+
96 108
   _sendAction(action, data) {
97
-    this.webviewBridge.sendToBridge(JSON.stringify({type: action, data}));
109
+    let jsonString = JSON.stringify({type: action, data});
110
+    jsonString = this.escapeJSONString(jsonString);
111
+    this.webviewBridge.sendToBridge(jsonString);
98 112
   }
99 113
 
100 114
   //-------------------------------------------------------------------------------