Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue_1356_1376_1388: Fixed escaping for copying #1389

Merged
merged 5 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions src/common/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,35 @@ export const floatToPercent = (value: number) => {
return Math.trunc(value * 100);
};

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please, create unit tests for this function

};

export const copyToClipboard = (text: string) => {
// "\\\\3" - needed to get "\3" in 'eval()'
const transformedText = text
.replace(/'/g, "\\'")
.replace(/\n/g, "\\n")
.replace(/#\\3/g, "#\\\\3")
.replace(/=\\'\\3/g, "=\\'\\\\3") // two different cases for \\3 to avoid affecting something else...
.replace(/\\/g, "\\\\")
.replace(/"/g, '\\"');
const transformedText = escapeLocator(text);
chrome.devtools.inspectedWindow.eval(`copy('${transformedText}')`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please, create a new module for these two functions. They are more than just helpers from now

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

};

Expand Down
7 changes: 3 additions & 4 deletions src/pageServices/contentScripts/generationData.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,14 @@ export const getGenerationAttributes = () => {

const isSelectorByGeneratorString = typeof selectorByGenerator === "string";
const isSelectorByFinderString = typeof selectorByFinder === "string";
const transformSelector = (selector) => selector.replace(/"/g, "'");

if (isSelectorByGeneratorString && isSelectorByFinderString) {
const selector = selectorByGenerator.length < selectorByFinder.length ? selectorByGenerator : selectorByFinder;
return transformSelector(selector);
return selector;
} else if (!isSelectorByFinderString && isSelectorByGeneratorString) {
return transformSelector(selectorByGenerator);
return selectorByGenerator;
} else if (!isSelectorByGeneratorString && isSelectorByFinderString) {
return transformSelector(selectorByFinder);
return selectorByFinder;
} else {
return "CSS selector generation was failed";
}
Expand Down
Loading