Title: [230021] trunk
Revision
230021
Author
[email protected]
Date
2018-03-27 19:51:18 -0700 (Tue, 27 Mar 2018)

Log Message

Web Inspector: Modernize some utility functions
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.

Modified Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (230020 => 230021)


--- trunk/LayoutTests/ChangeLog	2018-03-28 02:37:05 UTC (rev 230020)
+++ trunk/LayoutTests/ChangeLog	2018-03-28 02:51:18 UTC (rev 230021)
@@ -1,3 +1,13 @@
+2018-03-27  Timothy Hatcher  <[email protected]>
+
+        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  <[email protected]>
 
         Skip http/wpt/service-workers/third-party-registration.html.

Modified: trunk/LayoutTests/inspector/unit-tests/string-utilities-expected.txt (230020 => 230021)


--- trunk/LayoutTests/inspector/unit-tests/string-utilities-expected.txt	2018-03-28 02:37:05 UTC (rev 230020)
+++ trunk/LayoutTests/inspector/unit-tests/string-utilities-expected.txt	2018-03-28 02:51:18 UTC (rev 230021)
@@ -56,3 +56,12 @@
 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.
+

Modified: trunk/LayoutTests/inspector/unit-tests/string-utilities.html (230020 => 230021)


--- trunk/LayoutTests/inspector/unit-tests/string-utilities.html	2018-03-28 02:37:05 UTC (rev 230020)
+++ trunk/LayoutTests/inspector/unit-tests/string-utilities.html	2018-03-28 02:51:18 UTC (rev 230021)
@@ -54,7 +54,6 @@
         }
     });
 
-
     suite.addTestCase({
         name: "String.prototype.get lineCount",
         test() {
@@ -101,6 +100,20 @@
         }
     });
 
+    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>

Modified: trunk/Source/WebInspectorUI/ChangeLog (230020 => 230021)


--- trunk/Source/WebInspectorUI/ChangeLog	2018-03-28 02:37:05 UTC (rev 230020)
+++ trunk/Source/WebInspectorUI/ChangeLog	2018-03-28 02:51:18 UTC (rev 230021)
@@ -1,3 +1,15 @@
+2018-03-27  Timothy Hatcher  <[email protected]>
+
+        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  <[email protected]>
 
         Web Inspector: Command-Shift-left/right arrow keys should not switch tabs when focused on color picker text fields

Modified: trunk/Source/WebInspectorUI/UserInterface/Base/Utilities.js (230020 => 230021)


--- trunk/Source/WebInspectorUI/UserInterface/Base/Utilities.js	2018-03-28 02:37:05 UTC (rev 230020)
+++ trunk/Source/WebInspectorUI/UserInterface/Base/Utilities.js	2018-03-28 02:51:18 UTC (rev 230021)
@@ -122,9 +122,11 @@
 {
     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;
     }
 });
@@ -131,12 +133,13 @@
 
 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, "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();
     }
 });
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to