Commit cbe05ec391f743f583ec887c17383bd3b3d036be

Michael Schmidt 2019-04-23T20:52:47

JSON: Kinda fixed comment issue (#1853) This is a partial workaround for the greedy matching bug so that pure JSON will always be matched correctly while JSON + comments might suffer from some issues.

diff --git a/components/prism-json.js b/components/prism-json.js
index 5f40179..2352c38 100644
--- a/components/prism-json.js
+++ b/components/prism-json.js
@@ -1,5 +1,4 @@
 Prism.languages.json = {
-	'comment': /\/\/.*|\/\*[\s\S]*?(?:\*\/|$)/,
 	'property': {
 		pattern: /"(?:\\.|[^\\"\r\n])*"(?=\s*:)/,
 		greedy: true
@@ -8,6 +7,7 @@ Prism.languages.json = {
 		pattern: /"(?:\\.|[^\\"\r\n])*"(?!\s*:)/,
 		greedy: true
 	},
+	'comment': /\/\/.*|\/\*[\s\S]*?(?:\*\/|$)/,
 	'number': /-?\d+\.?\d*(e[+-]?\d+)?/i,
 	'punctuation': /[{}[\],]/,
 	'operator': /:/,
diff --git a/components/prism-json.min.js b/components/prism-json.min.js
index 6ea539d..b67c29f 100644
--- a/components/prism-json.min.js
+++ b/components/prism-json.min.js
@@ -1 +1 @@
-Prism.languages.json={comment:/\/\/.*|\/\*[\s\S]*?(?:\*\/|$)/,property:{pattern:/"(?:\\.|[^\\"\r\n])*"(?=\s*:)/,greedy:!0},string:{pattern:/"(?:\\.|[^\\"\r\n])*"(?!\s*:)/,greedy:!0},number:/-?\d+\.?\d*(e[+-]?\d+)?/i,punctuation:/[{}[\],]/,operator:/:/,boolean:/\b(?:true|false)\b/,null:{pattern:/\bnull\b/,alias:"keyword"}};
\ No newline at end of file
+Prism.languages.json={property:{pattern:/"(?:\\.|[^\\"\r\n])*"(?=\s*:)/,greedy:!0},string:{pattern:/"(?:\\.|[^\\"\r\n])*"(?!\s*:)/,greedy:!0},comment:/\/\/.*|\/\*[\s\S]*?(?:\*\/|$)/,number:/-?\d+\.?\d*(e[+-]?\d+)?/i,punctuation:/[{}[\],]/,operator:/:/,boolean:/\b(?:true|false)\b/,null:{pattern:/\bnull\b/,alias:"keyword"}};
\ No newline at end of file
diff --git a/tests/languages/json/issue1852.test b/tests/languages/json/issue1852.test
new file mode 100644
index 0000000..5ed99c0
--- /dev/null
+++ b/tests/languages/json/issue1852.test
@@ -0,0 +1,27 @@
+{
+	"A": "/*",
+	"B": "B",
+	"C": "C"
+}
+
+----------------------------------------------------
+
+[
+	["punctuation", "{"],
+	["property", "\"A\""],
+	["operator", ":"],
+	["string", "\"/*\""],
+	["punctuation", ","],
+	["property", "\"B\""],
+	["operator", ":"],
+	["string", "\"B\""],
+	["punctuation", ","],
+	["property", "\"C\""],
+	["operator", ":"],
+	["string", "\"C\""],
+	["punctuation", "}"]
+]
+
+----------------------------------------------------
+
+Checks for issue #1852.