String Editor fails to open for some kinds of keys (#1101)
* Fix regex to find value when opening string editor
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('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 @@
};
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 @@
};
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,