Web Inspector: speedup highlight regex API in DefaultTextEditor
[WebKit-https.git] / Source / WebCore / inspector / front-end / DefaultTextEditor.js
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  * Copyright (C) 2010 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 /**
33  * @constructor
34  * @extends {WebInspector.View}
35  * @implements {WebInspector.TextEditor}
36  * @param {?string} url
37  * @param {WebInspector.TextEditorDelegate} delegate
38  */
39 WebInspector.DefaultTextEditor = function(url, delegate)
40 {
41     WebInspector.View.call(this);
42     this._delegate = delegate;
43     this._url = url;
44
45     this.registerRequiredCSS("textEditor.css");
46
47     this.element.className = "text-editor monospace";
48
49     // Prevent middle-click pasting in the editor unless it is explicitly enabled for certain component.
50     this.element.addEventListener("mouseup", preventDefaultOnMouseUp.bind(this), false);
51     function preventDefaultOnMouseUp(event)
52     {
53         if (event.button === 1)
54             event.consume(true);
55     }
56
57     this._textModel = new WebInspector.TextEditorModel();
58     this._textModel.addEventListener(WebInspector.TextEditorModel.Events.TextChanged, this._textChanged, this);
59
60     var syncScrollListener = this._syncScroll.bind(this);
61     var syncDecorationsForLineListener = this._syncDecorationsForLine.bind(this);
62     var syncLineHeightListener = this._syncLineHeight.bind(this);
63     this._mainPanel = new WebInspector.TextEditorMainPanel(this._delegate, this._textModel, url, syncScrollListener, syncDecorationsForLineListener);
64     this._gutterPanel = new WebInspector.TextEditorGutterPanel(this._textModel, syncDecorationsForLineListener, syncLineHeightListener);
65
66     this._mainPanel.element.addEventListener("scroll", this._handleScrollChanged.bind(this), false);
67
68     this._gutterPanel.element.addEventListener("mousedown", this._onMouseDown.bind(this), true);
69
70     // Explicitly enable middle-click pasting in the editor main panel.
71     this._mainPanel.element.addEventListener("mouseup", consumeMouseUp.bind(this), false);
72     function consumeMouseUp(event)
73     {
74         if (event.button === 1)
75             event.consume(false);
76     }
77
78     this.element.appendChild(this._mainPanel.element);
79     this.element.appendChild(this._gutterPanel.element);
80
81     // Forward mouse wheel events from the unscrollable gutter to the main panel.
82     function forwardWheelEvent(event)
83     {
84         var clone = document.createEvent("WheelEvent");
85         clone.initWebKitWheelEvent(event.wheelDeltaX, event.wheelDeltaY,
86                                    event.view,
87                                    event.screenX, event.screenY,
88                                    event.clientX, event.clientY,
89                                    event.ctrlKey, event.altKey, event.shiftKey, event.metaKey);
90         this._mainPanel.element.dispatchEvent(clone);
91     }
92     this._gutterPanel.element.addEventListener("mousewheel", forwardWheelEvent.bind(this), false);
93
94     this.element.addEventListener("keydown", this._handleKeyDown.bind(this), false);
95     this.element.addEventListener("contextmenu", this._contextMenu.bind(this), true);
96
97     this._registerShortcuts();
98 }
99
100 /**
101  * @constructor
102  * @param {WebInspector.TextRange} range
103  * @param {string} text
104  */
105 WebInspector.DefaultTextEditor.EditInfo = function(range, text)
106 {
107     this.range = range;
108     this.text = text;
109 }
110
111 WebInspector.DefaultTextEditor.prototype = {
112     /**
113      * @param {string} regex
114      * @param {string} cssClass
115      */
116     highlightRegex: function(regex, cssClass)
117     {
118         this._mainPanel.highlightRegex(regex, cssClass);
119     },
120
121     /**
122      * @param {string} regex
123      * @return {boolean}
124      */
125     removeRegexHighlight: function(regex)
126     {
127         return this._mainPanel.removeRegexHighlight(regex);
128     },
129
130     /**
131      * @param {string} mimeType
132      */
133     set mimeType(mimeType)
134     {
135         this._mainPanel.mimeType = mimeType;
136     },
137
138     /**
139      * @param {boolean} readOnly
140      */
141     setReadOnly: function(readOnly)
142     {
143         if (this._mainPanel.readOnly() === readOnly)
144             return;
145         this._mainPanel.setReadOnly(readOnly, this.isShowing());
146         WebInspector.markBeingEdited(this.element, !readOnly);
147     },
148
149     /**
150      * @return {boolean}
151      */
152     readOnly: function()
153     {
154         return this._mainPanel.readOnly();
155     },
156
157     /**
158      * @return {Element}
159      */
160     defaultFocusedElement: function()
161     {
162         return this._mainPanel.defaultFocusedElement();
163     },
164
165     /**
166      * @param {number} lineNumber
167      */
168     revealLine: function(lineNumber)
169     {
170         this._mainPanel.revealLine(lineNumber);
171     },
172
173     _onMouseDown: function(event)
174     {
175         var target = event.target.enclosingNodeOrSelfWithClass("webkit-line-number");
176         if (!target)
177             return;
178         this.dispatchEventToListeners(WebInspector.TextEditor.Events.GutterClick, { lineNumber: target.lineNumber, event: event });
179     },
180
181     /**
182      * @param {number} lineNumber
183      * @param {boolean} disabled
184      * @param {boolean} conditional
185      */
186     addBreakpoint: function(lineNumber, disabled, conditional)
187     {
188         this.beginUpdates();
189         this._gutterPanel.addDecoration(lineNumber, "webkit-breakpoint");
190         if (disabled)
191             this._gutterPanel.addDecoration(lineNumber, "webkit-breakpoint-disabled");
192         else
193             this._gutterPanel.removeDecoration(lineNumber, "webkit-breakpoint-disabled");
194         if (conditional)
195             this._gutterPanel.addDecoration(lineNumber, "webkit-breakpoint-conditional");
196         else
197             this._gutterPanel.removeDecoration(lineNumber, "webkit-breakpoint-conditional");
198         this.endUpdates();
199     },
200
201     /**
202      * @param {number} lineNumber
203      */
204     removeBreakpoint: function(lineNumber)
205     {
206         this.beginUpdates();
207         this._gutterPanel.removeDecoration(lineNumber, "webkit-breakpoint");
208         this._gutterPanel.removeDecoration(lineNumber, "webkit-breakpoint-disabled");
209         this._gutterPanel.removeDecoration(lineNumber, "webkit-breakpoint-conditional");
210         this.endUpdates();
211     },
212
213     /**
214      * @param {number} lineNumber
215      */
216     setExecutionLine: function(lineNumber)
217     {
218         this._executionLineNumber = lineNumber;
219         this._mainPanel.addDecoration(lineNumber, "webkit-execution-line");
220         this._gutterPanel.addDecoration(lineNumber, "webkit-execution-line");
221     },
222
223     clearExecutionLine: function()
224     {
225         if (typeof this._executionLineNumber === "number") {
226             this._mainPanel.removeDecoration(this._executionLineNumber, "webkit-execution-line");
227             this._gutterPanel.removeDecoration(this._executionLineNumber, "webkit-execution-line");
228         }
229         delete this._executionLineNumber;
230     },
231
232     /**
233      * @param {number} lineNumber
234      * @param {Element} element
235      */
236     addDecoration: function(lineNumber, element)
237     {
238         this._mainPanel.addDecoration(lineNumber, element);
239         this._gutterPanel.addDecoration(lineNumber, element);
240         this._syncDecorationsForLine(lineNumber);
241     },
242
243     /**
244      * @param {number} lineNumber
245      * @param {Element} element
246      */
247     removeDecoration: function(lineNumber, element)
248     {
249         this._mainPanel.removeDecoration(lineNumber, element);
250         this._gutterPanel.removeDecoration(lineNumber, element);
251         this._syncDecorationsForLine(lineNumber);
252     },
253
254     /**
255      * @param {WebInspector.TextRange} range
256      */
257     markAndRevealRange: function(range)
258     {
259         if (range)
260             this.setSelection(range);
261         this._mainPanel.markAndRevealRange(range);
262     },
263
264     /**
265      * @param {number} lineNumber
266      */
267     highlightLine: function(lineNumber)
268     {
269         if (typeof lineNumber !== "number" || lineNumber < 0)
270             return;
271
272         lineNumber = Math.min(lineNumber, this._textModel.linesCount - 1);
273         this._mainPanel.highlightLine(lineNumber);
274     },
275
276     clearLineHighlight: function()
277     {
278         this._mainPanel.clearLineHighlight();
279     },
280
281     /**
282      * @return {Array.<Element>}
283      */
284     elementsToRestoreScrollPositionsFor: function()
285     {
286         return [this._mainPanel.element];
287     },
288
289     /**
290      * @param {WebInspector.TextEditor} textEditor
291      */
292     inheritScrollPositions: function(textEditor)
293     {
294         this._mainPanel.element._scrollTop = textEditor._mainPanel.element.scrollTop;
295         this._mainPanel.element._scrollLeft = textEditor._mainPanel.element.scrollLeft;
296     },
297
298     beginUpdates: function()
299     {
300         this._mainPanel.beginUpdates();
301         this._gutterPanel.beginUpdates();
302     },
303
304     endUpdates: function()
305     {
306         this._mainPanel.endUpdates();
307         this._gutterPanel.endUpdates();
308         this._updatePanelOffsets();
309     },
310
311     onResize: function()
312     {
313         this._mainPanel.resize();
314         this._gutterPanel.resize();
315         this._updatePanelOffsets();
316     },
317
318     _textChanged: function(event)
319     {
320         this._mainPanel.textChanged(event.data.oldRange, event.data.newRange);
321         this._gutterPanel.textChanged(event.data.oldRange, event.data.newRange);
322         this._updatePanelOffsets();
323         if (event.data.editRange)
324             this._delegate.onTextChanged(event.data.oldRange, event.data.newRange);
325     },
326
327     /**
328      * @param {WebInspector.TextRange} range
329      * @param {string} text
330      * @return {WebInspector.TextRange}
331      */
332     editRange: function(range, text)
333     {
334         return this._textModel.editRange(range, text);
335     },
336
337     _updatePanelOffsets: function()
338     {
339         var lineNumbersWidth = this._gutterPanel.element.offsetWidth;
340         if (lineNumbersWidth)
341             this._mainPanel.element.style.setProperty("left", (lineNumbersWidth + 2) + "px");
342         else
343             this._mainPanel.element.style.removeProperty("left"); // Use default value set in CSS.
344     },
345
346     _syncScroll: function()
347     {
348         var mainElement = this._mainPanel.element;
349         var gutterElement = this._gutterPanel.element;
350         // Handle horizontal scroll bar at the bottom of the main panel.
351         this._gutterPanel.syncClientHeight(mainElement.clientHeight);
352         gutterElement.scrollTop = mainElement.scrollTop;
353     },
354
355     /**
356      * @param {number} lineNumber
357      */
358     _syncDecorationsForLine: function(lineNumber)
359     {
360         if (lineNumber >= this._textModel.linesCount)
361             return;
362
363         var mainChunk = this._mainPanel.chunkForLine(lineNumber);
364         if (mainChunk.linesCount === 1 && mainChunk.isDecorated()) {
365             var gutterChunk = this._gutterPanel.makeLineAChunk(lineNumber);
366             var height = mainChunk.height;
367             if (height)
368                 gutterChunk.element.style.setProperty("height", height + "px");
369             else
370                 gutterChunk.element.style.removeProperty("height");
371         } else {
372             var gutterChunk = this._gutterPanel.chunkForLine(lineNumber);
373             if (gutterChunk.linesCount === 1)
374                 gutterChunk.element.style.removeProperty("height");
375         }
376     },
377
378     /**
379      * @param {Element} gutterRow
380      */
381     _syncLineHeight: function(gutterRow)
382     {
383         if (this._lineHeightSynced)
384             return;
385         if (gutterRow && gutterRow.offsetHeight) {
386             // Force equal line heights for the child panels.
387             this.element.style.setProperty("line-height", gutterRow.offsetHeight + "px");
388             this._lineHeightSynced = true;
389         }
390     },
391
392     _registerShortcuts: function()
393     {
394         var keys = WebInspector.KeyboardShortcut.Keys;
395         var modifiers = WebInspector.KeyboardShortcut.Modifiers;
396
397         this._shortcuts = {};
398
399         var handleEnterKey = this._mainPanel.handleEnterKey.bind(this._mainPanel);
400         this._shortcuts[WebInspector.KeyboardShortcut.makeKey(keys.Enter.code, WebInspector.KeyboardShortcut.Modifiers.None)] = handleEnterKey;
401
402         this._shortcuts[WebInspector.KeyboardShortcut.makeKey("z", modifiers.CtrlOrMeta)] = this._mainPanel.handleUndoRedo.bind(this._mainPanel, false);
403         this._shortcuts[WebInspector.KeyboardShortcut.SelectAll] = this._handleSelectAll.bind(this);
404
405         var handleRedo = this._mainPanel.handleUndoRedo.bind(this._mainPanel, true);
406         this._shortcuts[WebInspector.KeyboardShortcut.makeKey("z", modifiers.Shift | modifiers.CtrlOrMeta)] = handleRedo;
407         if (!WebInspector.isMac())
408             this._shortcuts[WebInspector.KeyboardShortcut.makeKey("y", modifiers.CtrlOrMeta)] = handleRedo;
409
410         var handleTabKey = this._mainPanel.handleTabKeyPress.bind(this._mainPanel, false);
411         var handleShiftTabKey = this._mainPanel.handleTabKeyPress.bind(this._mainPanel, true);
412         this._shortcuts[WebInspector.KeyboardShortcut.makeKey(keys.Tab.code)] = handleTabKey;
413         this._shortcuts[WebInspector.KeyboardShortcut.makeKey(keys.Tab.code, modifiers.Shift)] = handleShiftTabKey;
414     },
415
416     _handleSelectAll: function()
417     {
418         this.setSelection(this._textModel.range());
419         return true;
420     },
421
422     _handleKeyDown: function(e)
423     {
424         // If the event was not triggered from the entire editor, then
425         // ignore it. https://bugs.webkit.org/show_bug.cgi?id=102906
426         if (e.target.enclosingNodeOrSelfWithClass("webkit-line-decorations"))
427             return;
428
429         var shortcutKey = WebInspector.KeyboardShortcut.makeKeyFromEvent(e);
430
431         var handler = this._shortcuts[shortcutKey];
432         if (handler && handler()) {
433             e.consume(true);
434             return;
435         }
436         this._mainPanel.handleKeyDown(e);
437     },
438
439     _contextMenu: function(event)
440     {
441         var anchor = event.target.enclosingNodeOrSelfWithNodeName("a");
442         if (anchor)
443             return;
444         var contextMenu = new WebInspector.ContextMenu(event);
445         var target = event.target.enclosingNodeOrSelfWithClass("webkit-line-number");
446         if (target)
447             this._delegate.populateLineGutterContextMenu(contextMenu, target.lineNumber);
448         else {
449             this._mainPanel.populateContextMenu(event.target, contextMenu);
450         }
451         contextMenu.show();
452     },
453
454     _handleScrollChanged: function(event)
455     {
456         var visibleFrom = this._mainPanel.scrollTop();
457         var firstVisibleLineNumber = this._mainPanel.lineNumberAtOffset(visibleFrom);
458         this._delegate.scrollChanged(firstVisibleLineNumber);
459     },
460
461     /**
462      * @param {number} lineNumber
463      */
464     scrollToLine: function(lineNumber)
465     {
466         this._mainPanel.scrollToLine(lineNumber);
467     },
468
469     /**
470      * @return {WebInspector.TextRange}
471      */
472     selection: function(textRange)
473     {
474         return this._mainPanel.selection();
475     },
476
477     /**
478      * @return {WebInspector.TextRange?}
479      */
480     lastSelection: function()
481     {
482         return this._mainPanel.lastSelection();
483     },
484
485     /**
486      * @param {WebInspector.TextRange} textRange
487      */
488     setSelection: function(textRange)
489     {
490         this._mainPanel.setSelection(textRange);
491     },
492
493     /**
494      * @param {string} text
495      */
496     setText: function(text)
497     {
498         this._textModel.setText(text);
499     },
500
501     /**
502      * @return {string}
503      */
504     text: function()
505     {
506         return this._textModel.text();
507     },
508
509     /**
510      * @return {WebInspector.TextRange}
511      */
512     range: function()
513     {
514         return this._textModel.range();
515     },
516
517     /**
518      * @param {number} lineNumber
519      * @return {string}
520      */
521     line: function(lineNumber)
522     {
523         return this._textModel.line(lineNumber);
524     },
525
526     /**
527      * @return {number}
528      */
529     get linesCount()
530     {
531         return this._textModel.linesCount;
532     },
533
534     /**
535      * @param {number} line
536      * @param {string} name
537      * @param {?Object} value
538      */
539     setAttribute: function(line, name, value)
540     {
541         this._textModel.setAttribute(line, name, value);
542     },
543
544     /**
545      * @param {number} line
546      * @param {string} name
547      * @return {?Object} value
548      */
549     getAttribute: function(line, name)
550     {
551         return this._textModel.getAttribute(line, name);
552     },
553
554     /**
555      * @param {number} line
556      * @param {string} name
557      */
558     removeAttribute: function(line, name)
559     {
560         this._textModel.removeAttribute(line, name);
561     },
562
563     wasShown: function()
564     {
565         if (!this.readOnly())
566             WebInspector.markBeingEdited(this.element, true);
567
568         this._mainPanel.wasShown();
569     },
570
571     willHide: function()
572     {
573         this._mainPanel.willHide();
574         this._gutterPanel.willHide();
575
576         if (!this.readOnly())
577             WebInspector.markBeingEdited(this.element, false);
578     },
579
580     /**
581      * @param {Element} element
582      * @param {Array.<Object>} resultRanges
583      * @param {string} styleClass
584      * @param {Array.<Object>=} changes
585      */
586     highlightRangesWithStyleClass: function(element, resultRanges, styleClass, changes)
587     {
588         this._mainPanel.beginDomUpdates();
589         WebInspector.highlightRangesWithStyleClass(element, resultRanges, styleClass, changes);
590         this._mainPanel.endDomUpdates();
591     },
592
593     /**
594      * @param {Element} element
595      * @param {Object} skipClasses
596      * @param {Object} skipTokens
597      * @return {Element}
598      */
599     highlightExpression: function(element, skipClasses, skipTokens)
600     {
601         // Collect tokens belonging to evaluated expression.
602         var tokens = [element];
603         var token = element.previousSibling;
604         while (token && (skipClasses[token.className] || skipTokens[token.textContent.trim()])) {
605             tokens.push(token);
606             token = token.previousSibling;
607         }
608         tokens.reverse();
609
610         // Wrap them with highlight element.
611         this._mainPanel.beginDomUpdates();
612         var parentElement = element.parentElement;
613         var nextElement = element.nextSibling;
614         var container = document.createElement("span");
615         for (var i = 0; i < tokens.length; ++i)
616             container.appendChild(tokens[i]);
617         parentElement.insertBefore(container, nextElement);
618         this._mainPanel.endDomUpdates();
619         return container;
620     },
621
622     /**
623      * @param {Element} highlightElement
624      */
625     hideHighlightedExpression: function(highlightElement)
626     {
627         this._mainPanel.beginDomUpdates();
628         var parentElement = highlightElement.parentElement;
629         if (parentElement) {
630             var child = highlightElement.firstChild;
631             while (child) {
632                 var nextSibling = child.nextSibling;
633                 parentElement.insertBefore(child, highlightElement);
634                 child = nextSibling;
635             }
636             parentElement.removeChild(highlightElement);
637         }
638         this._mainPanel.endDomUpdates();
639     },
640
641     /**
642      * @param {number} scrollTop
643      * @param {number} clientHeight
644      * @param {number} chunkSize
645      */
646     overrideViewportForTest: function(scrollTop, clientHeight, chunkSize)
647     {
648         this._mainPanel.overrideViewportForTest(scrollTop, clientHeight, chunkSize);
649     },
650
651     __proto__: WebInspector.View.prototype
652 }
653
654 /**
655  * @constructor
656  * @param {WebInspector.TextEditorModel} textModel
657  */
658 WebInspector.TextEditorChunkedPanel = function(textModel)
659 {
660     this._textModel = textModel;
661
662     this.element = document.createElement("div");
663     this.element.addEventListener("scroll", this._scroll.bind(this), false);
664
665     this._defaultChunkSize = 50;
666     this._paintCoalescingLevel = 0;
667     this._domUpdateCoalescingLevel = 0;
668 }
669
670 WebInspector.TextEditorChunkedPanel.prototype = {
671     /**
672      * @param {number} lineNumber
673      */
674     scrollToLine: function(lineNumber)
675     {
676         if (lineNumber >= this._textModel.linesCount)
677             return;
678
679         var chunk = this.makeLineAChunk(lineNumber);
680         this.element.scrollTop = chunk.offsetTop;
681     },
682
683     /**
684      * @param {number} lineNumber
685      */
686     revealLine: function(lineNumber)
687     {
688         if (lineNumber >= this._textModel.linesCount)
689             return;
690
691         var chunk = this.makeLineAChunk(lineNumber);
692         chunk.element.scrollIntoViewIfNeeded();
693     },
694
695     /**
696      * @param {number} lineNumber
697      * @param {string|Element} decoration
698      */
699     addDecoration: function(lineNumber, decoration)
700     {
701         if (lineNumber >= this._textModel.linesCount)
702             return;
703
704         var chunk = this.makeLineAChunk(lineNumber);
705         chunk.addDecoration(decoration);
706     },
707
708     /**
709      * @param {number} lineNumber
710      * @param {string|Element} decoration
711      */
712     removeDecoration: function(lineNumber, decoration)
713     {
714         if (lineNumber >= this._textModel.linesCount)
715             return;
716
717         var chunk = this.chunkForLine(lineNumber);
718         chunk.removeDecoration(decoration);
719     },
720
721     buildChunks: function()
722     {
723         this.beginDomUpdates();
724
725         this._container.removeChildren();
726
727         this._textChunks = [];
728         for (var i = 0; i < this._textModel.linesCount; i += this._defaultChunkSize) {
729             var chunk = this.createNewChunk(i, i + this._defaultChunkSize);
730             this._textChunks.push(chunk);
731             this._container.appendChild(chunk.element);
732         }
733
734         this.repaintAll();
735
736         this.endDomUpdates();
737     },
738
739     /**
740      * @param {number} lineNumber
741      * @return {Object}
742      */
743     makeLineAChunk: function(lineNumber)
744     {
745         var chunkNumber = this.chunkNumberForLine(lineNumber);
746         var oldChunk = this._textChunks[chunkNumber];
747
748         if (!oldChunk) {
749             console.error("No chunk for line number: " + lineNumber);
750             return null;
751         }
752
753         if (oldChunk.linesCount === 1)
754             return oldChunk;
755
756         return this.splitChunkOnALine(lineNumber, chunkNumber, true);
757     },
758
759     /**
760      * @param {number} lineNumber
761      * @param {number} chunkNumber
762      * @param {boolean=} createSuffixChunk
763      * @return {Object}
764      */
765     splitChunkOnALine: function(lineNumber, chunkNumber, createSuffixChunk)
766     {
767         this.beginDomUpdates();
768
769         var oldChunk = this._textChunks[chunkNumber];
770         var wasExpanded = oldChunk.expanded();
771         oldChunk.collapse();
772
773         var insertIndex = chunkNumber + 1;
774
775         // Prefix chunk.
776         if (lineNumber > oldChunk.startLine) {
777             var prefixChunk = this.createNewChunk(oldChunk.startLine, lineNumber);
778             this._textChunks.splice(insertIndex++, 0, prefixChunk);
779             this._container.insertBefore(prefixChunk.element, oldChunk.element);
780         }
781
782         // Line chunk.
783         var endLine = createSuffixChunk ? lineNumber + 1 : oldChunk.startLine + oldChunk.linesCount;
784         var lineChunk = this.createNewChunk(lineNumber, endLine);
785         this._textChunks.splice(insertIndex++, 0, lineChunk);
786         this._container.insertBefore(lineChunk.element, oldChunk.element);
787
788         // Suffix chunk.
789         if (oldChunk.startLine + oldChunk.linesCount > endLine) {
790             var suffixChunk = this.createNewChunk(endLine, oldChunk.startLine + oldChunk.linesCount);
791             this._textChunks.splice(insertIndex, 0, suffixChunk);
792             this._container.insertBefore(suffixChunk.element, oldChunk.element);
793         }
794
795         // Remove enclosing chunk.
796         this._textChunks.splice(chunkNumber, 1);
797         this._container.removeChild(oldChunk.element);
798
799         if (wasExpanded) {
800             if (prefixChunk)
801                 prefixChunk.expand();
802             lineChunk.expand();
803             if (suffixChunk)
804                 suffixChunk.expand();
805         }
806
807         this.endDomUpdates();
808
809         return lineChunk;
810     },
811
812     createNewChunk: function(startLine, endLine)
813     {
814         throw new Error("createNewChunk() should be implemented by descendants");
815     },
816
817     _scroll: function()
818     {
819         this._scheduleRepaintAll();
820         if (this._syncScrollListener)
821             this._syncScrollListener();
822     },
823
824     _scheduleRepaintAll: function()
825     {
826         if (this._repaintAllTimer)
827             clearTimeout(this._repaintAllTimer);
828         this._repaintAllTimer = setTimeout(this.repaintAll.bind(this), 50);
829     },
830
831     beginUpdates: function()
832     {
833         this._paintCoalescingLevel++;
834     },
835
836     endUpdates: function()
837     {
838         this._paintCoalescingLevel--;
839         if (!this._paintCoalescingLevel)
840             this.repaintAll();
841     },
842
843     beginDomUpdates: function()
844     {
845         this._domUpdateCoalescingLevel++;
846     },
847
848     endDomUpdates: function()
849     {
850         this._domUpdateCoalescingLevel--;
851     },
852
853     /**
854      * @param {number} lineNumber
855      * @return {number}
856      */
857     chunkNumberForLine: function(lineNumber)
858     {
859         function compareLineNumbers(value, chunk)
860         {
861             return value < chunk.startLine ? -1 : 1;
862         }
863         var insertBefore = insertionIndexForObjectInListSortedByFunction(lineNumber, this._textChunks, compareLineNumbers);
864         return insertBefore - 1;
865     },
866
867     /**
868      * @param {number} lineNumber
869      * @return {Object}
870      */
871     chunkForLine: function(lineNumber)
872     {
873         return this._textChunks[this.chunkNumberForLine(lineNumber)];
874     },
875
876     /**
877      * @param {number} visibleFrom
878      * @return {number}
879      */
880     _findFirstVisibleChunkNumber: function(visibleFrom)
881     {
882         function compareOffsetTops(value, chunk)
883         {
884             return value < chunk.offsetTop ? -1 : 1;
885         }
886         var insertBefore = insertionIndexForObjectInListSortedByFunction(visibleFrom, this._textChunks, compareOffsetTops);
887         return insertBefore - 1;
888     },
889
890     /**
891      * @param {number} visibleFrom
892      * @param {number} visibleTo
893      * @return {{start: number, end: number}}
894      */
895     findVisibleChunks: function(visibleFrom, visibleTo)
896     {
897         var from = this._findFirstVisibleChunkNumber(visibleFrom);
898         for (var to = from + 1; to < this._textChunks.length; ++to) {
899             if (this._textChunks[to].offsetTop >= visibleTo)
900                 break;
901         }
902         return { start: from, end: to };
903     },
904
905     /**
906      * @param {number} visibleFrom
907      * @return {number}
908      */
909     lineNumberAtOffset: function(visibleFrom)
910     {
911         var chunk = this._textChunks[this._findFirstVisibleChunkNumber(visibleFrom)];
912         if (!chunk.expanded())
913             return chunk.startLine;
914
915         var lineNumbers = [];
916         for (var i = 0; i < chunk.linesCount; ++i) {
917             lineNumbers.push(chunk.startLine + i);
918         }
919
920         function compareLineRowOffsetTops(value, lineNumber)
921         {
922             var lineRow = chunk.expandedLineRow(lineNumber);
923             return value < lineRow.offsetTop ? -1 : 1;
924         }
925         var insertBefore = insertionIndexForObjectInListSortedByFunction(visibleFrom, lineNumbers, compareLineRowOffsetTops);
926         return lineNumbers[insertBefore - 1];
927     },
928
929     repaintAll: function()
930     {
931         delete this._repaintAllTimer;
932
933         if (this._paintCoalescingLevel)
934             return;
935
936         var visibleFrom = this.scrollTop();
937         var visibleTo = visibleFrom + this.clientHeight();
938
939         if (visibleTo) {
940             var result = this.findVisibleChunks(visibleFrom, visibleTo);
941             this.expandChunks(result.start, result.end);
942         }
943     },
944
945     scrollTop: function()
946     {
947         return typeof this._scrollTopOverrideForTest === "number" ? this._scrollTopOverrideForTest : this.element.scrollTop;
948     },
949
950     clientHeight: function()
951     {
952         return typeof this._clientHeightOverrideForTest === "number" ? this._clientHeightOverrideForTest : this.element.clientHeight;
953     },
954
955     /**
956      * @param {number} fromIndex
957      * @param {number} toIndex
958      */
959     expandChunks: function(fromIndex, toIndex)
960     {
961         // First collapse chunks to collect the DOM elements into a cache to reuse them later.
962         for (var i = 0; i < fromIndex; ++i)
963             this._textChunks[i].collapse();
964         for (var i = toIndex; i < this._textChunks.length; ++i)
965             this._textChunks[i].collapse();
966         for (var i = fromIndex; i < toIndex; ++i)
967             this._textChunks[i].expand();
968     },
969
970     /**
971      * @param {Element} firstElement
972      * @param {Element=} lastElement
973      * @return {number}
974      */
975     totalHeight: function(firstElement, lastElement)
976     {
977         lastElement = (lastElement || firstElement).nextElementSibling;
978         if (lastElement)
979             return lastElement.offsetTop - firstElement.offsetTop;
980
981         var offsetParent = firstElement.offsetParent;
982         if (offsetParent && offsetParent.scrollHeight > offsetParent.clientHeight)
983             return offsetParent.scrollHeight - firstElement.offsetTop;
984
985         var total = 0;
986         while (firstElement && firstElement !== lastElement) {
987             total += firstElement.offsetHeight;
988             firstElement = firstElement.nextElementSibling;
989         }
990         return total;
991     },
992
993     resize: function()
994     {
995         this.repaintAll();
996     }
997 }
998
999 /**
1000  * @constructor
1001  * @extends {WebInspector.TextEditorChunkedPanel}
1002  * @param {WebInspector.TextEditorModel} textModel
1003  * @param {function(number)} syncDecorationsForLineListener
1004  * @param {function(Element)} syncLineHeightListener
1005  */
1006 WebInspector.TextEditorGutterPanel = function(textModel, syncDecorationsForLineListener, syncLineHeightListener)
1007 {
1008     WebInspector.TextEditorChunkedPanel.call(this, textModel);
1009
1010     this._syncDecorationsForLineListener = syncDecorationsForLineListener;
1011     this._syncLineHeightListener = syncLineHeightListener;
1012
1013     this.element.className = "text-editor-lines";
1014
1015     this._container = document.createElement("div");
1016     this._container.className = "inner-container";
1017     this.element.appendChild(this._container);
1018
1019     this._freeCachedElements();
1020     this.buildChunks();
1021     this._decorations = {};
1022 }
1023
1024 WebInspector.TextEditorGutterPanel.prototype = {
1025     _freeCachedElements: function()
1026     {
1027         this._cachedRows = [];
1028     },
1029
1030     willHide: function()
1031     {
1032         this._freeCachedElements();
1033     },
1034
1035     /**
1036      * @param {number} startLine
1037      * @param {number} endLine
1038      * @return {WebInspector.TextEditorGutterChunk}
1039      */
1040     createNewChunk: function(startLine, endLine)
1041     {
1042         return new WebInspector.TextEditorGutterChunk(this, startLine, endLine);
1043     },
1044
1045     /**
1046      * @param {WebInspector.TextRange} oldRange
1047      * @param {WebInspector.TextRange} newRange
1048      */
1049     textChanged: function(oldRange, newRange)
1050     {
1051         this.beginDomUpdates();
1052
1053         var linesDiff = newRange.linesCount - oldRange.linesCount;
1054         if (linesDiff) {
1055             // Remove old chunks (if needed).
1056             for (var chunkNumber = this._textChunks.length - 1; chunkNumber >= 0; --chunkNumber) {
1057                 var chunk = this._textChunks[chunkNumber];
1058                 if (chunk.startLine + chunk.linesCount <= this._textModel.linesCount)
1059                     break;
1060                 chunk.collapse();
1061                 this._container.removeChild(chunk.element);
1062             }
1063             this._textChunks.length = chunkNumber + 1;
1064
1065             // Add new chunks (if needed).
1066             var totalLines = 0;
1067             if (this._textChunks.length) {
1068                 var lastChunk = this._textChunks[this._textChunks.length - 1];
1069                 totalLines = lastChunk.startLine + lastChunk.linesCount;
1070             }
1071
1072             for (var i = totalLines; i < this._textModel.linesCount; i += this._defaultChunkSize) {
1073                 var chunk = this.createNewChunk(i, i + this._defaultChunkSize);
1074                 this._textChunks.push(chunk);
1075                 this._container.appendChild(chunk.element);
1076             }
1077
1078             // Shift decorations if necessary
1079             for (var lineNumber in this._decorations) {
1080                 lineNumber = parseInt(lineNumber, 10);
1081
1082                 // Do not move decorations before the start position.
1083                 if (lineNumber < oldRange.startLine)
1084                     continue;
1085                 // Decorations follow the first character of line.
1086                 if (lineNumber === oldRange.startLine && oldRange.startColumn)
1087                     continue;
1088
1089                 var lineDecorationsCopy = this._decorations[lineNumber].slice();
1090                 for (var i = 0; i < lineDecorationsCopy.length; ++i) {
1091                     var decoration = lineDecorationsCopy[i];
1092                     this.removeDecoration(lineNumber, decoration);
1093
1094                     // Do not restore the decorations before the end position.
1095                     if (lineNumber < oldRange.endLine)
1096                         continue;
1097
1098                     this.addDecoration(lineNumber + linesDiff, decoration);
1099                 }
1100             }
1101
1102             this.repaintAll();
1103         } else {
1104             // Decorations may have been removed, so we may have to sync those lines.
1105             var chunkNumber = this.chunkNumberForLine(newRange.startLine);
1106             var chunk = this._textChunks[chunkNumber];
1107             while (chunk && chunk.startLine <= newRange.endLine) {
1108                 if (chunk.linesCount === 1)
1109                     this._syncDecorationsForLineListener(chunk.startLine);
1110                 chunk = this._textChunks[++chunkNumber];
1111             }
1112         }
1113
1114         this.endDomUpdates();
1115     },
1116
1117     /**
1118      * @param {number} clientHeight
1119      */
1120     syncClientHeight: function(clientHeight)
1121     {
1122         if (this.element.offsetHeight > clientHeight)
1123             this._container.style.setProperty("padding-bottom", (this.element.offsetHeight - clientHeight) + "px");
1124         else
1125             this._container.style.removeProperty("padding-bottom");
1126     },
1127
1128     /**
1129      * @param {number} lineNumber
1130      * @param {string|Element} decoration
1131      */
1132     addDecoration: function(lineNumber, decoration)
1133     {
1134         WebInspector.TextEditorChunkedPanel.prototype.addDecoration.call(this, lineNumber, decoration);
1135         var decorations = this._decorations[lineNumber];
1136         if (!decorations) {
1137             decorations = [];
1138             this._decorations[lineNumber] = decorations;
1139         }
1140         decorations.push(decoration);
1141     },
1142
1143     /**
1144      * @param {number} lineNumber
1145      * @param {string|Element} decoration
1146      */
1147     removeDecoration: function(lineNumber, decoration)
1148     {
1149         WebInspector.TextEditorChunkedPanel.prototype.removeDecoration.call(this, lineNumber, decoration);
1150         var decorations = this._decorations[lineNumber];
1151         if (decorations) {
1152             decorations.remove(decoration);
1153             if (!decorations.length)
1154                 delete this._decorations[lineNumber];
1155         }
1156     },
1157
1158     __proto__: WebInspector.TextEditorChunkedPanel.prototype
1159 }
1160
1161 /**
1162  * @constructor
1163  * @param {WebInspector.TextEditorGutterPanel} chunkedPanel
1164  * @param {number} startLine
1165  * @param {number} endLine
1166  */
1167 WebInspector.TextEditorGutterChunk = function(chunkedPanel, startLine, endLine)
1168 {
1169     this._chunkedPanel = chunkedPanel;
1170     this._textModel = chunkedPanel._textModel;
1171
1172     this.startLine = startLine;
1173     endLine = Math.min(this._textModel.linesCount, endLine);
1174     this.linesCount = endLine - startLine;
1175
1176     this._expanded = false;
1177
1178     this.element = document.createElement("div");
1179     this.element.lineNumber = startLine;
1180     this.element.className = "webkit-line-number";
1181
1182     if (this.linesCount === 1) {
1183         // Single line chunks are typically created for decorations. Host line number in
1184         // the sub-element in order to allow flexible border / margin management.
1185         var innerSpan = document.createElement("span");
1186         innerSpan.className = "webkit-line-number-inner";
1187         innerSpan.textContent = startLine + 1;
1188         var outerSpan = document.createElement("div");
1189         outerSpan.className = "webkit-line-number-outer";
1190         outerSpan.appendChild(innerSpan);
1191         this.element.appendChild(outerSpan);
1192     } else {
1193         var lineNumbers = [];
1194         for (var i = startLine; i < endLine; ++i)
1195             lineNumbers.push(i + 1);
1196         this.element.textContent = lineNumbers.join("\n");
1197     }
1198 }
1199
1200 WebInspector.TextEditorGutterChunk.prototype = {
1201     /**
1202      * @param {string} decoration
1203      */
1204     addDecoration: function(decoration)
1205     {
1206         this._chunkedPanel.beginDomUpdates();
1207         if (typeof decoration === "string")
1208             this.element.addStyleClass(decoration);
1209         this._chunkedPanel.endDomUpdates();
1210     },
1211
1212     /**
1213      * @param {string} decoration
1214      */
1215     removeDecoration: function(decoration)
1216     {
1217         this._chunkedPanel.beginDomUpdates();
1218         if (typeof decoration === "string")
1219             this.element.removeStyleClass(decoration);
1220         this._chunkedPanel.endDomUpdates();
1221     },
1222
1223     /**
1224      * @return {boolean}
1225      */
1226     expanded: function()
1227     {
1228         return this._expanded;
1229     },
1230
1231     expand: function()
1232     {
1233         if (this.linesCount === 1)
1234             this._chunkedPanel._syncDecorationsForLineListener(this.startLine);
1235
1236         if (this._expanded)
1237             return;
1238
1239         this._expanded = true;
1240
1241         if (this.linesCount === 1)
1242             return;
1243
1244         this._chunkedPanel.beginDomUpdates();
1245
1246         this._expandedLineRows = [];
1247         var parentElement = this.element.parentElement;
1248         for (var i = this.startLine; i < this.startLine + this.linesCount; ++i) {
1249             var lineRow = this._createRow(i);
1250             parentElement.insertBefore(lineRow, this.element);
1251             this._expandedLineRows.push(lineRow);
1252         }
1253         parentElement.removeChild(this.element);
1254         this._chunkedPanel._syncLineHeightListener(this._expandedLineRows[0]);
1255
1256         this._chunkedPanel.endDomUpdates();
1257     },
1258
1259     collapse: function()
1260     {
1261         if (this.linesCount === 1)
1262             this._chunkedPanel._syncDecorationsForLineListener(this.startLine);
1263
1264         if (!this._expanded)
1265             return;
1266
1267         this._expanded = false;
1268
1269         if (this.linesCount === 1)
1270             return;
1271
1272         this._chunkedPanel.beginDomUpdates();
1273
1274         var elementInserted = false;
1275         for (var i = 0; i < this._expandedLineRows.length; ++i) {
1276             var lineRow = this._expandedLineRows[i];
1277             var parentElement = lineRow.parentElement;
1278             if (parentElement) {
1279                 if (!elementInserted) {
1280                     elementInserted = true;
1281                     parentElement.insertBefore(this.element, lineRow);
1282                 }
1283                 parentElement.removeChild(lineRow);
1284             }
1285             this._chunkedPanel._cachedRows.push(lineRow);
1286         }
1287         delete this._expandedLineRows;
1288
1289         this._chunkedPanel.endDomUpdates();
1290     },
1291
1292     /**
1293      * @return {number}
1294      */
1295     get height()
1296     {
1297         if (!this._expandedLineRows)
1298             return this._chunkedPanel.totalHeight(this.element);
1299         return this._chunkedPanel.totalHeight(this._expandedLineRows[0], this._expandedLineRows[this._expandedLineRows.length - 1]);
1300     },
1301
1302     /**
1303      * @return {number}
1304      */
1305     get offsetTop()
1306     {
1307         return (this._expandedLineRows && this._expandedLineRows.length) ? this._expandedLineRows[0].offsetTop : this.element.offsetTop;
1308     },
1309
1310     /**
1311      * @param {number} lineNumber
1312      * @return {Element}
1313      */
1314     _createRow: function(lineNumber)
1315     {
1316         var lineRow = this._chunkedPanel._cachedRows.pop() || document.createElement("div");
1317         lineRow.lineNumber = lineNumber;
1318         lineRow.className = "webkit-line-number";
1319         lineRow.textContent = lineNumber + 1;
1320         return lineRow;
1321     }
1322 }
1323
1324 /**
1325  * @constructor
1326  * @extends {WebInspector.TextEditorChunkedPanel}
1327  * @param {WebInspector.TextEditorDelegate} delegate
1328  * @param {WebInspector.TextEditorModel} textModel
1329  * @param {?string} url
1330  * @param {function()} syncScrollListener
1331  * @param {function(number)} syncDecorationsForLineListener
1332  */
1333 WebInspector.TextEditorMainPanel = function(delegate, textModel, url, syncScrollListener, syncDecorationsForLineListener)
1334 {
1335     WebInspector.TextEditorChunkedPanel.call(this, textModel);
1336
1337     this._delegate = delegate;
1338     this._syncScrollListener = syncScrollListener;
1339     this._syncDecorationsForLineListener = syncDecorationsForLineListener;
1340
1341     this._url = url;
1342     this._highlighter = new WebInspector.TextEditorHighlighter(textModel, this._highlightDataReady.bind(this));
1343     this._readOnly = true;
1344
1345     this.element.className = "text-editor-contents";
1346     this.element.tabIndex = 0;
1347
1348     this._container = document.createElement("div");
1349     this._container.className = "inner-container";
1350     this._container.tabIndex = 0;
1351     this.element.appendChild(this._container);
1352
1353     this.element.addEventListener("focus", this._handleElementFocus.bind(this), false);
1354     this.element.addEventListener("textInput", this._handleTextInput.bind(this), false);
1355     this.element.addEventListener("cut", this._handleCut.bind(this), false);
1356
1357     this._container.addEventListener("focus", this._handleFocused.bind(this), false);
1358
1359     this._highlightRegexs = {};
1360
1361     this._freeCachedElements();
1362     this.buildChunks();
1363 }
1364
1365 WebInspector.TextEditorMainPanel.prototype = {
1366     /**
1367      * @param {string} regex
1368      * @param {string} cssClass
1369      */
1370     highlightRegex: function(regex, cssClass)
1371     {
1372         this._highlightRegexs[regex] = {
1373             regex: new RegExp(regex, "g"),
1374             cssClass: cssClass
1375         };
1376         this._repaintVisibleChunks();
1377     },
1378
1379     /**
1380      * @param {string} regex
1381      * @return {boolean}
1382      */
1383     removeRegexHighlight: function(regex)
1384     {
1385         var result = delete this._highlightRegexs[regex];
1386         this._repaintVisibleChunks();
1387         return result;
1388     },
1389
1390     _repaintVisibleChunks: function()
1391     {
1392         var visibleFrom = this.scrollTop();
1393         var visibleTo = visibleFrom + this.clientHeight();
1394
1395         var visibleChunks = this.findVisibleChunks(visibleFrom, visibleTo);
1396         var selection = this.selection();
1397
1398         for(var i = visibleChunks.start; i < visibleChunks.end; ++i) {
1399             var chunk = this._textChunks[i];
1400             this._paintLines(chunk._startLine, chunk._startLine + chunk.linesCount);
1401         }
1402         this._restoreSelection(selection);
1403     },
1404
1405     wasShown: function()
1406     {
1407         this._boundSelectionChangeListener = this._handleSelectionChange.bind(this);
1408         document.addEventListener("selectionchange", this._boundSelectionChangeListener, false);
1409
1410         this._isShowing = true;
1411         this._attachMutationObserver();
1412     },
1413
1414     willHide: function()
1415     {
1416         document.removeEventListener("selectionchange", this._boundSelectionChangeListener, false);
1417         delete this._boundSelectionChangeListener;
1418
1419         this._detachMutationObserver();
1420         this._isShowing = false;
1421         this._freeCachedElements();
1422     },
1423
1424     /**
1425      * @param {Element} eventTarget
1426      * @param {WebInspector.ContextMenu} contextMenu
1427      */
1428     populateContextMenu: function(eventTarget, contextMenu)
1429     {
1430         var target = this._enclosingLineRowOrSelf(eventTarget);
1431         this._delegate.populateTextAreaContextMenu(contextMenu, target && target.lineNumber);
1432     },
1433
1434     /**
1435      * @param {WebInspector.TextRange} textRange
1436      */
1437     setSelection: function(textRange)
1438     {
1439         this._lastSelection = textRange;
1440         if (this.element.isAncestor(document.activeElement))
1441             this._restoreSelection(textRange);
1442     },
1443
1444     _handleFocused: function()
1445     {
1446         if (this._lastSelection)
1447             this.setSelection(this._lastSelection);
1448     },
1449
1450     _attachMutationObserver: function()
1451     {
1452         if (!this._isShowing)
1453             return;
1454
1455         if (this._mutationObserver)
1456             this._mutationObserver.disconnect();
1457         this._mutationObserver = new NonLeakingMutationObserver(this._handleMutations.bind(this));
1458         this._mutationObserver.observe(this._container, { subtree: true, childList: true, characterData: true });
1459     },
1460
1461     _detachMutationObserver: function()
1462     {
1463         if (!this._isShowing)
1464             return;
1465
1466         if (this._mutationObserver) {
1467             this._mutationObserver.disconnect();
1468             delete this._mutationObserver;
1469         }
1470     },
1471
1472     /**
1473      * @param {string} mimeType
1474      */
1475     set mimeType(mimeType)
1476     {
1477         this._highlighter.mimeType = mimeType;
1478     },
1479
1480     /**
1481      * @param {boolean} readOnly
1482      * @param {boolean} requestFocus
1483      */
1484     setReadOnly: function(readOnly, requestFocus)
1485     {
1486         if (this._readOnly === readOnly)
1487             return;
1488
1489         this.beginDomUpdates();
1490         this._readOnly = readOnly;
1491         if (this._readOnly)
1492             this._container.removeStyleClass("text-editor-editable");
1493         else {
1494             this._container.addStyleClass("text-editor-editable");
1495             if (requestFocus)
1496                 this._updateSelectionOnStartEditing();
1497         }
1498         this.endDomUpdates();
1499     },
1500
1501     /**
1502      * @return {boolean}
1503      */
1504     readOnly: function()
1505     {
1506         return this._readOnly;
1507     },
1508
1509     _handleElementFocus: function()
1510     {
1511         if (!this._readOnly)
1512             this._container.focus();
1513     },
1514
1515     /**
1516      * @return {Element}
1517      */
1518     defaultFocusedElement: function()
1519     {
1520         if (this._readOnly)
1521             return this.element;
1522         return this._container;
1523     },
1524
1525     _updateSelectionOnStartEditing: function()
1526     {
1527         // focus() needs to go first for the case when the last selection was inside the editor and
1528         // the "Edit" button was clicked. In this case we bail at the check below, but the
1529         // editor does not receive the focus, thus "Esc" does not cancel editing until at least
1530         // one change has been made to the editor contents.
1531         this._container.focus();
1532         var selection = window.getSelection();
1533         if (selection.rangeCount) {
1534             var commonAncestorContainer = selection.getRangeAt(0).commonAncestorContainer;
1535             if (this._container.isSelfOrAncestor(commonAncestorContainer))
1536                 return;
1537         }
1538
1539         selection.removeAllRanges();
1540         var range = document.createRange();
1541         range.setStart(this._container, 0);
1542         range.setEnd(this._container, 0);
1543         selection.addRange(range);
1544     },
1545
1546     /**
1547      * @param {WebInspector.TextRange} range
1548      */
1549     markAndRevealRange: function(range)
1550     {
1551         if (this._rangeToMark) {
1552             var markedLine = this._rangeToMark.startLine;
1553             delete this._rangeToMark;
1554             // Remove the marked region immediately.
1555             this.beginDomUpdates();
1556             var chunk = this.chunkForLine(markedLine);
1557             var wasExpanded = chunk.expanded();
1558             chunk.collapse();
1559             chunk.updateCollapsedLineRow();
1560             if (wasExpanded)
1561                 chunk.expand();
1562             this.endDomUpdates();
1563         }
1564
1565         if (range) {
1566             this._rangeToMark = range;
1567             this.revealLine(range.startLine);
1568             var chunk = this.makeLineAChunk(range.startLine);
1569             this._paintLines(chunk.startLine, chunk.startLine + 1);
1570             if (this._markedRangeElement)
1571                 this._markedRangeElement.scrollIntoViewIfNeeded();
1572         }
1573         delete this._markedRangeElement;
1574     },
1575
1576     /**
1577      * @param {number} lineNumber
1578      */
1579     highlightLine: function(lineNumber)
1580     {
1581         this.clearLineHighlight();
1582         this._highlightedLine = lineNumber;
1583         this.revealLine(lineNumber);
1584
1585         if (!this._readOnly)
1586             this._restoreSelection(WebInspector.TextRange.createFromLocation(lineNumber, 0), false);
1587
1588         this.addDecoration(lineNumber, "webkit-highlighted-line");
1589     },
1590
1591     clearLineHighlight: function()
1592     {
1593         if (typeof this._highlightedLine === "number") {
1594             this.removeDecoration(this._highlightedLine, "webkit-highlighted-line");
1595             delete this._highlightedLine;
1596         }
1597     },
1598
1599     _freeCachedElements: function()
1600     {
1601         this._cachedSpans = [];
1602         this._cachedTextNodes = [];
1603         this._cachedRows = [];
1604     },
1605
1606     /**
1607      * @param {boolean} redo
1608      * @return {boolean}
1609      */
1610     handleUndoRedo: function(redo)
1611     {
1612         if (this.readOnly())
1613             return false;
1614
1615         this.beginUpdates();
1616
1617         var range = redo ? this._textModel.redo() : this._textModel.undo();
1618
1619         this.endUpdates();
1620
1621         // Restore location post-repaint.
1622         if (range)
1623             this._restoreSelection(range, true);
1624
1625         return true;
1626     },
1627
1628     /**
1629      * @param {boolean} shiftKey
1630      * @return {boolean}
1631      */
1632     handleTabKeyPress: function(shiftKey)
1633     {
1634         if (this.readOnly())
1635             return false;
1636
1637         var selection = this.selection();
1638         if (!selection)
1639             return false;
1640
1641         var range = selection.normalize();
1642
1643         this.beginUpdates();
1644
1645         var newRange;
1646         var rangeWasEmpty = range.isEmpty();
1647         if (shiftKey)
1648             newRange = this._textModel.unindentLines(range);
1649         else {
1650             if (rangeWasEmpty)
1651                 newRange = this._textModel.editRange(range, WebInspector.settings.textEditorIndent.get());
1652             else
1653                 newRange = this._textModel.indentLines(range);
1654         }
1655
1656         this.endUpdates();
1657         if (rangeWasEmpty)
1658             newRange.startColumn = newRange.endColumn;
1659         this._restoreSelection(newRange, true);
1660         return true;
1661     },
1662
1663     handleEnterKey: function()
1664     {
1665         if (this.readOnly())
1666             return false;
1667
1668         var range = this.selection();
1669         if (!range)
1670             return false;
1671
1672         range = range.normalize();
1673
1674         if (range.endColumn === 0)
1675             return false;
1676
1677         var line = this._textModel.line(range.startLine);
1678         var linePrefix = line.substring(0, range.startColumn);
1679         var indentMatch = linePrefix.match(/^\s+/);
1680         var currentIndent = indentMatch ? indentMatch[0] : "";
1681
1682         var textEditorIndent = WebInspector.settings.textEditorIndent.get();
1683         var indent = WebInspector.TextEditorModel.endsWithBracketRegex.test(linePrefix) ? currentIndent + textEditorIndent : currentIndent;
1684
1685         if (!indent)
1686             return false;
1687
1688         this.beginDomUpdates();
1689
1690         var lineBreak = this._textModel.lineBreak;
1691         var newRange;
1692         if (range.isEmpty() && line.substr(range.endColumn - 1, 2) === '{}') {
1693             // {|}
1694             // becomes
1695             // {
1696             //     |
1697             // }
1698             newRange = this._textModel.editRange(range, lineBreak + indent + lineBreak + currentIndent);
1699             newRange.endLine--;
1700             newRange.endColumn += textEditorIndent.length;
1701         } else
1702             newRange = this._textModel.editRange(range, lineBreak + indent);
1703
1704         this.endDomUpdates();
1705         this._restoreSelection(newRange.collapseToEnd(), true);
1706
1707         return true;
1708     },
1709
1710     /**
1711      * @param {number} lineNumber
1712      * @param {number} chunkNumber
1713      * @param {boolean=} createSuffixChunk
1714      * @return {Object}
1715      */
1716     splitChunkOnALine: function(lineNumber, chunkNumber, createSuffixChunk)
1717     {
1718         var selection = this.selection();
1719         var chunk = WebInspector.TextEditorChunkedPanel.prototype.splitChunkOnALine.call(this, lineNumber, chunkNumber, createSuffixChunk);
1720         this._restoreSelection(selection);
1721         return chunk;
1722     },
1723
1724     beginDomUpdates: function()
1725     {
1726         if (!this._domUpdateCoalescingLevel)
1727             this._detachMutationObserver();
1728         WebInspector.TextEditorChunkedPanel.prototype.beginDomUpdates.call(this);
1729     },
1730
1731     endDomUpdates: function()
1732     {
1733         WebInspector.TextEditorChunkedPanel.prototype.endDomUpdates.call(this);
1734         if (!this._domUpdateCoalescingLevel)
1735             this._attachMutationObserver();
1736     },
1737
1738     buildChunks: function()
1739     {
1740         for (var i = 0; i < this._textModel.linesCount; ++i)
1741             this._textModel.removeAttribute(i, "highlight");
1742
1743         WebInspector.TextEditorChunkedPanel.prototype.buildChunks.call(this);
1744     },
1745
1746     /**
1747      * @param {number} startLine
1748      * @param {number} endLine
1749      * @return {WebInspector.TextEditorMainChunk}
1750      */
1751     createNewChunk: function(startLine, endLine)
1752     {
1753         return new WebInspector.TextEditorMainChunk(this, startLine, endLine);
1754     },
1755
1756     /**
1757      * @param {number} fromIndex
1758      * @param {number} toIndex
1759      */
1760     expandChunks: function(fromIndex, toIndex)
1761     {
1762         var lastChunk = this._textChunks[toIndex - 1];
1763         var lastVisibleLine = lastChunk.startLine + lastChunk.linesCount;
1764
1765         var selection = this.selection();
1766
1767         this._muteHighlightListener = true;
1768         this._highlighter.highlight(lastVisibleLine);
1769         delete this._muteHighlightListener;
1770
1771         WebInspector.TextEditorChunkedPanel.prototype.expandChunks.call(this, fromIndex, toIndex);
1772
1773         this._restoreSelection(selection);
1774     },
1775
1776     /**
1777      * @param {number} fromLine
1778      * @param {number} toLine
1779      */
1780     _highlightDataReady: function(fromLine, toLine)
1781     {
1782         if (this._muteHighlightListener)
1783             return;
1784         this._paintLines(fromLine, toLine, true /*restoreSelection*/);
1785     },
1786
1787     /**
1788      * @param {number} fromLine
1789      * @param {number} toLine
1790      * @param {boolean=} restoreSelection
1791      */
1792     _paintLines: function(fromLine, toLine, restoreSelection)
1793     {
1794         var chunk;
1795         var selection;
1796         var lineRows = [];
1797         for (var lineNumber = fromLine; lineNumber < toLine; ++lineNumber) {
1798             if (!chunk || lineNumber < chunk.startLine || lineNumber >= chunk.startLine + chunk.linesCount)
1799                 chunk = this.chunkForLine(lineNumber);
1800             var lineRow = chunk.expandedLineRow(lineNumber);
1801             if (!lineRow)
1802                 continue;
1803             if (restoreSelection && !selection)
1804                 selection = this.selection();
1805             lineRows.push(lineRow);
1806         }
1807
1808         var highlight = {};
1809         this.beginDomUpdates();
1810         for(var regexString in this._highlightRegexs) {
1811             var regexHighlightDescriptor = this._highlightRegexs[regexString];
1812             this._measureRegexHighlight(highlight, lineRows, regexHighlightDescriptor.regex, regexHighlightDescriptor.cssClass);
1813         }
1814
1815         for(var i = 0; i < lineRows.length; ++i)
1816             this._paintLine(lineRows[i], highlight[lineRows[i].lineNumber]);
1817
1818         this.endDomUpdates();
1819
1820         if (restoreSelection)
1821             this._restoreSelection(selection);
1822     },
1823
1824     /**
1825      * @param {string} line
1826      * @param {RegExp} regex
1827      * @return {Array.<{startColumn: number, endColumn: number}>}
1828      */
1829     _findRegexOccurrences: function(line, regex)
1830     {
1831         var ranges = [];
1832         var regexResult;
1833         while (regexResult = regex.exec(line)) {
1834             ranges.push({
1835                 startColumn: regexResult.index,
1836                 endColumn: regexResult.index + regexResult[0].length - 1
1837             });
1838         }
1839         return ranges;
1840     },
1841
1842     /**
1843      * @param {Object.<number, Array.<WebInspector.TextEditorMainPanel.LineOverlayHighlight>>} highlight
1844      * @param {Array.<Element>} lineRows
1845      * @param {RegExp} regex
1846      * @param {string} cssClass
1847      */
1848     _measureRegexHighlight: function(highlight, lineRows, regex, cssClass)
1849     {
1850         var rowsToMeasure = [];
1851         for(var i = 0; i < lineRows.length; ++i) {
1852             var lineRow = lineRows[i];
1853             var line = this._textModel.line(lineRow.lineNumber);
1854             var ranges = this._findRegexOccurrences(line, regex);
1855             if (ranges.length === 0)
1856                 continue;
1857
1858             this._renderRanges(lineRow, line, ranges);
1859             rowsToMeasure.push(lineRow);
1860         }
1861
1862         for(var i = 0; i < rowsToMeasure.length; ++i) {
1863             var lineRow = rowsToMeasure[i];
1864             var lineNumber = lineRow.lineNumber;
1865             var metrics = this._measureSpans(lineRow);
1866
1867             if (!highlight[lineNumber])
1868                 highlight[lineNumber] = [];
1869
1870             highlight[lineNumber].push(new WebInspector.TextEditorMainPanel.LineOverlayHighlight(metrics, cssClass));
1871         }
1872     },
1873
1874     /**
1875      * @param {Element} lineRow
1876      * @return {Array.<WebInspector.TextEditorMainPanel.ElementMetrics>}
1877      */
1878     _measureSpans: function(lineRow)
1879     {
1880         var spans = lineRow.getElementsByTagName("span");
1881         var metrics = [];
1882         for(var i = 0; i < spans.length; ++i)
1883             metrics.push(new WebInspector.TextEditorMainPanel.ElementMetrics(spans[i]));
1884         return metrics;
1885     },
1886
1887     /**
1888      * @param {Element} lineRow
1889      * @param {WebInspector.TextEditorMainPanel.LineOverlayHighlight} highlight
1890      */
1891     _appendOverlayHighlight: function(lineRow, highlight)
1892     {
1893         const extraWidth = 1;
1894         var metrics = highlight.metrics;
1895         var cssClass = highlight.cssClass;
1896         for(var i = 0; i < metrics.length; ++i) {
1897             var highlightSpan = document.createElement("span");
1898             highlightSpan.addStyleClass(cssClass);
1899             highlightSpan.style.left = (metrics[i].left - extraWidth) + "px";
1900             highlightSpan.style.width = (metrics[i].width + extraWidth * 2) + "px";
1901             highlightSpan.textContent = " ";
1902             highlightSpan.addStyleClass("text-editor-overlay-highlight");
1903             lineRow.appendChild(highlightSpan);
1904         }
1905     },
1906
1907     /**
1908      * @param {Element} lineRow
1909      * @param {string} line
1910      * @param {Array.<{startColumn: number, endColumn: number, token: ?string}>} ranges
1911      */
1912     _renderRanges: function(lineRow, line, ranges)
1913     {
1914         var decorationsElement = lineRow.decorationsElement;
1915
1916         if (!decorationsElement)
1917             lineRow.removeChildren();
1918         else {
1919             while (true) {
1920                 var child = lineRow.firstChild;
1921                 if (!child || child === decorationsElement)
1922                     break;
1923                 lineRow.removeChild(child);
1924             }
1925         }
1926
1927         if (!line)
1928             lineRow.insertBefore(document.createElement("br"), decorationsElement);
1929
1930         var plainTextStart = 0;
1931         for(var i = 0; i < ranges.length; i++) {
1932             var rangeStart = ranges[i].startColumn;
1933             var rangeEnd = ranges[i].endColumn;
1934             var cssClass = ranges[i].token ? "webkit-" + ranges[i].token : "";
1935
1936             if (plainTextStart < rangeStart) {
1937                 this._insertTextNodeBefore(lineRow, decorationsElement, line.substring(plainTextStart, rangeStart));
1938             }
1939             this._insertSpanBefore(lineRow, decorationsElement, line.substring(rangeStart, rangeEnd + 1), cssClass);
1940             plainTextStart = rangeEnd + 1;
1941         }
1942         if (plainTextStart < line.length) {
1943             this._insertTextNodeBefore(lineRow, decorationsElement, line.substring(plainTextStart, line.length));
1944         }
1945     },
1946
1947     /**
1948      * @param {Element} lineRow
1949      * @param {Array.<WebInspector.TextEditorMainPanel.LineOverlayHighlight>} overlayHighlight
1950      */
1951     _paintLine: function(lineRow, overlayHighlight)
1952     {
1953         var lineNumber = lineRow.lineNumber;
1954
1955         this.beginDomUpdates();
1956         try {
1957             var syntaxHighlight = this._textModel.getAttribute(lineNumber, "highlight");
1958             if (!syntaxHighlight)
1959                 return;
1960
1961             var line = this._textModel.line(lineNumber);
1962             var ranges = syntaxHighlight.ranges;
1963             this._renderRanges(lineRow, line, ranges);
1964
1965             if (overlayHighlight)
1966                 for(var i = 0; i < overlayHighlight.length; ++i)
1967                     this._appendOverlayHighlight(lineRow, overlayHighlight[i]);
1968         } finally {
1969             if (this._rangeToMark && this._rangeToMark.startLine === lineNumber)
1970                 this._markedRangeElement = WebInspector.highlightSearchResult(lineRow, this._rangeToMark.startColumn, this._rangeToMark.endColumn - this._rangeToMark.startColumn);
1971             this.endDomUpdates();
1972         }
1973     },
1974
1975     /**
1976      * @param {Element} lineRow
1977      */
1978     _releaseLinesHighlight: function(lineRow)
1979     {
1980         if (!lineRow)
1981             return;
1982         if ("spans" in lineRow) {
1983             var spans = lineRow.spans;
1984             for (var j = 0; j < spans.length; ++j)
1985                 this._cachedSpans.push(spans[j]);
1986             delete lineRow.spans;
1987         }
1988         if ("textNodes" in lineRow) {
1989             var textNodes = lineRow.textNodes;
1990             for (var j = 0; j < textNodes.length; ++j)
1991                 this._cachedTextNodes.push(textNodes[j]);
1992             delete lineRow.textNodes;
1993         }
1994         this._cachedRows.push(lineRow);
1995     },
1996
1997     /**
1998      * @param {?Node=} lastUndamagedLineRow
1999      * @return {WebInspector.TextRange}
2000      */
2001     selection: function(lastUndamagedLineRow)
2002     {
2003         var selection = window.getSelection();
2004         if (!selection.rangeCount)
2005             return null;
2006         // Selection may be outside of the editor.
2007         if (!this._container.isAncestor(selection.anchorNode) || !this._container.isAncestor(selection.focusNode))
2008             return null;
2009         var start = this._selectionToPosition(selection.anchorNode, selection.anchorOffset, lastUndamagedLineRow);
2010         var end = selection.isCollapsed ? start : this._selectionToPosition(selection.focusNode, selection.focusOffset, lastUndamagedLineRow);
2011         return new WebInspector.TextRange(start.line, start.column, end.line, end.column);
2012     },
2013
2014     lastSelection: function()
2015     {
2016         return this._lastSelection;
2017     },
2018
2019     /**
2020      * @param {boolean=} scrollIntoView
2021      */
2022     _restoreSelection: function(range, scrollIntoView)
2023     {
2024         if (!range)
2025             return;
2026
2027         var start = this._positionToSelection(range.startLine, range.startColumn);
2028         var end = range.isEmpty() ? start : this._positionToSelection(range.endLine, range.endColumn);
2029         window.getSelection().setBaseAndExtent(start.container, start.offset, end.container, end.offset);
2030
2031         if (scrollIntoView) {
2032             for (var node = end.container; node; node = node.parentElement) {
2033                 if (node.scrollIntoViewIfNeeded) {
2034                     node.scrollIntoViewIfNeeded();
2035                     break;
2036                 }
2037             }
2038         }
2039         this._lastSelection = range;
2040     },
2041
2042     /**
2043      * @param {Node} container
2044      * @param {number} offset
2045      * @param {?Node=} lastUndamagedLineRow
2046      * @return {{line: number, column: number}}
2047      */
2048     _selectionToPosition: function(container, offset, lastUndamagedLineRow)
2049     {
2050         if (container === this._container && offset === 0)
2051             return { line: 0, column: 0 };
2052         if (container === this._container && offset === 1)
2053             return { line: this._textModel.linesCount - 1, column: this._textModel.lineLength(this._textModel.linesCount - 1) };
2054
2055         // This method can be called on the damaged DOM (when DOM does not match model).
2056         // We need to start counting lines from the first undamaged line if it is given.
2057         var lineNumber;
2058         var column = 0;
2059         var node;
2060         var scopeNode;
2061         if (lastUndamagedLineRow === null) {
2062              // Last undamaged row is given, but is null - force traverse from the beginning
2063             node = this._container.firstChild;
2064             scopeNode = this._container;
2065             lineNumber = 0;
2066         } else {
2067             var lineRow = this._enclosingLineRowOrSelf(container);
2068             if (!lastUndamagedLineRow || (typeof lineRow.lineNumber === "number" && lineRow.lineNumber <= lastUndamagedLineRow.lineNumber)) {
2069                 // DOM is consistent (or we belong to the first damaged row)- lookup the row we belong to and start with it.
2070                 node = lineRow;
2071                 scopeNode = node;
2072                 lineNumber = node.lineNumber;
2073             } else {
2074                 // Start with the node following undamaged row. It corresponds to lineNumber + 1.
2075                 node = lastUndamagedLineRow.nextSibling;
2076                 scopeNode = this._container;
2077                 lineNumber = lastUndamagedLineRow.lineNumber + 1;
2078             }
2079         }
2080
2081         // Fast return the line start.
2082         if (container === node && offset === 0)
2083             return { line: lineNumber, column: 0 };
2084
2085         // Traverse text and increment lineNumber / column.
2086         for (; node && node !== container; node = node.traverseNextNode(scopeNode)) {
2087             if (node.nodeName.toLowerCase() === "br") {
2088                 lineNumber++;
2089                 column = 0;
2090             } else if (node.nodeType === Node.TEXT_NODE) {
2091                 var text = node.textContent;
2092                 for (var i = 0; i < text.length; ++i) {
2093                     if (text.charAt(i) === "\n") {
2094                         lineNumber++;
2095                         column = 0;
2096                     } else
2097                         column++;
2098                 }
2099             }
2100         }
2101
2102         // We reached our container node, traverse within itself until we reach given offset.
2103         if (node === container && offset) {
2104             var text = node.textContent;
2105             // In case offset == 1 and lineRow is a chunk div, we need to traverse it all.
2106             var textOffset = (node._chunk && offset === 1) ? text.length : offset;
2107             for (var i = 0; i < textOffset; ++i) {
2108                 if (text.charAt(i) === "\n") {
2109                     lineNumber++;
2110                     column = 0;
2111                 } else
2112                     column++;
2113             }
2114         }
2115         return { line: lineNumber, column: column };
2116     },
2117
2118     /**
2119      * @param {number} line
2120      * @param {number} column
2121      * @return {{container: Element, offset: number}}
2122      */
2123     _positionToSelection: function(line, column)
2124     {
2125         var chunk = this.chunkForLine(line);
2126         // One-lined collapsed chunks may still stay highlighted.
2127         var lineRow = chunk.linesCount === 1 ? chunk.element : chunk.expandedLineRow(line);
2128         if (lineRow)
2129             var rangeBoundary = lineRow.rangeBoundaryForOffset(column);
2130         else {
2131             var offset = column;
2132             for (var i = chunk.startLine; i < line && i < this._textModel.linesCount; ++i)
2133                 offset += this._textModel.lineLength(i) + 1; // \n
2134             lineRow = chunk.element;
2135             if (lineRow.firstChild)
2136                 var rangeBoundary = { container: lineRow.firstChild, offset: offset };
2137             else
2138                 var rangeBoundary = { container: lineRow, offset: 0 };
2139         }
2140         return rangeBoundary;
2141     },
2142
2143     /**
2144      * @param {Node} element
2145      * @return {?Node}
2146      */
2147     _enclosingLineRowOrSelf: function(element)
2148     {
2149         var lineRow = element.enclosingNodeOrSelfWithClass("webkit-line-content");
2150         if (lineRow)
2151             return lineRow;
2152
2153         for (lineRow = element; lineRow; lineRow = lineRow.parentElement) {
2154             if (lineRow.parentElement === this._container)
2155                 return lineRow;
2156         }
2157         return null;
2158     },
2159
2160     /**
2161      * @param {Element} element
2162      * @param {Element} oldChild
2163      * @param {string} content
2164      * @param {string} className
2165      */
2166     _insertSpanBefore: function(element, oldChild, content, className)
2167     {
2168         if (className === "html-resource-link" || className === "html-external-link") {
2169             element.insertBefore(this._createLink(content, className === "html-external-link"), oldChild);
2170             return;
2171         }
2172
2173         var span = this._cachedSpans.pop() || document.createElement("span");
2174         span.className = className;
2175         if (WebInspector.FALSE) // For paint debugging.
2176             span.addStyleClass("debug-fadeout");
2177         span.textContent = content;
2178         element.insertBefore(span, oldChild);
2179         if (!("spans" in element))
2180             element.spans = [];
2181         element.spans.push(span);
2182     },
2183
2184     /**
2185      * @param {Element} element
2186      * @param {Element} oldChild
2187      * @param {string} text
2188      */
2189     _insertTextNodeBefore: function(element, oldChild, text)
2190     {
2191         var textNode = this._cachedTextNodes.pop();
2192         if (textNode)
2193             textNode.nodeValue = text;
2194         else
2195             textNode = document.createTextNode(text);
2196         element.insertBefore(textNode, oldChild);
2197         if (!("textNodes" in element))
2198             element.textNodes = [];
2199         element.textNodes.push(textNode);
2200     },
2201
2202     /**
2203      * @param {string} content
2204      * @param {boolean} isExternal
2205      * @return {Element}
2206      */
2207     _createLink: function(content, isExternal)
2208     {
2209         var quote = content.charAt(0);
2210         if (content.length > 1 && (quote === "\"" || quote === "'"))
2211             content = content.substring(1, content.length - 1);
2212         else
2213             quote = null;
2214
2215         var span = document.createElement("span");
2216         span.className = "webkit-html-attribute-value";
2217         if (quote)
2218             span.appendChild(document.createTextNode(quote));
2219         span.appendChild(this._delegate.createLink(content, isExternal));
2220         if (quote)
2221             span.appendChild(document.createTextNode(quote));
2222         return span;
2223     },
2224
2225     /**
2226      * @param {Array.<WebKitMutation>} mutations
2227      */
2228     _handleMutations: function(mutations)
2229     {
2230         if (this._readOnly) {
2231             delete this._keyDownCode;
2232             return;
2233         }
2234
2235         // Annihilate noop BR addition + removal that takes place upon line removal.
2236         var filteredMutations = mutations.slice();
2237         var addedBRs = new Map();
2238         for (var i = 0; i < mutations.length; ++i) {
2239             var mutation = mutations[i];
2240             if (mutation.type !== "childList")
2241                 continue;
2242             if (mutation.addedNodes.length === 1 && mutation.addedNodes[0].nodeName === "BR")
2243                 addedBRs.put(mutation.addedNodes[0], mutation);
2244             else if (mutation.removedNodes.length === 1 && mutation.removedNodes[0].nodeName === "BR") {
2245                 var noopMutation = addedBRs.get(mutation.removedNodes[0]);
2246                 if (noopMutation) {
2247                     filteredMutations.remove(mutation);
2248                     filteredMutations.remove(noopMutation);
2249                 }
2250             }
2251         }
2252
2253         var dirtyLines;
2254         for (var i = 0; i < filteredMutations.length; ++i) {
2255             var mutation = filteredMutations[i];
2256             var changedNodes = [];
2257             if (mutation.type === "childList" && mutation.addedNodes.length)
2258                 changedNodes = Array.prototype.slice.call(mutation.addedNodes);
2259             else if (mutation.type === "childList" && mutation.removedNodes.length)
2260                 changedNodes = Array.prototype.slice.call(mutation.removedNodes);
2261             changedNodes.push(mutation.target);
2262
2263             for (var j = 0; j < changedNodes.length; ++j) {
2264                 var lines = this._collectDirtyLines(mutation, changedNodes[j]);
2265                 if (!lines)
2266                     continue;
2267                 if (!dirtyLines) {
2268                     dirtyLines = lines;
2269                     continue;
2270                 }
2271                 dirtyLines.start = Math.min(dirtyLines.start, lines.start);
2272                 dirtyLines.end = Math.max(dirtyLines.end, lines.end);
2273             }
2274         }
2275         if (dirtyLines) {
2276             delete this._rangeToMark;
2277             this._applyDomUpdates(dirtyLines);
2278         }
2279
2280         this._assertDOMMatchesTextModel();
2281
2282         delete this._keyDownCode;
2283     },
2284
2285     /**
2286      * @param {WebKitMutation} mutation
2287      * @param {Node} target
2288      * @return {?Object}
2289      */
2290     _collectDirtyLines: function(mutation, target)
2291     {
2292         var lineRow = this._enclosingLineRowOrSelf(target);
2293         if (!lineRow)
2294             return null;
2295
2296         if (lineRow.decorationsElement && lineRow.decorationsElement.isSelfOrAncestor(target)) {
2297             if (this._syncDecorationsForLineListener)
2298                 this._syncDecorationsForLineListener(lineRow.lineNumber);
2299             return null;
2300         }
2301
2302         if (typeof lineRow.lineNumber !== "number")
2303             return null;
2304
2305         var startLine = lineRow.lineNumber;
2306         var endLine = lineRow._chunk ? lineRow._chunk.endLine - 1 : lineRow.lineNumber;
2307         return { start: startLine, end: endLine };
2308     },
2309
2310     /**
2311      * @param {Object} dirtyLines
2312      */
2313     _applyDomUpdates: function(dirtyLines)
2314     {
2315         var lastUndamagedLineNumber = dirtyLines.start - 1; // Can be -1
2316         var firstUndamagedLineNumber = dirtyLines.end + 1; // Can be this._textModel.linesCount
2317
2318         var lastUndamagedLineChunk = lastUndamagedLineNumber >= 0 ? this._textChunks[this.chunkNumberForLine(lastUndamagedLineNumber)] : null;
2319         var firstUndamagedLineChunk = firstUndamagedLineNumber < this._textModel.linesCount ? this._textChunks[this.chunkNumberForLine(firstUndamagedLineNumber)] : null;
2320
2321         var collectLinesFromNode = lastUndamagedLineChunk ? lastUndamagedLineChunk.lineRowContainingLine(lastUndamagedLineNumber) : null;
2322         var collectLinesToNode = firstUndamagedLineChunk ? firstUndamagedLineChunk.lineRowContainingLine(firstUndamagedLineNumber) : null;
2323         var lines = this._collectLinesFromDOM(collectLinesFromNode, collectLinesToNode);
2324
2325         var startLine = dirtyLines.start;
2326         var endLine = dirtyLines.end;
2327
2328         var editInfo = this._guessEditRangeBasedOnSelection(startLine, endLine, lines);
2329         if (!editInfo) {
2330             if (WebInspector.debugDefaultTextEditor)
2331                 console.warn("Falling back to expensive edit");
2332             var range = new WebInspector.TextRange(startLine, 0, endLine, this._textModel.lineLength(endLine));
2333             if (!lines.length) {
2334                 // Entire damaged area has collapsed. Replace everything between start and end lines with nothing.
2335                 editInfo = new WebInspector.DefaultTextEditor.EditInfo(this._textModel.growRangeRight(range), "");
2336             } else
2337                 editInfo = new WebInspector.DefaultTextEditor.EditInfo(range, lines.join("\n"));
2338         }
2339
2340         var selection = this.selection(collectLinesFromNode);
2341
2342         // Unindent after block
2343         if (editInfo.text === "}" && editInfo.range.isEmpty() && selection.isEmpty() && !this._textModel.line(editInfo.range.endLine).trim()) {
2344             var offset = this._closingBlockOffset(editInfo.range, selection);
2345             if (offset >= 0) {
2346                 editInfo.range.startColumn = offset;
2347                 selection.startColumn = offset + 1;
2348                 selection.endColumn = offset + 1;
2349             }
2350         }
2351
2352         this._textModel.editRange(editInfo.range, editInfo.text);
2353         this._restoreSelection(selection);
2354     },
2355
2356     /**
2357      * @param {number} startLine
2358      * @param {number} endLine
2359      * @param {Array.<string>} lines
2360      * @return {?WebInspector.DefaultTextEditor.EditInfo}
2361      */
2362     _guessEditRangeBasedOnSelection: function(startLine, endLine, lines)
2363     {
2364         // Analyze input data
2365         var textInputData = this._textInputData;
2366         delete this._textInputData;
2367         var isBackspace = this._keyDownCode === WebInspector.KeyboardShortcut.Keys.Backspace.code;
2368         var isDelete = this._keyDownCode === WebInspector.KeyboardShortcut.Keys.Delete.code;
2369
2370         if (!textInputData && (isDelete || isBackspace))
2371             textInputData = "";
2372
2373         // Return if there is no input data or selection
2374         if (typeof textInputData === "undefined" || !this._lastSelection)
2375             return null;
2376
2377         // Adjust selection based on the keyboard actions (grow for backspace, etc.).
2378         textInputData = textInputData || "";
2379         var range = this._lastSelection.normalize();
2380         if (isBackspace && range.isEmpty())
2381             range = this._textModel.growRangeLeft(range);
2382         else if (isDelete && range.isEmpty())
2383             range = this._textModel.growRangeRight(range);
2384
2385         // Test that selection intersects damaged lines
2386         if (startLine > range.endLine || endLine < range.startLine)
2387             return null;
2388
2389         var replacementLineCount = textInputData.split("\n").length - 1;
2390         var lineCountDelta = replacementLineCount - range.linesCount;
2391         if (startLine + lines.length - endLine - 1 !== lineCountDelta)
2392             return null;
2393
2394         // Clone text model of the size that fits both: selection before edit and the damaged lines after edit.
2395         var cloneFromLine = Math.min(range.startLine, startLine);
2396         var postLastLine = startLine + lines.length + lineCountDelta;
2397         var cloneToLine = Math.min(Math.max(postLastLine, range.endLine) + 1, this._textModel.linesCount);
2398         var domModel = this._textModel.slice(cloneFromLine, cloneToLine);
2399         domModel.editRange(range.shift(-cloneFromLine), textInputData);
2400
2401         // Then we'll test if this new model matches the DOM lines.
2402         for (var i = 0; i < lines.length; ++i) {
2403             if (domModel.line(i + startLine - cloneFromLine) !== lines[i])
2404                 return null;
2405         }
2406         return new WebInspector.DefaultTextEditor.EditInfo(range, textInputData);
2407     },
2408
2409     _assertDOMMatchesTextModel: function()
2410     {
2411         if (!WebInspector.debugDefaultTextEditor)
2412             return;
2413
2414         console.assert(this.element.innerText === this._textModel.text() + "\n", "DOM does not match model.");
2415         for (var lineRow = this._container.firstChild; lineRow; lineRow = lineRow.nextSibling) {
2416             var lineNumber = lineRow.lineNumber;
2417             if (typeof lineNumber !== "number") {
2418                 console.warn("No line number on line row");
2419                 continue;
2420             }
2421             if (lineRow._chunk) {
2422                 var chunk = lineRow._chunk;
2423                 console.assert(lineNumber === chunk.startLine);
2424                 var chunkText = this._textModel.copyRange(new WebInspector.TextRange(chunk.startLine, 0, chunk.endLine - 1, this._textModel.lineLength(chunk.endLine - 1)));
2425                 if (chunkText !== lineRow.textContent)
2426                     console.warn("Chunk is not matching: %d %O", lineNumber, lineRow);
2427             } else if (this._textModel.line(lineNumber) !== lineRow.textContent)
2428                 console.warn("Line is not matching: %d %O", lineNumber, lineRow);
2429         }
2430     },
2431
2432     /**
2433      * @param {WebInspector.TextRange} oldRange
2434      * @param {WebInspector.TextRange} selection
2435      * @return {number}
2436      */
2437     _closingBlockOffset: function(oldRange, selection)
2438     {
2439         var nestingLevel = 1;
2440         for (var i = oldRange.endLine; i >= 0; --i) {
2441             var attribute = this._textModel.getAttribute(i, "highlight");
2442             if (!attribute)
2443                 continue;
2444             var ranges = attribute.ranges;
2445             for (var j = ranges.length - 1; j >= 0; j--) {
2446                 var token = ranges[j].token;
2447                 if (token === "block-start") {
2448                     if (!(--nestingLevel)) {
2449                         var lineContent = this._textModel.line(i);
2450                         return lineContent.length - lineContent.trimLeft().length;
2451                     }
2452                 }
2453                 if (token === "block-end")
2454                     ++nestingLevel;
2455             }
2456         }
2457         return -1;
2458     },
2459
2460     /**
2461      * @param {WebInspector.TextRange} oldRange
2462      * @param {WebInspector.TextRange} newRange
2463      */
2464     textChanged: function(oldRange, newRange)
2465     {
2466         this.beginDomUpdates();
2467         this._removeDecorationsInRange(oldRange);
2468         this._updateChunksForRanges(oldRange, newRange);
2469         this._updateHighlightsForRange(newRange);
2470         this.endDomUpdates();
2471     },
2472
2473     /**
2474      * @param {WebInspector.TextRange} range
2475      */
2476     _removeDecorationsInRange: function(range)
2477     {
2478         for (var i = this.chunkNumberForLine(range.startLine); i < this._textChunks.length; ++i) {
2479             var chunk = this._textChunks[i];
2480             if (chunk.startLine > range.endLine)
2481                 break;
2482             chunk.removeAllDecorations();
2483         }
2484     },
2485
2486     /**
2487      * @param {WebInspector.TextRange} oldRange
2488      * @param {WebInspector.TextRange} newRange
2489      */
2490     _updateChunksForRanges: function(oldRange, newRange)
2491     {
2492         var firstDamagedChunkNumber = this.chunkNumberForLine(oldRange.startLine);
2493         var lastDamagedChunkNumber = firstDamagedChunkNumber;
2494         while (lastDamagedChunkNumber + 1 < this._textChunks.length) {
2495             if (this._textChunks[lastDamagedChunkNumber + 1].startLine > oldRange.endLine)
2496                 break;
2497             ++lastDamagedChunkNumber;
2498         }
2499
2500         var firstDamagedChunk = this._textChunks[firstDamagedChunkNumber];
2501         var lastDamagedChunk = this._textChunks[lastDamagedChunkNumber];
2502
2503         var linesDiff = newRange.linesCount - oldRange.linesCount;
2504
2505         // First, detect chunks that have not been modified and simply shift them.
2506         if (linesDiff) {
2507             for (var chunkNumber = lastDamagedChunkNumber + 1; chunkNumber < this._textChunks.length; ++chunkNumber)
2508                 this._textChunks[chunkNumber].startLine += linesDiff;
2509         }
2510
2511         // Remove damaged chunks from DOM and from textChunks model.
2512         var lastUndamagedChunk = firstDamagedChunkNumber > 0 ? this._textChunks[firstDamagedChunkNumber - 1] : null;
2513         var firstUndamagedChunk = lastDamagedChunkNumber + 1 < this._textChunks.length ? this._textChunks[lastDamagedChunkNumber + 1] : null;
2514
2515         var removeDOMFromNode = lastUndamagedChunk ? lastUndamagedChunk.lastElement().nextSibling : this._container.firstChild;
2516         var removeDOMToNode = firstUndamagedChunk ? firstUndamagedChunk.firstElement() : null;
2517
2518         // Fast case - patch single expanded chunk that did not grow / shrink during edit.
2519         if (!linesDiff && firstDamagedChunk === lastDamagedChunk && firstDamagedChunk._expandedLineRows) {
2520             var lastUndamagedLineRow = lastDamagedChunk.expandedLineRow(oldRange.startLine - 1);
2521             var firstUndamagedLineRow = firstDamagedChunk.expandedLineRow(oldRange.endLine + 1);
2522             var localRemoveDOMFromNode = lastUndamagedLineRow ? lastUndamagedLineRow.nextSibling : removeDOMFromNode;
2523             var localRemoveDOMToNode = firstUndamagedLineRow || removeDOMToNode;
2524             removeSubsequentNodes(localRemoveDOMFromNode, localRemoveDOMToNode);
2525             for (var i = newRange.startLine; i < newRange.endLine + 1; ++i) {
2526                 var row = firstDamagedChunk._createRow(i);
2527                 firstDamagedChunk._expandedLineRows[i - firstDamagedChunk.startLine] = row;
2528                 this._container.insertBefore(row, localRemoveDOMToNode);
2529             }
2530             firstDamagedChunk.updateCollapsedLineRow();
2531             this._assertDOMMatchesTextModel();
2532             return;
2533         }
2534
2535         removeSubsequentNodes(removeDOMFromNode, removeDOMToNode);
2536         this._textChunks.splice(firstDamagedChunkNumber, lastDamagedChunkNumber - firstDamagedChunkNumber + 1);
2537
2538         // Compute damaged chunks span
2539         var startLine = firstDamagedChunk.startLine;
2540         var endLine = lastDamagedChunk.endLine + linesDiff;
2541         var lineSpan = endLine - startLine;
2542
2543         // Re-create chunks for damaged area.
2544         var insertionIndex = firstDamagedChunkNumber;
2545         var chunkSize = Math.ceil(lineSpan / Math.ceil(lineSpan / this._defaultChunkSize));
2546
2547         for (var i = startLine; i < endLine; i += chunkSize) {
2548             var chunk = this.createNewChunk(i, Math.min(endLine, i + chunkSize));
2549             this._textChunks.splice(insertionIndex++, 0, chunk);
2550             this._container.insertBefore(chunk.element, removeDOMToNode);
2551         }
2552
2553         this._assertDOMMatchesTextModel();
2554     },
2555
2556     /**
2557      * @param {WebInspector.TextRange} range
2558      */
2559     _updateHighlightsForRange: function(range)
2560     {
2561         var visibleFrom = this.scrollTop();
2562         var visibleTo = visibleFrom + this.clientHeight();
2563
2564         var result = this.findVisibleChunks(visibleFrom, visibleTo);
2565         var chunk = this._textChunks[result.end - 1];
2566         var lastVisibleLine = chunk.startLine + chunk.linesCount;
2567
2568         lastVisibleLine = Math.max(lastVisibleLine, range.endLine + 1);
2569         lastVisibleLine = Math.min(lastVisibleLine, this._textModel.linesCount);
2570
2571         var updated = this._highlighter.updateHighlight(range.startLine, lastVisibleLine);
2572         if (!updated) {
2573             // Highlights for the chunks below are invalid, so just collapse them.
2574             for (var i = this.chunkNumberForLine(range.startLine); i < this._textChunks.length; ++i)
2575                 this._textChunks[i].collapse();
2576         }
2577
2578         this.repaintAll();
2579     },
2580
2581     /**
2582      * @param {Node} from
2583      * @param {Node} to
2584      * @return {Array.<string>}
2585      */
2586     _collectLinesFromDOM: function(from, to)
2587     {
2588         var textContents = [];
2589         var hasContent = false;
2590         for (var node = from ? from.nextSibling : this._container; node && node !== to; node = node.traverseNextNode(this._container)) {
2591             if (node._isDecorationsElement) {
2592                 // Skip all children of the decoration container.
2593                 node = node.nextSibling;
2594                 if (!node || node === to)
2595                     break;
2596             }
2597             hasContent = true;
2598             if (node.nodeName.toLowerCase() === "br")
2599                 textContents.push("\n");
2600             else if (node.nodeType === Node.TEXT_NODE)
2601                 textContents.push(node.textContent);
2602         }
2603         if (!hasContent)
2604             return [];
2605
2606         var textContent = textContents.join("");
2607         // The last \n (if any) does not "count" in a DIV.
2608         textContent = textContent.replace(/\n$/, "");
2609
2610         return textContent.split("\n");
2611     },
2612
2613     /**
2614      * @param {Event} event
2615      */
2616     _handleSelectionChange: function(event)
2617     {
2618         var textRange = this.selection();
2619         if (textRange)
2620             this._lastSelection = textRange;
2621         this._delegate.selectionChanged(textRange);
2622     },
2623
2624     /**
2625      * @param {Event} event
2626      */
2627     _handleTextInput: function(event)
2628     {
2629         this._textInputData = event.data;
2630     },
2631
2632     /**
2633      * @param {Event} event
2634      */
2635     handleKeyDown: function(event)
2636     {
2637         this._keyDownCode = event.keyCode;
2638     },
2639
2640     /**
2641      * @param {Event} event
2642      */
2643     _handleCut: function(event)
2644     {
2645         this._keyDownCode = WebInspector.KeyboardShortcut.Keys.Delete.code;
2646     },
2647
2648     /**
2649      * @param {number} scrollTop
2650      * @param {number} clientHeight
2651      * @param {number} chunkSize
2652      */
2653     overrideViewportForTest: function(scrollTop, clientHeight, chunkSize)
2654     {
2655         this._scrollTopOverrideForTest = scrollTop;
2656         this._clientHeightOverrideForTest = clientHeight;
2657         this._defaultChunkSize = chunkSize;
2658     },
2659
2660     __proto__: WebInspector.TextEditorChunkedPanel.prototype
2661 }
2662
2663 /**
2664  * @constructor
2665  * @param {Element} element
2666  */
2667 WebInspector.TextEditorMainPanel.ElementMetrics = function(element)
2668 {
2669     this.width = element.offsetWidth;
2670     this.left = element.offsetLeft;
2671 }
2672
2673 /**
2674  * @constructor
2675  * @param {Array.<WebInspector.TextEditorMainPanel.ElementMetrics>} metrics
2676  * @param {string} cssClass
2677  */
2678 WebInspector.TextEditorMainPanel.LineOverlayHighlight = function(metrics, cssClass)
2679 {
2680     this.metrics = metrics;
2681     this.cssClass = cssClass;
2682 }
2683
2684 /**
2685  * @constructor
2686  * @param {WebInspector.TextEditorChunkedPanel} chunkedPanel
2687  * @param {number} startLine
2688  * @param {number} endLine
2689  */
2690 WebInspector.TextEditorMainChunk = function(chunkedPanel, startLine, endLine)
2691 {
2692     this._chunkedPanel = chunkedPanel;
2693     this._textModel = chunkedPanel._textModel;
2694
2695     this.element = document.createElement("div");
2696     this.element.lineNumber = startLine;
2697     this.element.className = "webkit-line-content";
2698     this.element._chunk = this;
2699
2700     this._startLine = startLine;
2701     endLine = Math.min(this._textModel.linesCount, endLine);
2702     this.linesCount = endLine - startLine;
2703
2704     this._expanded = false;
2705
2706     this.updateCollapsedLineRow();
2707 }
2708
2709 WebInspector.TextEditorMainChunk.prototype = {
2710     /**
2711      * @param {Element|string} decoration
2712      */
2713     addDecoration: function(decoration)
2714     {
2715         this._chunkedPanel.beginDomUpdates();
2716         if (typeof decoration === "string")
2717             this.element.addStyleClass(decoration);
2718         else {
2719             if (!this.element.decorationsElement) {
2720                 this.element.decorationsElement = document.createElement("div");
2721                 this.element.decorationsElement.className = "webkit-line-decorations";
2722                 this.element.decorationsElement._isDecorationsElement = true;
2723                 this.element.appendChild(this.element.decorationsElement);
2724             }
2725             this.element.decorationsElement.appendChild(decoration);
2726         }
2727         this._chunkedPanel.endDomUpdates();
2728     },
2729
2730     /**
2731      * @param {string|Element} decoration
2732      */
2733     removeDecoration: function(decoration)
2734     {
2735         this._chunkedPanel.beginDomUpdates();
2736         if (typeof decoration === "string")
2737             this.element.removeStyleClass(decoration);
2738         else if (this.element.decorationsElement)
2739             this.element.decorationsElement.removeChild(decoration);
2740         this._chunkedPanel.endDomUpdates();
2741     },
2742
2743     removeAllDecorations: function()
2744     {
2745         this._chunkedPanel.beginDomUpdates();
2746         this.element.className = "webkit-line-content";
2747         if (this.element.decorationsElement) {
2748             this.element.removeChild(this.element.decorationsElement);
2749             delete this.element.decorationsElement;
2750         }
2751         this._chunkedPanel.endDomUpdates();
2752     },
2753
2754     /**
2755      * @return {boolean}
2756      */
2757     isDecorated: function()
2758     {
2759         return this.element.className !== "webkit-line-content" || !!(this.element.decorationsElement && this.element.decorationsElement.firstChild);
2760     },
2761
2762     /**
2763      * @return {number}
2764      */
2765     get startLine()
2766     {
2767         return this._startLine;
2768     },
2769
2770     /**
2771      * @return {number}
2772      */
2773     get endLine()
2774     {
2775         return this._startLine + this.linesCount;
2776     },
2777
2778     set startLine(startLine)
2779     {
2780         this._startLine = startLine;
2781         this.element.lineNumber = startLine;
2782         if (this._expandedLineRows) {
2783             for (var i = 0; i < this._expandedLineRows.length; ++i)
2784                 this._expandedLineRows[i].lineNumber = startLine + i;
2785         }
2786     },
2787
2788     /**
2789      * @return {boolean}
2790      */
2791     expanded: function()
2792     {
2793         return this._expanded;
2794     },
2795
2796     expand: function()
2797     {
2798         if (this._expanded)
2799             return;
2800
2801         this._expanded = true;
2802
2803         if (this.linesCount === 1) {
2804             this._chunkedPanel._paintLines(this.startLine, this.startLine + 1);
2805             return;
2806         }
2807
2808         this._chunkedPanel.beginDomUpdates();
2809
2810         this._expandedLineRows = [];
2811         var parentElement = this.element.parentElement;
2812         for (var i = this.startLine; i < this.startLine + this.linesCount; ++i) {
2813             var lineRow = this._createRow(i);
2814             parentElement.insertBefore(lineRow, this.element);
2815             this._expandedLineRows.push(lineRow);
2816         }
2817         parentElement.removeChild(this.element);
2818         this._chunkedPanel._paintLines(this.startLine, this.startLine + this.linesCount);
2819
2820         this._chunkedPanel.endDomUpdates();
2821     },
2822
2823     collapse: function()
2824     {
2825         if (!this._expanded)
2826             return;
2827
2828         this._expanded = false;
2829         if (this.linesCount === 1)
2830             return;
2831
2832         this._chunkedPanel.beginDomUpdates();
2833
2834         var elementInserted = false;
2835         for (var i = 0; i < this._expandedLineRows.length; ++i) {
2836             var lineRow = this._expandedLineRows[i];
2837             var parentElement = lineRow.parentElement;
2838             if (parentElement) {
2839                 if (!elementInserted) {
2840                     elementInserted = true;
2841                     parentElement.insertBefore(this.element, lineRow);
2842                 }
2843                 parentElement.removeChild(lineRow);
2844             }
2845             this._chunkedPanel._releaseLinesHighlight(lineRow);
2846         }
2847         delete this._expandedLineRows;
2848
2849         this._chunkedPanel.endDomUpdates();
2850     },
2851
2852     /**
2853      * @return {number}
2854      */
2855     get height()
2856     {
2857         if (!this._expandedLineRows)
2858             return this._chunkedPanel.totalHeight(this.element);
2859         return this._chunkedPanel.totalHeight(this._expandedLineRows[0], this._expandedLineRows[this._expandedLineRows.length - 1]);
2860     },
2861
2862     /**
2863      * @return {number}
2864      */
2865     get offsetTop()
2866     {
2867         return (this._expandedLineRows && this._expandedLineRows.length) ? this._expandedLineRows[0].offsetTop : this.element.offsetTop;
2868     },
2869
2870     /**
2871      * @param {number} lineNumber
2872      * @return {Element}
2873      */
2874     _createRow: function(lineNumber)
2875     {
2876         var lineRow = this._chunkedPanel._cachedRows.pop() || document.createElement("div");
2877         lineRow.lineNumber = lineNumber;
2878         lineRow.className = "webkit-line-content";
2879         lineRow.textContent = this._textModel.line(lineNumber);
2880         if (!lineRow.textContent)
2881             lineRow.appendChild(document.createElement("br"));
2882         return lineRow;
2883     },
2884
2885     /**
2886      * Called on potentially damaged / inconsistent chunk
2887      * @param {number} lineNumber
2888      * @return {?Node}
2889      */
2890     lineRowContainingLine: function(lineNumber)
2891     {
2892         if (!this._expanded)
2893             return this.element;
2894         return this.expandedLineRow(lineNumber);
2895     },
2896
2897     /**
2898      * @param {number} lineNumber
2899      * @return {Element}
2900      */
2901     expandedLineRow: function(lineNumber)
2902     {
2903         if (!this._expanded || lineNumber < this.startLine || lineNumber >= this.startLine + this.linesCount)
2904             return null;
2905         if (!this._expandedLineRows)
2906             return this.element;
2907         return this._expandedLineRows[lineNumber - this.startLine];
2908     },
2909
2910     updateCollapsedLineRow: function()
2911     {
2912         if (this.linesCount === 1 && this._expanded)
2913             return;
2914
2915         var lines = [];
2916         for (var i = this.startLine; i < this.startLine + this.linesCount; ++i)
2917             lines.push(this._textModel.line(i));
2918
2919         if (WebInspector.FALSE)
2920             console.log("Rebuilding chunk with " + lines.length + " lines");
2921
2922         this.element.removeChildren();
2923         this.element.textContent = lines.join("\n");
2924         // The last empty line will get swallowed otherwise.
2925         if (!lines[lines.length - 1])
2926             this.element.appendChild(document.createElement("br"));
2927     },
2928
2929     firstElement: function()
2930     {
2931         return this._expandedLineRows ? this._expandedLineRows[0] : this.element;
2932     },
2933
2934     /**
2935      * @return {Element}
2936      */
2937     lastElement: function()
2938     {
2939         return this._expandedLineRows ? this._expandedLineRows[this._expandedLineRows.length - 1] : this.element;
2940     }
2941 }
2942
2943 WebInspector.debugDefaultTextEditor = false;