source: node_modules/refractor/lang/cshtml.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 7.3 KB
Line 
1'use strict'
2var refractorCsharp = require('./csharp.js')
3module.exports = cshtml
4cshtml.displayName = 'cshtml'
5cshtml.aliases = ['razor']
6function cshtml(Prism) {
7 Prism.register(refractorCsharp)
8 // Docs:
9 // https://docs.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-5.0&tabs=visual-studio
10 // https://docs.microsoft.com/en-us/aspnet/core/mvc/views/razor?view=aspnetcore-5.0
11 ;(function (Prism) {
12 var commentLike = /\/(?![/*])|\/\/.*[\r\n]|\/\*[^*]*(?:\*(?!\/)[^*]*)*\*\//
13 .source
14 var stringLike =
15 /@(?!")|"(?:[^\r\n\\"]|\\.)*"|@"(?:[^\\"]|""|\\[\s\S])*"(?!")/.source +
16 '|' +
17 /'(?:(?:[^\r\n'\\]|\\.|\\[Uux][\da-fA-F]{1,8})'|(?=[^\\](?!')))/.source
18 /**
19 * Creates a nested pattern where all occurrences of the string `<<self>>` are replaced with the pattern itself.
20 *
21 * @param {string} pattern
22 * @param {number} depthLog2
23 * @returns {string}
24 */
25 function nested(pattern, depthLog2) {
26 for (var i = 0; i < depthLog2; i++) {
27 pattern = pattern.replace(/<self>/g, function () {
28 return '(?:' + pattern + ')'
29 })
30 }
31 return pattern
32 .replace(/<self>/g, '[^\\s\\S]')
33 .replace(/<str>/g, '(?:' + stringLike + ')')
34 .replace(/<comment>/g, '(?:' + commentLike + ')')
35 }
36 var round = nested(/\((?:[^()'"@/]|<str>|<comment>|<self>)*\)/.source, 2)
37 var square = nested(/\[(?:[^\[\]'"@/]|<str>|<comment>|<self>)*\]/.source, 2)
38 var curly = nested(/\{(?:[^{}'"@/]|<str>|<comment>|<self>)*\}/.source, 2)
39 var angle = nested(/<(?:[^<>'"@/]|<str>|<comment>|<self>)*>/.source, 2) // Note about the above bracket patterns:
40 // They all ignore HTML expressions that might be in the C# code. This is a problem because HTML (like strings and
41 // comments) is parsed differently. This is a huge problem because HTML might contain brackets and quotes which
42 // messes up the bracket and string counting implemented by the above patterns.
43 //
44 // This problem is not fixable because 1) HTML expression are highly context sensitive and very difficult to detect
45 // and 2) they require one capturing group at every nested level. See the `tagRegion` pattern to admire the
46 // complexity of an HTML expression.
47 //
48 // To somewhat alleviate the problem a bit, the patterns for characters (e.g. 'a') is very permissive, it also
49 // allows invalid characters to support HTML expressions like this: <p>That's it!</p>.
50 var tagAttrs =
51 /(?:\s(?:\s*[^\s>\/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))|(?=[\s/>])))+)?/
52 .source
53 var tagContent = /(?!\d)[^\s>\/=$<%]+/.source + tagAttrs + /\s*\/?>/.source
54 var tagRegion =
55 /\B@?/.source +
56 '(?:' +
57 /<([a-zA-Z][\w:]*)/.source +
58 tagAttrs +
59 /\s*>/.source +
60 '(?:' +
61 (/[^<]/.source +
62 '|' + // all tags that are not the start tag
63 // eslint-disable-next-line regexp/strict
64 /<\/?(?!\1\b)/.source +
65 tagContent +
66 '|' + // nested start tag
67 nested(
68 // eslint-disable-next-line regexp/strict
69 /<\1/.source +
70 tagAttrs +
71 /\s*>/.source +
72 '(?:' +
73 (/[^<]/.source +
74 '|' + // all tags that are not the start tag
75 // eslint-disable-next-line regexp/strict
76 /<\/?(?!\1\b)/.source +
77 tagContent +
78 '|' +
79 '<self>') +
80 ')*' + // eslint-disable-next-line regexp/strict
81 /<\/\1\s*>/.source,
82 2
83 )) +
84 ')*' + // eslint-disable-next-line regexp/strict
85 /<\/\1\s*>/.source +
86 '|' +
87 /</.source +
88 tagContent +
89 ')' // Now for the actual language definition(s):
90 //
91 // Razor as a language has 2 parts:
92 // 1) CSHTML: A markup-like language that has been extended with inline C# code expressions and blocks.
93 // 2) C#+HTML: A variant of C# that can contain CSHTML tags as expressions.
94 //
95 // In the below code, both CSHTML and C#+HTML will be create as separate language definitions that reference each
96 // other. However, only CSHTML will be exported via `Prism.languages`.
97 Prism.languages.cshtml = Prism.languages.extend('markup', {})
98 var csharpWithHtml = Prism.languages.insertBefore(
99 'csharp',
100 'string',
101 {
102 html: {
103 pattern: RegExp(tagRegion),
104 greedy: true,
105 inside: Prism.languages.cshtml
106 }
107 },
108 {
109 csharp: Prism.languages.extend('csharp', {})
110 }
111 )
112 var cs = {
113 pattern: /\S[\s\S]*/,
114 alias: 'language-csharp',
115 inside: csharpWithHtml
116 }
117 Prism.languages.insertBefore('cshtml', 'prolog', {
118 'razor-comment': {
119 pattern: /@\*[\s\S]*?\*@/,
120 greedy: true,
121 alias: 'comment'
122 },
123 block: {
124 pattern: RegExp(
125 /(^|[^@])@/.source +
126 '(?:' +
127 [
128 // @{ ... }
129 curly, // @code{ ... }
130 /(?:code|functions)\s*/.source + curly, // @for (...) { ... }
131 /(?:for|foreach|lock|switch|using|while)\s*/.source +
132 round +
133 /\s*/.source +
134 curly, // @do { ... } while (...);
135 /do\s*/.source +
136 curly +
137 /\s*while\s*/.source +
138 round +
139 /(?:\s*;)?/.source, // @try { ... } catch (...) { ... } finally { ... }
140 /try\s*/.source +
141 curly +
142 /\s*catch\s*/.source +
143 round +
144 /\s*/.source +
145 curly +
146 /\s*finally\s*/.source +
147 curly, // @if (...) {...} else if (...) {...} else {...}
148 /if\s*/.source +
149 round +
150 /\s*/.source +
151 curly +
152 '(?:' +
153 /\s*else/.source +
154 '(?:' +
155 /\s+if\s*/.source +
156 round +
157 ')?' +
158 /\s*/.source +
159 curly +
160 ')*'
161 ].join('|') +
162 ')'
163 ),
164 lookbehind: true,
165 greedy: true,
166 inside: {
167 keyword: /^@\w*/,
168 csharp: cs
169 }
170 },
171 directive: {
172 pattern:
173 /^([ \t]*)@(?:addTagHelper|attribute|implements|inherits|inject|layout|model|namespace|page|preservewhitespace|removeTagHelper|section|tagHelperPrefix|using)(?=\s).*/m,
174 lookbehind: true,
175 greedy: true,
176 inside: {
177 keyword: /^@\w+/,
178 csharp: cs
179 }
180 },
181 value: {
182 pattern: RegExp(
183 /(^|[^@])@/.source +
184 /(?:await\b\s*)?/.source +
185 '(?:' +
186 /\w+\b/.source +
187 '|' +
188 round +
189 ')' +
190 '(?:' +
191 /[?!]?\.\w+\b/.source +
192 '|' +
193 round +
194 '|' +
195 square +
196 '|' +
197 angle +
198 round +
199 ')*'
200 ),
201 lookbehind: true,
202 greedy: true,
203 alias: 'variable',
204 inside: {
205 keyword: /^@/,
206 csharp: cs
207 }
208 },
209 'delegate-operator': {
210 pattern: /(^|[^@])@(?=<)/,
211 lookbehind: true,
212 alias: 'operator'
213 }
214 })
215 Prism.languages.razor = Prism.languages.cshtml
216 })(Prism)
217}
Note: See TracBrowser for help on using the repository browser.