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) {
59 var container = document.createElement("DIV"),
60 nums = document.createElement("DIV"),
61 scroller = document.createElement("DIV");
63 nums.style.position = "absolute";
64 nums.style.height = "100%";
65 if (nums.style.setExpression) {
66 try {nums.style.setExpression("height", "this.previousSibling.offsetHeight + 'px'");}
67 catch(e) {} // Seems to throw 'Not Implemented' on some IE8 versions
69 nums.style.top = "0px";
70 nums.style.overflow = "hidden";
72 container.style.position = "absolute";
73 container.style.left = "0px";
74 container.style.right = "0px";
75 container.style.bottom = "0px";
76 container.style.top = "0px";
78 node.style.position = "absolute";
79 node.style.top = "0px";
80 node.style.right = "0px";
81 node.style.bottom = "0px";
82 node.style.left = "16px"
85 container.appendChild(node);
86 container.appendChild(nums);
87 scroller.className = "CodeMirror-line-numbers";
88 nums.appendChild(scroller);
92 function applyLineNumbers(frame) {
93 var win = frame.contentWindow, doc = win.document,
94 nums = frame.parentNode.nextSibling, scroller = nums.firstChild;
96 var nextNum = 1, barWidth = null;
98 if (!frame.offsetWidth || !win.Editor) {
99 for (var cur = frame; cur.parentNode; cur = cur.parentNode) {
100 if (cur != document) {
101 clearInterval(sizeInterval);
107 if (nums.offsetWidth != barWidth) {
108 barWidth = nums.offsetWidth;
109 // nums.style.left = "-" + (frame.parentNode.style.marginLeft = barWidth + "px");
113 var diff = 20 + Math.max(doc.body.offsetHeight, frame.offsetHeight) - scroller.offsetHeight;
114 for (var n = Math.ceil(diff / 10); n > 0; n--) {
115 var div = document.createElement("DIV");
116 div.appendChild(document.createTextNode(nextNum++));
117 scroller.appendChild(div);
119 nums.scrollTop = doc.body.scrollTop || doc.documentElement.scrollTop || 0;
123 win.addEventHandler(win, "scroll", update);
124 win.addEventHandler(win, "resize", update);
125 var sizeInterval = setInterval(sizeBar, 500);
128 function CodeMirror(place, options) {
129 // Backward compatibility for deprecated options.
130 if (options.dumbTabs) options.tabMode = "spaces";
131 else if (options.normalTab) options.tabMode = "default";
133 // Use passed options, if any, to override defaults.
134 this.options = options = options || {};
135 setDefaults(options, CodeMirrorConfig);
137 var frame = this.frame = document.createElement("IFRAME");
138 if (options.iframeClass) frame.className = options.iframeClass;
139 frame.frameBorder = 0;
140 frame.src = "javascript:false;";
141 frame.style.border = "0";
142 frame.style.width = "100%";
143 frame.style.height = "100%";
144 // display: block occasionally suppresses some Firefox bugs, so we
145 // always add it, redundant as it sounds.
146 frame.style.display = "block";
148 if (place.appendChild) {
150 place = function(n){node.appendChild(n);};
153 var iframe_container = document.createElement("DIV");
154 iframe_container.appendChild(frame);
156 var content_wrapper = document.createElement("DIV");
157 content_wrapper.appendChild(iframe_container);
158 content_wrapper.style.position = 'relative';
159 content_wrapper.className = 'CodeMirror-content-wrapper';
161 iframe_container.style.position = 'absolute';
162 iframe_container.style.top = '0px';
163 iframe_container.style.right = '0px';
164 iframe_container.style.bottom = '0px';
165 iframe_container.style.left = '28px';
167 if (options.lineNumbers) {
168 var nums = document.createElement("DIV"),
169 scroller = document.createElement("DIV");
171 nums.style.position = "absolute";
172 nums.style.height = "100%";
174 nums.style.top = "0px";
175 nums.style.left = "0px";
176 nums.style.overflow = "hidden";
178 scroller.className = "CodeMirror-line-numbers";
179 nums.appendChild(scroller);
180 content_wrapper.appendChild(nums);
182 iframe_container.style.right = nums.width;
185 place(content_wrapper);
187 // Link back to this object, so that the editor can fetch options
188 // and add a reference to itself.
189 frame.CodeMirror = this;
190 this.win = frame.contentWindow;
192 if (typeof options.parserfile == "string")
193 options.parserfile = [options.parserfile];
194 if (typeof options.stylesheet == "string")
195 options.stylesheet = [options.stylesheet];
197 var html = ["<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"><html><head>"];
198 // Hack to work around a bunch of IE8-specific problems.
199 html.push("<meta http-equiv=\"X-UA-Compatible\" content=\"IE=EmulateIE7\"/>");
200 forEach(options.stylesheet, function(file) {
201 html.push("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + file + "\"/>");
203 forEach(options.basefiles.concat(options.parserfile), function(file) {
204 html.push("<script type=\"text/javascript\" src=\"" + options.path + file + "\"></script>");
206 html.push("</head><body style=\"border-width: 0;\" class=\"editbox\" spellcheck=\"" +
207 (options.disableSpellcheck ? "false" : "true") + "\"></body></html>");
209 var doc = this.win.document;
211 doc.write(html.join(""));
215 CodeMirror.prototype = {
217 if (this.options.initCallback) this.options.initCallback(this);
218 if (this.options.lineNumbers) applyLineNumbers(this.frame);
219 if (this.options.reindentOnLoad) this.reindent();
222 getCode: function() {return this.editor.getCode();},
223 setCode: function(code) {this.editor.importCode(code);},
224 selection: function() {return this.editor.selectedText();},
225 reindent: function() {this.editor.reindent();},
226 reindentSelection: function() {this.editor.reindentSelection(null);},
230 if (this.editor.selectionSnapshot) // IE hack
231 this.win.select.selectCoords(this.win, this.editor.selectionSnapshot);
233 replaceSelection: function(text) {
235 this.editor.replaceSelection(text);
238 replaceChars: function(text, start, end) {
239 this.editor.replaceChars(text, start, end);
241 getSearchCursor: function(string, fromCursor) {
242 return this.editor.getSearchCursor(string, fromCursor);
245 undo: function() {this.editor.history.undo();},
246 redo: function() {this.editor.history.redo();},
247 historySize: function() {return this.editor.history.historySize();},
248 clearHistory: function() {this.editor.history.clear();},
250 grabKeys: function(callback, filter) {this.editor.grabKeys(callback, filter);},
251 ungrabKeys: function() {this.editor.ungrabKeys();},
253 setParser: function(name) {this.editor.setParser(name);},
255 cursorPosition: function(start) {
256 if (this.win.select.ie_selection) this.focus();
257 return this.editor.cursorPosition(start);
259 firstLine: function() {return this.editor.firstLine();},
260 lastLine: function() {return this.editor.lastLine();},
261 nextLine: function(line) {return this.editor.nextLine(line);},
262 prevLine: function(line) {return this.editor.prevLine(line);},
263 lineContent: function(line) {return this.editor.lineContent(line);},
264 setLineContent: function(line, content) {this.editor.setLineContent(line, content);},
265 insertIntoLine: function(line, position, content) {this.editor.insertIntoLine(line, position, content);},
266 selectLines: function(startLine, startOffset, endLine, endOffset) {
268 this.editor.selectLines(startLine, startOffset, endLine, endOffset);
270 nthLine: function(n) {
271 var line = this.firstLine();
272 for (; n > 1 && line !== false; n--)
273 line = this.nextLine(line);
276 lineNumber: function(line) {
278 while (line !== false) {
280 line = this.prevLine(line);
285 // Old number-based line interface
286 jumpToLine: function(n) {
287 this.selectLines(this.nthLine(n), 0);
290 currentLine: function() {
291 return this.lineNumber(this.cursorPosition().line);
295 CodeMirror.InvalidLineHandle = {toString: function(){return "CodeMirror.InvalidLineHandle";}};
297 CodeMirror.replace = function(element) {
298 if (typeof element == "string")
299 element = document.getElementById(element);
300 return function(newElement) {
301 element.parentNode.replaceChild(newElement, element);
305 CodeMirror.fromTextArea = function(area, options) {
306 if (typeof area == "string")
307 area = document.getElementById(area);
309 options = options || {};
310 if (area.style.width && options.width == null)
311 options.width = area.style.width;
312 if (area.style.height && options.height == null)
313 options.height = area.style.height;
314 if (options.content == null) options.content = area.value;
317 function updateField() {
318 area.value = mirror.getCode();
320 if (typeof area.form.addEventListener == "function")
321 area.form.addEventListener("submit", updateField, false);
323 area.form.attachEvent("onsubmit", updateField);
326 function insert(frame) {
327 if (area.nextSibling)
328 area.parentNode.insertBefore(frame, area.nextSibling);
330 area.parentNode.appendChild(frame);
333 area.style.display = "none";
334 var mirror = new CodeMirror(insert, options);
338 CodeMirror.isProbablySupported = function() {
339 // This is rather awful, but can be useful.
342 return Number(window.opera.version()) >= 9.52;
343 else if (/Apple Computers, Inc/.test(navigator.vendor) && (match = navigator.userAgent.match(/Version\/(\d+(?:\.\d+)?)\./)))
344 return Number(match[1]) >= 3;
345 else if (document.selection && window.ActiveXObject && (match = navigator.userAgent.match(/MSIE (\d+(?:\.\d*)?)\b/)))
346 return Number(match[1]) >= 6;
347 else if (match = navigator.userAgent.match(/gecko\/(\d{8})/i))
348 return Number(match[1]) >= 20050901;
349 else if (match = navigator.userAgent.match(/AppleWebKit\/(\d+)/))
350 return Number(match[1]) >= 525;