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