couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amaran...@apache.org
Subject [couchdb-fauxton] branch master updated: String Editor fails to open for some kinds of keys (#1101)
Date Mon, 23 Jul 2018 19:47:22 GMT
This is an automated email from the ASF dual-hosted git repository.

amaranhao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-fauxton.git


The following commit(s) were added to refs/heads/master by this push:
     new f09563c  String Editor fails to open for some kinds of keys (#1101)
f09563c is described below

commit f09563c60d26bf92833dac11e8f9b0aaad982758
Author: Antonio Maranhao <30349380+Antonio-Maranhao@users.noreply.github.com>
AuthorDate: Mon Jul 23 15:47:20 2018 -0400

    String Editor fails to open for some kinds of keys (#1101)
    
    * Fix regex to find value when opening string editor
---
 app/addons/components/__tests__/codeEditor.test.js | 37 ++++++++++++++++++++++
 app/addons/components/components/codeeditor.js     | 20 ++++++------
 2 files changed, 47 insertions(+), 10 deletions(-)

diff --git a/app/addons/components/__tests__/codeEditor.test.js b/app/addons/components/__tests__/codeEditor.test.js
index 84d770e..a56acd4 100644
--- a/app/addons/components/__tests__/codeEditor.test.js
+++ b/app/addons/components/__tests__/codeEditor.test.js
@@ -97,4 +97,41 @@ describe('Code Editor', () => {
     });
   });
 
+  describe('parseLineForStringMatch', () => {
+    const initEditor = (code) => {
+      const editor = mount(
+        <ReactComponents.CodeEditor defaultCode={code} />
+      );
+      sinon.stub(editor.instance(), 'getSelectionStart').returns({row: 1});
+      sinon.stub(editor.instance(), 'getSelectionEnd').returns({row: 1});
+      sinon.stub(editor.instance(), 'isRowExpanded').returns(true);
+      return editor;
+    };
+
+    it('returns matches on pretty formatted code', () => {
+      const code = '{\n "field": "my string value" \n}';
+      codeEditorEl = initEditor(code);
+      const matches = codeEditorEl.instance().parseLineForStringMatch();
+      assert.equal('"my string value" ', matches[3]);
+    });
+    it('returns matches when line ends with comma', () => {
+      const code = '{\n "field": "my string value", \n "field2": 123 \n}';
+      codeEditorEl = initEditor(code);
+      const matches = codeEditorEl.instance().parseLineForStringMatch();
+      assert.equal('"my string value", ', matches[3]);
+    });
+    it('returns matches on code with extra spaces', () => {
+      const code = '{\n "field"  \t :  \t "my string value"  \t  ,  \t  \n "field2": 123
\n}';
+      codeEditorEl = initEditor(code);
+      const matches = codeEditorEl.instance().parseLineForStringMatch();
+      assert.equal('"my string value"  \t  ,  \t  ', matches[3]);
+    });
+    it('returns matches on code with special and non-ASCII chars', () => {
+      const code = '{\n "@langua漢字g e" : "my string value",\n "field2": 123 \n}';
+      codeEditorEl = initEditor(code);
+      const matches = codeEditorEl.instance().parseLineForStringMatch();
+      assert.equal('"my string value",', matches[3]);
+    });
+  });
+
 });
diff --git a/app/addons/components/components/codeeditor.js b/app/addons/components/components/codeeditor.js
index 2671e1f..6fa4a0f 100644
--- a/app/addons/components/components/codeeditor.js
+++ b/app/addons/components/components/codeeditor.js
@@ -246,14 +246,13 @@ export class CodeEditor extends React.Component {
   };
 
   parseLineForStringMatch = () => {
-    var selStart = this.getSelectionStart().row;
-    var selEnd   = this.getSelectionEnd().row;
+    const selStart = this.getSelectionStart().row;
+    const selEnd   = this.getSelectionEnd().row;
 
     // one JS(ON) string can't span more than one line - we edit one string, so ensure we
don't select several lines
     if (selStart >= 0 && selEnd >= 0 && selStart === selEnd &&
this.isRowExpanded(selStart)) {
-      var editLine = this.getLine(selStart),
-          editMatch = editLine.match(/^([ \t]*)("[a-zA-Z0-9_-]*["|']: )?(["|'].*",?[ \t]*)$/);
-
+      const editLine = this.getLine(selStart);
+      const editMatch = editLine.match(/^([ \t]*)("[^"]*["][ \t]*:[ \t]*)?(["|'].*"[ \t]*,?[
\t]*)$/);
       if (editMatch) {
         return editMatch;
       }
@@ -262,13 +261,14 @@ export class CodeEditor extends React.Component {
   };
 
   openStringEditModal = () => {
-    var matches = this.parseLineForStringMatch();
-    var string = matches[3];
-    var lastChar = string.length - 1;
+    const matches = this.parseLineForStringMatch();
+    let string = matches[3].trim();
+    // Removes trailing comma and surrouding spaces
     if (string.substring(string.length - 1) === ',') {
-      lastChar = string.length - 2;
+      string = string.substring(0, string.length - 1).trim();
     }
-    string = string.substring(1, lastChar);
+    // Removes surrouding quotes
+    string = string.substring(1, string.length - 1);
 
     this.setState({
       stringEditModalVisible: true,


Mime
View raw message