Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / cm / headlesscodemirror.js
1 // Content of the function is equal to runmode-standalone.js file
2 // from CodeMirror distribution
3 (function(window) {
4 window.CodeMirror = {};
5
6 (function() {
7 "use strict";
8
9 function splitLines(string){ return string.split(/\r?\n|\r/); };
10
11 function StringStream(string) {
12   this.pos = this.start = 0;
13   this.string = string;
14   this.lineStart = 0;
15 }
16 StringStream.prototype = {
17   eol: function() {return this.pos >= this.string.length;},
18   sol: function() {return this.pos == 0;},
19   peek: function() {return this.string.charAt(this.pos) || null;},
20   next: function() {
21     if (this.pos < this.string.length)
22       return this.string.charAt(this.pos++);
23   },
24   eat: function(match) {
25     var ch = this.string.charAt(this.pos);
26     if (typeof match == "string") var ok = ch == match;
27     else var ok = ch && (match.test ? match.test(ch) : match(ch));
28     if (ok) {++this.pos; return ch;}
29   },
30   eatWhile: function(match) {
31     var start = this.pos;
32     while (this.eat(match)){}
33     return this.pos > start;
34   },
35   eatSpace: function() {
36     var start = this.pos;
37     while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;
38     return this.pos > start;
39   },
40   skipToEnd: function() {this.pos = this.string.length;},
41   skipTo: function(ch) {
42     var found = this.string.indexOf(ch, this.pos);
43     if (found > -1) {this.pos = found; return true;}
44   },
45   backUp: function(n) {this.pos -= n;},
46   column: function() {return this.start - this.lineStart;},
47   indentation: function() {return 0;},
48   match: function(pattern, consume, caseInsensitive) {
49     if (typeof pattern == "string") {
50       var cased = function(str) {return caseInsensitive ? str.toLowerCase() : str;};
51       var substr = this.string.substr(this.pos, pattern.length);
52       if (cased(substr) == cased(pattern)) {
53         if (consume !== false) this.pos += pattern.length;
54         return true;
55       }
56     } else {
57       var match = this.string.slice(this.pos).match(pattern);
58       if (match && match.index > 0) return null;
59       if (match && consume !== false) this.pos += match[0].length;
60       return match;
61     }
62   },
63   current: function(){return this.string.slice(this.start, this.pos);},
64   hideFirstChars: function(n, inner) {
65     this.lineStart += n;
66     try { return inner(); }
67     finally { this.lineStart -= n; }
68   }
69 };
70 CodeMirror.StringStream = StringStream;
71
72 CodeMirror.startState = function (mode, a1, a2) {
73   return mode.startState ? mode.startState(a1, a2) : true;
74 };
75
76 var modes = CodeMirror.modes = {}, mimeModes = CodeMirror.mimeModes = {};
77 CodeMirror.defineMode = function (name, mode) { modes[name] = mode; };
78 CodeMirror.defineMIME = function (mime, spec) { mimeModes[mime] = spec; };
79 CodeMirror.resolveMode = function(spec) {
80   if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) {
81     spec = mimeModes[spec];
82   } else if (spec && typeof spec.name == "string" && mimeModes.hasOwnProperty(spec.name)) {
83     spec = mimeModes[spec.name];
84   }
85   if (typeof spec == "string") return {name: spec};
86   else return spec || {name: "null"};
87 };
88 CodeMirror.getMode = function (options, spec) {
89   spec = CodeMirror.resolveMode(spec);
90   var mfactory = modes[spec.name];
91   if (!mfactory) throw new Error("Unknown mode: " + spec);
92   return mfactory(options, spec);
93 };
94 CodeMirror.registerHelper = CodeMirror.registerGlobalHelper = Math.min;
95 CodeMirror.defineMode("null", function() {
96   return {token: function(stream) {stream.skipToEnd();}};
97 });
98 CodeMirror.defineMIME("text/plain", "null");
99
100 CodeMirror.runMode = function (string, modespec, callback, options) {
101   var mode = CodeMirror.getMode({ indentUnit: 2 }, modespec);
102
103   if (callback.nodeType == 1) {
104     var tabSize = (options && options.tabSize) || 4;
105     var node = callback, col = 0;
106     node.innerHTML = "";
107     callback = function (text, style) {
108       if (text == "\n") {
109         node.appendChild(document.createElement("br"));
110         col = 0;
111         return;
112       }
113       var content = "";
114       // replace tabs
115       for (var pos = 0; ;) {
116         var idx = text.indexOf("\t", pos);
117         if (idx == -1) {
118           content += text.slice(pos);
119           col += text.length - pos;
120           break;
121         } else {
122           col += idx - pos;
123           content += text.slice(pos, idx);
124           var size = tabSize - col % tabSize;
125           col += size;
126           for (var i = 0; i < size; ++i) content += " ";
127           pos = idx + 1;
128         }
129       }
130
131       if (style) {
132         var sp = node.appendChild(document.createElement("span"));
133         sp.className = "cm-" + style.replace(/ +/g, " cm-");
134         sp.appendChild(document.createTextNode(content));
135       } else {
136         node.appendChild(document.createTextNode(content));
137       }
138     };
139   }
140
141   var lines = splitLines(string), state = (options && options.state) || CodeMirror.startState(mode);
142   for (var i = 0, e = lines.length; i < e; ++i) {
143     if (i) callback("\n");
144     var stream = new CodeMirror.StringStream(lines[i]);
145     while (!stream.eol()) {
146       var style = mode.token(stream, state);
147       callback(stream.current(), style, i, stream.start, state);
148       stream.start = stream.pos;
149     }
150   }
151 };
152 })();
153
154 }(this))