browser/devtools/sourceeditor/codemirror/htmlmixed.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 (function(mod) {
michael@0 2 if (typeof exports == "object" && typeof module == "object") // CommonJS
michael@0 3 mod(require("../../lib/codemirror"), require("../xml/xml"), require("../javascript/javascript"), require("../css/css"));
michael@0 4 else if (typeof define == "function" && define.amd) // AMD
michael@0 5 define(["../../lib/codemirror", "../xml/xml", "../javascript/javascript", "../css/css"], mod);
michael@0 6 else // Plain browser env
michael@0 7 mod(CodeMirror);
michael@0 8 })(function(CodeMirror) {
michael@0 9 "use strict";
michael@0 10
michael@0 11 CodeMirror.defineMode("htmlmixed", function(config, parserConfig) {
michael@0 12 var htmlMode = CodeMirror.getMode(config, {name: "xml",
michael@0 13 htmlMode: true,
michael@0 14 multilineTagIndentFactor: parserConfig.multilineTagIndentFactor,
michael@0 15 multilineTagIndentPastTag: parserConfig.multilineTagIndentPastTag});
michael@0 16 var cssMode = CodeMirror.getMode(config, "css");
michael@0 17
michael@0 18 var scriptTypes = [], scriptTypesConf = parserConfig && parserConfig.scriptTypes;
michael@0 19 scriptTypes.push({matches: /^(?:text|application)\/(?:x-)?(?:java|ecma)script$|^$/i,
michael@0 20 mode: CodeMirror.getMode(config, "javascript")});
michael@0 21 if (scriptTypesConf) for (var i = 0; i < scriptTypesConf.length; ++i) {
michael@0 22 var conf = scriptTypesConf[i];
michael@0 23 scriptTypes.push({matches: conf.matches, mode: conf.mode && CodeMirror.getMode(config, conf.mode)});
michael@0 24 }
michael@0 25 scriptTypes.push({matches: /./,
michael@0 26 mode: CodeMirror.getMode(config, "text/plain")});
michael@0 27
michael@0 28 function html(stream, state) {
michael@0 29 var tagName = state.htmlState.tagName;
michael@0 30 var style = htmlMode.token(stream, state.htmlState);
michael@0 31 if (tagName == "script" && /\btag\b/.test(style) && stream.current() == ">") {
michael@0 32 // Script block: mode to change to depends on type attribute
michael@0 33 var scriptType = stream.string.slice(Math.max(0, stream.pos - 100), stream.pos).match(/\btype\s*=\s*("[^"]+"|'[^']+'|\S+)[^<]*$/i);
michael@0 34 scriptType = scriptType ? scriptType[1] : "";
michael@0 35 if (scriptType && /[\"\']/.test(scriptType.charAt(0))) scriptType = scriptType.slice(1, scriptType.length - 1);
michael@0 36 for (var i = 0; i < scriptTypes.length; ++i) {
michael@0 37 var tp = scriptTypes[i];
michael@0 38 if (typeof tp.matches == "string" ? scriptType == tp.matches : tp.matches.test(scriptType)) {
michael@0 39 if (tp.mode) {
michael@0 40 state.token = script;
michael@0 41 state.localMode = tp.mode;
michael@0 42 state.localState = tp.mode.startState && tp.mode.startState(htmlMode.indent(state.htmlState, ""));
michael@0 43 }
michael@0 44 break;
michael@0 45 }
michael@0 46 }
michael@0 47 } else if (tagName == "style" && /\btag\b/.test(style) && stream.current() == ">") {
michael@0 48 state.token = css;
michael@0 49 state.localMode = cssMode;
michael@0 50 state.localState = cssMode.startState(htmlMode.indent(state.htmlState, ""));
michael@0 51 }
michael@0 52 return style;
michael@0 53 }
michael@0 54 function maybeBackup(stream, pat, style) {
michael@0 55 var cur = stream.current();
michael@0 56 var close = cur.search(pat), m;
michael@0 57 if (close > -1) stream.backUp(cur.length - close);
michael@0 58 else if (m = cur.match(/<\/?$/)) {
michael@0 59 stream.backUp(cur.length);
michael@0 60 if (!stream.match(pat, false)) stream.match(cur);
michael@0 61 }
michael@0 62 return style;
michael@0 63 }
michael@0 64 function script(stream, state) {
michael@0 65 if (stream.match(/^<\/\s*script\s*>/i, false)) {
michael@0 66 state.token = html;
michael@0 67 state.localState = state.localMode = null;
michael@0 68 return html(stream, state);
michael@0 69 }
michael@0 70 return maybeBackup(stream, /<\/\s*script\s*>/,
michael@0 71 state.localMode.token(stream, state.localState));
michael@0 72 }
michael@0 73 function css(stream, state) {
michael@0 74 if (stream.match(/^<\/\s*style\s*>/i, false)) {
michael@0 75 state.token = html;
michael@0 76 state.localState = state.localMode = null;
michael@0 77 return html(stream, state);
michael@0 78 }
michael@0 79 return maybeBackup(stream, /<\/\s*style\s*>/,
michael@0 80 cssMode.token(stream, state.localState));
michael@0 81 }
michael@0 82
michael@0 83 return {
michael@0 84 startState: function() {
michael@0 85 var state = htmlMode.startState();
michael@0 86 return {token: html, localMode: null, localState: null, htmlState: state};
michael@0 87 },
michael@0 88
michael@0 89 copyState: function(state) {
michael@0 90 if (state.localState)
michael@0 91 var local = CodeMirror.copyState(state.localMode, state.localState);
michael@0 92 return {token: state.token, localMode: state.localMode, localState: local,
michael@0 93 htmlState: CodeMirror.copyState(htmlMode, state.htmlState)};
michael@0 94 },
michael@0 95
michael@0 96 token: function(stream, state) {
michael@0 97 return state.token(stream, state);
michael@0 98 },
michael@0 99
michael@0 100 indent: function(state, textAfter) {
michael@0 101 if (!state.localMode || /^\s*<\//.test(textAfter))
michael@0 102 return htmlMode.indent(state.htmlState, textAfter);
michael@0 103 else if (state.localMode.indent)
michael@0 104 return state.localMode.indent(state.localState, textAfter);
michael@0 105 else
michael@0 106 return CodeMirror.Pass;
michael@0 107 },
michael@0 108
michael@0 109 innerMode: function(state) {
michael@0 110 return {state: state.localState || state.htmlState, mode: state.localMode || htmlMode};
michael@0 111 }
michael@0 112 };
michael@0 113 }, "xml", "javascript", "css");
michael@0 114
michael@0 115 CodeMirror.defineMIME("text/html", "htmlmixed");
michael@0 116
michael@0 117 });

mercurial