<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[193956] trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/193956">193956</a></dd>
<dt>Author</dt> <dd>nvasilyev@apple.com</dd>
<dt>Date</dt> <dd>2015-12-11 09:34:32 -0800 (Fri, 11 Dec 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Web Inspector: When logging strings, detect stack traces and show them as StackTraceView
https://bugs.webkit.org/show_bug.cgi?id=149790

Reviewed by Timothy Hatcher.

Source/WebInspectorUI:

* UserInterface/Models/StackTrace.js:
(WebInspector.StackTrace.isLikelyStackTrace): Added.

* UserInterface/Views/ConsoleMessageView.css:
(.console-message-extra-parameter .stack-trace):
Display stack trace view on the same line as a list bullet point from
console message extra parameter.

* UserInterface/Views/ConsoleMessageView.js:
(WebInspector.ConsoleMessageView.prototype._appendFormattedArguments):
Don't format with string substitutions for stack traces. E.g. there is
no need to replace %s with the next argument.

(WebInspector.ConsoleMessageView.prototype._isStackTrace): Added.
(WebInspector.ConsoleMessageView.prototype._formatParameterAsString):
Detect stack traces and format them appropriately.

LayoutTests:

* inspector/console/js-isLikelyStackTrace.html: Added.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkSourceWebInspectorUIChangeLog">trunk/Source/WebInspectorUI/ChangeLog</a></li>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceModelsStackTracejs">trunk/Source/WebInspectorUI/UserInterface/Models/StackTrace.js</a></li>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceViewsConsoleMessageViewcss">trunk/Source/WebInspectorUI/UserInterface/Views/ConsoleMessageView.css</a></li>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceViewsConsoleMessageViewjs">trunk/Source/WebInspectorUI/UserInterface/Views/ConsoleMessageView.js</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsinspectorconsolejsisLikelyStackTraceexpectedtxt">trunk/LayoutTests/inspector/console/js-isLikelyStackTrace-expected.txt</a></li>
<li><a href="#trunkLayoutTestsinspectorconsolejsisLikelyStackTracehtml">trunk/LayoutTests/inspector/console/js-isLikelyStackTrace.html</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (193955 => 193956)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2015-12-11 17:15:58 UTC (rev 193955)
+++ trunk/LayoutTests/ChangeLog        2015-12-11 17:34:32 UTC (rev 193956)
</span><span class="lines">@@ -1,3 +1,12 @@
</span><ins>+2015-12-11  Nikita Vasilyev  &lt;nvasilyev@apple.com&gt;
+
+        Web Inspector: When logging strings, detect stack traces and show them as StackTraceView
+        https://bugs.webkit.org/show_bug.cgi?id=149790
+
+        Reviewed by Timothy Hatcher.
+
+        * inspector/console/js-isLikelyStackTrace.html: Added.
+
</ins><span class="cx"> 2015-12-10  Brady Eidson  &lt;beidson@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Modern IDB: storage/indexeddb/objectstore-count.html fails.
</span></span></pre></div>
<a id="trunkLayoutTestsinspectorconsolejsisLikelyStackTraceexpectedtxt"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/inspector/console/js-isLikelyStackTrace-expected.txt (0 => 193956)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/inspector/console/js-isLikelyStackTrace-expected.txt                                (rev 0)
+++ trunk/LayoutTests/inspector/console/js-isLikelyStackTrace-expected.txt        2015-12-11 17:34:32 UTC (rev 193956)
</span><span class="lines">@@ -0,0 +1,15 @@
</span><ins>+Test stack trace detection heuristic.
+
+== Running test suite: WebInspector.StackTrace.isLikelyStackTrace
+-- Running test case: notStacktrace
+ASSERT: Should NOT be a stacktrace.
+
+-- Running test case: typeErrorWrap
+ASSERT: Should be a stacktrace.
+
+-- Running test case: getAnonymousFunctionError
+ASSERT: Should be a stacktrace.
+
+-- Running test case: testWithNativeCallInBetween
+ASSERT: Should be a stacktrace.
+
</ins></span></pre></div>
<a id="trunkLayoutTestsinspectorconsolejsisLikelyStackTracehtml"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/inspector/console/js-isLikelyStackTrace.html (0 => 193956)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/inspector/console/js-isLikelyStackTrace.html                                (rev 0)
+++ trunk/LayoutTests/inspector/console/js-isLikelyStackTrace.html        2015-12-11 17:34:32 UTC (rev 193956)
</span><span class="lines">@@ -0,0 +1,106 @@
</span><ins>+&lt;html&gt;
+&lt;head&gt;
+&lt;script src=&quot;../../http/tests/inspector/resources/inspector-test.js&quot;&gt;&lt;/script&gt;
+&lt;script&gt;
+function typeErrorWrap()
+{
+    return typeError();
+}
+
+function typeError()
+{
+    var object = {};
+    try {
+        object.propertyDoesnt.exist;
+    } catch(e) {
+        console.trace();
+        return e.stack;
+    }
+}
+
+function testWithNativeCallInBetween()
+{
+    return [42].map(typeError)[0];
+}
+
+var _anonymousFunctionError = null;
+
+(function() {
+    var object = {};
+    try {
+        object.methodDoesntExist();
+    } catch(e) {
+        _anonymousFunctionError = e.stack;
+    }
+})();
+
+function getAnonymousFunctionError()
+{
+    return _anonymousFunctionError;
+}
+
+function test()
+{
+    let suite = InspectorTest.createAsyncSuite(&quot;WebInspector.StackTrace.isLikelyStackTrace&quot;);
+
+    suite.addTestCase({
+        name: &quot;notStacktrace&quot;,
+        test: function(resolve, reject) {
+            var result = WebInspector.StackTrace.isLikelyStackTrace(&quot;Messages:42\nUnread:16&quot;);
+            InspectorTest.assert(result, &quot;Should NOT be a stacktrace.&quot;);
+            resolve();
+        }
+    });
+
+    suite.addTestCase({
+        name: &quot;typeErrorWrap&quot;,
+        test: function(resolve, reject) {
+            InspectorTest.evaluateInPage(&quot;typeErrorWrap()&quot;, function(error, result) {
+                if (error)
+                    reject(error);
+
+                InspectorTest.assert(WebInspector.StackTrace.isLikelyStackTrace(result), &quot;Should be a stacktrace.&quot;);
+
+                resolve();
+            });
+        }
+    });
+
+    suite.addTestCase({
+        name: &quot;getAnonymousFunctionError&quot;,
+        test: function(resolve, reject) {
+            InspectorTest.evaluateInPage(&quot;getAnonymousFunctionError()&quot;, function(error, result) {
+                if (error)
+                    reject(error);
+
+                InspectorTest.assert(WebInspector.StackTrace.isLikelyStackTrace(result), &quot;Should be a stacktrace.&quot;);
+
+                resolve();
+            });
+        }
+    });
+
+    suite.addTestCase({
+        name: &quot;testWithNativeCallInBetween&quot;,
+        test: function(resolve, reject) {
+            InspectorTest.evaluateInPage(&quot;testWithNativeCallInBetween()&quot;, function(error, result) {
+                if (error)
+                    reject(error);
+
+                InspectorTest.assert(WebInspector.StackTrace.isLikelyStackTrace(result), &quot;Should be a stacktrace.&quot;);
+
+                resolve();
+            });
+        }
+    });
+
+    suite.runTestCasesAndFinish();
+}
+&lt;/script&gt;
+&lt;/head&gt;
+&lt;body onload=&quot;runTest()&quot;&gt;
+&lt;p&gt;
+Test stack trace detection heuristic.&lt;br&gt;
+&lt;/p&gt;
+&lt;/body&gt;
+&lt;/html&gt;
</ins></span></pre></div>
<a id="trunkSourceWebInspectorUIChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/ChangeLog (193955 => 193956)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/ChangeLog        2015-12-11 17:15:58 UTC (rev 193955)
+++ trunk/Source/WebInspectorUI/ChangeLog        2015-12-11 17:34:32 UTC (rev 193956)
</span><span class="lines">@@ -1,3 +1,27 @@
</span><ins>+2015-12-11  Nikita Vasilyev  &lt;nvasilyev@apple.com&gt;
+
+        Web Inspector: When logging strings, detect stack traces and show them as StackTraceView
+        https://bugs.webkit.org/show_bug.cgi?id=149790
+
+        Reviewed by Timothy Hatcher.
+
+        * UserInterface/Models/StackTrace.js:
+        (WebInspector.StackTrace.isLikelyStackTrace): Added.
+
+        * UserInterface/Views/ConsoleMessageView.css:
+        (.console-message-extra-parameter .stack-trace):
+        Display stack trace view on the same line as a list bullet point from
+        console message extra parameter.
+
+        * UserInterface/Views/ConsoleMessageView.js:
+        (WebInspector.ConsoleMessageView.prototype._appendFormattedArguments):
+        Don't format with string substitutions for stack traces. E.g. there is
+        no need to replace %s with the next argument.
+
+        (WebInspector.ConsoleMessageView.prototype._isStackTrace): Added.
+        (WebInspector.ConsoleMessageView.prototype._formatParameterAsString):
+        Detect stack traces and format them appropriately.
+
</ins><span class="cx"> 2015-12-10  Nikita Vasilyev  &lt;nvasilyev@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Web Inspector: debugger dashboard's switching arrows are positioned too close to the dashboard border
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceModelsStackTracejs"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/UserInterface/Models/StackTrace.js (193955 => 193956)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/Models/StackTrace.js        2015-12-11 17:15:58 UTC (rev 193955)
+++ trunk/Source/WebInspectorUI/UserInterface/Models/StackTrace.js        2015-12-11 17:34:32 UTC (rev 193956)
</span><span class="lines">@@ -48,6 +48,31 @@
</span><span class="cx">         return WebInspector.StackTrace.fromPayload(payload);
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    // May produce false negatives; must not produce any false positives.
+    // It may return false on a valid stack trace, but it will never return true on an invalid stack trace.
+    static isLikelyStackTrace(stack)
+    {
+        // This function runs for every logged string. It penalizes the performance.
+        // As most logged strings are not stack traces, exit as early as possible.
+        const smallestPossibleStackTraceLength = &quot;http://a.bc/:9:1&quot;.length;
+        if (stack.length &lt; smallestPossibleStackTraceLength.length * 2)
+            return false;
+
+        const approximateStackLengthOf50Items = 5000;
+        if (stack.length &gt; approximateStackLengthOf50Items)
+            return false;
+
+        if (/^[^a-z$_]/i.test(stack[0]))
+            return false;
+
+        const reasonablyLongLineLength = 500;
+        const reasonablyLongNativeMethodLength = 120;
+        const stackTraceLine = `(.{1,${reasonablyLongLineLength}}:\\d+:\\d+|eval code|.{1,${reasonablyLongNativeMethodLength}}@\\[native code\\])`;
+        const stackTrace = new RegExp(`^${stackTraceLine}(\\n${stackTraceLine})*$`, &quot;g&quot;);
+
+        return stackTrace.test(stack);
+    }
+
</ins><span class="cx">     static _parseStackTrace(stack)
</span><span class="cx">     {
</span><span class="cx">         var lines = stack.split(/\n/g);
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceViewsConsoleMessageViewcss"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/UserInterface/Views/ConsoleMessageView.css (193955 => 193956)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/Views/ConsoleMessageView.css        2015-12-11 17:15:58 UTC (rev 193955)
+++ trunk/Source/WebInspectorUI/UserInterface/Views/ConsoleMessageView.css        2015-12-11 17:34:32 UTC (rev 193956)
</span><span class="lines">@@ -237,6 +237,10 @@
</span><span class="cx">     -webkit-user-select: text;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+.console-message-extra-parameter .stack-trace {
+    display: inline-block;
+}
+
</ins><span class="cx"> .console-message-location.call-frame {
</span><span class="cx">     display: inline-block;
</span><span class="cx">     -webkit-user-select: text;
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceViewsConsoleMessageViewjs"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/UserInterface/Views/ConsoleMessageView.js (193955 => 193956)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/Views/ConsoleMessageView.js        2015-12-11 17:15:58 UTC (rev 193955)
+++ trunk/Source/WebInspectorUI/UserInterface/Views/ConsoleMessageView.js        2015-12-11 17:34:32 UTC (rev 193956)
</span><span class="lines">@@ -419,6 +419,9 @@
</span><span class="cx"> 
</span><span class="cx">         console.assert(this._message.type !== WebInspector.ConsoleMessage.MessageType.Result);
</span><span class="cx"> 
</span><ins>+        if (shouldFormatWithStringSubstitution &amp;&amp; this._isStackTrace(parameters[0]))
+            shouldFormatWithStringSubstitution = false;
+
</ins><span class="cx">         // Format string / message / default message.
</span><span class="cx">         if (shouldFormatWithStringSubstitution) {
</span><span class="cx">             var result = this._formatWithSubstitutionString(parameters, builderElement);
</span><span class="lines">@@ -431,16 +434,19 @@
</span><span class="cx"> 
</span><span class="cx">         // Trailing parameters.
</span><span class="cx">         if (parameters.length) {
</span><del>-            if (parameters.length === 1) {
</del><ins>+            let enclosedElement = document.createElement(&quot;span&quot;);
+
+            if (parameters.length === 1 &amp;&amp; !this._isStackTrace(parameters[0])) {
+                let parameter = parameters[0];
+
</ins><span class="cx">                 // Single object. Show a preview.
</span><del>-                var enclosedElement = builderElement.appendChild(document.createElement(&quot;span&quot;));
</del><ins>+                builderElement.append(enclosedElement);
</ins><span class="cx">                 enclosedElement.classList.add(&quot;console-message-preview-divider&quot;);
</span><span class="cx">                 enclosedElement.textContent = &quot; \u2013 &quot;;
</span><span class="cx"> 
</span><span class="cx">                 var previewContainer = builderElement.appendChild(document.createElement(&quot;span&quot;));
</span><span class="cx">                 previewContainer.classList.add(&quot;console-message-preview&quot;);
</span><span class="cx"> 
</span><del>-                var parameter = parameters[0];
</del><span class="cx">                 var preview = WebInspector.FormattedValue.createObjectPreviewOrFormattedValueForRemoteObject(parameter, WebInspector.ObjectPreviewView.Mode.Brief);
</span><span class="cx">                 var isPreviewView = preview instanceof WebInspector.ObjectPreviewView;
</span><span class="cx"> 
</span><span class="lines">@@ -455,14 +461,23 @@
</span><span class="cx">                     this._extraParameters = null;
</span><span class="cx">             } else {
</span><span class="cx">                 // Multiple objects. Show an indicator.
</span><del>-                builderElement.append(&quot; &quot;);
-                var enclosedElement = builderElement.appendChild(document.createElement(&quot;span&quot;));
</del><ins>+                builderElement.append(&quot; &quot;, enclosedElement);
</ins><span class="cx">                 enclosedElement.classList.add(&quot;console-message-enclosed&quot;);
</span><span class="cx">                 enclosedElement.textContent = &quot;(&quot; + parameters.length + &quot;)&quot;;
</span><span class="cx">             }
</span><span class="cx">         }
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    _isStackTrace(parameter)
+    {
+        console.assert(parameter instanceof WebInspector.RemoteObject);
+
+        if (WebInspector.RemoteObject.type(parameter) !== &quot;string&quot;)
+            return false;
+
+        return WebInspector.StackTrace.isLikelyStackTrace(parameter.description);
+    }
+
</ins><span class="cx">     _shouldConsiderObjectLossless(object)
</span><span class="cx">     {
</span><span class="cx">         if (object.type === &quot;string&quot;) {
</span><span class="lines">@@ -515,6 +530,15 @@
</span><span class="cx"> 
</span><span class="cx">     _formatParameterAsString(object, fragment)
</span><span class="cx">     {
</span><ins>+        if (this._isStackTrace(object)) {
+            let stackTrace = WebInspector.StackTrace.fromString(object.description);
+            if (stackTrace.callFrames.length) {
+                let stackView = new WebInspector.StackTraceView(stackTrace);
+                fragment.appendChild(stackView.element);
+                return;
+            }
+        }
+
</ins><span class="cx">         fragment.appendChild(WebInspector.FormattedValue.createLinkifiedElementString(object.description));
</span><span class="cx">     }
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>