From c9ffeafb1359d868e3071894fdbde43e8f0000bd Mon Sep 17 00:00:00 2001 From: Helen Kosova Date: Thu, 27 Jul 2017 16:07:16 +0300 Subject: [PATCH] Fix isJSON() to recognize leading tabs and new line characters When a user pastes JSON into the editor, the editor prompts to convert it to YAML. This works if the JSON contains leading spaces: ``` {"foo": "bar"} ``` but does not currently work if the leading whitespace includes new line characters or tabs -- in this case JSON is pasted "as is". ``` {"foo": "bar"} ``` This PR improves the `isJSON()` function to recognize `\t`, `\r` and `\n` in the leading whitespace. Tabs and new lines are allowed in the leading whitespace according to [RFC 4627](https://tools.ietf.org/html/rfc4627) (section 2) and [ECMA-404](https://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf) (section 4): > Insignificant whitespace is allowed before or after any of the six structural characters. > > The whitespace characters are: character tabulation (U+0009), line feed (U+000A), carriage return (U+000D), and space (U+0020). --- src/plugins/editor/editor-plugins/json-to-yaml.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/editor/editor-plugins/json-to-yaml.js b/src/plugins/editor/editor-plugins/json-to-yaml.js index dd371a33c74..0219e235835 100644 --- a/src/plugins/editor/editor-plugins/json-to-yaml.js +++ b/src/plugins/editor/editor-plugins/json-to-yaml.js @@ -36,7 +36,7 @@ export default function(editor) { function isJSON (str){ // basic test: "does this look like JSON?" - let regex = /^ *[{\[]/ + let regex = /^[ \r\n\t]*[{\[]/ return regex.test(str)