5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"), require("../xml/xml"), require("../javascript/javascript"))
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror", "../xml/xml", "../javascript/javascript"], mod)
11
})(function(CodeMirror) {
17
function Context(state, mode, depth, prev) {
18
this.state = state; this.mode = mode; this.depth = depth; this.prev = prev
21
function copyContext(context) {
22
return new Context(CodeMirror.copyState(context.mode, context.state),
25
context.prev && copyContext(context.prev))
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")
32
function flatXMLIndent(state) {
33
var tagName = state.tagName
35
var result = xmlMode.indent(state, "", "")
36
state.tagName = tagName
40
function token(stream, state) {
41
if (state.context.mode == xmlMode)
42
return xmlToken(stream, state, state.context)
44
return jsToken(stream, state, state.context)
47
function xmlToken(stream, state, cx) {
49
if (stream.match(/^.*?\*\//)) cx.depth = 1
50
else stream.skipToEnd()
54
if (stream.peek() == "{") {
55
xmlMode.skipAttribute(cx.state)
57
var indent = flatXMLIndent(cx.state), xmlContext = cx.state.context
59
if (xmlContext && stream.match(/^[^>]*>\s*$/, false)) {
60
while (xmlContext.prev && !xmlContext.startOfLine)
61
xmlContext = xmlContext.prev
63
if (xmlContext.startOfLine) indent -= config.indentUnit
65
else if (cx.prev.state.lexical) indent = cx.prev.state.lexical.indented
67
} else if (cx.depth == 1) {
68
indent += config.indentUnit
71
state.context = new Context(CodeMirror.startState(jsMode, indent),
72
jsMode, 0, state.context)
77
if (stream.peek() == "<") {
78
xmlMode.skipAttribute(cx.state)
79
state.context = new Context(CodeMirror.startState(xmlMode, flatXMLIndent(cx.state)),
80
xmlMode, 0, state.context)
82
} else if (stream.match("//")) {
85
} else if (stream.match("/*")) {
87
return token(stream, state)
91
var style = xmlMode.token(stream, cx.state), cur = stream.current(), stop
92
if (/\btag\b/.test(style)) {
94
if (cx.state.context) cx.depth = 0
95
else state.context = state.context.prev
96
} else if (/^</.test(cur)) {
99
} else if (!style && (stop = cur.indexOf("{")) > -1) {
100
stream.backUp(cur.length - stop)
105
function jsToken(stream, state, cx) {
106
if (stream.peek() == "<" && jsMode.expressionAllowed(stream, cx.state)) {
107
state.context = new Context(CodeMirror.startState(xmlMode, jsMode.indent(cx.state, "", "")),
108
xmlMode, 0, state.context)
109
jsMode.skipExpression(cx.state)
113
var style = jsMode.token(stream, cx.state)
114
if (!style && cx.depth != null) {
115
var cur = stream.current()
118
} else if (cur == "}") {
119
if (--cx.depth == 0) state.context = state.context.prev
126
startState: function() {
127
return {context: new Context(CodeMirror.startState(jsMode), jsMode)}
130
copyState: function(state) {
131
return {context: copyContext(state.context)}
136
indent: function(state, textAfter, fullLine) {
137
return state.context.mode.indent(state.context.state, textAfter, fullLine)
140
innerMode: function(state) {
144
}, "xml", "javascript")
146
CodeMirror.defineMIME("text/jsx", "jsx")
147
CodeMirror.defineMIME("text/typescript-jsx", {name: "jsx", base: {name: "javascript", typescript: true}})