<!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>[37287] trunk/WebCore</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/37287">37287</a></dd>
<dt>Author</dt> <dd>timothy@apple.com</dd>
<dt>Date</dt> <dd>2008-10-04 01:09:41 -0700 (Sat, 04 Oct 2008)</dd>
</dl>

<h3>Log Message</h3>
<pre>        Changes how searching works in the Web Inspector's Elements
        panel. The search tasks are divided into chunks that are small
        units of work that are performed at a time interval. This
        change also prevents queries that will select all elements,
        since that isn't useful.

        https://bugs.webkit.org/show_bug.cgi?id=21285

        Reviewed by Oliver Hunt.

        * page/inspector/ElementsPanel.js:
        (WebInspector.ElementsPanel.prototype.searchCancled):
        Remove the searchResultsProperty form results since there might
        be an unfinished search.
        (WebInspector.ElementsPanel.prototype.performSearch): Divide the
        documents and search functions into chunks that are performed on
        a interval of 25ms. Prevent searches for &quot;//*&quot; and &quot;*&quot;.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkWebCoreChangeLog">trunk/WebCore/ChangeLog</a></li>
<li><a href="#trunkWebCorepageinspectorElementsPaneljs">trunk/WebCore/page/inspector/ElementsPanel.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/WebCore/ChangeLog (37286 => 37287)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/WebCore/ChangeLog        2008-10-04 08:00:42 UTC (rev 37286)
+++ trunk/WebCore/ChangeLog        2008-10-04 08:09:41 UTC (rev 37287)
</span><span class="lines">@@ -1,3 +1,23 @@
</span><ins>+2008-10-03  Timothy Hatcher  &lt;timothy@apple.com&gt;
+
+        Changes how searching works in the Web Inspector's Elements
+        panel. The search tasks are divided into chunks that are small
+        units of work that are performed at a time interval. This
+        change also prevents queries that will select all elements,
+        since that isn't useful.
+
+        https://bugs.webkit.org/show_bug.cgi?id=21285
+
+        Reviewed by Oliver Hunt.
+
+        * page/inspector/ElementsPanel.js:
+        (WebInspector.ElementsPanel.prototype.searchCancled):
+        Remove the searchResultsProperty form results since there might
+        be an unfinished search.
+        (WebInspector.ElementsPanel.prototype.performSearch): Divide the
+        documents and search functions into chunks that are performed on
+        a interval of 25ms. Prevent searches for &quot;//*&quot; and &quot;*&quot;.
+
</ins><span class="cx"> 2008-10-03  Maciej Stachowiak  &lt;mjs@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Reviewed by Cameron Zwarich.
</span></span></pre></div>
<a id="trunkWebCorepageinspectorElementsPaneljs"></a>
<div class="modfile"><h4>Modified: trunk/WebCore/page/inspector/ElementsPanel.js (37286 => 37287)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/WebCore/page/inspector/ElementsPanel.js        2008-10-04 08:00:42 UTC (rev 37286)
+++ trunk/WebCore/page/inspector/ElementsPanel.js        2008-10-04 08:09:41 UTC (rev 37287)
</span><span class="lines">@@ -217,11 +217,18 @@
</span><span class="cx">         }
</span><span class="cx">     },
</span><span class="cx"> 
</span><ins>+    includedInSearchResultsPropertyName: &quot;__includedInInspectorSearchResults&quot;,
+
</ins><span class="cx">     searchCanceled: function()
</span><span class="cx">     {
</span><span class="cx">         if (this._searchResults) {
</span><ins>+            const searchResultsProperty = this.includedInSearchResultsPropertyName;
</ins><span class="cx">             for (var i = 0; i &lt; this._searchResults.length; ++i) {
</span><span class="cx">                 var node = this._searchResults[i];
</span><ins>+
+                // Remove the searchResultsProperty since there might be an unfinished search.
+                delete node[searchResultsProperty];
+
</ins><span class="cx">                 var treeElement = this.treeOutline.findTreeElement(node);
</span><span class="cx">                 if (treeElement)
</span><span class="cx">                     treeElement.highlighted = false;
</span><span class="lines">@@ -230,6 +237,11 @@
</span><span class="cx"> 
</span><span class="cx">         WebInspector.updateSearchMatchesCount(0, this);
</span><span class="cx"> 
</span><ins>+        if (this._currentSearchChunkIntervalIdentifier) {
+            clearInterval(this._currentSearchChunkIntervalIdentifier);
+            delete this._currentSearchChunkIntervalIdentifier;
+        }
+
</ins><span class="cx">         this._currentSearchResultIndex = 0;
</span><span class="cx">         this._searchResults = [];
</span><span class="cx">     },
</span><span class="lines">@@ -239,81 +251,161 @@
</span><span class="cx">         // Call searchCanceled since it will reset everything we need before doing a new search.
</span><span class="cx">         this.searchCanceled();
</span><span class="cx"> 
</span><del>-        var escapedQuery = query.escapeCharacters(&quot;'&quot;);
-        var plainTextXPathQuery = &quot;//*[contains(name(),'&quot; + escapedQuery + &quot;') or contains(@*,'&quot; + escapedQuery + &quot;')] | //text()[contains(.,'&quot; + escapedQuery + &quot;')] | //comment()[contains(.,'&quot; + escapedQuery + &quot;')]&quot;;
</del><ins>+        const whitespaceTrimmedQuery = query.trimWhitespace();
+        if (!whitespaceTrimmedQuery.length)
+            return;
</ins><span class="cx"> 
</span><del>-        var evaluateFunction = InspectorController.inspectedWindow().Document.prototype.evaluate;
-        var querySelectorAllFunction = InspectorController.inspectedWindow().Document.prototype.querySelectorAll;
</del><ins>+        const escapedQuery = query.escapeCharacters(&quot;'&quot;);
+        const escapedWhitespaceTrimmedQuery = whitespaceTrimmedQuery.escapeCharacters(&quot;'&quot;);
+        const searchResultsProperty = this.includedInSearchResultsPropertyName;
</ins><span class="cx"> 
</span><del>-        const searchResultsProperty = &quot;__includedInInspectorSearchResults&quot;;
</del><ins>+        var updatedMatchCountOnce = false;
+        var matchesCountUpdateTimeout = null;
+
+        function updateMatchesCount()
+        {
+            WebInspector.updateSearchMatchesCount(this._searchResults.length, this);
+            matchesCountUpdateTimeout = null;
+            updatedMatchCountOnce = true;
+        }
+
+        function updateMatchesCountSoon()
+        {
+            if (!updatedMatchCountOnce)
+                return updateMatchesCount.call(this);
+            if (matchesCountUpdateTimeout)
+                return;
+            // Update the matches count every half-second so it doesn't feel twitchy.
+            matchesCountUpdateTimeout = setTimeout(updateMatchesCount.bind(this), 500);
+        }
+
</ins><span class="cx">         function addNodesToResults(nodes, length, getItem)
</span><span class="cx">         {
</span><ins>+            if (!length)
+                return;
+
</ins><span class="cx">             for (var i = 0; i &lt; length; ++i) {
</span><del>-                var node = getItem(nodes, i);
</del><ins>+                var node = getItem.call(nodes, i);
</ins><span class="cx">                 // Skip this node if it already has the property.
</span><span class="cx">                 if (searchResultsProperty in node)
</span><span class="cx">                     continue;
</span><ins>+
+                if (!this._searchResults.length) {
+                    this._currentSearchResultIndex = 0;
+                    this.focusedDOMNode = node;
+                }
+
</ins><span class="cx">                 node[searchResultsProperty] = true;
</span><span class="cx">                 this._searchResults.push(node);
</span><ins>+
+                // Highlight the tree element to show it matched the search.
+                // FIXME: highlight the substrings in text nodes and attributes.
+                var treeElement = this.treeOutline.findTreeElement(node);
+                if (treeElement)
+                    treeElement.highlighted = true;
</ins><span class="cx">             }
</span><ins>+
+            updateMatchesCountSoon.call(this);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><del>-        function searchDocument(doc)
</del><ins>+        function matchPartialTagNamesAndAttributeValues(doc)
</ins><span class="cx">         {
</span><del>-            try {
-                var result = evaluateFunction.call(doc, plainTextXPathQuery, doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
-                addNodesToResults.call(this, result, result.snapshotLength, function(l, i) { return l.snapshotItem(i); });
-            } catch(err) {
-                // ignore any exceptions. the query might be malformed, but we allow that.
-            }
</del><ins>+            const result = doc.__proto__.evaluate.call(doc, &quot;//*[contains(name(), '&quot; + escapedWhitespaceTrimmedQuery + &quot;') or contains(@*, '&quot; + escapedQuery + &quot;')]&quot;, doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
+            addNodesToResults.call(this, result, result.snapshotLength, result.snapshotItem);
+        }
</ins><span class="cx"> 
</span><del>-            try {
-                var result = evaluateFunction.call(doc, query, doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
-                addNodesToResults.call(this, result, result.snapshotLength, function(l, i) { return l.snapshotItem(i); });
-            } catch(err) {
-                // ignore any exceptions. the query might be malformed, but we allow that.
-            }
</del><ins>+        function matchPartialAttributeValues(doc)
+        {
+            const result = doc.__proto__.evaluate.call(doc, &quot;//*[contains(@*, '&quot; + escapedQuery + &quot;')]&quot;, doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
+            addNodesToResults.call(this, result, result.snapshotLength, result.snapshotItem);
+        }
</ins><span class="cx"> 
</span><del>-            try {
-                var result = querySelectorAllFunction.call(doc, query);
-                addNodesToResults.call(this, result, result.length, function(l, i) { return l.item(i); });
-            } catch(err) {
-                // ignore any exceptions. the query isn't necessarily a valid selector.
-            }
</del><ins>+        function matchStyleSelector(doc)
+        {
+            const result = doc.__proto__.querySelectorAll.call(doc, whitespaceTrimmedQuery);
+            addNodesToResults.call(this, result, result.length, result.item);
+        }
</ins><span class="cx"> 
</span><ins>+        function matchPlainText(doc)
+        {
+            const result = doc.__proto__.evaluate.call(doc, &quot;//text()[contains(., '&quot; + escapedQuery + &quot;')] | //comment()[contains(., '&quot; + escapedQuery + &quot;')]&quot;, doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
+            addNodesToResults.call(this, result, result.snapshotLength, result.snapshotItem);
+        }
+
+        function matchXPathQuery(doc)
+        {
+            const result = doc.__proto__.evaluate.call(doc, whitespaceTrimmedQuery, doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
+            addNodesToResults.call(this, result, result.snapshotLength, result.snapshotItem);
+        }
+
+        function finishedSearching()
+        {
</ins><span class="cx">             // Remove the searchResultsProperty now that the search is finished.
</span><span class="cx">             for (var i = 0; i &lt; this._searchResults.length; ++i)
</span><span class="cx">                 delete this._searchResults[i][searchResultsProperty];
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        var mainFrameDocument = InspectorController.inspectedWindow().document;
</del><ins>+        const mainFrameDocument = InspectorController.inspectedWindow().document;
+        const searchDocuments = [mainFrameDocument];
</ins><span class="cx"> 
</span><del>-        searchDocument.call(this, mainFrameDocument);
</del><ins>+        if (whitespaceTrimmedQuery === &quot;//*&quot; || whitespaceTrimmedQuery === &quot;*&quot;) {
+            // These queries will match every node. Matching everything isn't useful and can be slow for large pages,
+            // so limit the search functions list to plain text and attribute matching.
+            const searchFunctions = [matchPartialAttributeValues, matchPlainText];
+        } else
+            const searchFunctions = [matchStyleSelector, matchPartialTagNamesAndAttributeValues, matchPlainText, matchXPathQuery];
</ins><span class="cx"> 
</span><span class="cx">         // Find all frames, iframes and object elements to search their documents.
</span><del>-        var subdocumentQuery = &quot;//iframe | //frame | //object&quot;;
-        var subdocumentResult = evaluateFunction.call(mainFrameDocument, subdocumentQuery, mainFrameDocument, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
</del><ins>+        const querySelectorAllFunction = InspectorController.inspectedWindow().Document.prototype.querySelectorAll;
+        const subdocumentResult = querySelectorAllFunction.call(mainFrameDocument, &quot;iframe, frame, object&quot;);
</ins><span class="cx"> 
</span><del>-        for (var i = 0; i &lt; subdocumentResult.snapshotLength; ++i) {
-            var element = subdocumentResult.snapshotItem(i);
</del><ins>+        for (var i = 0; i &lt; subdocumentResult.length; ++i) {
+            var element = subdocumentResult.item(i);
</ins><span class="cx">             if (element.contentDocument)
</span><del>-                searchDocument.call(this, element.contentDocument);
</del><ins>+                searchDocuments.push(element.contentDocument);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><del>-        if (!this._searchResults.length)
-            return;
</del><ins>+        const panel = this;
+        var documentIndex = 0;
+        var searchFunctionIndex = 0;
+        var chunkIntervalIdentifier = null;
</ins><span class="cx"> 
</span><del>-        for (var i = 0; i &lt; this._searchResults.length; ++i) {
-            var node = this._searchResults[i];
-            var treeElement = this.treeOutline.findTreeElement(node);
-            if (treeElement)
-                treeElement.highlighted = true;
</del><ins>+        // Split up the work into chunks so we don't block the UI thread while processing.
+
+        function processChunk()
+        {
+            var searchDocument = searchDocuments[documentIndex];
+            var searchFunction = searchFunctions[searchFunctionIndex];
+
+            if (++searchFunctionIndex &gt; searchFunctions.length) {
+                searchFunction = searchFunctions[0];
+                searchFunctionIndex = 0;
+
+                if (++documentIndex &gt; searchDocuments.length) {
+                    if (panel._currentSearchChunkIntervalIdentifier === chunkIntervalIdentifier)
+                        delete panel._currentSearchChunkIntervalIdentifier;
+                    clearInterval(chunkIntervalIdentifier);
+                    finishedSearching.call(panel);
+                    return;
+                }
+
+                searchDocument = searchDocuments[documentIndex];
+            }
+
+            if (!searchDocument || !searchFunction)
+                return;
+
+            try {
+                searchFunction.call(panel, searchDocument);
+            } catch(err) {
+                // ignore any exceptions. the query might be malformed, but we allow that.
+            }
</ins><span class="cx">         }
</span><span class="cx"> 
</span><del>-        WebInspector.updateSearchMatchesCount(this._searchResults.length, this);
</del><ins>+        processChunk();
</ins><span class="cx"> 
</span><del>-        this._currentSearchResultIndex = 0;
-        this.focusedDOMNode = this._searchResults[0];
</del><ins>+        chunkIntervalIdentifier = setInterval(processChunk, 25);
+        this._currentSearchChunkIntervalIdentifier = chunkIntervalIdentifier;
</ins><span class="cx">     },
</span><span class="cx"> 
</span><span class="cx">     jumpToNextSearchResult: function()
</span></span></pre>
</div>
</div>

</body>
</html>