Title: [285711] trunk/Source/WebInspectorUI
Revision
285711
Author
rcali...@apple.com
Date
2021-11-12 02:54:05 -0800 (Fri, 12 Nov 2021)

Log Message

Web Inspector: Extract reusable logic from ResourceQueryController, ResourceQueryResult and ResourceQueryMatch
https://bugs.webkit.org/show_bug.cgi?id=231604
<rdar://problem/84160281>

Reviewed by Devin Rousso.

Extract reusable logic from `ResourceQueryController` into a generic `QueryController`
to enable subclassing for other specialized use cases.

* UserInterface/Controllers/QueryController.js: Added.
(WI.QueryController.prototype.executeQuery):
(WI.QueryController.prototype.findQueryMatches.pushMatch):
(WI.QueryController.prototype.findQueryMatches.matchNextSpecialCharacter):
(WI.QueryController.prototype.findQueryMatches.backtrack):
(WI.QueryController.prototype.findQueryMatches):
(WI.QueryController):

Keep only the reusable matching logic in `QueryController`.
Subclasses like `ResourceQueryController` are responsible for agregating
the data to be queried, customization for special characters and sorting results.

* UserInterface/Controllers/ResourceQueryController.js:
(WI.ResourceQueryController.prototype.executeQuery):
(WI.ResourceQueryController.prototype._findQueryMatches.pushMatch): Deleted.
(WI.ResourceQueryController.prototype._findQueryMatches.matchNextSpecialCharacter): Deleted.
(WI.ResourceQueryController.prototype._findQueryMatches.backtrack): Deleted.
(WI.ResourceQueryController.prototype._findQueryMatches): Deleted.

* UserInterface/Main.html:

* UserInterface/Models/QueryMatch.js: Renamed from Source/WebInspectorUI/UserInterface/Models/ResourceQueryMatch.js.

`ResourceQueryMatch` doesn't contain any resource-specific logic. It can be generalized to `QueryMatch`.

* UserInterface/Models/QueryResult.js: Copied from Source/WebInspectorUI/UserInterface/Models/ResourceQueryResult.js.
(WI.QueryResult):
(WI.QueryResult.prototype.get value):
(WI.QueryResult.prototype.get rank):
(WI.QueryResult.prototype.get matchingTextRanges):
(WI.QueryResult.prototype._calculateRank.getMultiplier):
(WI.QueryResult.prototype._calculateRank):
(WI.QueryResult.prototype._createMatchingTextRanges):

A generic `QueryResult` can be extracted from `ResourceQueryResult` containing
the reusable logic for ranking results and identifing matching text ranges.

* UserInterface/Models/ResourceQueryResult.js:
(WI.ResourceQueryResult):
(WI.ResourceQueryResult.prototype.get resource):
(WI.ResourceQueryResult.prototype.__test_createMatchesMask):
(WI.ResourceQueryResult.prototype.get rank): Deleted.
(WI.ResourceQueryResult.prototype.get matchingTextRanges): Deleted.
(WI.ResourceQueryResult.prototype._calculateRank.getMultiplier): Deleted.
(WI.ResourceQueryResult.prototype._calculateRank): Deleted.
(WI.ResourceQueryResult.prototype._createMatchingTextRanges): Deleted.

`ResourceQueryResult` extends `QueryResult` with resource-specifc members:
- the `cookie` property which holds the optional line and column info used when jumping to matched files
- the `resource` property which maps to the generic `QueryResult.value`; this is used in tests and when sorting in `ResourceQueryController`

* UserInterface/Test.html:

Modified Paths

Added Paths

Removed Paths

Diff

Modified: trunk/Source/WebInspectorUI/ChangeLog (285710 => 285711)


--- trunk/Source/WebInspectorUI/ChangeLog	2021-11-12 10:50:47 UTC (rev 285710)
+++ trunk/Source/WebInspectorUI/ChangeLog	2021-11-12 10:54:05 UTC (rev 285711)
@@ -1,3 +1,67 @@
+2021-11-12  Razvan Caliman  <rcali...@apple.com>
+
+        Web Inspector: Extract reusable logic from ResourceQueryController, ResourceQueryResult and ResourceQueryMatch
+        https://bugs.webkit.org/show_bug.cgi?id=231604
+        <rdar://problem/84160281>
+
+        Reviewed by Devin Rousso.
+
+        Extract reusable logic from `ResourceQueryController` into a generic `QueryController`
+        to enable subclassing for other specialized use cases.
+
+        * UserInterface/Controllers/QueryController.js: Added.
+        (WI.QueryController.prototype.executeQuery):
+        (WI.QueryController.prototype.findQueryMatches.pushMatch):
+        (WI.QueryController.prototype.findQueryMatches.matchNextSpecialCharacter):
+        (WI.QueryController.prototype.findQueryMatches.backtrack):
+        (WI.QueryController.prototype.findQueryMatches):
+        (WI.QueryController):
+
+        Keep only the reusable matching logic in `QueryController`.
+        Subclasses like `ResourceQueryController` are responsible for agregating
+        the data to be queried, customization for special characters and sorting results.
+
+        * UserInterface/Controllers/ResourceQueryController.js:
+        (WI.ResourceQueryController.prototype.executeQuery):
+        (WI.ResourceQueryController.prototype._findQueryMatches.pushMatch): Deleted.
+        (WI.ResourceQueryController.prototype._findQueryMatches.matchNextSpecialCharacter): Deleted.
+        (WI.ResourceQueryController.prototype._findQueryMatches.backtrack): Deleted.
+        (WI.ResourceQueryController.prototype._findQueryMatches): Deleted.
+
+        * UserInterface/Main.html:
+
+        * UserInterface/Models/QueryMatch.js: Renamed from Source/WebInspectorUI/UserInterface/Models/ResourceQueryMatch.js.
+
+        `ResourceQueryMatch` doesn't contain any resource-specific logic. It can be generalized to `QueryMatch`.
+
+        * UserInterface/Models/QueryResult.js: Copied from Source/WebInspectorUI/UserInterface/Models/ResourceQueryResult.js.
+        (WI.QueryResult):
+        (WI.QueryResult.prototype.get value):
+        (WI.QueryResult.prototype.get rank):
+        (WI.QueryResult.prototype.get matchingTextRanges):
+        (WI.QueryResult.prototype._calculateRank.getMultiplier):
+        (WI.QueryResult.prototype._calculateRank):
+        (WI.QueryResult.prototype._createMatchingTextRanges):
+
+        A generic `QueryResult` can be extracted from `ResourceQueryResult` containing
+        the reusable logic for ranking results and identifing matching text ranges.
+
+        * UserInterface/Models/ResourceQueryResult.js:
+        (WI.ResourceQueryResult):
+        (WI.ResourceQueryResult.prototype.get resource):
+        (WI.ResourceQueryResult.prototype.__test_createMatchesMask):
+        (WI.ResourceQueryResult.prototype.get rank): Deleted.
+        (WI.ResourceQueryResult.prototype.get matchingTextRanges): Deleted.
+        (WI.ResourceQueryResult.prototype._calculateRank.getMultiplier): Deleted.
+        (WI.ResourceQueryResult.prototype._calculateRank): Deleted.
+        (WI.ResourceQueryResult.prototype._createMatchingTextRanges): Deleted.
+
+        `ResourceQueryResult` extends `QueryResult` with resource-specifc members:
+        - the `cookie` property which holds the optional line and column info used when jumping to matched files
+        - the `resource` property which maps to the generic `QueryResult.value`; this is used in tests and when sorting in `ResourceQueryController`
+
+        * UserInterface/Test.html:
+
 2021-11-10  Tim Nguyen  <n...@apple.com>
 
         Remove non-standard -webkit-border-fit CSS property

Added: trunk/Source/WebInspectorUI/UserInterface/Controllers/QueryController.js (0 => 285711)


--- trunk/Source/WebInspectorUI/UserInterface/Controllers/QueryController.js	                        (rev 0)
+++ trunk/Source/WebInspectorUI/UserInterface/Controllers/QueryController.js	2021-11-12 10:54:05 UTC (rev 285711)
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2021 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+WI.QueryController = class QueryController
+{
+    // Public
+
+    executeQuery(query)
+    {
+        throw WI.NotImplementedError.subclassMustOverride();
+    }
+
+    findQueryMatches(query, searchString, specialCharacterIndices)
+    {
+        if (query.length > searchString.length)
+            return [];
+
+        let matches = [];
+        let queryIndex = 0;
+        let searchIndex = 0;
+        let specialIndex = 0;
+        let deadBranches = (new Array(query.length)).fill(Infinity);
+        let type = WI.QueryMatch.Type.Special;
+
+        function pushMatch(index) {
+            matches.push(new WI.QueryMatch(type, index, queryIndex));
+            searchIndex = index + 1;
+            queryIndex++;
+        }
+
+        function matchNextSpecialCharacter() {
+            if (specialIndex >= specialCharacterIndices.length)
+                return false;
+
+            let originalSpecialIndex = specialIndex;
+            while (specialIndex < specialCharacterIndices.length) {
+                // Normal character matching can move past special characters,
+                // so advance the special character index if it's before the
+                // current search string position.
+                let index = specialCharacterIndices[specialIndex++];
+                if (index < searchIndex)
+                    continue;
+
+                if (query[queryIndex] === searchString[index]) {
+                    pushMatch(index);
+                    return true;
+                }
+            }
+
+            specialIndex = originalSpecialIndex;
+            return false;
+        }
+
+        function backtrack() {
+            while (matches.length) {
+                queryIndex--;
+
+                let lastMatch = matches.pop();
+                if (lastMatch.type !== WI.QueryMatch.Type.Special)
+                    continue;
+
+                deadBranches[lastMatch.queryIndex] = lastMatch.index;
+                searchIndex = matches.lastValue ? matches.lastValue.index + 1 : 0;
+                return true;
+            }
+
+            return false;
+        }
+
+        while (queryIndex < query.length && searchIndex <= searchString.length) {
+            if (type === WI.QueryMatch.Type.Special && !matchNextSpecialCharacter())
+                type = WI.QueryMatch.Type.Normal;
+
+            if (type === WI.QueryMatch.Type.Normal) {
+                let index = searchString.indexOf(query[queryIndex], searchIndex);
+                if (index >= 0 && index < deadBranches[queryIndex]) {
+                    pushMatch(index);
+                    type = WI.QueryMatch.Type.Special;
+                } else if (!backtrack())
+                    return [];
+            }
+        }
+
+        if (queryIndex < query.length)
+            return [];
+
+        return matches;
+    }
+};

Modified: trunk/Source/WebInspectorUI/UserInterface/Controllers/ResourceQueryController.js (285710 => 285711)


--- trunk/Source/WebInspectorUI/UserInterface/Controllers/ResourceQueryController.js	2021-11-12 10:50:47 UTC (rev 285710)
+++ trunk/Source/WebInspectorUI/UserInterface/Controllers/ResourceQueryController.js	2021-11-12 10:54:05 UTC (rev 285711)
@@ -23,7 +23,7 @@
  * THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-WI.ResourceQueryController = class ResourceQueryController extends WI.Object
+WI.ResourceQueryController = class ResourceQueryController extends WI.QueryController
 {
     constructor()
     {
@@ -73,7 +73,7 @@
                 cachedData.specialCharacterIndices = this._findSpecialCharacterIndices(displayName);
             }
 
-            let matches = this._findQueryMatches(query, cachedData.searchString, cachedData.specialCharacterIndices);
+            let matches = this.findQueryMatches(query, cachedData.searchString, cachedData.specialCharacterIndices);
             if (matches.length)
                 results.push(new WI.ResourceQueryResult(resource, matches, cookie));
         }
@@ -89,86 +89,6 @@
 
     // Private
 
-    _findQueryMatches(query, searchString, specialCharacterIndices)
-    {
-        if (query.length > searchString.length)
-            return [];
-
-        let matches = [];
-        let queryIndex = 0;
-        let searchIndex = 0;
-        let specialIndex = 0;
-        let deadBranches = new Array(query.length).fill(Infinity);
-        let type = WI.ResourceQueryMatch.Type.Special;
-
-        function pushMatch(index)
-        {
-            matches.push(new WI.ResourceQueryMatch(type, index, queryIndex));
-            searchIndex = index + 1;
-            queryIndex++;
-        }
-
-        function matchNextSpecialCharacter()
-        {
-            if (specialIndex >= specialCharacterIndices.length)
-                return false;
-
-            let originalSpecialIndex = specialIndex;
-            while (specialIndex < specialCharacterIndices.length) {
-                // Normal character matching can move past special characters,
-                // so advance the special character index if it's before the
-                // current search string position.
-                let index = specialCharacterIndices[specialIndex++];
-                if (index < searchIndex)
-                    continue;
-
-                if (query[queryIndex] === searchString[index]) {
-                    pushMatch(index);
-                    return true;
-                }
-            }
-
-            specialIndex = originalSpecialIndex;
-            return false;
-        }
-
-        function backtrack()
-        {
-            while (matches.length) {
-                queryIndex--;
-
-                let lastMatch = matches.pop();
-                if (lastMatch.type !== WI.ResourceQueryMatch.Type.Special)
-                    continue;
-
-                deadBranches[lastMatch.queryIndex] = lastMatch.index;
-                searchIndex = matches.lastValue ? matches.lastValue.index + 1 : 0;
-                return true;
-            }
-
-            return false;
-        }
-
-        while (queryIndex < query.length && searchIndex <= searchString.length) {
-            if (type === WI.ResourceQueryMatch.Type.Special && !matchNextSpecialCharacter())
-                type = WI.ResourceQueryMatch.Type.Normal;
-
-            if (type === WI.ResourceQueryMatch.Type.Normal) {
-                let index = searchString.indexOf(query[queryIndex], searchIndex);
-                if (index >= 0 && index < deadBranches[queryIndex]) {
-                    pushMatch(index);
-                    type = WI.ResourceQueryMatch.Type.Special;
-                } else if (!backtrack())
-                    return [];
-            }
-        }
-
-        if (queryIndex < query.length)
-            return [];
-
-        return matches;
-    }
-
     _findSpecialCharacterIndices(string)
     {
         if (!string.length)

Modified: trunk/Source/WebInspectorUI/UserInterface/Main.html (285710 => 285711)


--- trunk/Source/WebInspectorUI/UserInterface/Main.html	2021-11-12 10:50:47 UTC (rev 285710)
+++ trunk/Source/WebInspectorUI/UserInterface/Main.html	2021-11-12 10:54:05 UTC (rev 285711)
@@ -468,6 +468,8 @@
     <script src=""
     <script src=""
     <script src=""
+    <script src=""
+    <script src=""
     <script src=""
     <script src=""
     <script src=""
@@ -476,7 +478,6 @@
     <script src=""
     <script src=""
     <script src=""
-    <script src=""
     <script src=""
     <script src=""
     <script src=""
@@ -887,6 +888,7 @@
 
     <script src=""
     <script src=""
+    <script src=""
 
     <script src=""
     <script src=""

Copied: trunk/Source/WebInspectorUI/UserInterface/Models/QueryMatch.js (from rev 285710, trunk/Source/WebInspectorUI/UserInterface/Models/ResourceQueryMatch.js) (0 => 285711)


--- trunk/Source/WebInspectorUI/UserInterface/Models/QueryMatch.js	                        (rev 0)
+++ trunk/Source/WebInspectorUI/UserInterface/Models/QueryMatch.js	2021-11-12 10:54:05 UTC (rev 285711)
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2021 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+WI.QueryMatch = class QueryMatch
+{
+    constructor(type, index, queryIndex)
+    {
+        console.assert(Object.values(WI.QueryMatch.Type).includes(type), type);
+        console.assert(index >= 0, index);
+        console.assert(queryIndex >= 0, queryIndex);
+        this._type = type;
+        this._index = index;
+        this._queryIndex = queryIndex;
+    }
+
+    // Public
+
+    get type() { return this._type; }
+    get index() { return this._index; }
+    get queryIndex() { return this._queryIndex; }
+};
+
+WI.QueryMatch.Type = {
+    Normal: Symbol("normal"),
+    Special: Symbol("special"),
+};

Copied: trunk/Source/WebInspectorUI/UserInterface/Models/QueryResult.js (from rev 285710, trunk/Source/WebInspectorUI/UserInterface/Models/ResourceQueryResult.js) (0 => 285711)


--- trunk/Source/WebInspectorUI/UserInterface/Models/QueryResult.js	                        (rev 0)
+++ trunk/Source/WebInspectorUI/UserInterface/Models/QueryResult.js	2021-11-12 10:54:05 UTC (rev 285711)
@@ -0,0 +1,126 @@
+/*
+ * Copyright (C) 2021 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+WI.QueryResult = class QueryResult
+{
+    constructor(value, matches)
+    {
+        console.assert(matches.length, "Query matches list can't be empty.");
+
+        this._value = value;
+        this._matches = matches;
+    }
+
+    // Public
+
+    get value() { return this._value; }
+
+    get rank()
+    {
+        if (this._rank === undefined)
+            this._calculateRank();
+
+        return this._rank;
+    }
+
+    get matchingTextRanges()
+    {
+        this._matchingTextRanges ??= this._createMatchingTextRanges();
+
+        return this._matchingTextRanges;
+    }
+
+    // Private
+
+    _calculateRank()
+    {
+        const normalWeight = 10;
+        const consecutiveWeight = 5;
+        const specialMultiplier = 5;
+
+        function getMultiplier(match) {
+            if (match.type === WI.QueryMatch.Type.Special)
+                return specialMultiplier;
+
+            return 1;
+        }
+
+        this._rank = 0;
+
+        let previousMatch = null;
+        let consecutiveMatchStart = null;
+        for (let match of this._matches) {
+            this._rank += normalWeight * getMultiplier(match);
+
+            let consecutive = previousMatch && previousMatch.index === match.index - 1;
+            if (consecutive) {
+                if (!consecutiveMatchStart)
+                    consecutiveMatchStart = previousMatch;
+
+                // If the first match in this consecutive series was a special character, give a
+                // bonus (more likely to match a specific word in the text).  Otherwise, multiply
+                // by the current length of the consecutive sequence (gives priority to fewer
+                // longer sequences instead of more short sequences).
+                this._rank += consecutiveWeight * getMultiplier(consecutiveMatchStart) * (match.index - consecutiveMatchStart.index);
+            } else if (consecutiveMatchStart)
+                consecutiveMatchStart = null;
+
+            previousMatch = match;
+
+            // The match index is deducted from the total rank, so matches that occur closer to
+            // the beginning of the string are ranked higher.  Increase the amount subtracted if
+            // the match is special, so as to favor matches towards the beginning of the string.
+            if (!consecutive)
+                this._rank -= match.index * getMultiplier(match);
+        }
+    }
+
+    _createMatchingTextRanges()
+    {
+        if (!this._matches.length)
+            return [];
+
+        let ranges = [];
+        let startIndex = this._matches[0].index;
+        let endIndex = startIndex;
+        for (let i = 1; i < this._matches.length; ++i) {
+            let match = this._matches[i];
+
+            // Increment endIndex for consecutive match.
+            if (match.index === endIndex + 1) {
+                endIndex++;
+                continue;
+            }
+
+            // Begin a new range when a gap between this match and the previous match is found.
+            ranges.push(new WI.TextRange(0, startIndex, 0, endIndex + 1));
+            startIndex = match.index;
+            endIndex = startIndex;
+        }
+
+        ranges.push(new WI.TextRange(0, startIndex, 0, endIndex + 1));
+        return ranges;
+    }
+};

Deleted: trunk/Source/WebInspectorUI/UserInterface/Models/ResourceQueryMatch.js (285710 => 285711)


--- trunk/Source/WebInspectorUI/UserInterface/Models/ResourceQueryMatch.js	2021-11-12 10:50:47 UTC (rev 285710)
+++ trunk/Source/WebInspectorUI/UserInterface/Models/ResourceQueryMatch.js	2021-11-12 10:54:05 UTC (rev 285711)
@@ -1,45 +0,0 @@
-/*
- * Copyright (C) 2016 Apple Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-WI.ResourceQueryMatch = class ResourceQueryMatch
-{
-    constructor(type, index, queryIndex)
-    {
-        this._type = type;
-        this._index = index;
-        this._queryIndex = queryIndex;
-    }
-
-    // Public
-
-    get type() { return this._type; }
-    get index() { return this._index; }
-    get queryIndex() { return this._queryIndex; }
-};
-
-WI.ResourceQueryMatch.Type = {
-    Normal: Symbol("normal"),
-    Special: Symbol("special")
-};

Modified: trunk/Source/WebInspectorUI/UserInterface/Models/ResourceQueryResult.js (285710 => 285711)


--- trunk/Source/WebInspectorUI/UserInterface/Models/ResourceQueryResult.js	2021-11-12 10:50:47 UTC (rev 285710)
+++ trunk/Source/WebInspectorUI/UserInterface/Models/ResourceQueryResult.js	2021-11-12 10:54:05 UTC (rev 285711)
@@ -23,115 +23,26 @@
  * THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-WI.ResourceQueryResult = class ResourceQueryResult
+WI.ResourceQueryResult = class ResourceQueryResult extends WI.QueryResult
 {
     constructor(resource, matches, cookie)
     {
-        console.assert(matches.length, "Query matches list can't be empty.");
+        console.assert(resource instanceof WI.Resource, resource);
+        super(resource, matches);
 
-        this._resource = resource;
-        this._matches = matches;
         this._cookie = cookie || null;
     }
 
     // Public
 
-    get resource() { return this._resource; }
+    get resource() { return this.value; }
     get cookie() { return this._cookie; }
 
-    get rank()
-    {
-        if (this._rank === undefined)
-            this._calculateRank();
-
-        return this._rank;
-    }
-
-    get matchingTextRanges()
-    {
-        if (!this._matchingTextRanges)
-            this._matchingTextRanges = this._createMatchingTextRanges();
-
-        return this._matchingTextRanges;
-    }
-
-    // Private
-
-    _calculateRank()
-    {
-        const normalWeight = 10;
-        const consecutiveWeight = 5;
-        const specialMultiplier = 5;
-
-        function getMultiplier(match) {
-            if (match.type === WI.ResourceQueryMatch.Type.Special)
-                return specialMultiplier;
-
-            return 1;
-        }
-
-        this._rank = 0;
-
-        let previousMatch = null;
-        let consecutiveMatchStart = null;
-        for (let match of this._matches) {
-            this._rank += normalWeight * getMultiplier(match);
-
-            let consecutive = previousMatch && previousMatch.index === match.index - 1;
-            if (consecutive) {
-                if (!consecutiveMatchStart)
-                    consecutiveMatchStart = previousMatch;
-
-                // If the first match in this consecutive series was a special character, give a
-                // bonus (more likely to match a specific word in the text).  Otherwise, multiply
-                // by the current length of the consecutive sequence (gives priority to fewer
-                // longer sequences instead of more short sequences).
-                this._rank += consecutiveWeight * getMultiplier(consecutiveMatchStart) * (match.index - consecutiveMatchStart.index);
-            } else if (consecutiveMatchStart)
-                consecutiveMatchStart = null;
-
-            previousMatch = match;
-
-            // The match index is deducted from the total rank, so matches that occur closer to
-            // the beginning of the string are ranked higher.  Increase the amount subtracted if
-            // the match is special, so as to favor matches towards the beginning of the string.
-            if (!consecutive)
-                this._rank -= match.index * getMultiplier(match);
-        }
-    }
-
-    _createMatchingTextRanges()
-    {
-        if (!this._matches.length)
-            return [];
-
-        let ranges = [];
-        let startIndex = this._matches[0].index;
-        let endIndex = startIndex;
-        for (let i = 1; i < this._matches.length; ++i) {
-            let match = this._matches[i];
-
-            // Increment endIndex for consecutive match.
-            if (match.index === endIndex + 1) {
-                endIndex++;
-                continue;
-            }
-
-            // Begin a new range when a gap between this match and the previous match is found.
-            ranges.push(new WI.TextRange(0, startIndex, 0, endIndex + 1));
-            startIndex = match.index;
-            endIndex = startIndex;
-        }
-
-        ranges.push(new WI.TextRange(0, startIndex, 0, endIndex + 1));
-        return ranges;
-    }
-
     // Testing
 
     __test_createMatchesMask()
     {
-        let filename = this._resource.displayName;
+        let filename = this.resource.displayName;
         let lastIndex = -1;
         let result = "";
 

Modified: trunk/Source/WebInspectorUI/UserInterface/Test.html (285710 => 285711)


--- trunk/Source/WebInspectorUI/UserInterface/Test.html	2021-11-12 10:50:47 UTC (rev 285710)
+++ trunk/Source/WebInspectorUI/UserInterface/Test.html	2021-11-12 10:54:05 UTC (rev 285711)
@@ -196,6 +196,8 @@
     <script src=""
     <script src=""
     <script src=""
+    <script src=""
+    <script src=""
     <script src=""
     <script src=""
     <script src=""
@@ -204,7 +206,6 @@
     <script src=""
     <script src=""
     <script src=""
-    <script src=""
     <script src=""
     <script src=""
     <script src=""
@@ -244,6 +245,8 @@
     <script src=""
     <script src=""
 
+    <script src=""
+
     <script src=""
     <script src=""
     <script src=""
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to