Repository: microsoft/vscode-edge-devtools
Pull request #157 (2020-06-04 22:27:47)

cssPaddingInline polyfill refactor
Modifying cssPaddingInline polyfill to return null when no text is replaced. Modifying cssPaddingInline tests to take code from the source file.


Patch:
diff --git a/src/host/polyfills/cssPaddingInline.ts b/src/host/polyfills/cssPaddingInline.ts
index fea78e3..ff93820 100644
--- a/src/host/polyfills/cssPaddingInline.ts
+++ b/src/host/polyfills/cssPaddingInline.ts
@@ -4,17 +4,21 @@
 export default function applyPaddingInlineCssPatch(content: string, isRelease?: boolean) {
     const separator = (isRelease ? "\\n" : "\n");
     const cssHeaderContents =
-        `.elements-disclosure .gutter-container {
-            display: none !important;
-        }`.replace(/\n/g, separator);
+    `.elements-disclosure .gutter-container {
+        display: none !important;
+    }`.replace(/\n/g, separator);
+    const elementsPattern = /(\.elements-disclosure\s*\.gutter-container\s*\{([^\}]*)?\})/g;
+    let result;
+    if (content.match(elementsPattern)) {
+        result = content.replace(elementsPattern, cssHeaderContents);
+    } else {
+        return null;
+    }
 
-    let result = content.replace(
-        /(\.elements-disclosure\s*\.gutter-container\s*\{([^\}]*)?\})/g,
-        cssHeaderContents);
-    result = result.replace(
-        /([^-])padding-inline-start:/g,
-        "$1-webkit-padding-start:",
-    );
-
-    return result;
+    const paddingPattern = /([^-])padding-inline-start:/g;
+    if (result.match(paddingPattern)) {
+        return result.replace(paddingPattern, "$1-webkit-padding-start:");
+    } else {
+        return null;
+    }
 }


Test patch:
diff --git a/src/host/polyfills/cssPaddingInline.test.ts b/src/host/polyfills/cssPaddingInline.test.ts
index 43f1933..71297f3 100644
--- a/src/host/polyfills/cssPaddingInline.test.ts
+++ b/src/host/polyfills/cssPaddingInline.test.ts
@@ -1,17 +1,37 @@
 // Copyright (c) Microsoft Corporation. All rights reserved.
 // Licensed under the MIT License.
+import { getTextFromFile } from "../../test/helpers";
 
 describe("cssPaddingInline", () => {
-    it("applyPaddingInlineCssPatch correctly changes text", async () => {
+    it("applyPaddingInlineCssPatch correctly changes elements-disclosure text", async () => {
+        const filePath = "elements/elements_module.js";
+        const fileContents = getTextFromFile(filePath);
+        if (!fileContents) {
+            throw new Error(`Could not find file: ${filePath}`);
+        }
+
+        const apply = await import("./cssPaddingInline");
+        const result = apply.default(fileContents);
+        expect(result).toEqual(
+            expect.stringContaining(".elements-disclosure .gutter-container {\n        display: none !important;"));
+    });
+
+    it("applyPaddingInlineCssPatch correctly changes padding-inline-start text", async () => {
+        const filePath = "elements/elements_module.js";
+        const fileContents = getTextFromFile(filePath);
+        if (!fileContents) {
+            throw new Error(`Could not find file: ${filePath}`);
+        }
+
         const apply = await import("./cssPaddingInline");
-        const result = apply.default("body { padding-inline-start: 10px; }");
-        expect(result).toEqual("body { -webkit-padding-start: 10px; }");
+        const result = apply.default(fileContents);
+        expect(result).toEqual(expect.stringContaining("webkit-padding-start"));
     });
 
     it("applyPaddingInlineCssPatch ignores other text", async () => {
         const apply = await import("./cssPaddingInline");
         const expectedText = "body { scroll-padding-inline-start: 10px; }";
         const result = apply.default(expectedText);
-        expect(result).toEqual(expectedText);
+        expect(result).toEqual(null);
     });
 });