Skip to content

Commit

Permalink
minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
arslanbekova committed Jul 19, 2023
1 parent 6037ffd commit 2e39d6b
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { locatorMocks } from "./__mocks__/locatorEscaped.mock";
import { escapeLocator } from "../../../common/utils/helpers";
import { escapeLocator } from "../../../common/utils/copyToClipboard";

test("escape symbols in locator", () => {
locatorMocks.forEach((locator) => {
Expand Down
2 changes: 1 addition & 1 deletion src/common/components/CopyButton.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Button, Tooltip } from "antd";
import { Copy } from "phosphor-react";
import React, { useState } from "react";
import { copyToClipboard } from "../utils/helpers";
import { copyToClipboard } from "../utils/copyToClipboard";
import { CopyTitle } from "../types/common";

interface Props {
Expand Down
38 changes: 38 additions & 0 deletions src/common/utils/copyToClipboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export const escapeLocator = (locator: string) => {
let transformedText = locator.replace(/[\\'\n]/g, (match: string) => {
switch (match) {
case "\\":
return "\\\\\\\\";
case "'":
return "\\'";
case "\n":
return "\\n";
default:
return match;
}
});
const lastDoubleQuote = transformedText.lastIndexOf('"');
const firstDoubleQuote = transformedText.indexOf('"');
const beforeFirstDoubleQuote = transformedText.slice(0, firstDoubleQuote + 1);
const afterLastDoubleQuote = transformedText.slice(lastDoubleQuote);
let insideOfDoubleQuotes = transformedText.slice(firstDoubleQuote + 1, lastDoubleQuote);

if (insideOfDoubleQuotes.includes('"')) {
insideOfDoubleQuotes = insideOfDoubleQuotes.replace(/"/g, '\\\\"');
transformedText = beforeFirstDoubleQuote + insideOfDoubleQuotes + afterLastDoubleQuote;
}

return transformedText;
};

export const copyToClipboard = (value: string | string[]) => {
let transformedText;

if (typeof value === "string") {
transformedText = escapeLocator(value);
} else {
transformedText = value.map((el: string) => escapeLocator(el)).join("\\n\\n");
}

chrome.devtools.inspectedWindow.eval(`copy('${transformedText}')`);
};
32 changes: 0 additions & 32 deletions src/common/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,6 @@ export const floatToPercent = (value: number) => {
return Math.trunc(value * 100);
};

export const escapeLocator = (locator: string) => {
let transformedText = locator.replace(/[\\'\n]/g, (match: string) => {
switch (match) {
case "\\":
return "\\\\\\\\";
case "'":
return "\\'";
case "\n":
return "\\n";
default:
return match;
}
});
const lastDoubleQuote = transformedText.lastIndexOf('"');
const firstDoubleQuote = transformedText.indexOf('"');
const beforeFirstDoubleQuote = transformedText.slice(0, firstDoubleQuote + 1);
const afterLastDoubleQuote = transformedText.slice(lastDoubleQuote);
let insideOfDoubleQuotes = transformedText.slice(firstDoubleQuote + 1, lastDoubleQuote);

if (insideOfDoubleQuotes.includes('"')) {
insideOfDoubleQuotes = insideOfDoubleQuotes.replace(/"/g, '\\\\"');
transformedText = beforeFirstDoubleQuote + insideOfDoubleQuotes + afterLastDoubleQuote;
}

return transformedText;
};

export const copyToClipboard = (text: string) => {
const transformedText = escapeLocator(text);
chrome.devtools.inspectedWindow.eval(`copy('${transformedText}')`);
};

export const getLocatorString = (locator: LocatorValue, type: ElementLibrary | ElementClass, name: string): string =>
`@UI("${locator.output}")\npublic ${type} ${name};`;

Expand Down
2 changes: 1 addition & 1 deletion src/features/locators/components/LocatorCopyButton.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Button, Tooltip } from "antd";
import { CopySimple } from "phosphor-react";
import React, { useState } from "react";
import { copyToClipboard, getLocatorString } from "../../../common/utils/helpers";
import { copyToClipboard, getLocatorString } from "../../../common/utils/copyToClipboard";
import { CopyTitle } from "../../../common/types/common";

export const LocatorCopyButton = ({ element }) => {
Expand Down
3 changes: 2 additions & 1 deletion src/features/locators/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import {
ElementId,
JDNHash,
} from "../types/locator.types";
import { copyToClipboard, getLocatorString, getElementFullXpath } from "../../../common/utils/helpers";
import { getLocatorString, getElementFullXpath } from "../../../common/utils/helpers";
import { LocatorOption } from "./constants";
import { LocatorType } from "../../../common/types/common";
import { isStringContainsNumbers } from "../../../common/utils/helpers";
import { FormInstance } from "antd/es/form/Form";
import { copyToClipboard } from "../../../common/utils/copyToClipboard";

export const getLocatorWithJDIAnnotation = (locator: string): string => `@UI("${locator}")`;

Expand Down
5 changes: 3 additions & 2 deletions src/features/pageObjects/components/PageObjCopyButton.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, { FC, MouseEvent, useState } from "react";

import { Button, Tooltip } from "antd";
import { copyToClipboard, getLocatorString } from "../../../common/utils/helpers";
import { getLocatorString } from "../../../common/utils/helpers";
import { CopySimple } from "phosphor-react";
import { Locator } from "../../locators/types/locator.types";
import { CopyTitle } from "../../../common/types/common";
import { copyToClipboard } from "../../../common/utils/copyToClipboard";

interface Props {
elements: Locator[];
Expand All @@ -14,7 +15,7 @@ export const PageObjCopyButton: FC<Props> = ({ elements }) => {
const [copyTooltipTitle, setTooltipTitle] = useState(CopyTitle.Copy);

const getPageObjectForCopying = (locators: Locator[]) => {
return locators.map(({ locator, type, name }) => getLocatorString(locator, type, name)).join("\n\n");
return locators.map(({ locator, type, name }) => getLocatorString(locator, type, name));
};

const handleCopy = (e: MouseEvent<HTMLElement>) => {
Expand Down

0 comments on commit 2e39d6b

Please sign in to comment.