From 6d663b6e6e9ba86c9f4bad34732527952efb97f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=A3=E9=87=8C=E5=A5=BD=E8=84=8F=E4=B8=8D=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5?= <453491931@qq.com> Date: Fri, 8 May 2020 04:17:54 +0800 Subject: [PATCH] Stylus: Fixed comments breaking declarations + minor improvements (#2372) Comments broke URLs in all non-greedy declarations. Comments are now matched as part of these declarations. This also adds support for comments inside selectors. --- components/prism-stylus.js | 11 ++++++----- components/prism-stylus.min.js | 2 +- .../stylus/atrule-declaration_feature.test | 13 +++++++++++++ tests/languages/stylus/keyword_feature.test | 8 ++++++++ tests/languages/stylus/selector_feature.test | 7 ++++++- .../stylus/variable-declaration_feature.test | 7 +++++++ 6 files changed, 41 insertions(+), 7 deletions(-) diff --git a/components/prism-stylus.js b/components/prism-stylus.js index fb738429e1..18d014dbef 100644 --- a/components/prism-stylus.js +++ b/components/prism-stylus.js @@ -74,10 +74,6 @@ }; Prism.languages.stylus = { - 'comment': { - pattern: /(^|[^\\])(?:\/\*[\s\S]*?\*\/|\/\/.*)/, - lookbehind: true - }, 'atrule-declaration': { pattern: /(^\s*)@.+/m, lookbehind: true, @@ -109,7 +105,6 @@ 'property-declaration': { pattern: /((?:^|\{)([ \t]*))(?:[\w-]|\{[^}\r\n]+\})+(?:\s*:\s*|[ \t]+)[^{\r\n]*(?:;|[^{\r\n,](?=$)(?!(?:\r?\n|\r)(?:\{|\2[ \t]+)))/m, lookbehind: true, - greedy: true, inside: { 'property': { pattern: /^[^\s:]+/, @@ -131,12 +126,18 @@ lookbehind: true, inside: { 'interpolation': inside.interpolation, + 'comment': inside.comment, 'punctuation': /[{},]/ } }, 'func': inside.func, 'string': inside.string, + 'comment': { + pattern: /(^|[^\\])(?:\/\*[\s\S]*?\*\/|\/\/.*)/, + lookbehind: true, + greedy: true + }, 'interpolation': inside.interpolation, 'punctuation': /[{}()\[\];:.]/ }; diff --git a/components/prism-stylus.min.js b/components/prism-stylus.min.js index 2c63c58159..f70528cf87 100644 --- a/components/prism-stylus.min.js +++ b/components/prism-stylus.min.js @@ -1 +1 @@ -!function(e){var n={pattern:/(\d)(?:%|[a-z]+)/,lookbehind:!0},r={pattern:/(^|[^\w.-])-?\d*\.?\d+/,lookbehind:!0},i={comment:{pattern:/(^|[^\\])(?:\/\*[\s\S]*?\*\/|\/\/.*)/,lookbehind:!0},url:{pattern:/url\((["']?).*?\1\)/i,greedy:!0},string:{pattern:/("|')(?:(?!\1)[^\\\r\n]|\\(?:\r\n|[\s\S]))*\1/,greedy:!0},interpolation:null,func:null,important:/\B!(?:important|optional)\b/i,keyword:{pattern:/(^|\s+)(?:(?:if|else|for|return|unless)(?=\s+|$)|@[\w-]+)/,lookbehind:!0},hexcode:/#[\da-f]{3,6}/i,color:[/\b(?:AliceBlue|AntiqueWhite|Aqua|Aquamarine|Azure|Beige|Bisque|Black|BlanchedAlmond|Blue|BlueViolet|Brown|BurlyWood|CadetBlue|Chartreuse|Chocolate|Coral|CornflowerBlue|Cornsilk|Crimson|Cyan|DarkBlue|DarkCyan|DarkGoldenRod|DarkGr[ae]y|DarkGreen|DarkKhaki|DarkMagenta|DarkOliveGreen|DarkOrange|DarkOrchid|DarkRed|DarkSalmon|DarkSeaGreen|DarkSlateBlue|DarkSlateGr[ae]y|DarkTurquoise|DarkViolet|DeepPink|DeepSkyBlue|DimGr[ae]y|DodgerBlue|FireBrick|FloralWhite|ForestGreen|Fuchsia|Gainsboro|GhostWhite|Gold|GoldenRod|Gr[ae]y|Green|GreenYellow|HoneyDew|HotPink|IndianRed|Indigo|Ivory|Khaki|Lavender|LavenderBlush|LawnGreen|LemonChiffon|LightBlue|LightCoral|LightCyan|LightGoldenRodYellow|LightGr[ae]y|LightGreen|LightPink|LightSalmon|LightSeaGreen|LightSkyBlue|LightSlateGr[ae]y|LightSteelBlue|LightYellow|Lime|LimeGreen|Linen|Magenta|Maroon|MediumAquaMarine|MediumBlue|MediumOrchid|MediumPurple|MediumSeaGreen|MediumSlateBlue|MediumSpringGreen|MediumTurquoise|MediumVioletRed|MidnightBlue|MintCream|MistyRose|Moccasin|NavajoWhite|Navy|OldLace|Olive|OliveDrab|Orange|OrangeRed|Orchid|PaleGoldenRod|PaleGreen|PaleTurquoise|PaleVioletRed|PapayaWhip|PeachPuff|Peru|Pink|Plum|PowderBlue|Purple|Red|RosyBrown|RoyalBlue|SaddleBrown|Salmon|SandyBrown|SeaGreen|SeaShell|Sienna|Silver|SkyBlue|SlateBlue|SlateGr[ae]y|Snow|SpringGreen|SteelBlue|Tan|Teal|Thistle|Tomato|Transparent|Turquoise|Violet|Wheat|White|WhiteSmoke|Yellow|YellowGreen)\b/i,{pattern:/\b(?:rgb|hsl)\(\s*\d{1,3}\s*,\s*\d{1,3}%?\s*,\s*\d{1,3}%?\s*\)\B|\b(?:rgb|hsl)a\(\s*\d{1,3}\s*,\s*\d{1,3}%?\s*,\s*\d{1,3}%?\s*,\s*(?:0|0?\.\d+|1)\s*\)\B/i,inside:{unit:n,number:r,function:/[\w-]+(?=\()/,punctuation:/[(),]/}}],entity:/\\[\da-f]{1,8}/i,unit:n,boolean:/\b(?:true|false)\b/,operator:[/~|[+!\/%<>?=]=?|[-:]=|\*[*=]?|\.{2,3}|&&|\|\||\B-\B|\b(?:and|in|is(?: a| defined| not|nt)?|not|or)\b/],number:r,punctuation:/[{}()\[\];:,]/};i.interpolation={pattern:/\{[^\r\n}:]+\}/,alias:"variable",inside:{delimiter:{pattern:/^{|}$/,alias:"punctuation"},rest:i}},i.func={pattern:/[\w-]+\([^)]*\).*/,inside:{function:/^[^(]+/,rest:i}},e.languages.stylus={comment:{pattern:/(^|[^\\])(?:\/\*[\s\S]*?\*\/|\/\/.*)/,lookbehind:!0},"atrule-declaration":{pattern:/(^\s*)@.+/m,lookbehind:!0,inside:{atrule:/^@[\w-]+/,rest:i}},"variable-declaration":{pattern:/(^[ \t]*)[\w$-]+\s*.?=[ \t]*(?:(?:\{[^}]*\}|.+)|$)/m,lookbehind:!0,inside:{variable:/^\S+/,rest:i}},statement:{pattern:/(^[ \t]*)(?:if|else|for|return|unless)[ \t]+.+/m,lookbehind:!0,inside:{keyword:/^\S+/,rest:i}},"property-declaration":{pattern:/((?:^|\{)([ \t]*))(?:[\w-]|\{[^}\r\n]+\})+(?:\s*:\s*|[ \t]+)[^{\r\n]*(?:;|[^{\r\n,](?=$)(?!(?:\r?\n|\r)(?:\{|\2[ \t]+)))/m,lookbehind:!0,greedy:!0,inside:{property:{pattern:/^[^\s:]+/,inside:{interpolation:i.interpolation}},rest:i}},selector:{pattern:/(^[ \t]*)(?:(?=\S)(?:[^{}\r\n:()]|::?[\w-]+(?:\([^)\r\n]*\))?|\{[^}\r\n]+\})+)(?:(?:\r?\n|\r)(?:\1(?:(?=\S)(?:[^{}\r\n:()]|::?[\w-]+(?:\([^)\r\n]*\))?|\{[^}\r\n]+\})+)))*(?:,$|\{|(?=(?:\r?\n|\r)(?:\{|\1[ \t]+)))/m,lookbehind:!0,inside:{interpolation:i.interpolation,punctuation:/[{},]/}},func:i.func,string:i.string,interpolation:i.interpolation,punctuation:/[{}()\[\];:.]/}}(Prism); \ No newline at end of file +!function(e){var n={pattern:/(\d)(?:%|[a-z]+)/,lookbehind:!0},r={pattern:/(^|[^\w.-])-?\d*\.?\d+/,lookbehind:!0},i={comment:{pattern:/(^|[^\\])(?:\/\*[\s\S]*?\*\/|\/\/.*)/,lookbehind:!0},url:{pattern:/url\((["']?).*?\1\)/i,greedy:!0},string:{pattern:/("|')(?:(?!\1)[^\\\r\n]|\\(?:\r\n|[\s\S]))*\1/,greedy:!0},interpolation:null,func:null,important:/\B!(?:important|optional)\b/i,keyword:{pattern:/(^|\s+)(?:(?:if|else|for|return|unless)(?=\s+|$)|@[\w-]+)/,lookbehind:!0},hexcode:/#[\da-f]{3,6}/i,color:[/\b(?:AliceBlue|AntiqueWhite|Aqua|Aquamarine|Azure|Beige|Bisque|Black|BlanchedAlmond|Blue|BlueViolet|Brown|BurlyWood|CadetBlue|Chartreuse|Chocolate|Coral|CornflowerBlue|Cornsilk|Crimson|Cyan|DarkBlue|DarkCyan|DarkGoldenRod|DarkGr[ae]y|DarkGreen|DarkKhaki|DarkMagenta|DarkOliveGreen|DarkOrange|DarkOrchid|DarkRed|DarkSalmon|DarkSeaGreen|DarkSlateBlue|DarkSlateGr[ae]y|DarkTurquoise|DarkViolet|DeepPink|DeepSkyBlue|DimGr[ae]y|DodgerBlue|FireBrick|FloralWhite|ForestGreen|Fuchsia|Gainsboro|GhostWhite|Gold|GoldenRod|Gr[ae]y|Green|GreenYellow|HoneyDew|HotPink|IndianRed|Indigo|Ivory|Khaki|Lavender|LavenderBlush|LawnGreen|LemonChiffon|LightBlue|LightCoral|LightCyan|LightGoldenRodYellow|LightGr[ae]y|LightGreen|LightPink|LightSalmon|LightSeaGreen|LightSkyBlue|LightSlateGr[ae]y|LightSteelBlue|LightYellow|Lime|LimeGreen|Linen|Magenta|Maroon|MediumAquaMarine|MediumBlue|MediumOrchid|MediumPurple|MediumSeaGreen|MediumSlateBlue|MediumSpringGreen|MediumTurquoise|MediumVioletRed|MidnightBlue|MintCream|MistyRose|Moccasin|NavajoWhite|Navy|OldLace|Olive|OliveDrab|Orange|OrangeRed|Orchid|PaleGoldenRod|PaleGreen|PaleTurquoise|PaleVioletRed|PapayaWhip|PeachPuff|Peru|Pink|Plum|PowderBlue|Purple|Red|RosyBrown|RoyalBlue|SaddleBrown|Salmon|SandyBrown|SeaGreen|SeaShell|Sienna|Silver|SkyBlue|SlateBlue|SlateGr[ae]y|Snow|SpringGreen|SteelBlue|Tan|Teal|Thistle|Tomato|Transparent|Turquoise|Violet|Wheat|White|WhiteSmoke|Yellow|YellowGreen)\b/i,{pattern:/\b(?:rgb|hsl)\(\s*\d{1,3}\s*,\s*\d{1,3}%?\s*,\s*\d{1,3}%?\s*\)\B|\b(?:rgb|hsl)a\(\s*\d{1,3}\s*,\s*\d{1,3}%?\s*,\s*\d{1,3}%?\s*,\s*(?:0|0?\.\d+|1)\s*\)\B/i,inside:{unit:n,number:r,function:/[\w-]+(?=\()/,punctuation:/[(),]/}}],entity:/\\[\da-f]{1,8}/i,unit:n,boolean:/\b(?:true|false)\b/,operator:[/~|[+!\/%<>?=]=?|[-:]=|\*[*=]?|\.{2,3}|&&|\|\||\B-\B|\b(?:and|in|is(?: a| defined| not|nt)?|not|or)\b/],number:r,punctuation:/[{}()\[\];:,]/};i.interpolation={pattern:/\{[^\r\n}:]+\}/,alias:"variable",inside:{delimiter:{pattern:/^{|}$/,alias:"punctuation"},rest:i}},i.func={pattern:/[\w-]+\([^)]*\).*/,inside:{function:/^[^(]+/,rest:i}},e.languages.stylus={"atrule-declaration":{pattern:/(^\s*)@.+/m,lookbehind:!0,inside:{atrule:/^@[\w-]+/,rest:i}},"variable-declaration":{pattern:/(^[ \t]*)[\w$-]+\s*.?=[ \t]*(?:(?:\{[^}]*\}|.+)|$)/m,lookbehind:!0,inside:{variable:/^\S+/,rest:i}},statement:{pattern:/(^[ \t]*)(?:if|else|for|return|unless)[ \t]+.+/m,lookbehind:!0,inside:{keyword:/^\S+/,rest:i}},"property-declaration":{pattern:/((?:^|\{)([ \t]*))(?:[\w-]|\{[^}\r\n]+\})+(?:\s*:\s*|[ \t]+)[^{\r\n]*(?:;|[^{\r\n,](?=$)(?!(?:\r?\n|\r)(?:\{|\2[ \t]+)))/m,lookbehind:!0,inside:{property:{pattern:/^[^\s:]+/,inside:{interpolation:i.interpolation}},rest:i}},selector:{pattern:/(^[ \t]*)(?:(?=\S)(?:[^{}\r\n:()]|::?[\w-]+(?:\([^)\r\n]*\))?|\{[^}\r\n]+\})+)(?:(?:\r?\n|\r)(?:\1(?:(?=\S)(?:[^{}\r\n:()]|::?[\w-]+(?:\([^)\r\n]*\))?|\{[^}\r\n]+\})+)))*(?:,$|\{|(?=(?:\r?\n|\r)(?:\{|\1[ \t]+)))/m,lookbehind:!0,inside:{interpolation:i.interpolation,comment:i.comment,punctuation:/[{},]/}},func:i.func,string:i.string,comment:{pattern:/(^|[^\\])(?:\/\*[\s\S]*?\*\/|\/\/.*)/,lookbehind:!0,greedy:!0},interpolation:i.interpolation,punctuation:/[{}()\[\];:.]/}}(Prism); \ No newline at end of file diff --git a/tests/languages/stylus/atrule-declaration_feature.test b/tests/languages/stylus/atrule-declaration_feature.test index d0e6505d5f..156a553020 100644 --- a/tests/languages/stylus/atrule-declaration_feature.test +++ b/tests/languages/stylus/atrule-declaration_feature.test @@ -3,6 +3,8 @@ @font-face { @keyframes { @media (max-{foo}: bar) +@namespace url(http://www.w3.org/1999/xhtml) // comment +@namespace svg url(http://www.w3.org/2000/svg) // comment ---------------------------------------------------- @@ -23,6 +25,17 @@ ["punctuation", ":"], " bar", ["punctuation", ")"] + ]], + ["atrule-declaration", [ + ["atrule", "@namespace"], + ["url", "url(http://www.w3.org/1999/xhtml)"], + ["comment", "// comment"] + ]], + ["atrule-declaration", [ + ["atrule", "@namespace"], + " svg ", + ["url", "url(http://www.w3.org/2000/svg)"], + ["comment", "// comment"] ]] ] diff --git a/tests/languages/stylus/keyword_feature.test b/tests/languages/stylus/keyword_feature.test index 5182775191..f5ebb4f6f5 100644 --- a/tests/languages/stylus/keyword_feature.test +++ b/tests/languages/stylus/keyword_feature.test @@ -2,6 +2,7 @@ for i in 1..5 if a == 3 z-index: 1 unless @z-index; return pair[1] if pair[0] == key for pair in hash +if url == "http://example.com" // comment ---------------------------------------------------- @@ -36,6 +37,13 @@ return pair[1] if pair[0] == key for pair in hash ["operator", "=="], " key ", ["keyword", "for"], " pair ", ["operator", "in"], " hash" + ]], + ["statement", [ + ["keyword", "if"], + " url ", + ["operator", "=="], + ["string", "\"http://example.com\""], + ["comment", "// comment"] ]] ] diff --git a/tests/languages/stylus/selector_feature.test b/tests/languages/stylus/selector_feature.test index 3868dfcaa3..50912ea2f6 100644 --- a/tests/languages/stylus/selector_feature.test +++ b/tests/languages/stylus/selector_feature.test @@ -18,6 +18,9 @@ color red {foo} {bar}:hover color red +div // comment + display inline-block // comment + ---------------------------------------------------- [ @@ -41,7 +44,9 @@ color red ]], ":hover" ]], - ["property-declaration", [["property", ["color"]], ["color", "red"]]] + ["property-declaration", [["property", ["color"]], ["color", "red"]]], + ["selector", ["div ", ["comment", "// comment"]]], + ["property-declaration", [["property", ["display"]], " inline-block ", ["comment", "// comment"]]] ] ---------------------------------------------------- diff --git a/tests/languages/stylus/variable-declaration_feature.test b/tests/languages/stylus/variable-declaration_feature.test index 1d68f9dfe0..2e98e4fb0d 100644 --- a/tests/languages/stylus/variable-declaration_feature.test +++ b/tests/languages/stylus/variable-declaration_feature.test @@ -2,6 +2,7 @@ foo = 'bar' a = 4 bar-baz = 5 a += 8 +url = "http://example.com" // comment ---------------------------------------------------- @@ -25,6 +26,12 @@ a += 8 ["variable", "a"], ["operator", "+="], ["number", "8"] + ]], + ["variable-declaration", [ + ["variable", "url"], + ["operator", "="], + ["string", "\"http://example.com\""], + ["comment", "// comment"] ]] ]