2
0

index.html 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>CodeMirror 2: JavaScript mode</title>
  5. <link rel="stylesheet" href="../../lib/codemirror.css">
  6. <script src="../../lib/codemirror.js"></script>
  7. <script src="javascript.js"></script>
  8. <link rel="stylesheet" href="../../theme/default.css">
  9. <link rel="stylesheet" href="../../css/docs.css">
  10. <style type="text/css">.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}</style>
  11. </head>
  12. <body>
  13. <h1>CodeMirror 2: JavaScript mode</h1>
  14. <div><textarea id="code" name="code">
  15. // Demo code (the actual new parser character stream implementation)
  16. function StringStream(string) {
  17. this.pos = 0;
  18. this.string = string;
  19. }
  20. StringStream.prototype = {
  21. done: function() {return this.pos >= this.string.length;},
  22. peek: function() {return this.string.charAt(this.pos);},
  23. next: function() {
  24. if (this.pos &lt; this.string.length)
  25. return this.string.charAt(this.pos++);
  26. },
  27. eat: function(match) {
  28. var ch = this.string.charAt(this.pos);
  29. if (typeof match == "string") var ok = ch == match;
  30. else var ok = ch &amp;&amp; match.test ? match.test(ch) : match(ch);
  31. if (ok) {this.pos++; return ch;}
  32. },
  33. eatWhile: function(match) {
  34. var start = this.pos;
  35. while (this.eat(match));
  36. if (this.pos > start) return this.string.slice(start, this.pos);
  37. },
  38. backUp: function(n) {this.pos -= n;},
  39. column: function() {return this.pos;},
  40. eatSpace: function() {
  41. var start = this.pos;
  42. while (/\s/.test(this.string.charAt(this.pos))) this.pos++;
  43. return this.pos - start;
  44. },
  45. match: function(pattern, consume, caseInsensitive) {
  46. if (typeof pattern == "string") {
  47. function cased(str) {return caseInsensitive ? str.toLowerCase() : str;}
  48. if (cased(this.string).indexOf(cased(pattern), this.pos) == this.pos) {
  49. if (consume !== false) this.pos += str.length;
  50. return true;
  51. }
  52. }
  53. else {
  54. var match = this.string.slice(this.pos).match(pattern);
  55. if (match &amp;&amp; consume !== false) this.pos += match[0].length;
  56. return match;
  57. }
  58. }
  59. };
  60. </textarea></div>
  61. <script>
  62. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
  63. lineNumbers: true,
  64. matchBrackets: true
  65. });
  66. </script>
  67. <p>JavaScript mode supports a single configuration
  68. option, <code>json</code>, which will set the mode to expect JSON
  69. data rather than a JavaScript program.</p>
  70. <p><strong>MIME types defined:</strong> <code>text/javascript</code>, <code>application/json</code>.</p>
  71. </body>
  72. </html>