From 25a541d6ddae4b5518a61ed72bccaef1efa5a29c Mon Sep 17 00:00:00 2001 From: Golmote Date: Wed, 28 Sep 2016 21:16:33 +0200 Subject: [PATCH] JSON: Fixed issues with properties and strings + added tests. Fix #1025 --- components/prism-json.js | 8 +++--- components/prism-json.min.js | 2 +- tests/languages/json/boolean_feature.test | 13 +++++++++ tests/languages/json/null_feature.test | 11 ++++++++ tests/languages/json/number_feature.test | 21 ++++++++++++++ tests/languages/json/property_feature.test | 33 ++++++++++++++++++++++ tests/languages/json/string_feature.test | 27 ++++++++++++++++++ 7 files changed, 110 insertions(+), 5 deletions(-) create mode 100644 tests/languages/json/boolean_feature.test create mode 100644 tests/languages/json/null_feature.test create mode 100644 tests/languages/json/number_feature.test create mode 100644 tests/languages/json/property_feature.test create mode 100644 tests/languages/json/string_feature.test diff --git a/components/prism-json.js b/components/prism-json.js index c7634b18ec..a7f54f6f99 100644 --- a/components/prism-json.js +++ b/components/prism-json.js @@ -1,11 +1,11 @@ Prism.languages.json = { - 'property': /".*?"(?=\s*:)/ig, - 'string': /"(?!:)(\\?[^"])*?"(?!:)/g, - 'number': /\b-?(0x[\dA-Fa-f]+|\d*\.?\d+([Ee]-?\d+)?)\b/g, + 'property': /"(?:\\.|[^|"])*"(?=\s*:)/ig, + 'string': /"(?!:)(?:\\.|[^|"])*"(?!:)/g, + 'number': /\b-?(0x[\dA-Fa-f]+|\d*\.?\d+([Ee][+-]?\d+)?)\b/g, 'punctuation': /[{}[\]);,]/g, 'operator': /:/g, 'boolean': /\b(true|false)\b/gi, - 'null': /\bnull\b/gi, + 'null': /\bnull\b/gi }; Prism.languages.jsonp = Prism.languages.json; diff --git a/components/prism-json.min.js b/components/prism-json.min.js index adf940323e..a437a3e370 100644 --- a/components/prism-json.min.js +++ b/components/prism-json.min.js @@ -1 +1 @@ -Prism.languages.json={property:/".*?"(?=\s*:)/gi,string:/"(?!:)(\\?[^"])*?"(?!:)/g,number:/\b-?(0x[\dA-Fa-f]+|\d*\.?\d+([Ee]-?\d+)?)\b/g,punctuation:/[{}[\]);,]/g,operator:/:/g,"boolean":/\b(true|false)\b/gi,"null":/\bnull\b/gi},Prism.languages.jsonp=Prism.languages.json; \ No newline at end of file +Prism.languages.json={property:/"(?:\\.|[^|"])*"(?=\s*:)/gi,string:/"(?!:)(?:\\.|[^|"])*"(?!:)/g,number:/\b-?(0x[\dA-Fa-f]+|\d*\.?\d+([Ee][+-]?\d+)?)\b/g,punctuation:/[{}[\]);,]/g,operator:/:/g,"boolean":/\b(true|false)\b/gi,"null":/\bnull\b/gi},Prism.languages.jsonp=Prism.languages.json; \ No newline at end of file diff --git a/tests/languages/json/boolean_feature.test b/tests/languages/json/boolean_feature.test new file mode 100644 index 0000000000..4019c444f8 --- /dev/null +++ b/tests/languages/json/boolean_feature.test @@ -0,0 +1,13 @@ +true +false + +---------------------------------------------------- + +[ + ["boolean", "true"], + ["boolean", "false"] +] + +---------------------------------------------------- + +Checks for booleans. \ No newline at end of file diff --git a/tests/languages/json/null_feature.test b/tests/languages/json/null_feature.test new file mode 100644 index 0000000000..1283944abe --- /dev/null +++ b/tests/languages/json/null_feature.test @@ -0,0 +1,11 @@ +null + +---------------------------------------------------- + +[ + ["null", "null"] +] + +---------------------------------------------------- + +Checks for null. \ No newline at end of file diff --git a/tests/languages/json/number_feature.test b/tests/languages/json/number_feature.test new file mode 100644 index 0000000000..ba045ec3d4 --- /dev/null +++ b/tests/languages/json/number_feature.test @@ -0,0 +1,21 @@ +0 +123 +3.14159 +5.0e8 +0.2E+2 +47e-5 + +---------------------------------------------------- + +[ + ["number", "0"], + ["number", "123"], + ["number", "3.14159"], + ["number", "5.0e8"], + ["number", "0.2E+2"], + ["number", "47e-5"] +] + +---------------------------------------------------- + +Checks for numbers. \ No newline at end of file diff --git a/tests/languages/json/property_feature.test b/tests/languages/json/property_feature.test new file mode 100644 index 0000000000..3880ad733d --- /dev/null +++ b/tests/languages/json/property_feature.test @@ -0,0 +1,33 @@ +{"foo\"bar\"baz":1,"foo":2} +{ + "foo": 1, + "b\"ar": 2 +} + +---------------------------------------------------- + +[ + ["punctuation", "{"], + ["property", "\"foo\\\"bar\\\"baz\""], + ["operator", ":"], + ["number", "1"], + ["punctuation", ","], + ["property", "\"foo\""], + ["operator", ":"], + ["number", "2"], + ["punctuation", "}"], + + ["punctuation", "{"], + ["property", "\"foo\""], + ["operator", ":"], + ["number", "1"], + ["punctuation", ","], + ["property", "\"b\\\"ar\""], + ["operator", ":"], + ["number", "2"], + ["punctuation", "}"] +] + +---------------------------------------------------- + +Checks for features. \ No newline at end of file diff --git a/tests/languages/json/string_feature.test b/tests/languages/json/string_feature.test new file mode 100644 index 0000000000..08ac8c06f7 --- /dev/null +++ b/tests/languages/json/string_feature.test @@ -0,0 +1,27 @@ +"" +"foo" +"foo\"bar\"baz" +"\u2642\\ " +{"foo":"bar","baz":"\""} + +---------------------------------------------------- + +[ + ["string", "\"\""], + ["string", "\"foo\""], + ["string", "\"foo\\\"bar\\\"baz\""], + ["string", "\"\\u2642\\\\ \""], + ["punctuation", "{"], + ["property", "\"foo\""], + ["operator", ":"], + ["string", "\"bar\""], + ["punctuation", ","], + ["property", "\"baz\""], + ["operator", ":"], + ["string", "\"\\\"\""], + ["punctuation", "}"] +] + +---------------------------------------------------- + +Checks for strings. \ No newline at end of file