browser/devtools/sourceeditor/codemirror/htmlmixed.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/sourceeditor/codemirror/htmlmixed.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,117 @@
     1.4 +(function(mod) {
     1.5 +  if (typeof exports == "object" && typeof module == "object") // CommonJS
     1.6 +    mod(require("../../lib/codemirror"), require("../xml/xml"), require("../javascript/javascript"), require("../css/css"));
     1.7 +  else if (typeof define == "function" && define.amd) // AMD
     1.8 +    define(["../../lib/codemirror", "../xml/xml", "../javascript/javascript", "../css/css"], mod);
     1.9 +  else // Plain browser env
    1.10 +    mod(CodeMirror);
    1.11 +})(function(CodeMirror) {
    1.12 +"use strict";
    1.13 +
    1.14 +CodeMirror.defineMode("htmlmixed", function(config, parserConfig) {
    1.15 +  var htmlMode = CodeMirror.getMode(config, {name: "xml",
    1.16 +                                             htmlMode: true,
    1.17 +                                             multilineTagIndentFactor: parserConfig.multilineTagIndentFactor,
    1.18 +                                             multilineTagIndentPastTag: parserConfig.multilineTagIndentPastTag});
    1.19 +  var cssMode = CodeMirror.getMode(config, "css");
    1.20 +
    1.21 +  var scriptTypes = [], scriptTypesConf = parserConfig && parserConfig.scriptTypes;
    1.22 +  scriptTypes.push({matches: /^(?:text|application)\/(?:x-)?(?:java|ecma)script$|^$/i,
    1.23 +                    mode: CodeMirror.getMode(config, "javascript")});
    1.24 +  if (scriptTypesConf) for (var i = 0; i < scriptTypesConf.length; ++i) {
    1.25 +    var conf = scriptTypesConf[i];
    1.26 +    scriptTypes.push({matches: conf.matches, mode: conf.mode && CodeMirror.getMode(config, conf.mode)});
    1.27 +  }
    1.28 +  scriptTypes.push({matches: /./,
    1.29 +                    mode: CodeMirror.getMode(config, "text/plain")});
    1.30 +
    1.31 +  function html(stream, state) {
    1.32 +    var tagName = state.htmlState.tagName;
    1.33 +    var style = htmlMode.token(stream, state.htmlState);
    1.34 +    if (tagName == "script" && /\btag\b/.test(style) && stream.current() == ">") {
    1.35 +      // Script block: mode to change to depends on type attribute
    1.36 +      var scriptType = stream.string.slice(Math.max(0, stream.pos - 100), stream.pos).match(/\btype\s*=\s*("[^"]+"|'[^']+'|\S+)[^<]*$/i);
    1.37 +      scriptType = scriptType ? scriptType[1] : "";
    1.38 +      if (scriptType && /[\"\']/.test(scriptType.charAt(0))) scriptType = scriptType.slice(1, scriptType.length - 1);
    1.39 +      for (var i = 0; i < scriptTypes.length; ++i) {
    1.40 +        var tp = scriptTypes[i];
    1.41 +        if (typeof tp.matches == "string" ? scriptType == tp.matches : tp.matches.test(scriptType)) {
    1.42 +          if (tp.mode) {
    1.43 +            state.token = script;
    1.44 +            state.localMode = tp.mode;
    1.45 +            state.localState = tp.mode.startState && tp.mode.startState(htmlMode.indent(state.htmlState, ""));
    1.46 +          }
    1.47 +          break;
    1.48 +        }
    1.49 +      }
    1.50 +    } else if (tagName == "style" && /\btag\b/.test(style) && stream.current() == ">") {
    1.51 +      state.token = css;
    1.52 +      state.localMode = cssMode;
    1.53 +      state.localState = cssMode.startState(htmlMode.indent(state.htmlState, ""));
    1.54 +    }
    1.55 +    return style;
    1.56 +  }
    1.57 +  function maybeBackup(stream, pat, style) {
    1.58 +    var cur = stream.current();
    1.59 +    var close = cur.search(pat), m;
    1.60 +    if (close > -1) stream.backUp(cur.length - close);
    1.61 +    else if (m = cur.match(/<\/?$/)) {
    1.62 +      stream.backUp(cur.length);
    1.63 +      if (!stream.match(pat, false)) stream.match(cur);
    1.64 +    }
    1.65 +    return style;
    1.66 +  }
    1.67 +  function script(stream, state) {
    1.68 +    if (stream.match(/^<\/\s*script\s*>/i, false)) {
    1.69 +      state.token = html;
    1.70 +      state.localState = state.localMode = null;
    1.71 +      return html(stream, state);
    1.72 +    }
    1.73 +    return maybeBackup(stream, /<\/\s*script\s*>/,
    1.74 +                       state.localMode.token(stream, state.localState));
    1.75 +  }
    1.76 +  function css(stream, state) {
    1.77 +    if (stream.match(/^<\/\s*style\s*>/i, false)) {
    1.78 +      state.token = html;
    1.79 +      state.localState = state.localMode = null;
    1.80 +      return html(stream, state);
    1.81 +    }
    1.82 +    return maybeBackup(stream, /<\/\s*style\s*>/,
    1.83 +                       cssMode.token(stream, state.localState));
    1.84 +  }
    1.85 +
    1.86 +  return {
    1.87 +    startState: function() {
    1.88 +      var state = htmlMode.startState();
    1.89 +      return {token: html, localMode: null, localState: null, htmlState: state};
    1.90 +    },
    1.91 +
    1.92 +    copyState: function(state) {
    1.93 +      if (state.localState)
    1.94 +        var local = CodeMirror.copyState(state.localMode, state.localState);
    1.95 +      return {token: state.token, localMode: state.localMode, localState: local,
    1.96 +              htmlState: CodeMirror.copyState(htmlMode, state.htmlState)};
    1.97 +    },
    1.98 +
    1.99 +    token: function(stream, state) {
   1.100 +      return state.token(stream, state);
   1.101 +    },
   1.102 +
   1.103 +    indent: function(state, textAfter) {
   1.104 +      if (!state.localMode || /^\s*<\//.test(textAfter))
   1.105 +        return htmlMode.indent(state.htmlState, textAfter);
   1.106 +      else if (state.localMode.indent)
   1.107 +        return state.localMode.indent(state.localState, textAfter);
   1.108 +      else
   1.109 +        return CodeMirror.Pass;
   1.110 +    },
   1.111 +
   1.112 +    innerMode: function(state) {
   1.113 +      return {state: state.localState || state.htmlState, mode: state.localMode || htmlMode};
   1.114 +    }
   1.115 +  };
   1.116 +}, "xml", "javascript", "css");
   1.117 +
   1.118 +CodeMirror.defineMIME("text/html", "htmlmixed");
   1.119 +
   1.120 +});

mercurial