Repository: microsoft/vscode-edge-devtools
Pull request #154 (2020-06-02 19:27:34)

TextSelection polyfill refactor
Modifying TextSelection polyfill to return null when no text is replaced.  Modifying TextSelection tests to take code from the source file.  Also changing patch target file from elements_module.js to ElementsPanel.js


Patch:
diff --git a/postBuildStep.ts b/postBuildStep.ts
index c6b5a3c..8fa2bd5 100644
--- a/postBuildStep.ts
+++ b/postBuildStep.ts
@@ -105,9 +105,11 @@ async function patchFilesForWebView(toolsOutDir: string, debugMode: boolean) {
             applyCommonRevealerPatch,
         ]);
         await patchFileForWebViewWrapper("elements/elements_module.js", toolsOutDir, true, [
-            applySetupTextSelectionPatch,
             applyPaddingInlineCssPatch,
         ]);
+        await patchFileForWebViewWrapper("elements/ElementsPanel.js", toolsOutDir, true, [
+            applySetupTextSelectionPatch,
+        ]);
         await patchFileForWebViewWrapper("inspector.html", toolsOutDir, true, [
             applyContentSecurityPolicyPatch,
         ]);
diff --git a/src/host/polyfills/textSelection.ts b/src/host/polyfills/textSelection.ts
index 72cdb27..f796ea2 100644
--- a/src/host/polyfills/textSelection.ts
+++ b/src/host/polyfills/textSelection.ts
@@ -2,8 +2,10 @@
 // Licensed under the MIT License.
 
 export default function applySetupTextSelectionPatch(content: string) {
-    return content.replace(
-        /_setupTextSelectionHack\(stylePaneWrapperElement\)\s*{/g,
-        "_setupTextSelectionHack() { return;",
-    );
+    const pattern = /_setupTextSelectionHack\(stylePaneWrapperElement\)\s*{/g;
+    if (content.match(pattern)) {
+        return content.replace(pattern, "_setupTextSelectionHack() { return;");
+    } else {
+        return null;
+    }
 }


Test patch:
diff --git a/src/host/polyfills/textSelection.test.ts b/src/host/polyfills/textSelection.test.ts
index a628449..770c6ee 100644
--- a/src/host/polyfills/textSelection.test.ts
+++ b/src/host/polyfills/textSelection.test.ts
@@ -1,17 +1,25 @@
 // Copyright (c) Microsoft Corporation. All rights reserved.
 // Licensed under the MIT License.
+import { getTextFromFile } from "../../test/helpers";
 
 describe("textSelection", () => {
     it("applySetupTextSelectionPatch correctly changes text", async () => {
+        const filePath = "elements/ElementsPanel.js";
+        const fileContents = getTextFromFile(filePath);
+        if (!fileContents) {
+            throw new Error(`Could not find file: ${filePath}`);
+        }
+
         const apply = await import("./textSelection");
-        const result = apply.default("_setupTextSelectionHack(stylePaneWrapperElement) { code }");
-        expect(result).toEqual("_setupTextSelectionHack() { return; code }");
+        const result = apply.default(fileContents);
+        expect(result).not.toEqual(null);
+        expect(result).toEqual(expect.stringContaining("_setupTextSelectionHack() { return;"));
     });
 
     it("applySetupTextSelectionPatch ignores other text", async () => {
         const apply = await import("./textSelection");
         const expectedText = "hello world";
         const result = apply.default(expectedText);
-        expect(result).toEqual(expectedText);
+        expect(result).toEqual(null);
     });
 });