1 /* CodeMirror main module
3 * Implements the CodeMirror constructor and prototype, which take care
4 * of initializing the editor frame, and providing the outside interface.
7 // The CodeMirrorConfig object is used to specify a default
8 // configuration. If you specify such an object before loading this
9 // file, the values you put into it will override the defaults given
10 // below. You can also assign to it after loading.
11 var CodeMirrorConfig = window.CodeMirrorConfig || {};
13 var CodeMirror = (function(){
14 function setDefaults(object, defaults) {
15 for (var option in defaults) {
16 if (!object.hasOwnProperty(option))
17 object[option] = defaults[option];
20 function forEach(array, action) {
21 for (var i = 0; i < array.length; i++)
25 // These default options can be overridden by passing a set of
26 // options to a specific CodeMirror constructor. See manual.html for
28 setDefaults(CodeMirrorConfig, {
32 basefiles: ["util.js", "stringstream.js", "select.js", "undo.js", "editor.js", "tokenize.js"],
36 continuousScanning: false,
41 disableSpellcheck: true,
46 autoMatchParens: false,
48 tabMode: "indent", // or "spaces", "default", "shift"
49 reindentOnLoad: false,
56 function wrapLineNumberDiv(place) {
57 return function(node) {
58 var container = document.createElement("DIV"),
59 nums = document.createElement("DIV"),
60 scroller = document.createElement("DIV");
61 container.style.position = "relative";
62 nums.style.position = "absolute";
63 nums.style.height = "100%";
64 if (nums.style.setExpression) {
65 try {nums.style.setExpression("height", "this.previousSibling.offsetHeight + 'px'");}
66 catch(e) {} // Seems to throw 'Not Implemented' on some IE8 versions
68 nums.style.top = "0px";
69 nums.style.overflow = "hidden";
71 container.appendChild(node);
72 container.appendChild(nums);
73 scroller.className = "CodeMirror-line-numbers";
74 nums.appendChild(scroller);
78 function applyLineNumbers(frame) {
79 var win = frame.contentWindow, doc = win.document,
80 nums = frame.nextSibling, scroller = nums.firstChild;
82 var nextNum = 1, barWidth = null;
84 if (!frame.offsetWidth || !win.Editor) {
85 for (var cur = frame; cur.parentNode; cur = cur.parentNode) {
86 if (cur != document) {
87 clearInterval(sizeInterval);
93 if (nums.offsetWidth != barWidth) {
94 barWidth = nums.offsetWidth;
95 nums.style.left = "-" + (frame.parentNode.style.marginLeft = barWidth + "px");
99 var diff = 20 + Math.max(doc.body.offsetHeight, frame.offsetHeight) - scroller.offsetHeight;
100 for (var n = Math.ceil(diff / 10); n > 0; n--) {
101 var div = document.createElement("DIV");
102 div.appendChild(document.createTextNode(nextNum++));
103 scroller.appendChild(div);
105 nums.scrollTop = doc.body.scrollTop || doc.documentElement.scrollTop || 0;
109 win.addEventHandler(win, "scroll", update);
110 win.addEventHandler(win, "resize", update);
111 var sizeInterval = setInterval(sizeBar, 500);
114 function CodeMirror(place, options) {
115 // Backward compatibility for deprecated options.
116 if (options.dumbTabs) options.tabMode = "spaces";
117 else if (options.normalTab) options.tabMode = "default";
119 // Use passed options, if any, to override defaults.
120 this.options = options = options || {};
121 setDefaults(options, CodeMirrorConfig);
123 var frame = this.frame = document.createElement("IFRAME");
124 if (options.iframeClass) frame.className = options.iframeClass;
125 frame.frameBorder = 0;
126 frame.src = "javascript:false;";
127 frame.style.border = "0";
128 frame.style.width = options.width;
129 frame.style.height = options.height;
130 // display: block occasionally suppresses some Firefox bugs, so we
131 // always add it, redundant as it sounds.
132 frame.style.display = "block";
134 if (place.appendChild) {
136 place = function(n){node.appendChild(n);};
138 if (options.lineNumbers) place = wrapLineNumberDiv(place);
141 // Link back to this object, so that the editor can fetch options
142 // and add a reference to itself.
143 frame.CodeMirror = this;
144 this.win = frame.contentWindow;
146 if (typeof options.parserfile == "string")
147 options.parserfile = [options.parserfile];
148 if (typeof options.stylesheet == "string")
149 options.stylesheet = [options.stylesheet];
151 var html = ["<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"><html><head>"];
152 // Hack to work around a bunch of IE8-specific problems.
153 html.push("<meta http-equiv=\"X-UA-Compatible\" content=\"IE=EmulateIE7\"/>");
154 forEach(options.stylesheet, function(file) {
155 html.push("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + file + "\"/>");
157 forEach(options.basefiles.concat(options.parserfile), function(file) {
158 html.push("<script type=\"text/javascript\" src=\"" + options.path + file + "\"></script>");
160 html.push("</head><body style=\"border-width: 0;\" class=\"editbox\" spellcheck=\"" +
161 (options.disableSpellcheck ? "false" : "true") + "\"></body></html>");
163 var doc = this.win.document;
165 doc.write(html.join(""));
169 CodeMirror.prototype = {
171 if (this.options.initCallback) this.options.initCallback(this);
172 if (this.options.lineNumbers) applyLineNumbers(this.frame);
173 if (this.options.reindentOnLoad) this.reindent();
176 getCode: function() {return this.editor.getCode();},
177 setCode: function(code) {this.editor.importCode(code);},
178 selection: function() {return this.editor.selectedText();},
179 reindent: function() {this.editor.reindent();},
180 reindentSelection: function() {this.editor.reindentSelection(null);},
184 if (this.editor.selectionSnapshot) // IE hack
185 this.win.select.selectCoords(this.win, this.editor.selectionSnapshot);
187 replaceSelection: function(text) {
189 this.editor.replaceSelection(text);
192 replaceChars: function(text, start, end) {
193 this.editor.replaceChars(text, start, end);
195 getSearchCursor: function(string, fromCursor) {
196 return this.editor.getSearchCursor(string, fromCursor);
199 undo: function() {this.editor.history.undo();},
200 redo: function() {this.editor.history.redo();},
201 historySize: function() {return this.editor.history.historySize();},
202 clearHistory: function() {this.editor.history.clear();},
204 grabKeys: function(callback, filter) {this.editor.grabKeys(callback, filter);},
205 ungrabKeys: function() {this.editor.ungrabKeys();},
207 setParser: function(name) {this.editor.setParser(name);},
209 cursorPosition: function(start) {
210 if (this.win.select.ie_selection) this.focus();
211 return this.editor.cursorPosition(start);
213 firstLine: function() {return this.editor.firstLine();},
214 lastLine: function() {return this.editor.lastLine();},
215 nextLine: function(line) {return this.editor.nextLine(line);},
216 prevLine: function(line) {return this.editor.prevLine(line);},
217 lineContent: function(line) {return this.editor.lineContent(line);},
218 setLineContent: function(line, content) {this.editor.setLineContent(line, content);},
219 insertIntoLine: function(line, position, content) {this.editor.insertIntoLine(line, position, content);},
220 selectLines: function(startLine, startOffset, endLine, endOffset) {
222 this.editor.selectLines(startLine, startOffset, endLine, endOffset);
224 nthLine: function(n) {
225 var line = this.firstLine();
226 for (; n > 1 && line !== false; n--)
227 line = this.nextLine(line);
230 lineNumber: function(line) {
232 while (line !== false) {
234 line = this.prevLine(line);
239 // Old number-based line interface
240 jumpToLine: function(n) {
241 this.selectLines(this.nthLine(n), 0);
244 currentLine: function() {
245 return this.lineNumber(this.cursorPosition().line);
249 CodeMirror.InvalidLineHandle = {toString: function(){return "CodeMirror.InvalidLineHandle";}};
251 CodeMirror.replace = function(element) {
252 if (typeof element == "string")
253 element = document.getElementById(element);
254 return function(newElement) {
255 element.parentNode.replaceChild(newElement, element);
259 CodeMirror.fromTextArea = function(area, options) {
260 if (typeof area == "string")
261 area = document.getElementById(area);
263 options = options || {};
264 if (area.style.width && options.width == null)
265 options.width = area.style.width;
266 if (area.style.height && options.height == null)
267 options.height = area.style.height;
268 if (options.content == null) options.content = area.value;
271 function updateField() {
272 area.value = mirror.getCode();
274 if (typeof area.form.addEventListener == "function")
275 area.form.addEventListener("submit", updateField, false);
277 area.form.attachEvent("onsubmit", updateField);
280 function insert(frame) {
281 if (area.nextSibling)
282 area.parentNode.insertBefore(frame, area.nextSibling);
284 area.parentNode.appendChild(frame);
287 area.style.display = "none";
288 var mirror = new CodeMirror(insert, options);
292 CodeMirror.isProbablySupported = function() {
293 // This is rather awful, but can be useful.
296 return Number(window.opera.version()) >= 9.52;
297 else if (/Apple Computers, Inc/.test(navigator.vendor) && (match = navigator.userAgent.match(/Version\/(\d+(?:\.\d+)?)\./)))
298 return Number(match[1]) >= 3;
299 else if (document.selection && window.ActiveXObject && (match = navigator.userAgent.match(/MSIE (\d+(?:\.\d*)?)\b/)))
300 return Number(match[1]) >= 6;
301 else if (match = navigator.userAgent.match(/gecko\/(\d{8})/i))
302 return Number(match[1]) >= 20050901;
303 else if (match = navigator.userAgent.match(/AppleWebKit\/(\d+)/))
304 return Number(match[1]) >= 525;