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

Fix for issue when single quotes are contained in the playlist name #82

Merged
Changes from all 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
37 changes: 35 additions & 2 deletions src/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ async function uploadVideo(videoJSON: Video) {
await page.focus(`#search-input`)
await page.type(`#search-input`, playlistName)

const playlistToSelectXPath = "//*[normalize-space(text())='" + playlistName + "']"
const escapedPlaylistName = escapeQuotesForXPath(playlistName);
const playlistToSelectXPath = "//*[normalize-space(text())=" + escapedPlaylistName + "]";
await page.waitForXPath(playlistToSelectXPath, { timeout: 10000 })
const playlistNameSelector = await page.$x(playlistToSelectXPath)
await page.evaluate((el) => el.click(), playlistNameSelector[0])
Expand Down Expand Up @@ -499,7 +500,9 @@ const updateVideoInfo = async (videoJSON: VideoToEdit) => {
await page.focus(`#search-input`)
await page.type(`#search-input`, playlistName)

const playlistToSelectXPath = "//*[normalize-space(text())='" + playlistName + "']"
const escapedPlaylistName = escapeQuotesForXPath(playlistName);
const playlistToSelectXPath = "//*[normalize-space(text())=" + escapedPlaylistName + "]"

await page.waitForXPath(playlistToSelectXPath, { timeout: 10000 })
const playlistNameSelector = await page.$x(playlistToSelectXPath)
await page.evaluate((el) => el.click(), playlistNameSelector[0])
Expand Down Expand Up @@ -892,3 +895,33 @@ async function changeChannel(channelName: string) {
waitUntil: "networkidle0"
});
}

function escapeQuotesForXPath(str: string) {
// If the value contains only single or double quotes, construct
// an XPath literal
if (!str.includes('"')){
return '"' + str + '"';
}
if (!str.includes("'")) {
return "'" + str + "'";
}
// If the value contains both single and double quotes, construct an
// expression that concatenates all non-double-quote substrings with
// the quotes, e.g.:
//
// concat("foo",'"',"bar")

const parts : string[] = [];
// First, put a '"' after each component in the string.
for (const part of str.split('"')) {
if (part.length > 0) {
parts.push('"' + part + '"');
}
parts.push("'\"'");
}
// Then remove the extra '"' after the last component.
parts.pop();
// Finally, put it together into a concat() function call.

return "concat(" + parts.join(",") + ")";
}