Web Inspector: MainScriptMapping should detect snippet scripts by means of sourceURL...
authorvsevik@chromium.org <vsevik@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 15 Mar 2012 18:26:11 +0000 (18:26 +0000)
committervsevik@chromium.org <vsevik@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 15 Mar 2012 18:26:11 +0000 (18:26 +0000)
https://bugs.webkit.org/show_bug.cgi?id=81031

Reviewed by Pavel Feldman.

Source/WebCore:

* inspector/front-end/SnippetsModel.js:
(WebInspector.SnippetsModel):
(WebInspector.SnippetsModel.prototype._saveSettings):
(WebInspector.SnippetsModel.prototype.deleteSnippet):
(WebInspector.SnippetsModel.prototype._snippetAdded):
(WebInspector.SnippetsModel.prototype._sourceURLForSnippet):
(WebInspector.SnippetsModel.prototype.snippetIdForSourceURL):
(WebInspector.SnippetsModel.prototype.snippetForSourceURL):
(WebInspector.Snippet.prototype.evaluate):
(WebInspector.SnippetsScriptMapping):
(WebInspector.SnippetsScriptMapping.prototype.uiLocationToRawLocation.get if):
(WebInspector.SnippetsScriptMapping.prototype.uiLocationToRawLocation):
(WebInspector.SnippetsScriptMapping.prototype._releasedUISourceCodes):
(WebInspector.SnippetsScriptMapping.prototype._snippetAdded):
(WebInspector.SnippetsScriptMapping.prototype._snippetWillBeEvaluated):
(WebInspector.SnippetsScriptMapping.prototype._snippetRemoved.get this):
(WebInspector.SnippetsScriptMapping.prototype._snippetRemoved):

LayoutTests:

* inspector/debugger/snippets-model-expected.txt:
* inspector/debugger/snippets-model.html:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@110864 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/inspector/debugger/snippets-model-expected.txt
LayoutTests/inspector/debugger/snippets-model.html
Source/WebCore/ChangeLog
Source/WebCore/inspector/front-end/SnippetsModel.js

index 57d106c..fcd78c4 100644 (file)
@@ -1,3 +1,13 @@
+2012-03-15  Vsevolod Vlasov  <vsevik@chromium.org>
+
+        Web Inspector: MainScriptMapping should detect snippet scripts by means of sourceURL set before evaluation.
+        https://bugs.webkit.org/show_bug.cgi?id=81031
+
+        Reviewed by Pavel Feldman.
+
+        * inspector/debugger/snippets-model-expected.txt:
+        * inspector/debugger/snippets-model.html:
+
 2012-03-15  Elliot Poger  <epoger@google.com>
 
         migrate Yang Guo's expectations from chromium to webkit
index d439351..ff267fd 100644 (file)
@@ -1,5 +1,7 @@
-Tests how snippets model stores, saves and loads snippets
+Tests snippets model.
 
+
+Running: testCreateRenameEditDelete
 Dumping saved snippets:
 Dumping model snippets:
 Snippet created.
@@ -33,3 +35,8 @@ Another snippet deleted.
 Dumping saved snippets:
 Dumping model snippets:
 
+Running: testEvaluate
+Last evaluation source url for snippet: snippets://1_1
+Last evaluation source url for snippet: snippets://2_2
+Last evaluation source url for snippet: snippets://1_3
+
index cbcdfc4..daa60c2 100644 (file)
 <script>
 function test()
 {
-    function dumpSnippets(snippets)
+    function resetSnippetsSettings()
     {
-        for (var i = 0; i < snippets.length; ++i) {
-            var snippet = snippets[i];
-            InspectorTest.addResult("    Snippet: id = " + snippet.id + ", name = '" + snippet.name + "', content = '" + snippet.content + "'.");
-        }
-    }
-
-    function dumpSavedSnippets()
-    {
-        InspectorTest.addResult("Dumping saved snippets:");
-        dumpSnippets(WebInspector.snippetsModel._snippetsSetting.get());
-    }
-
-    function dumpModelSnippets()
-    {
-        InspectorTest.addResult("Dumping model snippets:");
-        dumpSnippets(WebInspector.snippetsModel.snippets);
+        WebInspector.snippetsModel._lastSnippetIdentifierSetting.set(0);
+        WebInspector.snippetsModel._snippetsSetting.set([]);
+        WebInspector.snippetsModel._lastSnippetEvaluationIndexSetting.set(0);
     }
 
     // FIXME: Remove once snippets are taken out of experiments.
     if (!WebInspector.experimentsSettings.snippetsSupport.isEnabled())
         WebInspector.snippetsModel = new WebInspector.SnippetsModel();
 
-    dumpSavedSnippets();
-    dumpModelSnippets();
-    var snippet = WebInspector.snippetsModel.createSnippet();
-    InspectorTest.addResult("Snippet created.");
-    dumpSavedSnippets();
-    dumpModelSnippets();
-    snippet.name = "New snippet name";
-    InspectorTest.addResult("Snippet renamed.");
-    dumpSavedSnippets();
-    dumpModelSnippets();
-    snippet.content = "New snippet content";
-    InspectorTest.addResult("Snippet content changed.");
-    dumpSavedSnippets();
-    dumpModelSnippets();
-    var anotherSnippet = WebInspector.snippetsModel.createSnippet();
-    InspectorTest.addResult("Another snippet created.");
-    dumpSavedSnippets();
-    dumpModelSnippets();
-    WebInspector.snippetsModel.deleteSnippet(snippet);
-    InspectorTest.addResult("Snippet deleted.");
-    dumpSavedSnippets();
-    dumpModelSnippets();
-    WebInspector.snippetsModel.deleteSnippet(anotherSnippet);
-    InspectorTest.addResult("Another snippet deleted.");
-    dumpSavedSnippets();
-    dumpModelSnippets();
+    InspectorTest.runTestSuite([
+        function testCreateRenameEditDelete(next)
+        {
+            function dumpSnippets(snippets)
+            {
+                for (var i = 0; i < snippets.length; ++i) {
+                    var snippet = snippets[i];
+                    InspectorTest.addResult("    Snippet: id = " + snippet.id + ", name = '" + snippet.name + "', content = '" + snippet.content + "'.");
+                }
+            }
+
+            function dumpSavedSnippets()
+            {
+                InspectorTest.addResult("Dumping saved snippets:");
+                dumpSnippets(WebInspector.snippetsModel._snippetsSetting.get());
+            }
+
+            function dumpModelSnippets()
+            {
+                InspectorTest.addResult("Dumping model snippets:");
+                var snippets = [];
+                for (var id in WebInspector.snippetsModel._snippets)
+                    snippets.push(WebInspector.snippetsModel._snippets[id]);
+                dumpSnippets(snippets);
+            }
+
+            resetSnippetsSettings();
 
-    InspectorTest.completeTest();
+            dumpSavedSnippets();
+            dumpModelSnippets();
+            var snippet = WebInspector.snippetsModel.createSnippet();
+            InspectorTest.addResult("Snippet created.");
+            dumpSavedSnippets();
+            dumpModelSnippets();
+            snippet.name = "New snippet name";
+            InspectorTest.addResult("Snippet renamed.");
+            dumpSavedSnippets();
+            dumpModelSnippets();
+            snippet.content = "New snippet content";
+            InspectorTest.addResult("Snippet content changed.");
+            dumpSavedSnippets();
+            dumpModelSnippets();
+            var anotherSnippet = WebInspector.snippetsModel.createSnippet();
+            InspectorTest.addResult("Another snippet created.");
+            dumpSavedSnippets();
+            dumpModelSnippets();
+            WebInspector.snippetsModel.deleteSnippet(snippet);
+            InspectorTest.addResult("Snippet deleted.");
+            dumpSavedSnippets();
+            dumpModelSnippets();
+            WebInspector.snippetsModel.deleteSnippet(anotherSnippet);
+            InspectorTest.addResult("Another snippet deleted.");
+            dumpSavedSnippets();
+            dumpModelSnippets();
+
+            next();
+        },
+
+        function testEvaluate(next)
+        {
+            resetSnippetsSettings();
+
+            var snippet1 = WebInspector.snippetsModel.createSnippet();
+            snippet1.name = "Snippet1";
+            snippet1.content = "";
+            snippet1.content += "// This snippet does nothing.\n";
+            snippet1.content += "var i = 2+2;\n";
+            var snippet2 = WebInspector.snippetsModel.createSnippet();
+            snippet2.name = "Snippet2";
+            snippet2.content = "";
+            snippet2.content += "// This snippet creates a function that does nothing.\n";
+            snippet2.content += "function doesNothing()\n";
+            snippet2.content += "{\n";
+            snippet2.content += "    var  i = 2+2;\n";
+            snippet2.content += "}\n";
+
+            function evaluateSnippetAndDumpEvaluationDetails(snippet, callback)
+            {
+                InspectorTest.addSniffer(WebInspector.SnippetsScriptMapping.prototype, "addScript", dumpScript);
+                snippet.evaluate();
+                InspectorTest.addResult("Last evaluation source url for snippet: " + snippet._lastEvaluationSourceURL);
+                InspectorTest.assertEquals(snippet, WebInspector.snippetsModel.snippetForSourceURL(snippet._lastEvaluationSourceURL), "Snippet can not be identified by its evaluation sourceURL.");
+
+                function dumpScript(script)
+                {
+                    InspectorTest.addResult("Snippet script added, sourceURL = " + script.sourceURL);
+                    callback();
+                }
+
+                // FIXME: This should be removed once snippets evaluation itself is implemented.
+                callback();
+            }
+
+            evaluateSnippetAndDumpEvaluationDetails(snippet1, step2);
+
+            function step2()
+            {
+                evaluateSnippetAndDumpEvaluationDetails(snippet2, step3);
+            }
+
+            function step3()
+            {
+                evaluateSnippetAndDumpEvaluationDetails(snippet1, next);
+            }
+        }
+    ]);
 };
 </script>
 </head>
 <body onload="runTest()">
-<p>Tests how snippets model stores, saves and loads snippets</p>
+<p>Tests snippets model.</p>
 </body>
 </html>
index 0e54633..f364a18 100644 (file)
@@ -1,3 +1,28 @@
+2012-03-15  Vsevolod Vlasov  <vsevik@chromium.org>
+
+        Web Inspector: MainScriptMapping should detect snippet scripts by means of sourceURL set before evaluation.
+        https://bugs.webkit.org/show_bug.cgi?id=81031
+
+        Reviewed by Pavel Feldman.
+
+        * inspector/front-end/SnippetsModel.js:
+        (WebInspector.SnippetsModel):
+        (WebInspector.SnippetsModel.prototype._saveSettings):
+        (WebInspector.SnippetsModel.prototype.deleteSnippet):
+        (WebInspector.SnippetsModel.prototype._snippetAdded):
+        (WebInspector.SnippetsModel.prototype._sourceURLForSnippet):
+        (WebInspector.SnippetsModel.prototype.snippetIdForSourceURL):
+        (WebInspector.SnippetsModel.prototype.snippetForSourceURL):
+        (WebInspector.Snippet.prototype.evaluate):
+        (WebInspector.SnippetsScriptMapping):
+        (WebInspector.SnippetsScriptMapping.prototype.uiLocationToRawLocation.get if):
+        (WebInspector.SnippetsScriptMapping.prototype.uiLocationToRawLocation):
+        (WebInspector.SnippetsScriptMapping.prototype._releasedUISourceCodes):
+        (WebInspector.SnippetsScriptMapping.prototype._snippetAdded):
+        (WebInspector.SnippetsScriptMapping.prototype._snippetWillBeEvaluated):
+        (WebInspector.SnippetsScriptMapping.prototype._snippetRemoved.get this):
+        (WebInspector.SnippetsScriptMapping.prototype._snippetRemoved):
+
 2012-03-15  Kevin Ollivier  <kevino@theolliviers.com>
 
         [wx] Unreviewed. Build fixes for Mac and DOM bindings.
index 78f4ab3..9c7e48d 100644 (file)
  */
 WebInspector.SnippetsModel = function()
 {
-    /** @type {Array.<WebInspector.Snippet>} */ this._snippets;
+    this._snippets = {};
 
     this._lastSnippetIdentifierSetting = WebInspector.settings.createSetting("lastSnippetIdentifier", 0);
     this._snippetsSetting = WebInspector.settings.createSetting("snippets", []);
+    this._lastSnippetEvaluationIndexSetting = WebInspector.settings.createSetting("lastSnippetEvaluationIndex", 0);
 
     this._loadSettings();
 }
 
+WebInspector.SnippetsModel.snippetsSourceURLPrefix = "snippets://";
+
 WebInspector.SnippetsModel.EventTypes = {
     SnippetAdded: "SnippetAdded",
+    SnippetWillBeEvaluated: "SnippetWillBeEvaluated",
     SnippetRenamed: "SnippetRenamed",
     SnippetRemoved: "SnippetRemoved",
 }
 
 WebInspector.SnippetsModel.prototype = {
-    /**
-     * @return {Array.<WebInspector.Snippet>}
-     */
-    get snippets()
-    {
-        return this._snippets.slice();
-    },
-
     _saveSettings: function()
     {
         var savedSnippets = [];
-        for (var i = 0; i < this._snippets.length; ++i)
-            savedSnippets.push(this._snippets[i].serializeToObject());
-
+        for (var id in this._snippets)
+            savedSnippets.push(this._snippets[id].serializeToObject());
         this._snippetsSetting.set(savedSnippets);
     },
 
     _loadSettings: function()
     {
-        this._snippets = [];
         var savedSnippets = this._snippetsSetting.get();
         for (var i = 0; i < savedSnippets.length; ++i)
             this._snippetAdded(WebInspector.Snippet.fromObject(savedSnippets[i]));
@@ -77,14 +71,9 @@ WebInspector.SnippetsModel.prototype = {
      */
     deleteSnippet: function(snippet)
     {
-        for (var i = 0; i < this._snippets.length; ++i) {
-            if (snippet.id === this._snippets[i].id) {
-                this._snippets.splice(i, 1);
-                this._saveSettings();
-                this.dispatchEventToListeners(WebInspector.SnippetsModel.EventTypes.SnippetRemoved, snippet);
-                break;
-            }
-        }
+        delete this._snippets[snippet.id];
+        this._saveSettings();
+        this.dispatchEventToListeners(WebInspector.SnippetsModel.EventTypes.SnippetRemoved, snippet);
     },
 
     /**
@@ -92,8 +81,9 @@ WebInspector.SnippetsModel.prototype = {
      */
     createSnippet: function()
     {
-        var snippetId = this._lastSnippetIdentifierSetting.get() + 1;
-        this._lastSnippetIdentifierSetting.set(snippetId);
+        var nextId = this._lastSnippetIdentifierSetting.get() + 1;
+        var snippetId = String(nextId);
+        this._lastSnippetIdentifierSetting.set(nextId);
         var snippet = new WebInspector.Snippet(this, snippetId);
         this._snippetAdded(snippet);
         this._saveSettings();
@@ -106,12 +96,12 @@ WebInspector.SnippetsModel.prototype = {
      */
     _snippetAdded: function(snippet)
     {
-        this._snippets.push(snippet);
+        this._snippets[snippet.id] = snippet;
         this.dispatchEventToListeners(WebInspector.SnippetsModel.EventTypes.SnippetAdded, snippet);
     },
 
     /**
-     * @param {number} snippetId
+     * @param {string} snippetId
      */
     _snippetContentUpdated: function(snippetId)
     {
@@ -128,12 +118,30 @@ WebInspector.SnippetsModel.prototype = {
     },
 
     /**
-     * @param {number} snippetId
-     * @return {WebInspector.Snippet|undefined}
+     * @param {WebInspector.Snippet} snippet
+     */
+    _evaluateSnippet: function(snippet)
+    {
+        this.dispatchEventToListeners(WebInspector.SnippetsModel.EventTypes.SnippetWillBeEvaluated, snippet);
+        var evaluationIndex = this._lastSnippetEvaluationIndexSetting.get() + 1;
+        this._lastSnippetEvaluationIndexSetting.set(evaluationIndex);
+
+        var sourceURL = this._sourceURLForSnippet(snippet, evaluationIndex);
+        snippet._lastEvaluationSourceURL = sourceURL;
+        var expression = "\n//@ sourceURL=" + sourceURL + "\n" + snippet.content;
+        // FIXME: evaluate snippet here.
+    },
+
+    /**
+     * @param {WebInspector.Snippet} snippet
+     * @param {string} evaluationIndex
+     * @return {string}
      */
-    snippetForId: function(snippetId)
+    _sourceURLForSnippet: function(snippet, evaluationIndex)
     {
-        return this._snippets[snippetId];
+        var snippetsPrefix = WebInspector.SnippetsModel.snippetsSourceURLPrefix;
+        var evaluationSuffix = evaluationIndex ? "_" + evaluationIndex : "";
+        return snippetsPrefix + snippet.id + evaluationSuffix;
     },
 
     /**
@@ -142,8 +150,12 @@ WebInspector.SnippetsModel.prototype = {
      */
     snippetIdForSourceURL: function(sourceURL)
     {
-        // FIXME: to be implemented.
-        return null;
+        var snippetsPrefix = WebInspector.SnippetsModel.snippetsSourceURLPrefix;
+        if (sourceURL.indexOf(snippetsPrefix) !== 0)
+            return null;
+        var splittedURL = sourceURL.substring(snippetsPrefix.length).split("_");
+        var snippetId = splittedURL[0];
+        return snippetId;
     },
 
     /**
@@ -152,8 +164,13 @@ WebInspector.SnippetsModel.prototype = {
      */
     snippetForSourceURL: function(sourceURL)
     {
-        // FIXME: to be implemented.
-        return null;
+        var snippetId = this.snippetIdForSourceURL(sourceURL);
+        if (!snippetId)
+            return null;
+        var snippet = this._snippets[snippetId];
+        if (!snippet || snippet._lastEvaluationSourceURL !== sourceURL)
+            return null;
+        return snippet;
     }
 }
 
@@ -163,7 +180,7 @@ WebInspector.SnippetsModel.prototype.__proto__ = WebInspector.Object.prototype;
  * @constructor
  * @extends {WebInspector.Object}
  * @param {WebInspector.SnippetsModel} model
- * @param {number} id
+ * @param {string} id
  * @param {string=} name
  * @param {string=} content
  */
@@ -229,6 +246,11 @@ WebInspector.Snippet.prototype = {
         this._model._snippetContentUpdated(this._id);
     },
 
+    evaluate: function()
+    {
+        this._model._evaluateSnippet(this);
+    },
+
     /**
      * @return {Object}
      */
@@ -253,10 +275,10 @@ WebInspector.SnippetsScriptMapping = function()
     this._snippetForScriptId = {};
     this._uiSourceCodeForScriptId = {};
     this._scriptForUISourceCode = new Map();
-    this._snippetForUISourceCode = new Map();
     this._uiSourceCodeForSnippet = new Map();
     
     WebInspector.snippetsModel.addEventListener(WebInspector.SnippetsModel.EventTypes.SnippetAdded, this._snippetAdded.bind(this));
+    WebInspector.snippetsModel.addEventListener(WebInspector.SnippetsModel.EventTypes.SnippetWillBeEvaluated, this._snippetWillBeEvaluated.bind(this));
     WebInspector.snippetsModel.addEventListener(WebInspector.SnippetsModel.EventTypes.SnippetRemoved, this._snippetRemoved.bind(this));
 }
 
@@ -290,8 +312,7 @@ WebInspector.SnippetsScriptMapping.prototype = {
         if (!script)
             return null;
 
-        var snippet = this._snippetForUISourceCode.get(uiSourceCode);
-        if (snippet) {
+        if (uiSourceCode.isSnippet) {
             var rawLineNumber = lineNumber + WebInspector.Snippet.evaluatedSnippetExtraLinesCount;
             return WebInspector.debuggerModel.createRawLocation(script, rawLineNumber, columnNumber);
         }
@@ -319,7 +340,7 @@ WebInspector.SnippetsScriptMapping.prototype = {
         var result = [];
         for (var scriptId in this._uiSourceCodeForScriptId) {
             var uiSourceCode = this._uiSourceCodeForScriptId[scriptId];
-            if (this._snippetForUISourceCode.get(uiSourceCode))
+            if (uiSourceCode.isSnippet)
                 continue;
             result.push(uiSourceCode);
         }
@@ -336,8 +357,9 @@ WebInspector.SnippetsScriptMapping.prototype = {
             this._createUISourceCodeForScript(script);
             return;
         }
-        this._releaseSnippetScript(snippet);        
         var uiSourceCode = this._uiSourceCodeForSnippet.get(snippet);
+        console.assert(!this._scriptForUISourceCode.get(uiSourceCode));
+
         this._uiSourceCodeForScriptId[script.scriptId] = uiSourceCode;
         this._snippetForScriptId[script.scriptId] = snippet;
         this._scriptForUISourceCode.put(uiSourceCode, script);
@@ -357,12 +379,21 @@ WebInspector.SnippetsScriptMapping.prototype = {
         uiSourceCode.isSnippet = true;
         uiSourceCode.isEditable = true;
         this._uiSourceCodeForSnippet.put(snippet, uiSourceCode);
-        this._snippetForUISourceCode.put(uiSourceCode, snippet);
+        uiSourceCode.snippet = snippet;
         var data = { removedItems: [], addedItems: [uiSourceCode], scriptIds: [] };
         this.dispatchEventToListeners(WebInspector.ScriptMapping.Events.UISourceCodeListChanged, data);
     },
 
     /**
+     * @param {WebInspector.Event} event
+     */
+    _snippetWillBeEvaluated: function(event)
+    {
+        var snippet = /** @type {WebInspector.Snippet} */ event.data;
+        this._releaseSnippetScript(snippet);
+    },
+
+    /**
      * @param {WebInspector.Script} script
      */
     _createUISourceCodeForScript: function(script)
@@ -401,7 +432,6 @@ WebInspector.SnippetsScriptMapping.prototype = {
         var uiSourceCode = this._uiSourceCodeForSnippet.get(snippet);
         this._releaseSnippetScript(snippet);
         this._uiSourceCodeForSnippet.remove(snippet);
-        this._snippetForUISourceCode.remove(uiSourceCode);
         var data = { removedItems: [uiSourceCode], addedItems: [], scriptIds: [] };
         this.dispatchEventToListeners(WebInspector.ScriptMapping.Events.UISourceCodeListChanged, data);
     },