mirror of
https://github.com/Kong/insomnia.git
synced 2026-04-23 07:38:58 -04:00
Prevent GraphQL non breaking space (#2770)
Co-authored-by: Timothy Lim <tim.lim@intercom.io> Co-authored-by: gatzjames <jamesgatzos@gmail.com> Co-authored-by: Dimitri Mitropoulos <dimitrimitropoulos@gmail.com>
This commit is contained in:
@@ -28,6 +28,7 @@ import DropdownButton from '../base/dropdown/dropdown-button';
|
||||
import DropdownItem from '../base/dropdown/dropdown-item';
|
||||
import FilterHelpModal from '../modals/filter-help-modal';
|
||||
import { showModal } from '../modals/index';
|
||||
import { normalizeIrregularWhitespace } from './normalizeIrregularWhitespace';
|
||||
|
||||
const TAB_KEY = 9;
|
||||
const TAB_SIZE = 4;
|
||||
@@ -990,7 +991,7 @@ class CodeEditor extends Component<Props, State> {
|
||||
}
|
||||
}
|
||||
|
||||
_codemirrorValueBeforeChange(doc, change) {
|
||||
_codemirrorValueBeforeChange(doc: CodeMirror.Editor, change: CodeMirror.EditorChangeCancellable) {
|
||||
const value = this.codeMirror?.getDoc().getValue();
|
||||
|
||||
// If we're in single-line mode, merge all changed lines into one
|
||||
@@ -999,7 +1000,14 @@ class CodeEditor extends Component<Props, State> {
|
||||
.join('') // join all changed lines into one
|
||||
.replace(/\n/g, ' ');
|
||||
// Convert all whitespace to spaces
|
||||
change.update(change.from, change.to, [text]);
|
||||
change.update?.(change.from, change.to, [text]);
|
||||
}
|
||||
|
||||
// Don't allow non-breaking spaces because they break the GraphQL syntax
|
||||
if (doc.getOption('mode') === 'graphql') {
|
||||
const text = change.text.map(normalizeIrregularWhitespace);
|
||||
|
||||
change.update?.(change.from, change.to, text);
|
||||
}
|
||||
|
||||
// Suppress lint on empty doc or single space exists (default value)
|
||||
@@ -1007,11 +1015,6 @@ class CodeEditor extends Component<Props, State> {
|
||||
this._codemirrorSmartSetOption('lint', false);
|
||||
} else {
|
||||
this._codemirrorSmartSetOption('lint', this.props.lintOptions || true);
|
||||
|
||||
// Don't allow non-breaking spaces because they break the GraphQL syntax
|
||||
if (doc.options.mode === 'graphql' && change.text && change.text.length > 1) {
|
||||
change.text = change.text.map(text => text.replace(/\u00A0/g, ' '));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
// List of irregular whitespace characters adopted from https://eslint.org/docs/rules/no-irregular-whitespace#rule-details
|
||||
const irregularWhitespaceCharacters = [
|
||||
'\u000B',
|
||||
'\u000C',
|
||||
'\u00A0',
|
||||
'\u0085',
|
||||
'\u1680',
|
||||
'\u180E',
|
||||
'\ufeff',
|
||||
'\u2000',
|
||||
'\u2001',
|
||||
'\u2002',
|
||||
'\u2003',
|
||||
'\u2004',
|
||||
'\u2005',
|
||||
'\u2006',
|
||||
'\u2007',
|
||||
'\u2008',
|
||||
'\u2009',
|
||||
'\u200A',
|
||||
'\u200B',
|
||||
'\u2028',
|
||||
'\u2029',
|
||||
'\u202F',
|
||||
'\u205f',
|
||||
'\u3000',
|
||||
];
|
||||
|
||||
const irregularWhitespaceCharactersRegex = new RegExp(`[${irregularWhitespaceCharacters.join('')}]`);
|
||||
|
||||
export function normalizeIrregularWhitespace(text: string) {
|
||||
return text.replace(irregularWhitespaceCharactersRegex, ' ');
|
||||
}
|
||||
Reference in New Issue
Block a user