3 <title>CodeMirror: JavaScript mode
</title>
4 <meta charset=
"utf-8"/>
5 <link rel=stylesheet
href=
"../../doc/docs.css">
7 <link rel=
"stylesheet" href=
"../../lib/codemirror.css">
8 <script src=
"../../lib/codemirror.js"></script>
9 <script src=
"../../addon/edit/matchbrackets.js"></script>
10 <script src=
"../../addon/comment/continuecomment.js"></script>
11 <script src=
"../../addon/comment/comment.js"></script>
12 <script src=
"javascript.js"></script>
13 <style type=
"text/css">.CodeMirror {border-top:
1px solid black; border-bottom:
1px solid black;}
</style>
15 <a href=
"http://codemirror.net"><img id=logo
src=
"../../doc/logo.png"></a>
18 <li><a href=
"../../index.html">Home
</a>
19 <li><a href=
"../../doc/manual.html">Manual
</a>
20 <li><a href=
"https://github.com/marijnh/codemirror">Code
</a>
23 <li><a href=
"../index.html">Language modes
</a>
24 <li><a class=active
href=
"#">JavaScript
</a>
29 <h2>JavaScript mode
</h2>
32 <div><textarea id=
"code" name=
"code">
33 // Demo code (the actual new parser character stream implementation)
35 function StringStream(string) {
40 StringStream.prototype = {
41 done: function() {return this.pos
>= this.string.length;},
42 peek: function() {return this.string.charAt(this.pos);},
44 if (this.pos
< this.string.length)
45 return this.string.charAt(this.pos++);
47 eat: function(match) {
48 var ch = this.string.charAt(this.pos);
49 if (typeof match ==
"string") var ok = ch == match;
50 else var ok = ch
&& match.test ? match.test(ch) : match(ch);
51 if (ok) {this.pos++; return ch;}
53 eatWhile: function(match) {
55 while (this.eat(match));
56 if (this.pos
> start) return this.string.slice(start, this.pos);
58 backUp: function(n) {this.pos -= n;},
59 column: function() {return this.pos;},
60 eatSpace: function() {
62 while (/\s/.test(this.string.charAt(this.pos))) this.pos++;
63 return this.pos - start;
65 match: function(pattern, consume, caseInsensitive) {
66 if (typeof pattern ==
"string") {
67 function cased(str) {return caseInsensitive ? str.toLowerCase() : str;}
68 if (cased(this.string).indexOf(cased(pattern), this.pos) == this.pos) {
69 if (consume !== false) this.pos += str.length;
74 var match = this.string.slice(this.pos).match(pattern);
75 if (match
&& consume !== false) this.pos += match[
0].length;
83 var editor = CodeMirror.fromTextArea(document.getElementById(
"code"), {
86 continueComments:
"Enter",
87 extraKeys: {
"Ctrl-Q":
"toggleComment"}
92 JavaScript mode supports several configuration options:
94 <li><code>json
</code> which will set the mode to expect JSON
95 data rather than a JavaScript program.
</li>
96 <li><code>jsonld
</code> which will set the mode to expect
97 <a href=
"http://json-ld.org">JSON-LD
</a> linked data rather
98 than a JavaScript program (
<a href=
"json-ld.html">demo
</a>).
</li>
99 <li><code>typescript
</code> which will activate additional
100 syntax highlighting and some other things for TypeScript code
101 (
<a href=
"typescript.html">demo
</a>).
</li>
102 <li><code>statementIndent
</code> which (given a number) will
103 determine the amount of indentation to use for statements
104 continued on a new line.
</li>
108 <p><strong>MIME types defined:
</strong> <code>text/javascript
</code>,
<code>application/json
</code>,
<code>application/ld+json
</code>,
<code>text/typescript
</code>,
<code>application/typescript
</code>.
</p>