tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

jsx.js (5412B)


      1 // CodeMirror, copyright (c) by Marijn Haverbeke and others
      2 // Distributed under an MIT license: https://codemirror.net/LICENSE
      3 
      4 (function(mod) {
      5  if (typeof exports == "object" && typeof module == "object") // CommonJS
      6    mod(require("resource://devtools/client/shared/sourceeditor/codemirror/lib/codemirror.js"), require("resource://devtools/client/shared/sourceeditor/codemirror/mode/xml/xml.js"), require("resource://devtools/client/shared/sourceeditor/codemirror/mode/javascript/javascript.js"))
      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() == "<" && jsMode.expressionAllowed(stream, cx.state)) {
    107        jsMode.skipExpression(cx.state)
    108        state.context = new Context(CodeMirror.startState(xmlMode, jsMode.indent(cx.state, "", "")),
    109                                    xmlMode, 0, state.context)
    110        return null
    111      }
    112 
    113      var style = jsMode.token(stream, cx.state)
    114      if (!style && cx.depth != null) {
    115        var cur = stream.current()
    116        if (cur == "{") {
    117          cx.depth++
    118        } else if (cur == "}") {
    119          if (--cx.depth == 0) state.context = state.context.prev
    120        }
    121      }
    122      return style
    123    }
    124 
    125    return {
    126      startState: function() {
    127        return {context: new Context(CodeMirror.startState(jsMode), jsMode)}
    128      },
    129 
    130      copyState: function(state) {
    131        return {context: copyContext(state.context)}
    132      },
    133 
    134      token: token,
    135 
    136      indent: function(state, textAfter, fullLine) {
    137        return state.context.mode.indent(state.context.state, textAfter, fullLine)
    138      },
    139 
    140      innerMode: function(state) {
    141        return state.context
    142      }
    143    }
    144  }, "xml", "javascript")
    145 
    146  CodeMirror.defineMIME("text/jsx", "jsx")
    147  CodeMirror.defineMIME("text/typescript-jsx", {name: "jsx", base: {name: "javascript", typescript: true}})
    148 });