codemirror-rails 2.21.1 → 2.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. data/README.md +8 -0
  2. data/lib/codemirror/rails/version.rb +2 -2
  3. data/vendor/assets/javascripts/codemirror.js +160 -86
  4. data/vendor/assets/javascripts/codemirror/modes/clike.js +2 -2
  5. data/vendor/assets/javascripts/codemirror/modes/clojure.js +1 -1
  6. data/vendor/assets/javascripts/codemirror/modes/ecl.js +203 -0
  7. data/vendor/assets/javascripts/codemirror/modes/gfm.js +1 -1
  8. data/vendor/assets/javascripts/codemirror/modes/go.js +20 -22
  9. data/vendor/assets/javascripts/codemirror/modes/less.js +5 -2
  10. data/vendor/assets/javascripts/codemirror/modes/markdown.js +58 -24
  11. data/vendor/assets/javascripts/codemirror/modes/pascal.js +2 -46
  12. data/vendor/assets/javascripts/codemirror/modes/perl.js +1 -1
  13. data/vendor/assets/javascripts/codemirror/modes/php.js +53 -24
  14. data/vendor/assets/javascripts/codemirror/modes/properties.js +57 -0
  15. data/vendor/assets/javascripts/codemirror/modes/rpm-spec.css +5 -0
  16. data/vendor/assets/javascripts/codemirror/modes/rpm-spec.js +1 -1
  17. data/vendor/assets/javascripts/codemirror/modes/ruby.js +1 -1
  18. data/vendor/assets/javascripts/codemirror/modes/smalltalk.js +16 -16
  19. data/vendor/assets/javascripts/codemirror/modes/verilog.js +194 -194
  20. data/vendor/assets/javascripts/codemirror/modes/xml.js +15 -8
  21. data/vendor/assets/javascripts/codemirror/utils/dialog.js +63 -0
  22. data/vendor/assets/javascripts/codemirror/utils/foldcode.js +186 -0
  23. data/vendor/assets/javascripts/codemirror/utils/formatting.js +294 -0
  24. data/vendor/assets/javascripts/codemirror/utils/javascript-hint.js +132 -0
  25. data/vendor/assets/javascripts/codemirror/utils/match-highlighter.js +44 -0
  26. data/vendor/assets/javascripts/codemirror/{overlay.js → utils/overlay.js} +0 -0
  27. data/vendor/assets/javascripts/codemirror/utils/runmode.js +49 -0
  28. data/vendor/assets/javascripts/codemirror/utils/search.js +114 -0
  29. data/vendor/assets/javascripts/codemirror/utils/searchcursor.js +117 -0
  30. data/vendor/assets/javascripts/codemirror/utils/simple-hint.js +66 -0
  31. data/vendor/assets/stylesheets/codemirror.css +3 -0
  32. data/vendor/assets/stylesheets/codemirror/modes/properties.css +3 -0
  33. data/vendor/assets/stylesheets/codemirror/themes/rubyblue.css +1 -1
  34. data/vendor/assets/stylesheets/codemirror/utils/dialog.css +23 -0
  35. data/vendor/assets/stylesheets/codemirror/utils/simple-hint.css +16 -0
  36. metadata +20 -6
  37. data/vendor/assets/javascripts/codemirror/runmode.js +0 -27
@@ -4,8 +4,9 @@ CodeMirror.defineMode("xml", function(config, parserConfig) {
4
4
  autoSelfClosers: {"br": true, "img": true, "hr": true, "link": true, "input": true,
5
5
  "meta": true, "col": true, "frame": true, "base": true, "area": true},
6
6
  doNotIndent: {"pre": true},
7
- allowUnquoted: true
8
- } : {autoSelfClosers: {}, doNotIndent: {}, allowUnquoted: false};
7
+ allowUnquoted: true,
8
+ allowMissing: false
9
+ } : {autoSelfClosers: {}, doNotIndent: {}, allowUnquoted: false, allowMissing: false};
9
10
  var alignCDATA = parserConfig.alignCDATA;
10
11
 
11
12
  // Return variables for tokenizers
@@ -55,7 +56,7 @@ CodeMirror.defineMode("xml", function(config, parserConfig) {
55
56
  ok = stream.eatWhile(/[\d]/) && stream.eat(";");
56
57
  }
57
58
  } else {
58
- ok = stream.eatWhile(/[\w]/) && stream.eat(";");
59
+ ok = stream.eatWhile(/[\w\.\-:]/) && stream.eat(";");
59
60
  }
60
61
  return ok ? "atom" : "error";
61
62
  }
@@ -189,15 +190,21 @@ CodeMirror.defineMode("xml", function(config, parserConfig) {
189
190
  }
190
191
 
191
192
  function attributes(type) {
192
- if (type == "word") {setStyle = "attribute"; return cont(attributes);}
193
+ if (type == "word") {setStyle = "attribute"; return cont(attribute, attributes);}
194
+ if (type == "endTag" || type == "selfcloseTag") return pass();
195
+ setStyle = "error";
196
+ return cont(attributes);
197
+ }
198
+ function attribute(type) {
193
199
  if (type == "equals") return cont(attvalue, attributes);
194
- if (type == "string") {setStyle = "error"; return cont(attributes);}
195
- return pass();
200
+ if (!Kludges.allowMissing) setStyle = "error";
201
+ return (type == "endTag" || type == "selfcloseTag") ? pass() : cont();
196
202
  }
197
203
  function attvalue(type) {
198
- if (type == "word" && Kludges.allowUnquoted) {setStyle = "string"; return cont();}
199
204
  if (type == "string") return cont(attvaluemaybe);
200
- return pass();
205
+ if (type == "word" && Kludges.allowUnquoted) {setStyle = "string"; return cont();}
206
+ setStyle = "error";
207
+ return (type == "endTag" || type == "selfCloseTag") ? pass() : cont();
201
208
  }
202
209
  function attvaluemaybe(type) {
203
210
  if (type == "string") return cont(attvaluemaybe);
@@ -0,0 +1,63 @@
1
+ // Open simple dialogs on top of an editor. Relies on dialog.css.
2
+
3
+ (function() {
4
+ function dialogDiv(cm, template) {
5
+ var wrap = cm.getWrapperElement();
6
+ var dialog = wrap.insertBefore(document.createElement("div"), wrap.firstChild);
7
+ dialog.className = "CodeMirror-dialog";
8
+ dialog.innerHTML = '<div>' + template + '</div>';
9
+ return dialog;
10
+ }
11
+
12
+ CodeMirror.defineExtension("openDialog", function(template, callback) {
13
+ var dialog = dialogDiv(this, template);
14
+ var closed = false, me = this;
15
+ function close() {
16
+ if (closed) return;
17
+ closed = true;
18
+ dialog.parentNode.removeChild(dialog);
19
+ }
20
+ var inp = dialog.getElementsByTagName("input")[0];
21
+ if (inp) {
22
+ CodeMirror.connect(inp, "keydown", function(e) {
23
+ if (e.keyCode == 13 || e.keyCode == 27) {
24
+ CodeMirror.e_stop(e);
25
+ close();
26
+ me.focus();
27
+ if (e.keyCode == 13) callback(inp.value);
28
+ }
29
+ });
30
+ inp.focus();
31
+ CodeMirror.connect(inp, "blur", close);
32
+ }
33
+ return close;
34
+ });
35
+
36
+ CodeMirror.defineExtension("openConfirm", function(template, callbacks) {
37
+ var dialog = dialogDiv(this, template);
38
+ var buttons = dialog.getElementsByTagName("button");
39
+ var closed = false, me = this, blurring = 1;
40
+ function close() {
41
+ if (closed) return;
42
+ closed = true;
43
+ dialog.parentNode.removeChild(dialog);
44
+ me.focus();
45
+ }
46
+ buttons[0].focus();
47
+ for (var i = 0; i < buttons.length; ++i) {
48
+ var b = buttons[i];
49
+ (function(callback) {
50
+ CodeMirror.connect(b, "click", function(e) {
51
+ CodeMirror.e_preventDefault(e);
52
+ close();
53
+ if (callback) callback(me);
54
+ });
55
+ })(callbacks[i]);
56
+ CodeMirror.connect(b, "blur", function() {
57
+ --blurring;
58
+ setTimeout(function() { if (blurring <= 0) close(); }, 200);
59
+ });
60
+ CodeMirror.connect(b, "focus", function() { ++blurring; });
61
+ }
62
+ });
63
+ })();
@@ -0,0 +1,186 @@
1
+ // the tagRangeFinder function is
2
+ // Copyright (C) 2011 by Daniel Glazman <daniel@glazman.org>
3
+ // released under the MIT license (../../LICENSE) like the rest of CodeMirror
4
+ CodeMirror.tagRangeFinder = function(cm, line) {
5
+ var nameStartChar = "A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD";
6
+ var nameChar = nameStartChar + "\-\.0-9\\u00B7\\u0300-\\u036F\\u203F-\\u2040";
7
+ var xmlNAMERegExp = new RegExp("^[" + nameStartChar + "][" + nameChar + "]*");
8
+
9
+ var lineText = cm.getLine(line);
10
+ var found = false;
11
+ var tag = null;
12
+ var pos = 0;
13
+ while (!found) {
14
+ pos = lineText.indexOf("<", pos);
15
+ if (-1 == pos) // no tag on line
16
+ return;
17
+ if (pos + 1 < lineText.length && lineText[pos + 1] == "/") { // closing tag
18
+ pos++;
19
+ continue;
20
+ }
21
+ // ok we weem to have a start tag
22
+ if (!lineText.substr(pos + 1).match(xmlNAMERegExp)) { // not a tag name...
23
+ pos++;
24
+ continue;
25
+ }
26
+ var gtPos = lineText.indexOf(">", pos + 1);
27
+ if (-1 == gtPos) { // end of start tag not in line
28
+ var l = line + 1;
29
+ var foundGt = false;
30
+ var lastLine = cm.lineCount();
31
+ while (l < lastLine && !foundGt) {
32
+ var lt = cm.getLine(l);
33
+ var gt = lt.indexOf(">");
34
+ if (-1 != gt) { // found a >
35
+ foundGt = true;
36
+ var slash = lt.lastIndexOf("/", gt);
37
+ if (-1 != slash && slash < gt) {
38
+ var str = lineText.substr(slash, gt - slash + 1);
39
+ if (!str.match( /\/\s*\>/ )) // yep, that's the end of empty tag
40
+ return l+1;
41
+ }
42
+ }
43
+ l++;
44
+ }
45
+ found = true;
46
+ }
47
+ else {
48
+ var slashPos = lineText.lastIndexOf("/", gtPos);
49
+ if (-1 == slashPos) { // cannot be empty tag
50
+ found = true;
51
+ // don't continue
52
+ }
53
+ else { // empty tag?
54
+ // check if really empty tag
55
+ var str = lineText.substr(slashPos, gtPos - slashPos + 1);
56
+ if (!str.match( /\/\s*\>/ )) { // finally not empty
57
+ found = true;
58
+ // don't continue
59
+ }
60
+ }
61
+ }
62
+ if (found) {
63
+ var subLine = lineText.substr(pos + 1);
64
+ tag = subLine.match(xmlNAMERegExp);
65
+ if (tag) {
66
+ // we have an element name, wooohooo !
67
+ tag = tag[0];
68
+ // do we have the close tag on same line ???
69
+ if (-1 != lineText.indexOf("</" + tag + ">", pos)) // yep
70
+ {
71
+ found = false;
72
+ }
73
+ // we don't, so we have a candidate...
74
+ }
75
+ else
76
+ found = false;
77
+ }
78
+ if (!found)
79
+ pos++;
80
+ }
81
+
82
+ if (found) {
83
+ var startTag = "(\\<\\/" + tag + "\\>)|(\\<" + tag + "\\>)|(\\<" + tag + "\s)|(\\<" + tag + "$)";
84
+ var startTagRegExp = new RegExp(startTag, "g");
85
+ var endTag = "</" + tag + ">";
86
+ var depth = 1;
87
+ var l = line + 1;
88
+ var lastLine = cm.lineCount();
89
+ while (l < lastLine) {
90
+ lineText = cm.getLine(l);
91
+ var match = lineText.match(startTagRegExp);
92
+ if (match) {
93
+ for (var i = 0; i < match.length; i++) {
94
+ if (match[i] == endTag)
95
+ depth--;
96
+ else
97
+ depth++;
98
+ if (!depth)
99
+ return l+1;
100
+ }
101
+ }
102
+ l++;
103
+ }
104
+ return;
105
+ }
106
+ };
107
+
108
+ CodeMirror.braceRangeFinder = function(cm, line) {
109
+ var lineText = cm.getLine(line);
110
+ var startChar = lineText.lastIndexOf("{");
111
+ if (startChar < 0 || lineText.lastIndexOf("}") > startChar) return;
112
+ var tokenType = cm.getTokenAt({line: line, ch: startChar}).className;
113
+ var count = 1, lastLine = cm.lineCount(), end;
114
+ outer: for (var i = line + 1; i < lastLine; ++i) {
115
+ var text = cm.getLine(i), pos = 0;
116
+ for (;;) {
117
+ var nextOpen = text.indexOf("{", pos), nextClose = text.indexOf("}", pos);
118
+ if (nextOpen < 0) nextOpen = text.length;
119
+ if (nextClose < 0) nextClose = text.length;
120
+ pos = Math.min(nextOpen, nextClose);
121
+ if (pos == text.length) break;
122
+ if (cm.getTokenAt({line: i, ch: pos + 1}).className == tokenType) {
123
+ if (pos == nextOpen) ++count;
124
+ else if (!--count) { end = i; break outer; }
125
+ }
126
+ ++pos;
127
+ }
128
+ }
129
+ if (end == null || end == line + 1) return;
130
+ return end;
131
+ };
132
+
133
+ CodeMirror.indentRangeFinder = function(cm, line) {
134
+ var tabSize = cm.getOption("tabSize");
135
+ var myIndent = cm.getLineHandle(line).indentation(tabSize), last;
136
+ for (var i = line + 1, end = cm.lineCount(); i < end; ++i) {
137
+ var handle = cm.getLineHandle(i);
138
+ if (!/^\s*$/.test(handle.text)) {
139
+ if (handle.indentation(tabSize) <= myIndent) break;
140
+ last = i;
141
+ }
142
+ }
143
+ if (!last) return null;
144
+ return last + 1;
145
+ };
146
+
147
+ CodeMirror.newFoldFunction = function(rangeFinder, markText) {
148
+ var folded = [];
149
+ if (markText == null) markText = '<div style="position: absolute; left: 2px; color:#600">&#x25bc;</div>%N%';
150
+
151
+ function isFolded(cm, n) {
152
+ for (var i = 0; i < folded.length; ++i) {
153
+ var start = cm.lineInfo(folded[i].start);
154
+ if (!start) folded.splice(i--, 1);
155
+ else if (start.line == n) return {pos: i, region: folded[i]};
156
+ }
157
+ }
158
+
159
+ function expand(cm, region) {
160
+ cm.clearMarker(region.start);
161
+ for (var i = 0; i < region.hidden.length; ++i)
162
+ cm.showLine(region.hidden[i]);
163
+ }
164
+
165
+ return function(cm, line) {
166
+ cm.operation(function() {
167
+ var known = isFolded(cm, line);
168
+ if (known) {
169
+ folded.splice(known.pos, 1);
170
+ expand(cm, known.region);
171
+ } else {
172
+ var end = rangeFinder(cm, line);
173
+ if (end == null) return;
174
+ var hidden = [];
175
+ for (var i = line + 1; i < end; ++i) {
176
+ var handle = cm.hideLine(i);
177
+ if (handle) hidden.push(handle);
178
+ }
179
+ var first = cm.setMarker(line, markText);
180
+ var region = {start: first, hidden: hidden};
181
+ cm.onDeleteLine(first, function() { expand(cm, region); });
182
+ folded.push(region);
183
+ }
184
+ });
185
+ };
186
+ };
@@ -0,0 +1,294 @@
1
+ // ============== Formatting extensions ============================
2
+ // A common storage for all mode-specific formatting features
3
+ if (!CodeMirror.modeExtensions) CodeMirror.modeExtensions = {};
4
+
5
+ // Returns the extension of the editor's current mode
6
+ CodeMirror.defineExtension("getModeExt", function () {
7
+ var mname = CodeMirror.resolveMode(this.getOption("mode")).name;
8
+ var ext = CodeMirror.modeExtensions[mname];
9
+ if (!ext) throw new Error("No extensions found for mode " + mname);
10
+ return ext;
11
+ });
12
+
13
+ // If the current mode is 'htmlmixed', returns the extension of a mode located at
14
+ // the specified position (can be htmlmixed, css or javascript). Otherwise, simply
15
+ // returns the extension of the editor's current mode.
16
+ CodeMirror.defineExtension("getModeExtAtPos", function (pos) {
17
+ var token = this.getTokenAt(pos);
18
+ if (token && token.state && token.state.mode)
19
+ return CodeMirror.modeExtensions[token.state.mode == "html" ? "htmlmixed" : token.state.mode];
20
+ else
21
+ return this.getModeExt();
22
+ });
23
+
24
+ // Comment/uncomment the specified range
25
+ CodeMirror.defineExtension("commentRange", function (isComment, from, to) {
26
+ var curMode = this.getModeExtAtPos(this.getCursor());
27
+ if (isComment) { // Comment range
28
+ var commentedText = this.getRange(from, to);
29
+ this.replaceRange(curMode.commentStart + this.getRange(from, to) + curMode.commentEnd
30
+ , from, to);
31
+ if (from.line == to.line && from.ch == to.ch) { // An empty comment inserted - put cursor inside
32
+ this.setCursor(from.line, from.ch + curMode.commentStart.length);
33
+ }
34
+ }
35
+ else { // Uncomment range
36
+ var selText = this.getRange(from, to);
37
+ var startIndex = selText.indexOf(curMode.commentStart);
38
+ var endIndex = selText.lastIndexOf(curMode.commentEnd);
39
+ if (startIndex > -1 && endIndex > -1 && endIndex > startIndex) {
40
+ // Take string till comment start
41
+ selText = selText.substr(0, startIndex)
42
+ // From comment start till comment end
43
+ + selText.substring(startIndex + curMode.commentStart.length, endIndex)
44
+ // From comment end till string end
45
+ + selText.substr(endIndex + curMode.commentEnd.length);
46
+ }
47
+ this.replaceRange(selText, from, to);
48
+ }
49
+ });
50
+
51
+ // Applies automatic mode-aware indentation to the specified range
52
+ CodeMirror.defineExtension("autoIndentRange", function (from, to) {
53
+ var cmInstance = this;
54
+ this.operation(function () {
55
+ for (var i = from.line; i <= to.line; i++) {
56
+ cmInstance.indentLine(i, "smart");
57
+ }
58
+ });
59
+ });
60
+
61
+ // Applies automatic formatting to the specified range
62
+ CodeMirror.defineExtension("autoFormatRange", function (from, to) {
63
+ var absStart = this.indexFromPos(from);
64
+ var absEnd = this.indexFromPos(to);
65
+ // Insert additional line breaks where necessary according to the
66
+ // mode's syntax
67
+ var res = this.getModeExt().autoFormatLineBreaks(this.getValue(), absStart, absEnd);
68
+ var cmInstance = this;
69
+
70
+ // Replace and auto-indent the range
71
+ this.operation(function () {
72
+ cmInstance.replaceRange(res, from, to);
73
+ var startLine = cmInstance.posFromIndex(absStart).line;
74
+ var endLine = cmInstance.posFromIndex(absStart + res.length).line;
75
+ for (var i = startLine; i <= endLine; i++) {
76
+ cmInstance.indentLine(i, "smart");
77
+ }
78
+ });
79
+ });
80
+
81
+ // Define extensions for a few modes
82
+
83
+ CodeMirror.modeExtensions["css"] = {
84
+ commentStart: "/*",
85
+ commentEnd: "*/",
86
+ wordWrapChars: [";", "\\{", "\\}"],
87
+ autoFormatLineBreaks: function (text) {
88
+ return text.replace(new RegExp("(;|\\{|\\})([^\r\n])", "g"), "$1\n$2");
89
+ }
90
+ };
91
+
92
+ CodeMirror.modeExtensions["javascript"] = {
93
+ commentStart: "/*",
94
+ commentEnd: "*/",
95
+ wordWrapChars: [";", "\\{", "\\}"],
96
+
97
+ getNonBreakableBlocks: function (text) {
98
+ var nonBreakableRegexes = [
99
+ new RegExp("for\\s*?\\(([\\s\\S]*?)\\)"),
100
+ new RegExp("'([\\s\\S]*?)('|$)"),
101
+ new RegExp("\"([\\s\\S]*?)(\"|$)"),
102
+ new RegExp("//.*([\r\n]|$)")
103
+ ];
104
+ var nonBreakableBlocks = new Array();
105
+ for (var i = 0; i < nonBreakableRegexes.length; i++) {
106
+ var curPos = 0;
107
+ while (curPos < text.length) {
108
+ var m = text.substr(curPos).match(nonBreakableRegexes[i]);
109
+ if (m != null) {
110
+ nonBreakableBlocks.push({
111
+ start: curPos + m.index,
112
+ end: curPos + m.index + m[0].length
113
+ });
114
+ curPos += m.index + Math.max(1, m[0].length);
115
+ }
116
+ else { // No more matches
117
+ break;
118
+ }
119
+ }
120
+ }
121
+ nonBreakableBlocks.sort(function (a, b) {
122
+ return a.start - b.start;
123
+ });
124
+
125
+ return nonBreakableBlocks;
126
+ },
127
+
128
+ autoFormatLineBreaks: function (text) {
129
+ var curPos = 0;
130
+ var reLinesSplitter = new RegExp("(;|\\{|\\})([^\r\n])", "g");
131
+ var nonBreakableBlocks = this.getNonBreakableBlocks(text);
132
+ if (nonBreakableBlocks != null) {
133
+ var res = "";
134
+ for (var i = 0; i < nonBreakableBlocks.length; i++) {
135
+ if (nonBreakableBlocks[i].start > curPos) { // Break lines till the block
136
+ res += text.substring(curPos, nonBreakableBlocks[i].start).replace(reLinesSplitter, "$1\n$2");
137
+ curPos = nonBreakableBlocks[i].start;
138
+ }
139
+ if (nonBreakableBlocks[i].start <= curPos
140
+ && nonBreakableBlocks[i].end >= curPos) { // Skip non-breakable block
141
+ res += text.substring(curPos, nonBreakableBlocks[i].end);
142
+ curPos = nonBreakableBlocks[i].end;
143
+ }
144
+ }
145
+ if (curPos < text.length - 1) {
146
+ res += text.substr(curPos).replace(reLinesSplitter, "$1\n$2");
147
+ }
148
+ return res;
149
+ }
150
+ else {
151
+ return text.replace(reLinesSplitter, "$1\n$2");
152
+ }
153
+ }
154
+ };
155
+
156
+ CodeMirror.modeExtensions["xml"] = {
157
+ commentStart: "<!--",
158
+ commentEnd: "-->",
159
+ wordWrapChars: [">"],
160
+
161
+ autoFormatLineBreaks: function (text) {
162
+ var lines = text.split("\n");
163
+ var reProcessedPortion = new RegExp("(^\\s*?<|^[^<]*?)(.+)(>\\s*?$|[^>]*?$)");
164
+ var reOpenBrackets = new RegExp("<", "g");
165
+ var reCloseBrackets = new RegExp("(>)([^\r\n])", "g");
166
+ for (var i = 0; i < lines.length; i++) {
167
+ var mToProcess = lines[i].match(reProcessedPortion);
168
+ if (mToProcess != null && mToProcess.length > 3) { // The line starts with whitespaces and ends with whitespaces
169
+ lines[i] = mToProcess[1]
170
+ + mToProcess[2].replace(reOpenBrackets, "\n$&").replace(reCloseBrackets, "$1\n$2")
171
+ + mToProcess[3];
172
+ continue;
173
+ }
174
+ }
175
+
176
+ return lines.join("\n");
177
+ }
178
+ };
179
+
180
+ CodeMirror.modeExtensions["htmlmixed"] = {
181
+ commentStart: "<!--",
182
+ commentEnd: "-->",
183
+ wordWrapChars: [">", ";", "\\{", "\\}"],
184
+
185
+ getModeInfos: function (text, absPos) {
186
+ var modeInfos = new Array();
187
+ modeInfos[0] =
188
+ {
189
+ pos: 0,
190
+ modeExt: CodeMirror.modeExtensions["xml"],
191
+ modeName: "xml"
192
+ };
193
+
194
+ var modeMatchers = new Array();
195
+ modeMatchers[0] =
196
+ {
197
+ regex: new RegExp("<style[^>]*>([\\s\\S]*?)(</style[^>]*>|$)", "i"),
198
+ modeExt: CodeMirror.modeExtensions["css"],
199
+ modeName: "css"
200
+ };
201
+ modeMatchers[1] =
202
+ {
203
+ regex: new RegExp("<script[^>]*>([\\s\\S]*?)(</script[^>]*>|$)", "i"),
204
+ modeExt: CodeMirror.modeExtensions["javascript"],
205
+ modeName: "javascript"
206
+ };
207
+
208
+ var lastCharPos = (typeof (absPos) !== "undefined" ? absPos : text.length - 1);
209
+ // Detect modes for the entire text
210
+ for (var i = 0; i < modeMatchers.length; i++) {
211
+ var curPos = 0;
212
+ while (curPos <= lastCharPos) {
213
+ var m = text.substr(curPos).match(modeMatchers[i].regex);
214
+ if (m != null) {
215
+ if (m.length > 1 && m[1].length > 0) {
216
+ // Push block begin pos
217
+ var blockBegin = curPos + m.index + m[0].indexOf(m[1]);
218
+ modeInfos.push(
219
+ {
220
+ pos: blockBegin,
221
+ modeExt: modeMatchers[i].modeExt,
222
+ modeName: modeMatchers[i].modeName
223
+ });
224
+ // Push block end pos
225
+ modeInfos.push(
226
+ {
227
+ pos: blockBegin + m[1].length,
228
+ modeExt: modeInfos[0].modeExt,
229
+ modeName: modeInfos[0].modeName
230
+ });
231
+ curPos += m.index + m[0].length;
232
+ continue;
233
+ }
234
+ else {
235
+ curPos += m.index + Math.max(m[0].length, 1);
236
+ }
237
+ }
238
+ else { // No more matches
239
+ break;
240
+ }
241
+ }
242
+ }
243
+ // Sort mode infos
244
+ modeInfos.sort(function sortModeInfo(a, b) {
245
+ return a.pos - b.pos;
246
+ });
247
+
248
+ return modeInfos;
249
+ },
250
+
251
+ autoFormatLineBreaks: function (text, startPos, endPos) {
252
+ var modeInfos = this.getModeInfos(text);
253
+ var reBlockStartsWithNewline = new RegExp("^\\s*?\n");
254
+ var reBlockEndsWithNewline = new RegExp("\n\\s*?$");
255
+ var res = "";
256
+ // Use modes info to break lines correspondingly
257
+ if (modeInfos.length > 1) { // Deal with multi-mode text
258
+ for (var i = 1; i <= modeInfos.length; i++) {
259
+ var selStart = modeInfos[i - 1].pos;
260
+ var selEnd = (i < modeInfos.length ? modeInfos[i].pos : endPos);
261
+
262
+ if (selStart >= endPos) { // The block starts later than the needed fragment
263
+ break;
264
+ }
265
+ if (selStart < startPos) {
266
+ if (selEnd <= startPos) { // The block starts earlier than the needed fragment
267
+ continue;
268
+ }
269
+ selStart = startPos;
270
+ }
271
+ if (selEnd > endPos) {
272
+ selEnd = endPos;
273
+ }
274
+ var textPortion = text.substring(selStart, selEnd);
275
+ if (modeInfos[i - 1].modeName != "xml") { // Starting a CSS or JavaScript block
276
+ if (!reBlockStartsWithNewline.test(textPortion)
277
+ && selStart > 0) { // The block does not start with a line break
278
+ textPortion = "\n" + textPortion;
279
+ }
280
+ if (!reBlockEndsWithNewline.test(textPortion)
281
+ && selEnd < text.length - 1) { // The block does not end with a line break
282
+ textPortion += "\n";
283
+ }
284
+ }
285
+ res += modeInfos[i - 1].modeExt.autoFormatLineBreaks(textPortion);
286
+ }
287
+ }
288
+ else { // Single-mode text
289
+ res = modeInfos[0].modeExt.autoFormatLineBreaks(text.substring(startPos, endPos));
290
+ }
291
+
292
+ return res;
293
+ }
294
+ };