From 452fa95513f95ce38d568344de9802b81400dda8 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Wed, 8 Aug 2018 12:19:58 +0200 Subject: [PATCH 1/2] Added class-name alias to the tags of React components --- components/prism-jsx.js | 13 +++++++++++++ components/prism-jsx.min.js | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/components/prism-jsx.js b/components/prism-jsx.js index 43a09d8540..edd43c2c83 100644 --- a/components/prism-jsx.js +++ b/components/prism-jsx.js @@ -87,6 +87,19 @@ var walkTokens = function (tokens) { } else { notTagNorBrace = true } + + // class-name alias to tags which refer to classes + if (token.type === 'tag' && token.content.length === 2 && token.content[0].type === 'punctuation' && typeof token.content[1] === 'string') { + if (/^[A-Z]/.test(token.content[1])) { + var alias = 'class-name'; + if (!token.alias) + token.alias = alias; + else if (typeof token.alias === 'string') + token.alias = [token.alias, alias]; + else + token.alias.push(alias); + } + } } if (notTagNorBrace || typeof token === 'string') { if (openedTags.length > 0 && openedTags[openedTags.length - 1].openedBraces === 0) { diff --git a/components/prism-jsx.min.js b/components/prism-jsx.min.js index 3bc4d12bdc..da51f52647 100644 --- a/components/prism-jsx.min.js +++ b/components/prism-jsx.min.js @@ -1 +1 @@ -!function(t){var n=t.util.clone(t.languages.javascript);t.languages.jsx=t.languages.extend("markup",n),t.languages.jsx.tag.pattern=/<\/?(?:[\w.:-]+\s*(?:\s+(?:[\w.:-]+(?:=(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^\s{'">=]+|\{(?:\{(?:\{[^}]*\}|[^{}])*\}|[^{}])+\}))?|\{\.{3}[a-z_$][\w$]*(?:\.[a-z_$][\w$]*)*\}))*\s*\/?)?>/i,t.languages.jsx.tag.inside.tag.pattern=/^<\/?[^\s>\/]*/i,t.languages.jsx.tag.inside["attr-value"].pattern=/=(?!\{)(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^\s'">]+)/i,t.languages.insertBefore("inside","attr-name",{spread:{pattern:/\{\.{3}[a-z_$][\w$]*(?:\.[a-z_$][\w$]*)*\}/,inside:{punctuation:/\.{3}|[{}.]/,"attr-value":/\w+/}}},t.languages.jsx.tag),t.languages.insertBefore("inside","attr-value",{script:{pattern:/=(\{(?:\{(?:\{[^}]*\}|[^}])*\}|[^}])+\})/i,inside:{"script-punctuation":{pattern:/^=(?={)/,alias:"punctuation"},rest:t.languages.jsx},alias:"language-javascript"}},t.languages.jsx.tag);var e=function(t){return t?"string"==typeof t?t:"string"==typeof t.content?t.content:t.content.map(e).join(""):""},a=function(n){for(var s=[],g=0;g0&&s[s.length-1].tagName===e(o.content[0].content[1])&&s.pop():"/>"===o.content[o.content.length-1].content||s.push({tagName:e(o.content[0].content[1]),openedBraces:0}):s.length>0&&"punctuation"===o.type&&"{"===o.content?s[s.length-1].openedBraces++:s.length>0&&s[s.length-1].openedBraces>0&&"punctuation"===o.type&&"}"===o.content?s[s.length-1].openedBraces--:i=!0),(i||"string"==typeof o)&&s.length>0&&0===s[s.length-1].openedBraces){var p=e(o);g0&&("string"==typeof n[g-1]||"plain-text"===n[g-1].type)&&(p=e(n[g-1])+p,n.splice(g-1,1),g--),n[g]=new t.Token("plain-text",p,null,p)}o.content&&"string"!=typeof o.content&&a(o.content)}};t.hooks.add("after-tokenize",function(t){("jsx"===t.language||"tsx"===t.language)&&a(t.tokens)})}(Prism); \ No newline at end of file +!function(t){var n=t.util.clone(t.languages.javascript);t.languages.jsx=t.languages.extend("markup",n),t.languages.jsx.tag.pattern=/<\/?(?:[\w.:-]+\s*(?:\s+(?:[\w.:-]+(?:=(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^\s{'">=]+|\{(?:\{(?:\{[^}]*\}|[^{}])*\}|[^{}])+\}))?|\{\.{3}[a-z_$][\w$]*(?:\.[a-z_$][\w$]*)*\}))*\s*\/?)?>/i,t.languages.jsx.tag.inside.tag.pattern=/^<\/?[^\s>\/]*/i,t.languages.jsx.tag.inside["attr-value"].pattern=/=(?!\{)(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^\s'">]+)/i,t.languages.insertBefore("inside","attr-name",{spread:{pattern:/\{\.{3}[a-z_$][\w$]*(?:\.[a-z_$][\w$]*)*\}/,inside:{punctuation:/\.{3}|[{}.]/,"attr-value":/\w+/}}},t.languages.jsx.tag),t.languages.insertBefore("inside","attr-value",{script:{pattern:/=(\{(?:\{(?:\{[^}]*\}|[^}])*\}|[^}])+\})/i,inside:{"script-punctuation":{pattern:/^=(?={)/,alias:"punctuation"},rest:t.languages.jsx},alias:"language-javascript"}},t.languages.jsx.tag);var e=function(t){return t?"string"==typeof t?t:"string"==typeof t.content?t.content:t.content.map(e).join(""):""},a=function(n){for(var s=[],g=0;g0&&s[s.length-1].tagName===e(i.content[0].content[1])&&s.pop():"/>"===i.content[i.content.length-1].content||s.push({tagName:e(i.content[0].content[1]),openedBraces:0}):s.length>0&&"punctuation"===i.type&&"{"===i.content?s[s.length-1].openedBraces++:s.length>0&&s[s.length-1].openedBraces>0&&"punctuation"===i.type&&"}"===i.content?s[s.length-1].openedBraces--:o=!0,"tag"===i.type&&2===i.content.length&&"punctuation"===i.content[0].type&&"string"==typeof i.content[1]&&/^[A-Z]/.test(i.content[1]))){var p="class-name";i.alias?"string"==typeof i.alias?i.alias=[i.alias,p]:i.alias.push(p):i.alias=p}if((o||"string"==typeof i)&&s.length>0&&0===s[s.length-1].openedBraces){var l=e(i);g0&&("string"==typeof n[g-1]||"plain-text"===n[g-1].type)&&(l=e(n[g-1])+l,n.splice(g-1,1),g--),n[g]=new t.Token("plain-text",l,null,l)}i.content&&"string"!=typeof i.content&&a(i.content)}};t.hooks.add("after-tokenize",function(t){("jsx"===t.language||"tsx"===t.language)&&a(t.tokens)})}(Prism); \ No newline at end of file From 4a4a6a3ca0227cf7eb390609b79d2f8a45e9b6db Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Mon, 20 Aug 2018 16:04:45 +0200 Subject: [PATCH 2/2] Replaced token alias with new class-name pattern --- components/prism-jsx.js | 14 +------------- components/prism-jsx.min.js | 2 +- tests/languages/jsx/issue1235.test | 4 ++-- tests/languages/jsx/issue1236.test | 4 ++-- tests/languages/jsx/issue1335.test | 10 +++++----- tests/languages/jsx/issue1342.test | 6 +++--- tests/languages/jsx/issue1364.test | 4 ++-- tests/languages/jsx/tag_feature.test | 4 ++-- tests/languages/tsx/tag_feature.test | 4 ++-- 9 files changed, 20 insertions(+), 32 deletions(-) diff --git a/components/prism-jsx.js b/components/prism-jsx.js index edd43c2c83..970acb7402 100644 --- a/components/prism-jsx.js +++ b/components/prism-jsx.js @@ -7,6 +7,7 @@ Prism.languages.jsx.tag.pattern= /<\/?(?:[\w.:-]+\s*(?:\s+(?:[\w.:-]+(?:=(?:("|' Prism.languages.jsx.tag.inside['tag'].pattern = /^<\/?[^\s>\/]*/i; Prism.languages.jsx.tag.inside['attr-value'].pattern = /=(?!\{)(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^\s'">]+)/i; +Prism.languages.jsx.tag.inside['tag'].inside['class-name'] = /^[A-Z]\w*$/; Prism.languages.insertBefore('inside', 'attr-name', { 'spread': { @@ -87,19 +88,6 @@ var walkTokens = function (tokens) { } else { notTagNorBrace = true } - - // class-name alias to tags which refer to classes - if (token.type === 'tag' && token.content.length === 2 && token.content[0].type === 'punctuation' && typeof token.content[1] === 'string') { - if (/^[A-Z]/.test(token.content[1])) { - var alias = 'class-name'; - if (!token.alias) - token.alias = alias; - else if (typeof token.alias === 'string') - token.alias = [token.alias, alias]; - else - token.alias.push(alias); - } - } } if (notTagNorBrace || typeof token === 'string') { if (openedTags.length > 0 && openedTags[openedTags.length - 1].openedBraces === 0) { diff --git a/components/prism-jsx.min.js b/components/prism-jsx.min.js index da51f52647..188971840c 100644 --- a/components/prism-jsx.min.js +++ b/components/prism-jsx.min.js @@ -1 +1 @@ -!function(t){var n=t.util.clone(t.languages.javascript);t.languages.jsx=t.languages.extend("markup",n),t.languages.jsx.tag.pattern=/<\/?(?:[\w.:-]+\s*(?:\s+(?:[\w.:-]+(?:=(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^\s{'">=]+|\{(?:\{(?:\{[^}]*\}|[^{}])*\}|[^{}])+\}))?|\{\.{3}[a-z_$][\w$]*(?:\.[a-z_$][\w$]*)*\}))*\s*\/?)?>/i,t.languages.jsx.tag.inside.tag.pattern=/^<\/?[^\s>\/]*/i,t.languages.jsx.tag.inside["attr-value"].pattern=/=(?!\{)(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^\s'">]+)/i,t.languages.insertBefore("inside","attr-name",{spread:{pattern:/\{\.{3}[a-z_$][\w$]*(?:\.[a-z_$][\w$]*)*\}/,inside:{punctuation:/\.{3}|[{}.]/,"attr-value":/\w+/}}},t.languages.jsx.tag),t.languages.insertBefore("inside","attr-value",{script:{pattern:/=(\{(?:\{(?:\{[^}]*\}|[^}])*\}|[^}])+\})/i,inside:{"script-punctuation":{pattern:/^=(?={)/,alias:"punctuation"},rest:t.languages.jsx},alias:"language-javascript"}},t.languages.jsx.tag);var e=function(t){return t?"string"==typeof t?t:"string"==typeof t.content?t.content:t.content.map(e).join(""):""},a=function(n){for(var s=[],g=0;g0&&s[s.length-1].tagName===e(i.content[0].content[1])&&s.pop():"/>"===i.content[i.content.length-1].content||s.push({tagName:e(i.content[0].content[1]),openedBraces:0}):s.length>0&&"punctuation"===i.type&&"{"===i.content?s[s.length-1].openedBraces++:s.length>0&&s[s.length-1].openedBraces>0&&"punctuation"===i.type&&"}"===i.content?s[s.length-1].openedBraces--:o=!0,"tag"===i.type&&2===i.content.length&&"punctuation"===i.content[0].type&&"string"==typeof i.content[1]&&/^[A-Z]/.test(i.content[1]))){var p="class-name";i.alias?"string"==typeof i.alias?i.alias=[i.alias,p]:i.alias.push(p):i.alias=p}if((o||"string"==typeof i)&&s.length>0&&0===s[s.length-1].openedBraces){var l=e(i);g0&&("string"==typeof n[g-1]||"plain-text"===n[g-1].type)&&(l=e(n[g-1])+l,n.splice(g-1,1),g--),n[g]=new t.Token("plain-text",l,null,l)}i.content&&"string"!=typeof i.content&&a(i.content)}};t.hooks.add("after-tokenize",function(t){("jsx"===t.language||"tsx"===t.language)&&a(t.tokens)})}(Prism); \ No newline at end of file +!function(t){var n=t.util.clone(t.languages.javascript);t.languages.jsx=t.languages.extend("markup",n),t.languages.jsx.tag.pattern=/<\/?(?:[\w.:-]+\s*(?:\s+(?:[\w.:-]+(?:=(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^\s{'">=]+|\{(?:\{(?:\{[^}]*\}|[^{}])*\}|[^{}])+\}))?|\{\.{3}[a-z_$][\w$]*(?:\.[a-z_$][\w$]*)*\}))*\s*\/?)?>/i,t.languages.jsx.tag.inside.tag.pattern=/^<\/?[^\s>\/]*/i,t.languages.jsx.tag.inside["attr-value"].pattern=/=(?!\{)(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^\s'">]+)/i,t.languages.jsx.tag.inside.tag.inside["class-name"]=/^[A-Z]\w*$/,t.languages.insertBefore("inside","attr-name",{spread:{pattern:/\{\.{3}[a-z_$][\w$]*(?:\.[a-z_$][\w$]*)*\}/,inside:{punctuation:/\.{3}|[{}.]/,"attr-value":/\w+/}}},t.languages.jsx.tag),t.languages.insertBefore("inside","attr-value",{script:{pattern:/=(\{(?:\{(?:\{[^}]*\}|[^}])*\}|[^}])+\})/i,inside:{"script-punctuation":{pattern:/^=(?={)/,alias:"punctuation"},rest:t.languages.jsx},alias:"language-javascript"}},t.languages.jsx.tag);var e=function(t){return t?"string"==typeof t?t:"string"==typeof t.content?t.content:t.content.map(e).join(""):""},a=function(n){for(var s=[],g=0;g0&&s[s.length-1].tagName===e(i.content[0].content[1])&&s.pop():"/>"===i.content[i.content.length-1].content||s.push({tagName:e(i.content[0].content[1]),openedBraces:0}):s.length>0&&"punctuation"===i.type&&"{"===i.content?s[s.length-1].openedBraces++:s.length>0&&s[s.length-1].openedBraces>0&&"punctuation"===i.type&&"}"===i.content?s[s.length-1].openedBraces--:o=!0),(o||"string"==typeof i)&&s.length>0&&0===s[s.length-1].openedBraces){var p=e(i);g0&&("string"==typeof n[g-1]||"plain-text"===n[g-1].type)&&(p=e(n[g-1])+p,n.splice(g-1,1),g--),n[g]=new t.Token("plain-text",p,null,p)}i.content&&"string"!=typeof i.content&&a(i.content)}};t.hooks.add("after-tokenize",function(t){("jsx"===t.language||"tsx"===t.language)&&a(t.tokens)})}(Prism); \ No newline at end of file diff --git a/tests/languages/jsx/issue1235.test b/tests/languages/jsx/issue1235.test index f0fa43b334..d28d61ffc4 100644 --- a/tests/languages/jsx/issue1235.test +++ b/tests/languages/jsx/issue1235.test @@ -6,7 +6,7 @@ ["tag", [ ["tag", [ ["punctuation", "<"], - "Child" + ["class-name", "Child"] ]], ["attr-name", ["options"]], ["script", [ @@ -26,4 +26,4 @@ ---------------------------------------------------- Checks that attribute can contain an simple object. -See #1235. \ No newline at end of file +See #1235. diff --git a/tests/languages/jsx/issue1236.test b/tests/languages/jsx/issue1236.test index a46c48959c..3cc3ae2b8a 100644 --- a/tests/languages/jsx/issue1236.test +++ b/tests/languages/jsx/issue1236.test @@ -6,7 +6,7 @@ ["tag", [ ["tag", [ ["punctuation", "<"], - "Child" + ["class-name", "Child"] ]], ["spread", [ ["punctuation", "{"], @@ -23,4 +23,4 @@ ---------------------------------------------------- Checks that spread can contain an object property. -See #1236. \ No newline at end of file +See #1236. diff --git a/tests/languages/jsx/issue1335.test b/tests/languages/jsx/issue1335.test index cf749e94a6..b34880d5af 100644 --- a/tests/languages/jsx/issue1335.test +++ b/tests/languages/jsx/issue1335.test @@ -14,7 +14,7 @@ ["tag", [ ["tag", [ ["punctuation", "<"], - "Button" + ["class-name", "Button"] ]], ["attr-name", ["onClick"]], ["script", [ @@ -41,7 +41,7 @@ ["tag", [ ["tag", [ ["punctuation", "<"], - "Component" + ["class-name", "Component"] ]], ["attr-name", ["data"]], ["script", [ @@ -76,7 +76,7 @@ ["tag", [ ["tag", [ ["punctuation", "<"], - "Component" + ["class-name", "Component"] ]], ["attr-name", ["title"]], ["script", [ @@ -98,7 +98,7 @@ ["tag", [ ["tag", [ ["punctuation", "<"], - "Component" + ["class-name", "Component"] ]], ["attr-name", ["title"]], ["script", [ @@ -121,4 +121,4 @@ ---------------------------------------------------- -Handles nested pairs of curly braces inside tag attribute. See #1335 \ No newline at end of file +Handles nested pairs of curly braces inside tag attribute. See #1335 diff --git a/tests/languages/jsx/issue1342.test b/tests/languages/jsx/issue1342.test index 1d8a9fa0f4..43864dc61a 100644 --- a/tests/languages/jsx/issue1342.test +++ b/tests/languages/jsx/issue1342.test @@ -6,7 +6,7 @@ ["tag", [ ["tag", [ ["punctuation", "<"], - "CodeView" + ["class-name", "CodeView"] ]], ["attr-name", ["title"]], ["script", [ @@ -42,7 +42,7 @@ ["tag", [ ["tag", [ ["punctuation", ""] ]] @@ -50,4 +50,4 @@ ---------------------------------------------------- -Checks for nested tag with JSX inside another tag with JSX. See #1342 \ No newline at end of file +Checks for nested tag with JSX inside another tag with JSX. See #1342 diff --git a/tests/languages/jsx/issue1364.test b/tests/languages/jsx/issue1364.test index dc35938d52..92f49cef5c 100644 --- a/tests/languages/jsx/issue1364.test +++ b/tests/languages/jsx/issue1364.test @@ -6,7 +6,7 @@ ["tag", [ ["tag", [ ["punctuation", "<"], - "CodeView" + ["class-name", "CodeView"] ]], ["attr-name", ["title"]], ["script", [ @@ -22,4 +22,4 @@ ---------------------------------------------------- -Checks for empty objects in JSX. See #1364 \ No newline at end of file +Checks for empty objects in JSX. See #1364 diff --git a/tests/languages/jsx/tag_feature.test b/tests/languages/jsx/tag_feature.test index 6cfc82eff9..4bc5d101ac 100644 --- a/tests/languages/jsx/tag_feature.test +++ b/tests/languages/jsx/tag_feature.test @@ -31,7 +31,7 @@ var myElement = ; ["tag", [ ["tag", [ ["punctuation", "<"], - "MyComponent" + ["class-name", "MyComponent"] ]], ["attr-name", ["someProperty"]], ["script", [ @@ -80,4 +80,4 @@ var myElement = ; ---------------------------------------------------- -Checks for JSX tags. \ No newline at end of file +Checks for JSX tags. diff --git a/tests/languages/tsx/tag_feature.test b/tests/languages/tsx/tag_feature.test index fa6e87a313..3d5c0e9f96 100644 --- a/tests/languages/tsx/tag_feature.test +++ b/tests/languages/tsx/tag_feature.test @@ -35,7 +35,7 @@ class Test extends Component { ["tag", [ ["tag", [ ["punctuation", "<"], - "MyComponent" + ["class-name", "MyComponent"] ]], ["attr-name", ["someProperty"]], ["script", [ @@ -102,4 +102,4 @@ class Test extends Component { ---------------------------------------------------- -Checks for TSX tags. \ No newline at end of file +Checks for TSX tags.