5 <title>CodeMirror: JavaScript mode</title>
6 <link rel="stylesheet" href="../../lib/codemirror.css">
7 <script src="../../lib/codemirror.js"></script>
8 <script src="../../addon/edit/matchbrackets.js"></script>
9 <script src="../../addon/edit/continuecomment.js"></script>
10 <script src="javascript.js"></script>
11 <link rel="stylesheet" href="../../doc/docs.css">
12 <style type="text/css">.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}</style>
15 <h1>CodeMirror: JavaScript mode</h1>
17 <div><textarea id="code" name="code">
18 // Demo code (the actual new parser character stream implementation)
20 function StringStream(string) {
25 StringStream.prototype = {
26 done: function() {return this.pos >= this.string.length;},
27 peek: function() {return this.string.charAt(this.pos);},
29 if (this.pos < this.string.length)
30 return this.string.charAt(this.pos++);
32 eat: function(match) {
33 var ch = this.string.charAt(this.pos);
34 if (typeof match == "string") var ok = ch == match;
35 else var ok = ch && match.test ? match.test(ch) : match(ch);
36 if (ok) {this.pos++; return ch;}
38 eatWhile: function(match) {
40 while (this.eat(match));
41 if (this.pos > start) return this.string.slice(start, this.pos);
43 backUp: function(n) {this.pos -= n;},
44 column: function() {return this.pos;},
45 eatSpace: function() {
47 while (/\s/.test(this.string.charAt(this.pos))) this.pos++;
48 return this.pos - start;
50 match: function(pattern, consume, caseInsensitive) {
51 if (typeof pattern == "string") {
52 function cased(str) {return caseInsensitive ? str.toLowerCase() : str;}
53 if (cased(this.string).indexOf(cased(pattern), this.pos) == this.pos) {
54 if (consume !== false) this.pos += str.length;
59 var match = this.string.slice(this.pos).match(pattern);
60 if (match && consume !== false) this.pos += match[0].length;
68 var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
71 extraKeys: {"Enter": "newlineAndIndentContinueComment"}
76 JavaScript mode supports a two configuration
79 <li><code>json</code> which will set the mode to expect JSON data rather than a JavaScript program.</li>
81 <code>typescript</code> which will activate additional syntax highlighting and some other things for TypeScript code (<a href="typescript.html">demo</a>).
86 <p><strong>MIME types defined:</strong> <code>text/javascript</code>, <code>application/json</code>, <code>text/typescript</code>, <code>application/typescript</code>.</p>