OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2009 Joseph Pecoraro |
| 4 * |
| 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions |
| 7 * are met: |
| 8 * |
| 9 * 1. Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. |
| 11 * 2. Redistributions in binary form must reproduce the above copyright |
| 12 * notice, this list of conditions and the following disclaimer in the |
| 13 * documentation and/or other materials provided with the distribution. |
| 14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of |
| 15 * its contributors may be used to endorse or promote products derived |
| 16 * from this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 */ |
| 29 |
| 30 WebInspector.ConsoleView = function(drawer) |
| 31 { |
| 32 WebInspector.View.call(this, document.getElementById("console-view")); |
| 33 |
| 34 this.messages = []; |
| 35 this.drawer = drawer; |
| 36 |
| 37 this.clearButton = document.getElementById("clear-console-status-bar-item"); |
| 38 this.clearButton.title = WebInspector.UIString("Clear console log."); |
| 39 this.clearButton.addEventListener("click", this._clearButtonClicked.bind(thi
s), false); |
| 40 |
| 41 this.messagesElement = document.getElementById("console-messages"); |
| 42 this.messagesElement.addEventListener("selectstart", this._messagesSelectSta
rt.bind(this), false); |
| 43 this.messagesElement.addEventListener("click", this._messagesClicked.bind(th
is), true); |
| 44 |
| 45 this.promptElement = document.getElementById("console-prompt"); |
| 46 this.promptElement.handleKeyEvent = this._promptKeyDown.bind(this); |
| 47 this.prompt = new WebInspector.TextPrompt(this.promptElement, this.completio
ns.bind(this), " .=:[({;"); |
| 48 |
| 49 this.topGroup = new WebInspector.ConsoleGroup(null, 0); |
| 50 this.messagesElement.insertBefore(this.topGroup.element, this.promptElement)
; |
| 51 this.groupLevel = 0; |
| 52 this.currentGroup = this.topGroup; |
| 53 |
| 54 this.toggleConsoleButton = document.getElementById("console-status-bar-item"
); |
| 55 this.toggleConsoleButton.title = WebInspector.UIString("Show console."); |
| 56 this.toggleConsoleButton.addEventListener("click", this._toggleConsoleButton
Clicked.bind(this), false); |
| 57 |
| 58 var anchoredStatusBar = document.getElementById("anchored-status-bar-items")
; |
| 59 anchoredStatusBar.appendChild(this.toggleConsoleButton); |
| 60 |
| 61 // Will hold the list of filter elements |
| 62 this.filterBarElement = document.getElementById("console-filter"); |
| 63 |
| 64 function createFilterElement(category) { |
| 65 var categoryElement = document.createElement("li"); |
| 66 categoryElement.category = category; |
| 67 |
| 68 categoryElement.addStyleClass(categoryElement.category); |
| 69 |
| 70 var label = category.toString(); |
| 71 categoryElement.appendChild(document.createTextNode(label)); |
| 72 |
| 73 categoryElement.addEventListener("click", this._updateFilter.bind(this),
false); |
| 74 |
| 75 this.filterBarElement.appendChild(categoryElement); |
| 76 return categoryElement; |
| 77 } |
| 78 |
| 79 this.allElement = createFilterElement.call(this, "All"); |
| 80 this.errorElement = createFilterElement.call(this, "Errors"); |
| 81 this.warningElement = createFilterElement.call(this, "Warnings"); |
| 82 this.logElement = createFilterElement.call(this, "Logs"); |
| 83 |
| 84 this.filter(this.allElement); |
| 85 } |
| 86 |
| 87 WebInspector.ConsoleView.prototype = { |
| 88 |
| 89 _updateFilter: function(e) |
| 90 { |
| 91 this.filter(e.target); |
| 92 }, |
| 93 |
| 94 filter: function(target) |
| 95 { |
| 96 if (target.category == "All") { |
| 97 if (target.hasStyleClass("selected")) { |
| 98 // We can't unselect all, so we break early here |
| 99 return; |
| 100 } |
| 101 |
| 102 this.errorElement.removeStyleClass("selected"); |
| 103 this.warningElement.removeStyleClass("selected"); |
| 104 this.logElement.removeStyleClass("selected"); |
| 105 |
| 106 document.getElementById("console-messages").removeStyleClass("filter
-errors"); |
| 107 document.getElementById("console-messages").removeStyleClass("filter
-warnings"); |
| 108 document.getElementById("console-messages").removeStyleClass("filter
-logs"); |
| 109 } else { |
| 110 // Something other than all is being selected, so we want to unselec
t all |
| 111 if (this.allElement.hasStyleClass("selected")) { |
| 112 this.allElement.removeStyleClass("selected"); |
| 113 document.getElementById("console-messages").removeStyleClass("fi
lter-all"); |
| 114 } |
| 115 } |
| 116 |
| 117 if (target.hasStyleClass("selected")) { |
| 118 target.removeStyleClass("selected"); |
| 119 var newClass = "filter-" + target.category.toLowerCase(); |
| 120 var filterElement = document.getElementById("console-messages"); |
| 121 filterElement.removeStyleClass(newClass); |
| 122 } else { |
| 123 target.addStyleClass("selected"); |
| 124 var newClass = "filter-" + target.category.toLowerCase(); |
| 125 var filterElement = document.getElementById("console-messages"); |
| 126 filterElement.addStyleClass(newClass); |
| 127 } |
| 128 }, |
| 129 |
| 130 _toggleConsoleButtonClicked: function() |
| 131 { |
| 132 this.drawer.visibleView = this; |
| 133 }, |
| 134 |
| 135 attach: function(mainElement, statusBarElement) |
| 136 { |
| 137 mainElement.appendChild(this.element); |
| 138 statusBarElement.appendChild(this.clearButton); |
| 139 statusBarElement.appendChild(this.filterBarElement); |
| 140 }, |
| 141 |
| 142 show: function() |
| 143 { |
| 144 this.toggleConsoleButton.addStyleClass("toggled-on"); |
| 145 this.toggleConsoleButton.title = WebInspector.UIString("Hide console."); |
| 146 if (!this.prompt.isCaretInsidePrompt()) |
| 147 this.prompt.moveCaretToEndOfPrompt(); |
| 148 }, |
| 149 |
| 150 afterShow: function() |
| 151 { |
| 152 WebInspector.currentFocusElement = this.promptElement; |
| 153 }, |
| 154 |
| 155 hide: function() |
| 156 { |
| 157 this.toggleConsoleButton.removeStyleClass("toggled-on"); |
| 158 this.toggleConsoleButton.title = WebInspector.UIString("Show console."); |
| 159 }, |
| 160 |
| 161 addMessage: function(msg) |
| 162 { |
| 163 if (msg instanceof WebInspector.ConsoleMessage && !(msg instanceof WebIn
spector.ConsoleCommandResult)) { |
| 164 msg.totalRepeatCount = msg.repeatCount; |
| 165 msg.repeatDelta = msg.repeatCount; |
| 166 |
| 167 var messageRepeated = false; |
| 168 |
| 169 if (msg.isEqual && msg.isEqual(this.previousMessage)) { |
| 170 // Because sometimes we get a large number of repeated messages
and sometimes |
| 171 // we get them one at a time, we need to know the difference bet
ween how many |
| 172 // repeats we used to have and how many we have now. |
| 173 msg.repeatDelta -= this.previousMessage.totalRepeatCount; |
| 174 |
| 175 if (!isNaN(this.repeatCountBeforeCommand)) |
| 176 msg.repeatCount -= this.repeatCountBeforeCommand; |
| 177 |
| 178 if (!this.commandSincePreviousMessage) { |
| 179 // Recreate the previous message element to reset the repeat
count. |
| 180 var messagesElement = this.currentGroup.messagesElement; |
| 181 messagesElement.removeChild(messagesElement.lastChild); |
| 182 messagesElement.appendChild(msg.toMessageElement()); |
| 183 |
| 184 messageRepeated = true; |
| 185 } |
| 186 } else |
| 187 delete this.repeatCountBeforeCommand; |
| 188 |
| 189 // Increment the error or warning count |
| 190 switch (msg.level) { |
| 191 case WebInspector.ConsoleMessage.MessageLevel.Warning: |
| 192 WebInspector.warnings += msg.repeatDelta; |
| 193 break; |
| 194 case WebInspector.ConsoleMessage.MessageLevel.Error: |
| 195 WebInspector.errors += msg.repeatDelta; |
| 196 break; |
| 197 } |
| 198 |
| 199 // Add message to the resource panel |
| 200 if (msg.url in WebInspector.resourceURLMap) { |
| 201 msg.resource = WebInspector.resourceURLMap[msg.url]; |
| 202 if (WebInspector.panels.resources) |
| 203 WebInspector.panels.resources.addMessageToResource(msg.resou
rce, msg); |
| 204 } |
| 205 |
| 206 this.commandSincePreviousMessage = false; |
| 207 this.previousMessage = msg; |
| 208 |
| 209 if (messageRepeated) |
| 210 return; |
| 211 } else if (msg instanceof WebInspector.ConsoleCommand) { |
| 212 if (this.previousMessage) { |
| 213 this.commandSincePreviousMessage = true; |
| 214 this.repeatCountBeforeCommand = this.previousMessage.totalRepeat
Count; |
| 215 } |
| 216 } |
| 217 |
| 218 this.messages.push(msg); |
| 219 |
| 220 if (msg.type === WebInspector.ConsoleMessage.MessageType.EndGroup) { |
| 221 if (this.groupLevel < 1) |
| 222 return; |
| 223 |
| 224 this.groupLevel--; |
| 225 |
| 226 this.currentGroup = this.currentGroup.parentGroup; |
| 227 } else { |
| 228 if (msg.type === WebInspector.ConsoleMessage.MessageType.StartGroup)
{ |
| 229 this.groupLevel++; |
| 230 |
| 231 var group = new WebInspector.ConsoleGroup(this.currentGroup, thi
s.groupLevel); |
| 232 this.currentGroup.messagesElement.appendChild(group.element); |
| 233 this.currentGroup = group; |
| 234 } |
| 235 |
| 236 this.currentGroup.addMessage(msg); |
| 237 } |
| 238 |
| 239 this.promptElement.scrollIntoView(false); |
| 240 }, |
| 241 |
| 242 clearMessages: function(clearInspectorController) |
| 243 { |
| 244 if (clearInspectorController) |
| 245 InspectorController.clearMessages(false); |
| 246 if (WebInspector.panels.resources) |
| 247 WebInspector.panels.resources.clearMessages(); |
| 248 |
| 249 this.messages = []; |
| 250 |
| 251 this.groupLevel = 0; |
| 252 this.currentGroup = this.topGroup; |
| 253 this.topGroup.messagesElement.removeChildren(); |
| 254 |
| 255 WebInspector.errors = 0; |
| 256 WebInspector.warnings = 0; |
| 257 |
| 258 delete this.commandSincePreviousMessage; |
| 259 delete this.repeatCountBeforeCommand; |
| 260 delete this.previousMessage; |
| 261 }, |
| 262 |
| 263 completions: function(wordRange, bestMatchOnly, completionsReadyCallback) |
| 264 { |
| 265 // Pass less stop characters to rangeOfWord so the range will be a more
complete expression. |
| 266 const expressionStopCharacters = " =:{;"; |
| 267 var expressionRange = wordRange.startContainer.rangeOfWord(wordRange.sta
rtOffset, expressionStopCharacters, this.promptElement, "backward"); |
| 268 var expressionString = expressionRange.toString(); |
| 269 var lastIndex = expressionString.length - 1; |
| 270 |
| 271 var dotNotation = (expressionString[lastIndex] === "."); |
| 272 var bracketNotation = (expressionString[lastIndex] === "["); |
| 273 |
| 274 if (dotNotation || bracketNotation) |
| 275 expressionString = expressionString.substr(0, lastIndex); |
| 276 |
| 277 var prefix = wordRange.toString(); |
| 278 if (!expressionString && !prefix) |
| 279 return; |
| 280 |
| 281 var reportCompletions = this._reportCompletions.bind(this, bestMatchOnly
, completionsReadyCallback, dotNotation, bracketNotation, prefix); |
| 282 // Collect comma separated object properties for the completion. |
| 283 |
| 284 if (!expressionString) { |
| 285 if (WebInspector.panels.scripts && WebInspector.panels.scripts.pause
d) { |
| 286 // Evaluate into properties in scope of the selected call frame. |
| 287 reportCompletions(WebInspector.panels.scripts.variablesInSelecte
dCallFrame()); |
| 288 return; |
| 289 } else { |
| 290 expressionString = "this"; |
| 291 } |
| 292 } |
| 293 |
| 294 var includeInspectorCommandLineAPI = (!dotNotation && !bracketNotation); |
| 295 InjectedScriptAccess.getCompletions(expressionString, includeInspectorCo
mmandLineAPI, reportCompletions); |
| 296 }, |
| 297 |
| 298 _reportCompletions: function(bestMatchOnly, completionsReadyCallback, dotNot
ation, bracketNotation, prefix, result, isException) { |
| 299 if (isException) |
| 300 return; |
| 301 |
| 302 if (bracketNotation) { |
| 303 if (prefix.length && prefix[0] === "'") |
| 304 var quoteUsed = "'"; |
| 305 else |
| 306 var quoteUsed = "\""; |
| 307 } |
| 308 |
| 309 var results = []; |
| 310 var properties = Object.sortedProperties(result); |
| 311 |
| 312 for (var i = 0; i < properties.length; ++i) { |
| 313 var property = properties[i]; |
| 314 |
| 315 if (dotNotation && !/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(property)) |
| 316 continue; |
| 317 |
| 318 if (bracketNotation) { |
| 319 if (!/^[0-9]+$/.test(property)) |
| 320 property = quoteUsed + property.escapeCharacters(quoteUsed +
"\\") + quoteUsed; |
| 321 property += "]"; |
| 322 } |
| 323 |
| 324 if (property.length < prefix.length) |
| 325 continue; |
| 326 if (property.indexOf(prefix) !== 0) |
| 327 continue; |
| 328 |
| 329 results.push(property); |
| 330 if (bestMatchOnly) |
| 331 break; |
| 332 } |
| 333 setTimeout(completionsReadyCallback, 0, results); |
| 334 }, |
| 335 |
| 336 _clearButtonClicked: function() |
| 337 { |
| 338 this.clearMessages(true); |
| 339 }, |
| 340 |
| 341 _messagesSelectStart: function(event) |
| 342 { |
| 343 if (this._selectionTimeout) |
| 344 clearTimeout(this._selectionTimeout); |
| 345 |
| 346 this.prompt.clearAutoComplete(); |
| 347 |
| 348 function moveBackIfOutside() |
| 349 { |
| 350 delete this._selectionTimeout; |
| 351 if (!this.prompt.isCaretInsidePrompt() && window.getSelection().isCo
llapsed) |
| 352 this.prompt.moveCaretToEndOfPrompt(); |
| 353 this.prompt.autoCompleteSoon(); |
| 354 } |
| 355 |
| 356 this._selectionTimeout = setTimeout(moveBackIfOutside.bind(this), 100); |
| 357 }, |
| 358 |
| 359 _messagesClicked: function(event) |
| 360 { |
| 361 var link = event.target.enclosingNodeOrSelfWithNodeName("a"); |
| 362 if (!link || !link.representedNode) |
| 363 return; |
| 364 |
| 365 WebInspector.updateFocusedNode(link.representedNode.id); |
| 366 event.stopPropagation(); |
| 367 event.preventDefault(); |
| 368 }, |
| 369 |
| 370 _promptKeyDown: function(event) |
| 371 { |
| 372 switch (event.keyIdentifier) { |
| 373 case "Enter": |
| 374 this._enterKeyPressed(event); |
| 375 return; |
| 376 } |
| 377 |
| 378 this.prompt.handleKeyEvent(event); |
| 379 }, |
| 380 |
| 381 evalInInspectedWindow: function(expression, callback) |
| 382 { |
| 383 if (WebInspector.panels.scripts && WebInspector.panels.scripts.paused) { |
| 384 WebInspector.panels.scripts.evaluateInSelectedCallFrame(expression,
false, callback); |
| 385 return; |
| 386 } |
| 387 this.doEvalInWindow(expression, callback); |
| 388 }, |
| 389 |
| 390 doEvalInWindow: function(expression, callback) |
| 391 { |
| 392 if (!expression) { |
| 393 // There is no expression, so the completion should happen against g
lobal properties. |
| 394 expression = "this"; |
| 395 } |
| 396 |
| 397 function evalCallback(result) |
| 398 { |
| 399 callback(result.value, result.isException); |
| 400 }; |
| 401 InjectedScriptAccess.evaluate(expression, evalCallback); |
| 402 }, |
| 403 |
| 404 _enterKeyPressed: function(event) |
| 405 { |
| 406 if (event.altKey) |
| 407 return; |
| 408 |
| 409 event.preventDefault(); |
| 410 event.stopPropagation(); |
| 411 |
| 412 this.prompt.clearAutoComplete(true); |
| 413 |
| 414 var str = this.prompt.text; |
| 415 if (!str.length) |
| 416 return; |
| 417 |
| 418 var commandMessage = new WebInspector.ConsoleCommand(str); |
| 419 this.addMessage(commandMessage); |
| 420 |
| 421 var self = this; |
| 422 function printResult(result, exception) |
| 423 { |
| 424 self.prompt.history.push(str); |
| 425 self.prompt.historyOffset = 0; |
| 426 self.prompt.text = ""; |
| 427 self.addMessage(new WebInspector.ConsoleCommandResult(result, except
ion, commandMessage)); |
| 428 } |
| 429 this.evalInInspectedWindow(str, printResult); |
| 430 }, |
| 431 |
| 432 _format: function(output, forceObjectFormat) |
| 433 { |
| 434 var isProxy = (output != null && typeof output === "object"); |
| 435 |
| 436 if (forceObjectFormat) |
| 437 var type = "object"; |
| 438 else |
| 439 var type = Object.proxyType(output); |
| 440 |
| 441 if (isProxy && type !== "object" && type !== "function" && type !== "arr
ay" && type !== "node") { |
| 442 // Unwrap primitive value, skip decoration. |
| 443 output = output.description; |
| 444 type = "undecorated" |
| 445 } |
| 446 |
| 447 // We don't perform any special formatting on these types, so we just |
| 448 // pass them through the simple _formatvalue function. |
| 449 var undecoratedTypes = { |
| 450 "undefined": 1, |
| 451 "null": 1, |
| 452 "boolean": 1, |
| 453 "number": 1, |
| 454 "undecorated": 1 |
| 455 }; |
| 456 |
| 457 var formatter; |
| 458 if (forceObjectFormat) |
| 459 formatter = "_formatobject"; |
| 460 else if (type in undecoratedTypes) |
| 461 formatter = "_formatvalue"; |
| 462 else { |
| 463 formatter = "_format" + type; |
| 464 if (!(formatter in this)) { |
| 465 formatter = "_formatobject"; |
| 466 type = "object"; |
| 467 } |
| 468 } |
| 469 |
| 470 var span = document.createElement("span"); |
| 471 span.addStyleClass("console-formatted-" + type); |
| 472 this[formatter](output, span); |
| 473 return span; |
| 474 }, |
| 475 |
| 476 _formatvalue: function(val, elem) |
| 477 { |
| 478 elem.appendChild(document.createTextNode(val)); |
| 479 }, |
| 480 |
| 481 _formatfunction: function(func, elem) |
| 482 { |
| 483 elem.appendChild(document.createTextNode(func.description)); |
| 484 }, |
| 485 |
| 486 _formatdate: function(date, elem) |
| 487 { |
| 488 elem.appendChild(document.createTextNode(date)); |
| 489 }, |
| 490 |
| 491 _formatstring: function(str, elem) |
| 492 { |
| 493 elem.appendChild(document.createTextNode("\"" + str + "\"")); |
| 494 }, |
| 495 |
| 496 _formatregexp: function(re, elem) |
| 497 { |
| 498 var formatted = String(re.description).replace(/([\\\/])/g, "\\$1").repl
ace(/\\(\/[gim]*)$/, "$1").substring(1); |
| 499 elem.appendChild(document.createTextNode(formatted)); |
| 500 }, |
| 501 |
| 502 _formatarray: function(arr, elem) |
| 503 { |
| 504 var self = this; |
| 505 function printResult(properties) |
| 506 { |
| 507 if (!properties) |
| 508 return; |
| 509 elem.appendChild(document.createTextNode("[")); |
| 510 for (var i = 0; i < properties.length; ++i) { |
| 511 var property = properties[i].value; |
| 512 elem.appendChild(self._format(property)); |
| 513 if (i < properties.length - 1) |
| 514 elem.appendChild(document.createTextNode(", ")); |
| 515 } |
| 516 elem.appendChild(document.createTextNode("]")); |
| 517 } |
| 518 InjectedScriptAccess.getProperties(arr, false, printResult); |
| 519 }, |
| 520 |
| 521 _formatnode: function(object, elem) |
| 522 { |
| 523 function printNode(nodeId) |
| 524 { |
| 525 if (!nodeId) |
| 526 return; |
| 527 var treeOutline = new WebInspector.ElementsTreeOutline(); |
| 528 treeOutline.rootDOMNode = WebInspector.domAgent.nodeForId(nodeId); |
| 529 treeOutline.element.addStyleClass("outline-disclosure"); |
| 530 if (!treeOutline.children[0].hasChildren) |
| 531 treeOutline.element.addStyleClass("single-node"); |
| 532 elem.appendChild(treeOutline.element); |
| 533 } |
| 534 InjectedScriptAccess.pushNodeToFrontend(object, printNode); |
| 535 }, |
| 536 |
| 537 _formatobject: function(obj, elem) |
| 538 { |
| 539 elem.appendChild(new WebInspector.ObjectPropertiesSection(obj, obj.descr
iption, null, true).element); |
| 540 }, |
| 541 |
| 542 _formaterror: function(obj, elem) |
| 543 { |
| 544 var messageElement = document.createElement("span"); |
| 545 messageElement.className = "error-message"; |
| 546 messageElement.textContent = obj.name + ": " + obj.message; |
| 547 elem.appendChild(messageElement); |
| 548 |
| 549 if (obj.sourceURL) { |
| 550 var urlElement = document.createElement("a"); |
| 551 urlElement.className = "webkit-html-resource-link"; |
| 552 urlElement.href = obj.sourceURL; |
| 553 urlElement.lineNumber = obj.line; |
| 554 urlElement.preferredPanel = "scripts"; |
| 555 |
| 556 if (obj.line > 0) |
| 557 urlElement.textContent = WebInspector.displayNameForURL(obj.sour
ceURL) + ":" + obj.line; |
| 558 else |
| 559 urlElement.textContent = WebInspector.displayNameForURL(obj.sour
ceURL); |
| 560 |
| 561 elem.appendChild(document.createTextNode(" (")); |
| 562 elem.appendChild(urlElement); |
| 563 elem.appendChild(document.createTextNode(")")); |
| 564 } |
| 565 } |
| 566 } |
| 567 |
| 568 WebInspector.ConsoleView.prototype.__proto__ = WebInspector.View.prototype; |
| 569 |
| 570 WebInspector.ConsoleMessage = function(source, type, level, line, url, groupLeve
l, repeatCount) |
| 571 { |
| 572 this.source = source; |
| 573 this.type = type; |
| 574 this.level = level; |
| 575 this.line = line; |
| 576 this.url = url; |
| 577 this.groupLevel = groupLevel; |
| 578 this.repeatCount = repeatCount; |
| 579 if (arguments.length > 7) |
| 580 this.setMessageBody(Array.prototype.slice.call(arguments, 7)); |
| 581 } |
| 582 |
| 583 WebInspector.ConsoleMessage.prototype = { |
| 584 setMessageBody: function(args) |
| 585 { |
| 586 switch (this.type) { |
| 587 case WebInspector.ConsoleMessage.MessageType.Trace: |
| 588 var span = document.createElement("span"); |
| 589 span.addStyleClass("console-formatted-trace"); |
| 590 var stack = Array.prototype.slice.call(args); |
| 591 var funcNames = stack.map(function(f) { |
| 592 return f || WebInspector.UIString("(anonymous function)"); |
| 593 }); |
| 594 span.appendChild(document.createTextNode(funcNames.join("\n"))); |
| 595 this.formattedMessage = span; |
| 596 break; |
| 597 case WebInspector.ConsoleMessage.MessageType.Object: |
| 598 this.formattedMessage = this._format(["%O", args[0]]); |
| 599 break; |
| 600 default: |
| 601 this.formattedMessage = this._format(args); |
| 602 break; |
| 603 } |
| 604 |
| 605 // This is used for inline message bubbles in SourceFrames, or other pla
in-text representations. |
| 606 this.message = this.formattedMessage.textContent; |
| 607 }, |
| 608 |
| 609 isErrorOrWarning: function() |
| 610 { |
| 611 return (this.level === WebInspector.ConsoleMessage.MessageLevel.Warning
|| this.level === WebInspector.ConsoleMessage.MessageLevel.Error); |
| 612 }, |
| 613 |
| 614 _format: function(parameters) |
| 615 { |
| 616 var formattedResult = document.createElement("span"); |
| 617 |
| 618 if (!parameters.length) |
| 619 return formattedResult; |
| 620 |
| 621 function formatForConsole(obj) |
| 622 { |
| 623 return WebInspector.console._format(obj); |
| 624 } |
| 625 |
| 626 function formatAsObjectForConsole(obj) |
| 627 { |
| 628 return WebInspector.console._format(obj, true); |
| 629 } |
| 630 |
| 631 if (typeof parameters[0] === "string") { |
| 632 var formatters = {} |
| 633 for (var i in String.standardFormatters) |
| 634 formatters[i] = String.standardFormatters[i]; |
| 635 |
| 636 // Firebug uses %o for formatting objects. |
| 637 formatters.o = formatForConsole; |
| 638 // Firebug allows both %i and %d for formatting integers. |
| 639 formatters.i = formatters.d; |
| 640 // Support %O to force object formating, instead of the type-based %
o formatting. |
| 641 formatters.O = formatAsObjectForConsole; |
| 642 |
| 643 function append(a, b) |
| 644 { |
| 645 if (!(b instanceof Node)) |
| 646 a.appendChild(WebInspector.linkifyStringAsFragment(b.toStrin
g())); |
| 647 else |
| 648 a.appendChild(b); |
| 649 return a; |
| 650 } |
| 651 |
| 652 var result = String.format(parameters[0], parameters.slice(1), forma
tters, formattedResult, append); |
| 653 formattedResult = result.formattedResult; |
| 654 parameters = result.unusedSubstitutions; |
| 655 if (parameters.length) |
| 656 formattedResult.appendChild(document.createTextNode(" ")); |
| 657 } |
| 658 |
| 659 for (var i = 0; i < parameters.length; ++i) { |
| 660 if (typeof parameters[i] === "string") |
| 661 formattedResult.appendChild(WebInspector.linkifyStringAsFragment
(parameters[i])); |
| 662 else |
| 663 formattedResult.appendChild(formatForConsole(parameters[i])); |
| 664 |
| 665 if (i < parameters.length - 1) |
| 666 formattedResult.appendChild(document.createTextNode(" ")); |
| 667 } |
| 668 |
| 669 return formattedResult; |
| 670 }, |
| 671 |
| 672 toMessageElement: function() |
| 673 { |
| 674 if (this.propertiesSection) |
| 675 return this.propertiesSection.element; |
| 676 |
| 677 var element = document.createElement("div"); |
| 678 element.message = this; |
| 679 element.className = "console-message"; |
| 680 |
| 681 switch (this.source) { |
| 682 case WebInspector.ConsoleMessage.MessageSource.HTML: |
| 683 element.addStyleClass("console-html-source"); |
| 684 break; |
| 685 case WebInspector.ConsoleMessage.MessageSource.WML: |
| 686 element.addStyleClass("console-wml-source"); |
| 687 break; |
| 688 case WebInspector.ConsoleMessage.MessageSource.XML: |
| 689 element.addStyleClass("console-xml-source"); |
| 690 break; |
| 691 case WebInspector.ConsoleMessage.MessageSource.JS: |
| 692 element.addStyleClass("console-js-source"); |
| 693 break; |
| 694 case WebInspector.ConsoleMessage.MessageSource.CSS: |
| 695 element.addStyleClass("console-css-source"); |
| 696 break; |
| 697 case WebInspector.ConsoleMessage.MessageSource.Other: |
| 698 element.addStyleClass("console-other-source"); |
| 699 break; |
| 700 } |
| 701 |
| 702 switch (this.level) { |
| 703 case WebInspector.ConsoleMessage.MessageLevel.Tip: |
| 704 element.addStyleClass("console-tip-level"); |
| 705 break; |
| 706 case WebInspector.ConsoleMessage.MessageLevel.Log: |
| 707 element.addStyleClass("console-log-level"); |
| 708 break; |
| 709 case WebInspector.ConsoleMessage.MessageLevel.Debug: |
| 710 element.addStyleClass("console-debug-level"); |
| 711 break; |
| 712 case WebInspector.ConsoleMessage.MessageLevel.Warning: |
| 713 element.addStyleClass("console-warning-level"); |
| 714 break; |
| 715 case WebInspector.ConsoleMessage.MessageLevel.Error: |
| 716 element.addStyleClass("console-error-level"); |
| 717 break; |
| 718 } |
| 719 |
| 720 if (this.type === WebInspector.ConsoleMessage.MessageType.StartGroup) { |
| 721 element.addStyleClass("console-group-title"); |
| 722 } |
| 723 |
| 724 if (this.elementsTreeOutline) { |
| 725 element.addStyleClass("outline-disclosure"); |
| 726 element.appendChild(this.elementsTreeOutline.element); |
| 727 return element; |
| 728 } |
| 729 |
| 730 if (this.repeatCount > 1) { |
| 731 var messageRepeatCountElement = document.createElement("span"); |
| 732 messageRepeatCountElement.className = "bubble"; |
| 733 messageRepeatCountElement.textContent = this.repeatCount; |
| 734 |
| 735 element.appendChild(messageRepeatCountElement); |
| 736 element.addStyleClass("repeated-message"); |
| 737 } |
| 738 |
| 739 if (this.url && this.url !== "undefined") { |
| 740 var urlElement = document.createElement("a"); |
| 741 urlElement.className = "console-message-url webkit-html-resource-lin
k"; |
| 742 urlElement.href = this.url; |
| 743 urlElement.lineNumber = this.line; |
| 744 |
| 745 if (this.source === WebInspector.ConsoleMessage.MessageSource.JS) |
| 746 urlElement.preferredPanel = "scripts"; |
| 747 |
| 748 if (this.line > 0) |
| 749 urlElement.textContent = WebInspector.displayNameForURL(this.url
) + ":" + this.line; |
| 750 else |
| 751 urlElement.textContent = WebInspector.displayNameForURL(this.url
); |
| 752 |
| 753 element.appendChild(urlElement); |
| 754 } |
| 755 |
| 756 var messageTextElement = document.createElement("span"); |
| 757 messageTextElement.className = "console-message-text"; |
| 758 messageTextElement.appendChild(this.formattedMessage); |
| 759 element.appendChild(messageTextElement); |
| 760 |
| 761 return element; |
| 762 }, |
| 763 |
| 764 toString: function() |
| 765 { |
| 766 var sourceString; |
| 767 switch (this.source) { |
| 768 case WebInspector.ConsoleMessage.MessageSource.HTML: |
| 769 sourceString = "HTML"; |
| 770 break; |
| 771 case WebInspector.ConsoleMessage.MessageSource.WML: |
| 772 sourceString = "WML"; |
| 773 break; |
| 774 case WebInspector.ConsoleMessage.MessageSource.XML: |
| 775 sourceString = "XML"; |
| 776 break; |
| 777 case WebInspector.ConsoleMessage.MessageSource.JS: |
| 778 sourceString = "JS"; |
| 779 break; |
| 780 case WebInspector.ConsoleMessage.MessageSource.CSS: |
| 781 sourceString = "CSS"; |
| 782 break; |
| 783 case WebInspector.ConsoleMessage.MessageSource.Other: |
| 784 sourceString = "Other"; |
| 785 break; |
| 786 } |
| 787 |
| 788 var typeString; |
| 789 switch (this.type) { |
| 790 case WebInspector.ConsoleMessage.MessageType.Log: |
| 791 typeString = "Log"; |
| 792 break; |
| 793 case WebInspector.ConsoleMessage.MessageType.Object: |
| 794 typeString = "Object"; |
| 795 break; |
| 796 case WebInspector.ConsoleMessage.MessageType.Trace: |
| 797 typeString = "Trace"; |
| 798 break; |
| 799 case WebInspector.ConsoleMessage.MessageType.StartGroup: |
| 800 typeString = "Start Group"; |
| 801 break; |
| 802 case WebInspector.ConsoleMessage.MessageType.EndGroup: |
| 803 typeString = "End Group"; |
| 804 break; |
| 805 } |
| 806 |
| 807 var levelString; |
| 808 switch (this.level) { |
| 809 case WebInspector.ConsoleMessage.MessageLevel.Tip: |
| 810 levelString = "Tip"; |
| 811 break; |
| 812 case WebInspector.ConsoleMessage.MessageLevel.Log: |
| 813 levelString = "Log"; |
| 814 break; |
| 815 case WebInspector.ConsoleMessage.MessageLevel.Warning: |
| 816 levelString = "Warning"; |
| 817 break; |
| 818 case WebInspector.ConsoleMessage.MessageLevel.Debug: |
| 819 levelString = "Debug"; |
| 820 break; |
| 821 case WebInspector.ConsoleMessage.MessageLevel.Error: |
| 822 levelString = "Error"; |
| 823 break; |
| 824 } |
| 825 |
| 826 return sourceString + " " + typeString + " " + levelString + ": " + this
.formattedMessage.textContent + "\n" + this.url + " line " + this.line; |
| 827 }, |
| 828 |
| 829 isEqual: function(msg, disreguardGroup) |
| 830 { |
| 831 if (!msg) |
| 832 return false; |
| 833 |
| 834 var ret = (this.source == msg.source) |
| 835 && (this.type == msg.type) |
| 836 && (this.level == msg.level) |
| 837 && (this.line == msg.line) |
| 838 && (this.url == msg.url) |
| 839 && (this.message == msg.message); |
| 840 |
| 841 return (disreguardGroup ? ret : (ret && (this.groupLevel == msg.groupLev
el))); |
| 842 } |
| 843 } |
| 844 |
| 845 // Note: Keep these constants in sync with the ones in Console.h |
| 846 WebInspector.ConsoleMessage.MessageSource = { |
| 847 HTML: 0, |
| 848 WML: 1, |
| 849 XML: 2, |
| 850 JS: 3, |
| 851 CSS: 4, |
| 852 Other: 5 |
| 853 } |
| 854 |
| 855 WebInspector.ConsoleMessage.MessageType = { |
| 856 Log: 0, |
| 857 Object: 1, |
| 858 Trace: 2, |
| 859 StartGroup: 3, |
| 860 EndGroup: 4 |
| 861 } |
| 862 |
| 863 WebInspector.ConsoleMessage.MessageLevel = { |
| 864 Tip: 0, |
| 865 Log: 1, |
| 866 Warning: 2, |
| 867 Error: 3, |
| 868 Debug: 4 |
| 869 } |
| 870 |
| 871 WebInspector.ConsoleCommand = function(command) |
| 872 { |
| 873 this.command = command; |
| 874 } |
| 875 |
| 876 WebInspector.ConsoleCommand.prototype = { |
| 877 toMessageElement: function() |
| 878 { |
| 879 var element = document.createElement("div"); |
| 880 element.command = this; |
| 881 element.className = "console-user-command"; |
| 882 |
| 883 var commandTextElement = document.createElement("span"); |
| 884 commandTextElement.className = "console-message-text"; |
| 885 commandTextElement.textContent = this.command; |
| 886 element.appendChild(commandTextElement); |
| 887 |
| 888 return element; |
| 889 } |
| 890 } |
| 891 |
| 892 WebInspector.ConsoleTextMessage = function(text, level) |
| 893 { |
| 894 level = level || WebInspector.ConsoleMessage.MessageLevel.Log; |
| 895 WebInspector.ConsoleMessage.call(this, WebInspector.ConsoleMessage.MessageSo
urce.JS, WebInspector.ConsoleMessage.MessageType.Log, level, 0, null, null, 1, t
ext); |
| 896 } |
| 897 |
| 898 WebInspector.ConsoleTextMessage.prototype.__proto__ = WebInspector.ConsoleMessag
e.prototype; |
| 899 |
| 900 WebInspector.ConsoleCommandResult = function(result, exception, originatingComma
nd) |
| 901 { |
| 902 var level = (exception ? WebInspector.ConsoleMessage.MessageLevel.Error : We
bInspector.ConsoleMessage.MessageLevel.Log); |
| 903 var message = (exception ? String(result) : result); |
| 904 var line = (exception ? result.line : -1); |
| 905 var url = (exception ? result.sourceURL : null); |
| 906 |
| 907 WebInspector.ConsoleMessage.call(this, WebInspector.ConsoleMessage.MessageSo
urce.JS, WebInspector.ConsoleMessage.MessageType.Log, level, line, url, null, 1,
message); |
| 908 |
| 909 this.originatingCommand = originatingCommand; |
| 910 } |
| 911 |
| 912 WebInspector.ConsoleCommandResult.prototype = { |
| 913 toMessageElement: function() |
| 914 { |
| 915 var element = WebInspector.ConsoleMessage.prototype.toMessageElement.cal
l(this); |
| 916 element.addStyleClass("console-user-command-result"); |
| 917 return element; |
| 918 } |
| 919 } |
| 920 |
| 921 WebInspector.ConsoleCommandResult.prototype.__proto__ = WebInspector.ConsoleMess
age.prototype; |
| 922 |
| 923 WebInspector.ConsoleGroup = function(parentGroup, level) |
| 924 { |
| 925 this.parentGroup = parentGroup; |
| 926 this.level = level; |
| 927 |
| 928 var element = document.createElement("div"); |
| 929 element.className = "console-group"; |
| 930 element.group = this; |
| 931 this.element = element; |
| 932 |
| 933 var messagesElement = document.createElement("div"); |
| 934 messagesElement.className = "console-group-messages"; |
| 935 element.appendChild(messagesElement); |
| 936 this.messagesElement = messagesElement; |
| 937 } |
| 938 |
| 939 WebInspector.ConsoleGroup.prototype = { |
| 940 addMessage: function(msg) |
| 941 { |
| 942 var element = msg.toMessageElement(); |
| 943 |
| 944 if (msg.type === WebInspector.ConsoleMessage.MessageType.StartGroup) { |
| 945 this.messagesElement.parentNode.insertBefore(element, this.messagesE
lement); |
| 946 element.addEventListener("click", this._titleClicked.bind(this), tru
e); |
| 947 } else |
| 948 this.messagesElement.appendChild(element); |
| 949 |
| 950 if (element.previousSibling && msg.originatingCommand && element.previou
sSibling.command === msg.originatingCommand) |
| 951 element.previousSibling.addStyleClass("console-adjacent-user-command
-result"); |
| 952 }, |
| 953 |
| 954 _titleClicked: function(event) |
| 955 { |
| 956 var groupTitleElement = event.target.enclosingNodeOrSelfWithClass("conso
le-group-title"); |
| 957 if (groupTitleElement) { |
| 958 var groupElement = groupTitleElement.enclosingNodeOrSelfWithClass("c
onsole-group"); |
| 959 if (groupElement) |
| 960 if (groupElement.hasStyleClass("collapsed")) |
| 961 groupElement.removeStyleClass("collapsed"); |
| 962 else |
| 963 groupElement.addStyleClass("collapsed"); |
| 964 groupTitleElement.scrollIntoViewIfNeeded(true); |
| 965 } |
| 966 |
| 967 event.stopPropagation(); |
| 968 event.preventDefault(); |
| 969 } |
| 970 } |
OLD | NEW |