source: node_modules/refractor/lang/jsx.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: 5.8 KB
RevLine 
[d24f17c]1'use strict'
2
3module.exports = jsx
4jsx.displayName = 'jsx'
5jsx.aliases = []
6function jsx(Prism) {
7 ;(function (Prism) {
8 var javascript = Prism.util.clone(Prism.languages.javascript)
9 var space = /(?:\s|\/\/.*(?!.)|\/\*(?:[^*]|\*(?!\/))\*\/)/.source
10 var braces = /(?:\{(?:\{(?:\{[^{}]*\}|[^{}])*\}|[^{}])*\})/.source
11 var spread = /(?:\{<S>*\.{3}(?:[^{}]|<BRACES>)*\})/.source
12 /**
13 * @param {string} source
14 * @param {string} [flags]
15 */
16 function re(source, flags) {
17 source = source
18 .replace(/<S>/g, function () {
19 return space
20 })
21 .replace(/<BRACES>/g, function () {
22 return braces
23 })
24 .replace(/<SPREAD>/g, function () {
25 return spread
26 })
27 return RegExp(source, flags)
28 }
29 spread = re(spread).source
30 Prism.languages.jsx = Prism.languages.extend('markup', javascript)
31 Prism.languages.jsx.tag.pattern = re(
32 /<\/?(?:[\w.:-]+(?:<S>+(?:[\w.:$-]+(?:=(?:"(?:\\[\s\S]|[^\\"])*"|'(?:\\[\s\S]|[^\\'])*'|[^\s{'"/>=]+|<BRACES>))?|<SPREAD>))*<S>*\/?)?>/
33 .source
34 )
35 Prism.languages.jsx.tag.inside['tag'].pattern = /^<\/?[^\s>\/]*/
36 Prism.languages.jsx.tag.inside['attr-value'].pattern =
37 /=(?!\{)(?:"(?:\\[\s\S]|[^\\"])*"|'(?:\\[\s\S]|[^\\'])*'|[^\s'">]+)/
38 Prism.languages.jsx.tag.inside['tag'].inside['class-name'] =
39 /^[A-Z]\w*(?:\.[A-Z]\w*)*$/
40 Prism.languages.jsx.tag.inside['comment'] = javascript['comment']
41 Prism.languages.insertBefore(
42 'inside',
43 'attr-name',
44 {
45 spread: {
46 pattern: re(/<SPREAD>/.source),
47 inside: Prism.languages.jsx
48 }
49 },
50 Prism.languages.jsx.tag
51 )
52 Prism.languages.insertBefore(
53 'inside',
54 'special-attr',
55 {
56 script: {
57 // Allow for two levels of nesting
58 pattern: re(/=<BRACES>/.source),
59 alias: 'language-javascript',
60 inside: {
61 'script-punctuation': {
62 pattern: /^=(?=\{)/,
63 alias: 'punctuation'
64 },
65 rest: Prism.languages.jsx
66 }
67 }
68 },
69 Prism.languages.jsx.tag
70 ) // The following will handle plain text inside tags
71 var stringifyToken = function (token) {
72 if (!token) {
73 return ''
74 }
75 if (typeof token === 'string') {
76 return token
77 }
78 if (typeof token.content === 'string') {
79 return token.content
80 }
81 return token.content.map(stringifyToken).join('')
82 }
83 var walkTokens = function (tokens) {
84 var openedTags = []
85 for (var i = 0; i < tokens.length; i++) {
86 var token = tokens[i]
87 var notTagNorBrace = false
88 if (typeof token !== 'string') {
89 if (
90 token.type === 'tag' &&
91 token.content[0] &&
92 token.content[0].type === 'tag'
93 ) {
94 // We found a tag, now find its kind
95 if (token.content[0].content[0].content === '</') {
96 // Closing tag
97 if (
98 openedTags.length > 0 &&
99 openedTags[openedTags.length - 1].tagName ===
100 stringifyToken(token.content[0].content[1])
101 ) {
102 // Pop matching opening tag
103 openedTags.pop()
104 }
105 } else {
106 if (token.content[token.content.length - 1].content === '/>') {
107 // Autoclosed tag, ignore
108 } else {
109 // Opening tag
110 openedTags.push({
111 tagName: stringifyToken(token.content[0].content[1]),
112 openedBraces: 0
113 })
114 }
115 }
116 } else if (
117 openedTags.length > 0 &&
118 token.type === 'punctuation' &&
119 token.content === '{'
120 ) {
121 // Here we might have entered a JSX context inside a tag
122 openedTags[openedTags.length - 1].openedBraces++
123 } else if (
124 openedTags.length > 0 &&
125 openedTags[openedTags.length - 1].openedBraces > 0 &&
126 token.type === 'punctuation' &&
127 token.content === '}'
128 ) {
129 // Here we might have left a JSX context inside a tag
130 openedTags[openedTags.length - 1].openedBraces--
131 } else {
132 notTagNorBrace = true
133 }
134 }
135 if (notTagNorBrace || typeof token === 'string') {
136 if (
137 openedTags.length > 0 &&
138 openedTags[openedTags.length - 1].openedBraces === 0
139 ) {
140 // Here we are inside a tag, and not inside a JSX context.
141 // That's plain text: drop any tokens matched.
142 var plainText = stringifyToken(token) // And merge text with adjacent text
143 if (
144 i < tokens.length - 1 &&
145 (typeof tokens[i + 1] === 'string' ||
146 tokens[i + 1].type === 'plain-text')
147 ) {
148 plainText += stringifyToken(tokens[i + 1])
149 tokens.splice(i + 1, 1)
150 }
151 if (
152 i > 0 &&
153 (typeof tokens[i - 1] === 'string' ||
154 tokens[i - 1].type === 'plain-text')
155 ) {
156 plainText = stringifyToken(tokens[i - 1]) + plainText
157 tokens.splice(i - 1, 1)
158 i--
159 }
160 tokens[i] = new Prism.Token(
161 'plain-text',
162 plainText,
163 null,
164 plainText
165 )
166 }
167 }
168 if (token.content && typeof token.content !== 'string') {
169 walkTokens(token.content)
170 }
171 }
172 }
173 Prism.hooks.add('after-tokenize', function (env) {
174 if (env.language !== 'jsx' && env.language !== 'tsx') {
175 return
176 }
177 walkTokens(env.tokens)
178 })
179 })(Prism)
180}
Note: See TracBrowser for help on using the repository browser.