From 6124c97409e1f15f35f3aa5dcc4e6c817d9ddc43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilo=20Pr=C3=BCtz?= Date: Wed, 19 Feb 2020 20:03:29 +0100 Subject: [PATCH] YAML: Fixed highlighting of anchors and aliases (#2217) --- components/prism-yaml.js | 16 +- components/prism-yaml.min.js | 2 +- .../yaml/anchor_and_alias_feature.test | 174 ++++++++++++++++++ 3 files changed, 183 insertions(+), 9 deletions(-) create mode 100644 tests/languages/yaml/anchor_and_alias_feature.test diff --git a/components/prism-yaml.js b/components/prism-yaml.js index abe2db9fc9..89a582297e 100644 --- a/components/prism-yaml.js +++ b/components/prism-yaml.js @@ -1,6 +1,6 @@ Prism.languages.yaml = { 'scalar': { - pattern: /([\-:]\s*(?:![^\s]+)?[ \t]*[|>])[ \t]*(?:((?:\r?\n|\r)[ \t]+)[^\r\n]+(?:\2[^\r\n]+)*)/, + pattern: /([\-:]\s*(?:\s[!&*][^\s]+)*[ \t]*[|>])[ \t]*(?:((?:\r?\n|\r)[ \t]+)[^\r\n]+(?:\2[^\r\n]+)*)/, lookbehind: true, alias: 'string' }, @@ -16,32 +16,32 @@ Prism.languages.yaml = { alias: 'important' }, 'datetime': { - pattern: /([:\-,[{]\s*(?:![^\s]+)?[ \t]*)(?:\d{4}-\d\d?-\d\d?(?:[tT]|[ \t]+)\d\d?:\d{2}:\d{2}(?:\.\d*)?[ \t]*(?:Z|[-+]\d\d?(?::\d{2})?)?|\d{4}-\d{2}-\d{2}|\d\d?:\d{2}(?::\d{2}(?:\.\d*)?)?)(?=[ \t]*(?:$|,|]|}))/m, + pattern: /([:\-,[{]\s*(?:\s[!&*][^\s]+)*[ \t]*)(?:\d{4}-\d\d?-\d\d?(?:[tT]|[ \t]+)\d\d?:\d{2}:\d{2}(?:\.\d*)?[ \t]*(?:Z|[-+]\d\d?(?::\d{2})?)?|\d{4}-\d{2}-\d{2}|\d\d?:\d{2}(?::\d{2}(?:\.\d*)?)?)(?=[ \t]*(?:$|,|]|}))/m, lookbehind: true, alias: 'number' }, 'boolean': { - pattern: /([:\-,[{]\s*(?:![^\s]+)?[ \t]*)(?:true|false)[ \t]*(?=$|,|]|})/im, + pattern: /([:\-,[{]\s*(?:\s[!&*][^\s]+)*[ \t]*)(?:true|false)[ \t]*(?=$|,|]|})/im, lookbehind: true, alias: 'important' }, 'null': { - pattern: /([:\-,[{]\s*(?:![^\s]+)?[ \t]*)(?:null|~)[ \t]*(?=$|,|]|})/im, + pattern: /([:\-,[{]\s*(?:\s[!&*][^\s]+)*[ \t]*)(?:null|~)[ \t]*(?=$|,|]|})/im, lookbehind: true, alias: 'important' }, 'string': { - pattern: /([:\-,[{]\s*(?:![^\s]+)?[ \t]*)("|')(?:(?!\2)[^\\\r\n]|\\.)*\2(?=[ \t]*(?:$|,|]|}|\s*#))/m, + pattern: /([:\-,[{]\s*(?:\s[!&*][^\s]+)*[ \t]*)("|')(?:(?!\2)[^\\\r\n]|\\.)*\2(?=[ \t]*(?:$|,|]|}|\s*#))/m, lookbehind: true, greedy: true }, 'number': { - pattern: /([:\-,[{]\s*(?:![^\s]+)?[ \t]*)[+-]?(?:0x[\da-f]+|0o[0-7]+|(?:\d+\.?\d*|\.?\d+)(?:e[+-]?\d+)?|\.inf|\.nan)[ \t]*(?=$|,|]|})/im, + pattern: /([:\-,[{]\s*(?:\s[!&*][^\s]+)*[ \t]*)[+-]?(?:0x[\da-f]+|0o[0-7]+|(?:\d+\.?\d*|\.?\d+)(?:e[+-]?\d+)?|\.inf|\.nan)[ \t]*(?=$|,|]|})/im, lookbehind: true }, 'tag': /![^\s]+/, - 'important': /[&*][\w]+/, + 'important': /[&*][^\s\[\]{},]+/, 'punctuation': /---|[:[\]{}\-,|>?]|\.\.\./ }; -Prism.languages.yml = Prism.languages.yaml; \ No newline at end of file +Prism.languages.yml = Prism.languages.yaml; diff --git a/components/prism-yaml.min.js b/components/prism-yaml.min.js index 1a6fb682ff..c831047ee3 100644 --- a/components/prism-yaml.min.js +++ b/components/prism-yaml.min.js @@ -1 +1 @@ -Prism.languages.yaml={scalar:{pattern:/([\-:]\s*(?:![^\s]+)?[ \t]*[|>])[ \t]*(?:((?:\r?\n|\r)[ \t]+)[^\r\n]+(?:\2[^\r\n]+)*)/,lookbehind:!0,alias:"string"},comment:/#.*/,key:{pattern:/(\s*(?:^|[:\-,[{\r\n?])[ \t]*(?:![^\s]+)?[ \t]*)[^\r\n{[\]},#\s]+?(?=\s*:\s)/,lookbehind:!0,alias:"atrule"},directive:{pattern:/(^[ \t]*)%.+/m,lookbehind:!0,alias:"important"},datetime:{pattern:/([:\-,[{]\s*(?:![^\s]+)?[ \t]*)(?:\d{4}-\d\d?-\d\d?(?:[tT]|[ \t]+)\d\d?:\d{2}:\d{2}(?:\.\d*)?[ \t]*(?:Z|[-+]\d\d?(?::\d{2})?)?|\d{4}-\d{2}-\d{2}|\d\d?:\d{2}(?::\d{2}(?:\.\d*)?)?)(?=[ \t]*(?:$|,|]|}))/m,lookbehind:!0,alias:"number"},boolean:{pattern:/([:\-,[{]\s*(?:![^\s]+)?[ \t]*)(?:true|false)[ \t]*(?=$|,|]|})/im,lookbehind:!0,alias:"important"},null:{pattern:/([:\-,[{]\s*(?:![^\s]+)?[ \t]*)(?:null|~)[ \t]*(?=$|,|]|})/im,lookbehind:!0,alias:"important"},string:{pattern:/([:\-,[{]\s*(?:![^\s]+)?[ \t]*)("|')(?:(?!\2)[^\\\r\n]|\\.)*\2(?=[ \t]*(?:$|,|]|}|\s*#))/m,lookbehind:!0,greedy:!0},number:{pattern:/([:\-,[{]\s*(?:![^\s]+)?[ \t]*)[+-]?(?:0x[\da-f]+|0o[0-7]+|(?:\d+\.?\d*|\.?\d+)(?:e[+-]?\d+)?|\.inf|\.nan)[ \t]*(?=$|,|]|})/im,lookbehind:!0},tag:/![^\s]+/,important:/[&*][\w]+/,punctuation:/---|[:[\]{}\-,|>?]|\.\.\./},Prism.languages.yml=Prism.languages.yaml; \ No newline at end of file +Prism.languages.yaml={scalar:{pattern:/([\-:]\s*(?:\s[!&*][^\s]+)*[ \t]*[|>])[ \t]*(?:((?:\r?\n|\r)[ \t]+)[^\r\n]+(?:\2[^\r\n]+)*)/,lookbehind:!0,alias:"string"},comment:/#.*/,key:{pattern:/(\s*(?:^|[:\-,[{\r\n?])[ \t]*(?:![^\s]+)?[ \t]*)[^\r\n{[\]},#\s]+?(?=\s*:\s)/,lookbehind:!0,alias:"atrule"},directive:{pattern:/(^[ \t]*)%.+/m,lookbehind:!0,alias:"important"},datetime:{pattern:/([:\-,[{]\s*(?:\s[!&*][^\s]+)*[ \t]*)(?:\d{4}-\d\d?-\d\d?(?:[tT]|[ \t]+)\d\d?:\d{2}:\d{2}(?:\.\d*)?[ \t]*(?:Z|[-+]\d\d?(?::\d{2})?)?|\d{4}-\d{2}-\d{2}|\d\d?:\d{2}(?::\d{2}(?:\.\d*)?)?)(?=[ \t]*(?:$|,|]|}))/m,lookbehind:!0,alias:"number"},boolean:{pattern:/([:\-,[{]\s*(?:\s[!&*][^\s]+)*[ \t]*)(?:true|false)[ \t]*(?=$|,|]|})/im,lookbehind:!0,alias:"important"},null:{pattern:/([:\-,[{]\s*(?:\s[!&*][^\s]+)*[ \t]*)(?:null|~)[ \t]*(?=$|,|]|})/im,lookbehind:!0,alias:"important"},string:{pattern:/([:\-,[{]\s*(?:\s[!&*][^\s]+)*[ \t]*)("|')(?:(?!\2)[^\\\r\n]|\\.)*\2(?=[ \t]*(?:$|,|]|}|\s*#))/m,lookbehind:!0,greedy:!0},number:{pattern:/([:\-,[{]\s*(?:\s[!&*][^\s]+)*[ \t]*)[+-]?(?:0x[\da-f]+|0o[0-7]+|(?:\d+\.?\d*|\.?\d+)(?:e[+-]?\d+)?|\.inf|\.nan)[ \t]*(?=$|,|]|})/im,lookbehind:!0},tag:/![^\s]+/,important:/[&*][^\s\[\]{},]+/,punctuation:/---|[:[\]{}\-,|>?]|\.\.\./},Prism.languages.yml=Prism.languages.yaml; \ No newline at end of file diff --git a/tests/languages/yaml/anchor_and_alias_feature.test b/tests/languages/yaml/anchor_and_alias_feature.test new file mode 100644 index 0000000000..06db5862b1 --- /dev/null +++ b/tests/languages/yaml/anchor_and_alias_feature.test @@ -0,0 +1,174 @@ +x-number: &num-ber 13 +x-string: &stri_ng "good" +x-null: &nu-ll null +x-boolean: &boo-lean true +x-datetime: &date-time 2001-12-15T02:59:43.1Z +x-scalar: &sca-lar | + foo + bar + +x-utf8: &アンカー "japanese anchor" + +x-tag-string: &tag-string !!tag-string "13" +x-tag-number: &tag-number !!tag-number 42 +x-tag-null: &tag-null !!tag-null null +x-tag-boolean: &tag-bool !!tag-bool true +x-tag-datetime: &tag-date !!tag-date 2001-12-15T02:59:43.1Z +x-tag-scalar: &tag-scalar !!tag-scalar | + foo + bar + +x-tag-string: !!tag-string &tag-string "13" +x-tag-number: !!tag-number &tag-number 42 +x-tag-null: !!tag-null &tag-null null +x-tag-boolean: !!tag-bool &tag-bool true +x-tag-datetime: !!tag-date &tag-date 2001-12-15T02:59:43.1Z +x-tag-scalar: !!tag-scalar &tag-scalar | + foo + bar + +foobar: *num-ber +fubar: *stri_ng +japanese: *アンカー +taga: *tag-a !!taga +tagb: !!tagb *tag-b + +---------------------------------------------------- + +[ + ["key", "x-number"], + ["punctuation", ":"], + ["important", "&num-ber"], + ["number", "13"], + + ["key", "x-string"], + ["punctuation", ":"], + ["important", "&stri_ng"], + ["string", "\"good\""], + + ["key", "x-null"], + ["punctuation", ":"], + ["important", "&nu-ll"], + ["null", "null"], + + ["key", "x-boolean"], + ["punctuation", ":"], + ["important", "&boo-lean"], + ["boolean", "true"], + + ["key", "x-datetime"], + ["punctuation", ":"], + ["important", "&date-time"], + ["datetime", "2001-12-15T02:59:43.1Z"], + + ["key", "x-scalar"], + ["punctuation", ":"], + ["important", "&sca-lar"], + ["punctuation", "|"], + ["scalar", "\n foo\n bar"], + + ["key", "x-utf8"], + ["punctuation", ":"], + ["important", "&アンカー"], + ["string", "\"japanese anchor\""], + + ["key", "x-tag-string"], + ["punctuation", ":"], + ["important", "&tag-string"], + ["tag", "!!tag-string"], + ["string", "\"13\""], + + ["key", "x-tag-number"], + ["punctuation", ":"], + ["important", "&tag-number"], + ["tag", "!!tag-number"], + ["number", "42"], + + ["key", "x-tag-null"], + ["punctuation", ":"], + ["important", "&tag-null"], + ["tag", "!!tag-null"], + ["null", "null"], + + ["key", "x-tag-boolean"], + ["punctuation", ":"], + ["important", "&tag-bool"], + ["tag", "!!tag-bool"], + ["boolean", "true"], + + ["key", "x-tag-datetime"], + ["punctuation", ":"], + ["important", "&tag-date"], + ["tag", "!!tag-date"], + ["datetime", "2001-12-15T02:59:43.1Z"], + + ["key", "x-tag-scalar"], + ["punctuation", ":"], + ["important", "&tag-scalar"], + ["tag", "!!tag-scalar"], + ["punctuation", "|"], + ["scalar", "\n foo\n bar"], + + ["key", "x-tag-string"], + ["punctuation", ":"], + ["tag", "!!tag-string"], + ["important", "&tag-string"], + ["string", "\"13\""], + + ["key", "x-tag-number"], + ["punctuation", ":"], + ["tag", "!!tag-number"], + ["important", "&tag-number"], + ["number", "42"], + + ["key", "x-tag-null"], + ["punctuation", ":"], + ["tag", "!!tag-null"], + ["important", "&tag-null"], + ["null", "null"], + + ["key", "x-tag-boolean"], + ["punctuation", ":"], + ["tag", "!!tag-bool"], + ["important", "&tag-bool"], + ["boolean", "true"], + + ["key", "x-tag-datetime"], + ["punctuation", ":"], + ["tag", "!!tag-date"], + ["important", "&tag-date"], + ["datetime", "2001-12-15T02:59:43.1Z"], + + ["key", "x-tag-scalar"], + ["punctuation", ":"], + ["tag", "!!tag-scalar"], + ["important", "&tag-scalar"], + ["punctuation", "|"], + ["scalar", "\n foo\n bar"], + + ["key", "foobar"], + ["punctuation", ":"], + ["important", "*num-ber"], + + ["key", "fubar"], + ["punctuation", ":"], + ["important", "*stri_ng"], + + ["key", "japanese"], + ["punctuation", ":"], + ["important", "*アンカー"], + + ["key", "taga"], + ["punctuation", ":"], + ["important", "*tag-a"], + ["tag", "!!taga"], + + ["key", "tagb"], + ["punctuation", ":"], + ["tag", "!!tagb"], + ["important", "*tag-b"] +] + +---------------------------------------------------- + +Checks for anchors and aliases.