Web Inspector: Modernize some utility functions
authortimothy@apple.com <timothy@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 28 Mar 2018 02:51:18 +0000 (02:51 +0000)
committertimothy@apple.com <timothy@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 28 Mar 2018 02:51:18 +0000 (02:51 +0000)
https://bugs.webkit.org/show_bug.cgi?id=184047

Source/WebInspectorUI:

Reviewed by Matt Baker.

* UserInterface/Base/Utilities.js:
(Node.prototype.enclosingNodeOrSelfWithClass): Use parentElement instead of parentNode, so we don't need to check for document.
(Node.prototype.enclosingNodeOrSelfWithNodeNameInArray): Ditto. Also just toUpperCase input, since nodeName is already upper case.
(String.prototype.escapeCharacters): Use Set and string iteration instead of indexOf and charAt.

LayoutTests:

Rubber-stamped by Matt Baker.

* inspector/unit-tests/string-utilities-expected.txt: Updated.
* inspector/unit-tests/string-utilities.html: Added String.prototype.escapeCharacters test.

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

LayoutTests/ChangeLog
LayoutTests/inspector/unit-tests/string-utilities-expected.txt
LayoutTests/inspector/unit-tests/string-utilities.html
Source/WebInspectorUI/ChangeLog
Source/WebInspectorUI/UserInterface/Base/Utilities.js

index e0692de..aa85aa6 100644 (file)
@@ -1,3 +1,13 @@
+2018-03-27  Timothy Hatcher  <timothy@apple.com>
+
+        Web Inspector: Modernize some utility functions
+        https://bugs.webkit.org/show_bug.cgi?id=184047
+
+        Rubber-stamped by Matt Baker.
+
+        * inspector/unit-tests/string-utilities-expected.txt: Updated.
+        * inspector/unit-tests/string-utilities.html: Added String.prototype.escapeCharacters test.
+
 2018-03-26  Ryan Haddad  <ryanhaddad@apple.com>
 
         Skip http/wpt/service-workers/third-party-registration.html.
index d68c30e..be7dfe1 100644 (file)
@@ -56,3 +56,12 @@ PASS: String stays the same.
 PASS: Ellipsis is inserted in the middle.
 PASS: Ellipsis is inserted after the third character.
 
+-- Running test case: String.prototype.escapeCharacters
+PASS: String stays the same with no escape characters.
+PASS: String stays the same with empty escape characters.
+PASS: String stays the same with no matching escape characters.
+PASS: The letter 'c' is escaped.
+PASS: The letter 'c' and 'e' are escaped.
+PASS: The letter 'c' and 'e' are escaped.
+PASS: The letter 'c', 'd', and 'e' are escaped.
+
index 1707492..c413997 100644 (file)
@@ -54,7 +54,6 @@ function test()
         }
     });
 
-
     suite.addTestCase({
         name: "String.prototype.get lineCount",
         test() {
@@ -101,6 +100,20 @@ function test()
         }
     });
 
+    suite.addTestCase({
+        name: "String.prototype.escapeCharacters",
+        test() {
+            InspectorTest.expectEqual("abcdef".escapeCharacters(), "abcdef", "String stays the same with no escape characters.");
+            InspectorTest.expectEqual("abcdef".escapeCharacters(""), "abcdef", "String stays the same with empty escape characters.");
+            InspectorTest.expectEqual("abcdef".escapeCharacters("g"), "abcdef", "String stays the same with no matching escape characters.");
+            InspectorTest.expectEqual("abcdef".escapeCharacters("c"), "ab\\cdef", "The letter 'c' is escaped.");
+            InspectorTest.expectEqual("abcdef".escapeCharacters("ce"), "ab\\cd\\ef", "The letter 'c' and 'e' are escaped.");
+            InspectorTest.expectEqual("abcdef".escapeCharacters("cee"), "ab\\cd\\ef", "The letter 'c' and 'e' are escaped.");
+            InspectorTest.expectEqual("abcdef".escapeCharacters("ced"), "ab\\c\\d\\ef", "The letter 'c', 'd', and 'e' are escaped.");
+            return true;
+        }
+    });
+
     suite.runTestCasesAndFinish();
 }
 </script>
index cb01f4c..ab87cf0 100644 (file)
@@ -1,3 +1,15 @@
+2018-03-27  Timothy Hatcher  <timothy@apple.com>
+
+        Web Inspector: Modernize some utility functions
+        https://bugs.webkit.org/show_bug.cgi?id=184047
+
+        Reviewed by Matt Baker.
+
+        * UserInterface/Base/Utilities.js:
+        (Node.prototype.enclosingNodeOrSelfWithClass): Use parentElement instead of parentNode, so we don't need to check for document.
+        (Node.prototype.enclosingNodeOrSelfWithNodeNameInArray): Ditto. Also just toUpperCase input, since nodeName is already upper case.
+        (String.prototype.escapeCharacters): Use Set and string iteration instead of indexOf and charAt.
+
 2018-03-27  Nikita Vasilyev  <nvasilyev@apple.com>
 
         Web Inspector: Command-Shift-left/right arrow keys should not switch tabs when focused on color picker text fields
index eabb82c..8072ce1 100644 (file)
@@ -122,21 +122,24 @@ Object.defineProperty(Node.prototype, "enclosingNodeOrSelfWithClass",
 {
     value(className)
     {
-        for (var node = this; node && node !== this.ownerDocument; node = node.parentNode)
+        for (let node = this; node; node = node.parentElement) {
             if (node.nodeType === Node.ELEMENT_NODE && node.classList.contains(className))
                 return node;
+        }
+
         return null;
     }
 });
 
 Object.defineProperty(Node.prototype, "enclosingNodeOrSelfWithNodeNameInArray",
 {
-    value(nameArray)
+    value(nodeNames)
     {
-        var lowerCaseNameArray = nameArray.map(function(name) { return name.toLowerCase(); });
-        for (var node = this; node && node !== this.ownerDocument; node = node.parentNode) {
-            for (var i = 0; i < nameArray.length; ++i) {
-                if (node.nodeName.toLowerCase() === lowerCaseNameArray[i])
+        let upperCaseNodeNames = nodeNames.map((name) => name.toUpperCase());
+
+        for (let node = this; node; node = node.parentElement) {
+            for (let nodeName of upperCaseNodeNames) {
+                if (node.nodeName === nodeName)
                     return node;
             }
         }
@@ -590,27 +593,32 @@ Object.defineProperty(String.prototype, "removeWhitespace",
 
 Object.defineProperty(String.prototype, "escapeCharacters",
 {
-    value(chars)
+    value(charactersToEscape)
     {
-        var foundChar = false;
-        for (var i = 0; i < chars.length; ++i) {
-            if (this.indexOf(chars.charAt(i)) !== -1) {
-                foundChar = true;
-                break;
-            }
+        if (!charactersToEscape)
+            return this.valueOf();
+
+        let charactersToEscapeSet = new Set(charactersToEscape);
+
+        let foundCharacter = false;
+        for (let c of this) {
+            if (!charactersToEscapeSet.has(c))
+                continue;
+            foundCharacter = true;
+            break;
         }
 
-        if (!foundChar)
-            return this;
+        if (!foundCharacter)
+            return this.valueOf();
 
-        var result = "";
-        for (var i = 0; i < this.length; ++i) {
-            if (chars.indexOf(this.charAt(i)) !== -1)
+        let result = "";
+        for (let c of this) {
+            if (charactersToEscapeSet.has(c))
                 result += "\\";
-            result += this.charAt(i);
+            result += c;
         }
 
-        return result;
+        return result.valueOf();
     }
 });