You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by GitBox <gi...@apache.org> on 2018/07/23 19:47:22 UTC

[GitHub] Antonio-Maranhao closed pull request #1101: String Editor fails to open for some kinds of keys

Antonio-Maranhao closed pull request #1101: String Editor fails to open for some kinds of keys
URL: https://github.com/apache/couchdb-fauxton/pull/1101
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/app/addons/components/__tests__/codeEditor.test.js b/app/addons/components/__tests__/codeEditor.test.js
index 84d770efa..a56acd467 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 2671e1f06..6fa4a0f54 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,


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services