2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
4 * Copyright (C) 2007 Matt Lilek (pewtermoose@gmail.com).
5 * Copyright (C) 2009 Joseph Pecoraro
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * @param {!Element} element
34 * @param {?function(!MouseEvent): boolean} elementDragStart
35 * @param {function(!MouseEvent)} elementDrag
36 * @param {?function(!MouseEvent)} elementDragEnd
37 * @param {string} cursor
38 * @param {?string=} hoverCursor
40 WebInspector.installDragHandle = function(element, elementDragStart, elementDrag, elementDragEnd, cursor, hoverCursor)
42 element.addEventListener("mousedown", WebInspector.elementDragStart.bind(WebInspector, elementDragStart, elementDrag, elementDragEnd, cursor), false);
43 if (hoverCursor !== null)
44 element.style.cursor = hoverCursor || cursor;
48 * @param {?function(!MouseEvent):boolean} elementDragStart
49 * @param {function(!MouseEvent)} elementDrag
50 * @param {?function(!MouseEvent)} elementDragEnd
51 * @param {string} cursor
52 * @param {?Event} event
54 WebInspector.elementDragStart = function(elementDragStart, elementDrag, elementDragEnd, cursor, event)
56 // Only drag upon left button. Right will likely cause a context menu. So will ctrl-click on mac.
57 if (event.button || (WebInspector.isMac() && event.ctrlKey))
60 if (WebInspector._elementDraggingEventListener)
63 if (elementDragStart && !elementDragStart(/** @type {!MouseEvent} */ (event)))
66 if (WebInspector._elementDraggingGlassPane) {
67 WebInspector._elementDraggingGlassPane.dispose();
68 delete WebInspector._elementDraggingGlassPane;
71 var targetDocument = event.target.ownerDocument;
73 WebInspector._elementDraggingEventListener = elementDrag;
74 WebInspector._elementEndDraggingEventListener = elementDragEnd;
75 WebInspector._mouseOutWhileDraggingTargetDocument = targetDocument;
77 targetDocument.addEventListener("mousemove", WebInspector._elementDragMove, true);
78 targetDocument.addEventListener("mouseup", WebInspector._elementDragEnd, true);
79 targetDocument.addEventListener("mouseout", WebInspector._mouseOutWhileDragging, true);
81 targetDocument.body.style.cursor = cursor;
83 event.preventDefault();
86 WebInspector._mouseOutWhileDragging = function()
88 WebInspector._unregisterMouseOutWhileDragging();
89 WebInspector._elementDraggingGlassPane = new WebInspector.GlassPane();
92 WebInspector._unregisterMouseOutWhileDragging = function()
94 if (!WebInspector._mouseOutWhileDraggingTargetDocument)
96 WebInspector._mouseOutWhileDraggingTargetDocument.removeEventListener("mouseout", WebInspector._mouseOutWhileDragging, true);
97 delete WebInspector._mouseOutWhileDraggingTargetDocument;
101 * @param {!Event} event
103 WebInspector._elementDragMove = function(event)
105 if (WebInspector._elementDraggingEventListener(/** @type {!MouseEvent} */ (event)))
106 WebInspector._cancelDragEvents(event);
110 * @param {!Event} event
112 WebInspector._cancelDragEvents = function(event)
114 var targetDocument = event.target.ownerDocument;
115 targetDocument.removeEventListener("mousemove", WebInspector._elementDragMove, true);
116 targetDocument.removeEventListener("mouseup", WebInspector._elementDragEnd, true);
117 WebInspector._unregisterMouseOutWhileDragging();
119 targetDocument.body.style.removeProperty("cursor");
121 if (WebInspector._elementDraggingGlassPane)
122 WebInspector._elementDraggingGlassPane.dispose();
124 delete WebInspector._elementDraggingGlassPane;
125 delete WebInspector._elementDraggingEventListener;
126 delete WebInspector._elementEndDraggingEventListener;
130 * @param {!Event} event
132 WebInspector._elementDragEnd = function(event)
134 var elementDragEnd = WebInspector._elementEndDraggingEventListener;
136 WebInspector._cancelDragEvents(/** @type {!MouseEvent} */ (event));
138 event.preventDefault();
140 elementDragEnd(/** @type {!MouseEvent} */ (event));
146 WebInspector.GlassPane = function()
148 this.element = document.createElement("div");
149 this.element.style.cssText = "position:absolute;top:0;bottom:0;left:0;right:0;background-color:transparent;z-index:1000;";
150 this.element.id = "glass-pane";
151 document.body.appendChild(this.element);
152 WebInspector._glassPane = this;
155 WebInspector.GlassPane.prototype = {
158 delete WebInspector._glassPane;
159 if (WebInspector.GlassPane.DefaultFocusedViewStack.length)
160 WebInspector.GlassPane.DefaultFocusedViewStack[0].focus();
161 this.element.remove();
166 * @type {!Array.<!WebInspector.View>}
168 WebInspector.GlassPane.DefaultFocusedViewStack = [];
171 * @param {?Node=} node
174 WebInspector.isBeingEdited = function(node)
176 if (!node || node.nodeType !== Node.ELEMENT_NODE)
178 var element = /** {!Element} */ (node);
179 if (element.classList.contains("text-prompt") || element.nodeName === "INPUT" || element.nodeName === "TEXTAREA")
182 if (!WebInspector.__editingCount)
186 if (element.__editing)
188 element = element.parentElement;
194 * @param {!Element} element
195 * @param {boolean} value
198 WebInspector.markBeingEdited = function(element, value)
201 if (element.__editing)
203 element.classList.add("being-edited");
204 element.__editing = true;
205 WebInspector.__editingCount = (WebInspector.__editingCount || 0) + 1;
207 if (!element.__editing)
209 element.classList.remove("being-edited");
210 delete element.__editing;
211 --WebInspector.__editingCount;
216 WebInspector.CSSNumberRegex = /^(-?(?:\d+(?:\.\d+)?|\.\d+))$/;
218 WebInspector.StyleValueDelimiters = " \xA0\t\n\"':;,/()";
222 * @param {!Event} event
225 WebInspector._valueModificationDirection = function(event)
227 var direction = null;
228 if (event.type === "mousewheel") {
229 if (event.wheelDeltaY > 0)
231 else if (event.wheelDeltaY < 0)
234 if (event.keyIdentifier === "Up" || event.keyIdentifier === "PageUp")
236 else if (event.keyIdentifier === "Down" || event.keyIdentifier === "PageDown")
243 * @param {string} hexString
244 * @param {!Event} event
246 WebInspector._modifiedHexValue = function(hexString, event)
248 var direction = WebInspector._valueModificationDirection(event);
252 var number = parseInt(hexString, 16);
253 if (isNaN(number) || !isFinite(number))
256 var maxValue = Math.pow(16, hexString.length) - 1;
257 var arrowKeyOrMouseWheelEvent = (event.keyIdentifier === "Up" || event.keyIdentifier === "Down" || event.type === "mousewheel");
260 if (arrowKeyOrMouseWheelEvent)
261 delta = (direction === "Up") ? 1 : -1;
263 delta = (event.keyIdentifier === "PageUp") ? 16 : -16;
268 var result = number + delta;
270 result = 0; // Color hex values are never negative, so clamp to 0.
271 else if (result > maxValue)
274 // Ensure the result length is the same as the original hex value.
275 var resultString = result.toString(16).toUpperCase();
276 for (var i = 0, lengthDelta = hexString.length - resultString.length; i < lengthDelta; ++i)
277 resultString = "0" + resultString;
282 * @param {number} number
283 * @param {!Event} event
285 WebInspector._modifiedFloatNumber = function(number, event)
287 var direction = WebInspector._valueModificationDirection(event);
291 var arrowKeyOrMouseWheelEvent = (event.keyIdentifier === "Up" || event.keyIdentifier === "Down" || event.type === "mousewheel");
293 // Jump by 10 when shift is down or jump by 0.1 when Alt/Option is down.
294 // Also jump by 10 for page up and down, or by 100 if shift is held with a page key.
295 var changeAmount = 1;
296 if (event.shiftKey && !arrowKeyOrMouseWheelEvent)
298 else if (event.shiftKey || !arrowKeyOrMouseWheelEvent)
300 else if (event.altKey)
303 if (direction === "Down")
306 // Make the new number and constrain it to a precision of 6, this matches numbers the engine returns.
307 // Use the Number constructor to forget the fixed precision, so 1.100000 will print as 1.1.
308 var result = Number((number + changeAmount).toFixed(6));
309 if (!String(result).match(WebInspector.CSSNumberRegex))
316 * @param {?Event} event
317 * @param {!Element} element
318 * @param {function(string,string)=} finishHandler
319 * @param {function(string)=} suggestionHandler
320 * @param {function(number):number=} customNumberHandler
323 WebInspector.handleElementValueModifications = function(event, element, finishHandler, suggestionHandler, customNumberHandler)
325 var arrowKeyOrMouseWheelEvent = (event.keyIdentifier === "Up" || event.keyIdentifier === "Down" || event.type === "mousewheel");
326 var pageKeyPressed = (event.keyIdentifier === "PageUp" || event.keyIdentifier === "PageDown");
327 if (!arrowKeyOrMouseWheelEvent && !pageKeyPressed)
330 var selection = window.getSelection();
331 if (!selection.rangeCount)
334 var selectionRange = selection.getRangeAt(0);
335 if (!selectionRange.commonAncestorContainer.isSelfOrDescendant(element))
338 var originalValue = element.textContent;
339 var wordRange = selectionRange.startContainer.rangeOfWord(selectionRange.startOffset, WebInspector.StyleValueDelimiters, element);
340 var wordString = wordRange.toString();
342 if (suggestionHandler && suggestionHandler(wordString))
345 var replacementString;
346 var prefix, suffix, number;
349 matches = /(.*#)([\da-fA-F]+)(.*)/.exec(wordString);
350 if (matches && matches.length) {
353 number = WebInspector._modifiedHexValue(matches[2], event);
355 if (customNumberHandler)
356 number = customNumberHandler(number);
358 replacementString = prefix + number + suffix;
360 matches = /(.*?)(-?(?:\d+(?:\.\d+)?|\.\d+))(.*)/.exec(wordString);
361 if (matches && matches.length) {
364 number = WebInspector._modifiedFloatNumber(parseFloat(matches[2]), event);
366 // Need to check for null explicitly.
370 if (customNumberHandler)
371 number = customNumberHandler(number);
373 replacementString = prefix + number + suffix;
377 if (replacementString) {
378 var replacementTextNode = document.createTextNode(replacementString);
380 wordRange.deleteContents();
381 wordRange.insertNode(replacementTextNode);
383 var finalSelectionRange = document.createRange();
384 finalSelectionRange.setStart(replacementTextNode, 0);
385 finalSelectionRange.setEnd(replacementTextNode, replacementString.length);
387 selection.removeAllRanges();
388 selection.addRange(finalSelectionRange);
390 event.handled = true;
391 event.preventDefault();
394 finishHandler(originalValue, replacementString);
403 * @param {number=} precision
406 Number.preciseMillisToString = function(ms, precision)
408 precision = precision || 0;
409 var format = "%." + precision + "f\u2009ms";
410 return WebInspector.UIString(format, ms);
415 * @param {boolean=} higherResolution
418 Number.millisToString = function(ms, higherResolution)
426 if (higherResolution && ms < 1000)
427 return WebInspector.UIString("%.3f\u2009ms", ms);
429 return WebInspector.UIString("%.0f\u2009ms", ms);
431 var seconds = ms / 1000;
433 return WebInspector.UIString("%.2f\u2009s", seconds);
435 var minutes = seconds / 60;
437 return WebInspector.UIString("%.1f\u2009min", minutes);
439 var hours = minutes / 60;
441 return WebInspector.UIString("%.1f\u2009hrs", hours);
443 var days = hours / 24;
444 return WebInspector.UIString("%.1f\u2009days", days);
448 * @param {number} seconds
449 * @param {boolean=} higherResolution
452 Number.secondsToString = function(seconds, higherResolution)
454 if (!isFinite(seconds))
456 return Number.millisToString(seconds * 1000, higherResolution);
460 * @param {number} bytes
463 Number.bytesToString = function(bytes)
466 return WebInspector.UIString("%.0f\u2009B", bytes);
468 var kilobytes = bytes / 1024;
470 return WebInspector.UIString("%.1f\u2009KB", kilobytes);
471 if (kilobytes < 1024)
472 return WebInspector.UIString("%.0f\u2009KB", kilobytes);
474 var megabytes = kilobytes / 1024;
476 return WebInspector.UIString("%.1f\u2009MB", megabytes);
478 return WebInspector.UIString("%.0f\u2009MB", megabytes);
482 * @param {number} num
485 Number.withThousandsSeparator = function(num)
488 var re = /(\d+)(\d{3})/;
489 while (str.match(re))
490 str = str.replace(re, "$1\u2009$2"); // \u2009 is a thin space.
497 WebInspector.useLowerCaseMenuTitles = function()
499 return WebInspector.platform() === "windows";
503 * @param {string} format
504 * @param {?Array.<string>} substitutions
505 * @param {!Object.<string, function(string, ...):*>} formatters
506 * @param {string} initialValue
507 * @param {function(string, string): ?} append
508 * @return {!{formattedResult: string, unusedSubstitutions: ?Array.<string>}};
510 WebInspector.formatLocalized = function(format, substitutions, formatters, initialValue, append)
512 return String.format(WebInspector.UIString(format), substitutions, formatters, initialValue, append);
518 WebInspector.openLinkExternallyLabel = function()
520 return WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Open link in new tab" : "Open Link in New Tab");
526 WebInspector.copyLinkAddressLabel = function()
528 return WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Copy link address" : "Copy Link Address");
531 WebInspector.installPortStyles = function()
533 var platform = WebInspector.platform();
534 document.body.classList.add("platform-" + platform);
535 var flavor = WebInspector.platformFlavor();
537 document.body.classList.add("platform-" + flavor);
538 var port = WebInspector.port();
539 document.body.classList.add("port-" + port);
542 WebInspector._windowFocused = function(event)
544 if (event.target.document.nodeType === Node.DOCUMENT_NODE)
545 document.body.classList.remove("inactive");
548 WebInspector._windowBlurred = function(event)
550 if (event.target.document.nodeType === Node.DOCUMENT_NODE)
551 document.body.classList.add("inactive");
557 WebInspector.previousFocusElement = function()
559 return WebInspector._previousFocusElement;
565 WebInspector.currentFocusElement = function()
567 return WebInspector._currentFocusElement;
570 WebInspector._focusChanged = function(event)
572 WebInspector.setCurrentFocusElement(event.target);
575 WebInspector._documentBlurred = function(event)
577 // We want to know when currentFocusElement loses focus to nowhere.
578 // This is the case when event.relatedTarget is null (no element is being focused)
579 // and document.activeElement is reset to default (this is not a window blur).
580 if (!event.relatedTarget && document.activeElement === document.body)
581 WebInspector.setCurrentFocusElement(null);
584 WebInspector._textInputTypes = ["text", "search", "tel", "url", "email", "password"].keySet();
585 WebInspector._isTextEditingElement = function(element)
587 if (element instanceof HTMLInputElement)
588 return element.type in WebInspector._textInputTypes;
590 if (element instanceof HTMLTextAreaElement)
596 WebInspector.setCurrentFocusElement = function(x)
598 if (WebInspector._glassPane && x && !WebInspector._glassPane.element.isAncestor(x))
600 if (WebInspector._currentFocusElement !== x)
601 WebInspector._previousFocusElement = WebInspector._currentFocusElement;
602 WebInspector._currentFocusElement = x;
604 if (WebInspector._currentFocusElement) {
605 WebInspector._currentFocusElement.focus();
607 // Make a caret selection inside the new element if there isn't a range selection and there isn't already a caret selection inside.
608 // This is needed (at least) to remove caret from console when focus is moved to some element in the panel.
609 // The code below should not be applied to text fields and text areas, hence _isTextEditingElement check.
610 var selection = window.getSelection();
611 if (!WebInspector._isTextEditingElement(WebInspector._currentFocusElement) && selection.isCollapsed && !WebInspector._currentFocusElement.isInsertionCaretInside()) {
612 var selectionRange = WebInspector._currentFocusElement.ownerDocument.createRange();
613 selectionRange.setStart(WebInspector._currentFocusElement, 0);
614 selectionRange.setEnd(WebInspector._currentFocusElement, 0);
616 selection.removeAllRanges();
617 selection.addRange(selectionRange);
619 } else if (WebInspector._previousFocusElement)
620 WebInspector._previousFocusElement.blur();
623 WebInspector.restoreFocusFromElement = function(element)
625 if (element && element.isSelfOrAncestor(WebInspector.currentFocusElement()))
626 WebInspector.setCurrentFocusElement(WebInspector.previousFocusElement());
629 WebInspector.setToolbarColors = function(backgroundColor, color)
631 if (!WebInspector._themeStyleElement) {
632 WebInspector._themeStyleElement = document.createElement("style");
633 document.head.appendChild(WebInspector._themeStyleElement);
635 var parsedColor = WebInspector.Color.parse(color);
636 var shadowColor = parsedColor ? parsedColor.invert().setAlpha(0.33).toString(WebInspector.Color.Format.RGBA) : "white";
637 var prefix = WebInspector.isMac() ? "body:not(.undocked)" : "";
638 WebInspector._themeStyleElement.textContent =
640 "%s .toolbar-background {\
641 background-image: none !important;\
642 background-color: %s !important;\
643 color: %s !important;\
644 }", prefix, backgroundColor, color) +
646 "%s .toolbar-background button.status-bar-item .glyph, %s .toolbar-background button.status-bar-item .long-click-glyph {\
647 background-color: %s;\
648 }", prefix, prefix, color) +
650 "%s .toolbar-background button.status-bar-item .glyph.shadow, %s .toolbar-background button.status-bar-item .long-click-glyph.shadow {\
651 background-color: %s;\
652 }", prefix, prefix, shadowColor);
655 WebInspector.resetToolbarColors = function()
657 if (WebInspector._themeStyleElement)
658 WebInspector._themeStyleElement.textContent = "";
662 * @param {!Element} element
663 * @param {number} offset
664 * @param {number} length
665 * @param {!Array.<!Object>=} domChanges
668 WebInspector.highlightSearchResult = function(element, offset, length, domChanges)
670 var result = WebInspector.highlightSearchResults(element, [new WebInspector.SourceRange(offset, length)], domChanges);
671 return result.length ? result[0] : null;
675 * @param {!Element} element
676 * @param {!Array.<!WebInspector.SourceRange>} resultRanges
677 * @param {!Array.<!Object>=} changes
678 * @return {!Array.<!Element>}
680 WebInspector.highlightSearchResults = function(element, resultRanges, changes)
682 return WebInspector.highlightRangesWithStyleClass(element, resultRanges, "highlighted-search-result", changes);
686 * @param {!Element} element
687 * @param {string} className
689 WebInspector.runCSSAnimationOnce = function(element, className)
691 function animationEndCallback()
693 element.classList.remove(className);
694 element.removeEventListener("animationend", animationEndCallback, false);
697 if (element.classList.contains(className))
698 element.classList.remove(className);
700 element.addEventListener("animationend", animationEndCallback, false);
701 element.classList.add(className);
705 * @param {!Element} element
706 * @param {!Array.<!WebInspector.SourceRange>} resultRanges
707 * @param {string} styleClass
708 * @param {!Array.<!Object>=} changes
709 * @return {!Array.<!Element>}
711 WebInspector.highlightRangesWithStyleClass = function(element, resultRanges, styleClass, changes)
713 changes = changes || [];
714 var highlightNodes = [];
715 var lineText = element.textContent;
716 var ownerDocument = element.ownerDocument;
717 var textNodeSnapshot = ownerDocument.evaluate(".//text()", element, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
719 var snapshotLength = textNodeSnapshot.snapshotLength;
720 if (snapshotLength === 0)
721 return highlightNodes;
724 var rangeEndOffset = 0;
725 for (var i = 0; i < snapshotLength; ++i) {
727 range.offset = rangeEndOffset;
728 range.length = textNodeSnapshot.snapshotItem(i).textContent.length;
729 rangeEndOffset = range.offset + range.length;
730 nodeRanges.push(range);
734 for (var i = 0; i < resultRanges.length; ++i) {
735 var startOffset = resultRanges[i].offset;
736 var endOffset = startOffset + resultRanges[i].length;
738 while (startIndex < snapshotLength && nodeRanges[startIndex].offset + nodeRanges[startIndex].length <= startOffset)
740 var endIndex = startIndex;
741 while (endIndex < snapshotLength && nodeRanges[endIndex].offset + nodeRanges[endIndex].length < endOffset)
743 if (endIndex === snapshotLength)
746 var highlightNode = ownerDocument.createElement("span");
747 highlightNode.className = styleClass;
748 highlightNode.textContent = lineText.substring(startOffset, endOffset);
750 var lastTextNode = textNodeSnapshot.snapshotItem(endIndex);
751 var lastText = lastTextNode.textContent;
752 lastTextNode.textContent = lastText.substring(endOffset - nodeRanges[endIndex].offset);
753 changes.push({ node: lastTextNode, type: "changed", oldText: lastText, newText: lastTextNode.textContent });
755 if (startIndex === endIndex) {
756 lastTextNode.parentElement.insertBefore(highlightNode, lastTextNode);
757 changes.push({ node: highlightNode, type: "added", nextSibling: lastTextNode, parent: lastTextNode.parentElement });
758 highlightNodes.push(highlightNode);
760 var prefixNode = ownerDocument.createTextNode(lastText.substring(0, startOffset - nodeRanges[startIndex].offset));
761 lastTextNode.parentElement.insertBefore(prefixNode, highlightNode);
762 changes.push({ node: prefixNode, type: "added", nextSibling: highlightNode, parent: lastTextNode.parentElement });
764 var firstTextNode = textNodeSnapshot.snapshotItem(startIndex);
765 var firstText = firstTextNode.textContent;
766 var anchorElement = firstTextNode.nextSibling;
768 firstTextNode.parentElement.insertBefore(highlightNode, anchorElement);
769 changes.push({ node: highlightNode, type: "added", nextSibling: anchorElement, parent: firstTextNode.parentElement });
770 highlightNodes.push(highlightNode);
772 firstTextNode.textContent = firstText.substring(0, startOffset - nodeRanges[startIndex].offset);
773 changes.push({ node: firstTextNode, type: "changed", oldText: firstText, newText: firstTextNode.textContent });
775 for (var j = startIndex + 1; j < endIndex; j++) {
776 var textNode = textNodeSnapshot.snapshotItem(j);
777 var text = textNode.textContent;
778 textNode.textContent = "";
779 changes.push({ node: textNode, type: "changed", oldText: text, newText: textNode.textContent });
782 startIndex = endIndex;
783 nodeRanges[startIndex].offset = endOffset;
784 nodeRanges[startIndex].length = lastTextNode.textContent.length;
787 return highlightNodes;
790 WebInspector.applyDomChanges = function(domChanges)
792 for (var i = 0, size = domChanges.length; i < size; ++i) {
793 var entry = domChanges[i];
794 switch (entry.type) {
796 entry.parent.insertBefore(entry.node, entry.nextSibling);
799 entry.node.textContent = entry.newText;
805 WebInspector.revertDomChanges = function(domChanges)
807 for (var i = domChanges.length - 1; i >= 0; --i) {
808 var entry = domChanges[i];
809 switch (entry.type) {
814 entry.node.textContent = entry.oldText;
822 * @param {boolean} autoInvoke
824 WebInspector.InvokeOnceHandlers = function(autoInvoke)
826 this._handlers = null;
827 this._autoInvoke = autoInvoke;
830 WebInspector.InvokeOnceHandlers.prototype = {
832 * @param {!Object} object
833 * @param {function()} method
835 add: function(object, method)
837 if (!this._handlers) {
838 this._handlers = new Map();
839 if (this._autoInvoke)
840 this.scheduleInvoke();
842 var methods = this._handlers.get(object);
845 this._handlers.put(object, methods);
850 scheduleInvoke: function()
853 requestAnimationFrame(this._invoke.bind(this));
858 var handlers = this._handlers;
859 this._handlers = null;
860 var keys = handlers.keys();
861 for (var i = 0; i < keys.length; ++i) {
862 var object = keys[i];
863 var methods = handlers.get(object).values();
864 for (var j = 0; j < methods.length; ++j)
865 methods[j].call(object);
870 WebInspector._coalescingLevel = 0;
871 WebInspector._postUpdateHandlers = null;
873 WebInspector.startBatchUpdate = function()
875 if (!WebInspector._coalescingLevel++)
876 WebInspector._postUpdateHandlers = new WebInspector.InvokeOnceHandlers(false);
879 WebInspector.endBatchUpdate = function()
881 if (--WebInspector._coalescingLevel)
883 WebInspector._postUpdateHandlers.scheduleInvoke();
884 WebInspector._postUpdateHandlers = null;
888 * @param {!Object} object
889 * @param {function()} method
891 WebInspector.invokeOnceAfterBatchUpdate = function(object, method)
893 if (!WebInspector._postUpdateHandlers)
894 WebInspector._postUpdateHandlers = new WebInspector.InvokeOnceHandlers(true);
895 WebInspector._postUpdateHandlers.add(object, method);
900 function windowLoaded()
902 window.addEventListener("focus", WebInspector._windowFocused, false);
903 window.addEventListener("blur", WebInspector._windowBlurred, false);
904 document.addEventListener("focus", WebInspector._focusChanged, true);
905 document.addEventListener("blur", WebInspector._documentBlurred, true);
906 window.removeEventListener("DOMContentLoaded", windowLoaded, false);
909 window.addEventListener("DOMContentLoaded", windowLoaded, false);