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';
160 content_wrapper.style.width = options.width;
161 content_wrapper.style.height = options.height;
163 iframe_container.style.position = 'absolute';
164 iframe_container.style.top = '0px';
165 iframe_container.style.right = '0px';
166 iframe_container.style.bottom = '0px';
167 iframe_container.style.left = '0px';
169 if (options.lineNumbers) {
170 iframe_container.style.left = '28px';
172 var nums = document.createElement("DIV"),
173 scroller = document.createElement("DIV");
175 nums.style.position = "absolute";
176 nums.style.height = "100%";
178 nums.style.top = "0px";
179 nums.style.left = "0px";
180 nums.style.overflow = "hidden";
182 scroller.className = "CodeMirror-line-numbers";
183 nums.appendChild(scroller);
184 content_wrapper.appendChild(nums);
186 iframe_container.style.right = nums.width;
189 place(content_wrapper);
191 // Link back to this object, so that the editor can fetch options
192 // and add a reference to itself.
193 frame.CodeMirror = this;
194 this.win = frame.contentWindow;
196 if (typeof options.parserfile == "string")
197 options.parserfile = [options.parserfile];
198 if (typeof options.stylesheet == "string")
199 options.stylesheet = [options.stylesheet];
201 var html = ["<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"><html><head>"];
202 // Hack to work around a bunch of IE8-specific problems.
203 html.push("<meta http-equiv=\"X-UA-Compatible\" content=\"IE=EmulateIE7\"/>");
204 forEach(options.stylesheet, function(file) {
205 html.push("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + file + "\"/>");
207 forEach(options.basefiles.concat(options.parserfile), function(file) {
208 html.push("<script type=\"text/javascript\" src=\"" + options.path + file + "\"></script>");
210 html.push("</head><body style=\"border-width: 0;\" class=\"editbox\" spellcheck=\"" +
211 (options.disableSpellcheck ? "false" : "true") + "\"></body></html>");
213 var doc = this.win.document;
215 doc.write(html.join(""));
219 CodeMirror.prototype = {
221 if (this.options.initCallback) this.options.initCallback(this);
222 if (this.options.lineNumbers) applyLineNumbers(this.frame);
223 if (this.options.reindentOnLoad) this.reindent();
226 getCode: function() {return this.editor.getCode();},
227 setCode: function(code) {this.editor.importCode(code);},
228 selection: function() {return this.editor.selectedText();},
229 reindent: function() {this.editor.reindent();},
230 reindentSelection: function() {this.editor.reindentSelection(null);},
234 if (this.editor.selectionSnapshot) // IE hack
235 this.win.select.selectCoords(this.win, this.editor.selectionSnapshot);
237 replaceSelection: function(text) {
239 this.editor.replaceSelection(text);
242 replaceChars: function(text, start, end) {
243 this.editor.replaceChars(text, start, end);
245 getSearchCursor: function(string, fromCursor) {
246 return this.editor.getSearchCursor(string, fromCursor);
249 undo: function() {this.editor.history.undo();},
250 redo: function() {this.editor.history.redo();},
251 historySize: function() {return this.editor.history.historySize();},
252 clearHistory: function() {this.editor.history.clear();},
254 grabKeys: function(callback, filter) {this.editor.grabKeys(callback, filter);},
255 ungrabKeys: function() {this.editor.ungrabKeys();},
257 setParser: function(name) {this.editor.setParser(name);},
259 cursorPosition: function(start) {
260 if (this.win.select.ie_selection) this.focus();
261 return this.editor.cursorPosition(start);
263 firstLine: function() {return this.editor.firstLine();},
264 lastLine: function() {return this.editor.lastLine();},
265 nextLine: function(line) {return this.editor.nextLine(line);},
266 prevLine: function(line) {return this.editor.prevLine(line);},
267 lineContent: function(line) {return this.editor.lineContent(line);},
268 setLineContent: function(line, content) {this.editor.setLineContent(line, content);},
269 insertIntoLine: function(line, position, content) {this.editor.insertIntoLine(line, position, content);},
270 selectLines: function(startLine, startOffset, endLine, endOffset) {
272 this.editor.selectLines(startLine, startOffset, endLine, endOffset);
274 nthLine: function(n) {
275 var line = this.firstLine();
276 for (; n > 1 && line !== false; n--)
277 line = this.nextLine(line);
280 lineNumber: function(line) {
282 while (line !== false) {
284 line = this.prevLine(line);
289 // Old number-based line interface
290 jumpToLine: function(n) {
291 this.selectLines(this.nthLine(n), 0);
294 currentLine: function() {
295 return this.lineNumber(this.cursorPosition().line);
299 CodeMirror.InvalidLineHandle = {toString: function(){return "CodeMirror.InvalidLineHandle";}};
301 CodeMirror.replace = function(element) {
302 if (typeof element == "string")
303 element = document.getElementById(element);
304 return function(newElement) {
305 element.parentNode.replaceChild(newElement, element);
309 CodeMirror.fromTextArea = function(area, options) {
310 if (typeof area == "string")
311 area = document.getElementById(area);
313 options = options || {};
314 if (area.style.width && options.width == null)
315 options.width = area.style.width;
316 if (area.style.height && options.height == null)
317 options.height = area.style.height;
318 if (options.content == null) options.content = area.value;
321 function updateField() {
322 area.value = mirror.getCode();
324 if (typeof area.form.addEventListener == "function")
325 area.form.addEventListener("submit", updateField, false);
327 area.form.attachEvent("onsubmit", updateField);
330 function insert(frame) {
331 if (area.nextSibling)
332 area.parentNode.insertBefore(frame, area.nextSibling);
334 area.parentNode.appendChild(frame);
337 area.style.display = "none";
338 var mirror = new CodeMirror(insert, options);
342 CodeMirror.isProbablySupported = function() {
343 // This is rather awful, but can be useful.
346 return Number(window.opera.version()) >= 9.52;
347 else if (/Apple Computers, Inc/.test(navigator.vendor) && (match = navigator.userAgent.match(/Version\/(\d+(?:\.\d+)?)\./)))
348 return Number(match[1]) >= 3;
349 else if (document.selection && window.ActiveXObject && (match = navigator.userAgent.match(/MSIE (\d+(?:\.\d*)?)\b/)))
350 return Number(match[1]) >= 6;
351 else if (match = navigator.userAgent.match(/gecko\/(\d{8})/i))
352 return Number(match[1]) >= 20050901;
353 else if (match = navigator.userAgent.match(/AppleWebKit\/(\d+)/))
354 return Number(match[1]) >= 525;