Mercurial
comparison .cms/lib/codemirror/mode/jsx/jsx.js @ 0:78edf6b517a0 draft
24.10
author | Coffee CMS <info@coffee-cms.ru> |
---|---|
date | Fri, 11 Oct 2024 22:40:23 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:78edf6b517a0 |
---|---|
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others | |
2 // Distributed under an MIT license: https://codemirror.net/5/LICENSE | |
3 | |
4 (function(mod) { | |
5 if (typeof exports == "object" && typeof module == "object") // CommonJS | |
6 mod(require("../../lib/codemirror"), require("../xml/xml"), require("../javascript/javascript")) | |
7 else if (typeof define == "function" && define.amd) // AMD | |
8 define(["../../lib/codemirror", "../xml/xml", "../javascript/javascript"], mod) | |
9 else // Plain browser env | |
10 mod(CodeMirror) | |
11 })(function(CodeMirror) { | |
12 "use strict" | |
13 | |
14 // Depth means the amount of open braces in JS context, in XML | |
15 // context 0 means not in tag, 1 means in tag, and 2 means in tag | |
16 // and js block comment. | |
17 function Context(state, mode, depth, prev) { | |
18 this.state = state; this.mode = mode; this.depth = depth; this.prev = prev | |
19 } | |
20 | |
21 function copyContext(context) { | |
22 return new Context(CodeMirror.copyState(context.mode, context.state), | |
23 context.mode, | |
24 context.depth, | |
25 context.prev && copyContext(context.prev)) | |
26 } | |
27 | |
28 CodeMirror.defineMode("jsx", function(config, modeConfig) { | |
29 var xmlMode = CodeMirror.getMode(config, {name: "xml", allowMissing: true, multilineTagIndentPastTag: false, allowMissingTagName: true}) | |
30 var jsMode = CodeMirror.getMode(config, modeConfig && modeConfig.base || "javascript") | |
31 | |
32 function flatXMLIndent(state) { | |
33 var tagName = state.tagName | |
34 state.tagName = null | |
35 var result = xmlMode.indent(state, "", "") | |
36 state.tagName = tagName | |
37 return result | |
38 } | |
39 | |
40 function token(stream, state) { | |
41 if (state.context.mode == xmlMode) | |
42 return xmlToken(stream, state, state.context) | |
43 else | |
44 return jsToken(stream, state, state.context) | |
45 } | |
46 | |
47 function xmlToken(stream, state, cx) { | |
48 if (cx.depth == 2) { // Inside a JS /* */ comment | |
49 if (stream.match(/^.*?\*\//)) cx.depth = 1 | |
50 else stream.skipToEnd() | |
51 return "comment" | |
52 } | |
53 | |
54 if (stream.peek() == "{") { | |
55 xmlMode.skipAttribute(cx.state) | |
56 | |
57 var indent = flatXMLIndent(cx.state), xmlContext = cx.state.context | |
58 // If JS starts on same line as tag | |
59 if (xmlContext && stream.match(/^[^>]*>\s*$/, false)) { | |
60 while (xmlContext.prev && !xmlContext.startOfLine) | |
61 xmlContext = xmlContext.prev | |
62 // If tag starts the line, use XML indentation level | |
63 if (xmlContext.startOfLine) indent -= config.indentUnit | |
64 // Else use JS indentation level | |
65 else if (cx.prev.state.lexical) indent = cx.prev.state.lexical.indented | |
66 // Else if inside of tag | |
67 } else if (cx.depth == 1) { | |
68 indent += config.indentUnit | |
69 } | |
70 | |
71 state.context = new Context(CodeMirror.startState(jsMode, indent), | |
72 jsMode, 0, state.context) | |
73 return null | |
74 } | |
75 | |
76 if (cx.depth == 1) { // Inside of tag | |
77 if (stream.peek() == "<") { // Tag inside of tag | |
78 xmlMode.skipAttribute(cx.state) | |
79 state.context = new Context(CodeMirror.startState(xmlMode, flatXMLIndent(cx.state)), | |
80 xmlMode, 0, state.context) | |
81 return null | |
82 } else if (stream.match("//")) { | |
83 stream.skipToEnd() | |
84 return "comment" | |
85 } else if (stream.match("/*")) { | |
86 cx.depth = 2 | |
87 return token(stream, state) | |
88 } | |
89 } | |
90 | |
91 var style = xmlMode.token(stream, cx.state), cur = stream.current(), stop | |
92 if (/\btag\b/.test(style)) { | |
93 if (/>$/.test(cur)) { | |
94 if (cx.state.context) cx.depth = 0 | |
95 else state.context = state.context.prev | |
96 } else if (/^</.test(cur)) { | |
97 cx.depth = 1 | |
98 } | |
99 } else if (!style && (stop = cur.indexOf("{")) > -1) { | |
100 stream.backUp(cur.length - stop) | |
101 } | |
102 return style | |
103 } | |
104 | |
105 function jsToken(stream, state, cx) { | |
106 if (stream.peek() == "<" && !stream.match(/^<([^<>]|<[^>]*>)+,\s*>/, false) && | |
107 jsMode.expressionAllowed(stream, cx.state)) { | |
108 state.context = new Context(CodeMirror.startState(xmlMode, jsMode.indent(cx.state, "", "")), | |
109 xmlMode, 0, state.context) | |
110 jsMode.skipExpression(cx.state) | |
111 return null | |
112 } | |
113 | |
114 var style = jsMode.token(stream, cx.state) | |
115 if (!style && cx.depth != null) { | |
116 var cur = stream.current() | |
117 if (cur == "{") { | |
118 cx.depth++ | |
119 } else if (cur == "}") { | |
120 if (--cx.depth == 0) state.context = state.context.prev | |
121 } | |
122 } | |
123 return style | |
124 } | |
125 | |
126 return { | |
127 startState: function() { | |
128 return {context: new Context(CodeMirror.startState(jsMode), jsMode)} | |
129 }, | |
130 | |
131 copyState: function(state) { | |
132 return {context: copyContext(state.context)} | |
133 }, | |
134 | |
135 token: token, | |
136 | |
137 indent: function(state, textAfter, fullLine) { | |
138 return state.context.mode.indent(state.context.state, textAfter, fullLine) | |
139 }, | |
140 | |
141 innerMode: function(state) { | |
142 return state.context | |
143 } | |
144 } | |
145 }, "xml", "javascript") | |
146 | |
147 CodeMirror.defineMIME("text/jsx", "jsx") | |
148 CodeMirror.defineMIME("text/typescript-jsx", {name: "jsx", base: {name: "javascript", typescript: true}}) | |
149 }); |