Skip to content

Commit

Permalink
fix: sharepoint filenames with comma and dot (#660)
Browse files Browse the repository at this point in the history
  • Loading branch information
rofe authored Jan 24, 2024
1 parent 56091c6 commit 5d7130b
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 21 deletions.
27 changes: 17 additions & 10 deletions src/extension/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -1590,15 +1590,21 @@ import sampleRUM from './rum.js';
if (['/', '*', '\\', '!', '?'].find((pattern) => path.includes(pattern))) {
return `!ILLEGAL!_${path}`;
}
const nameParts = path.split('.');
let [file, ext] = nameParts;
if (isSharePointFolder(sk, sk.location) && ext === 'docx') {
let file = path;
let ext = '';
const lastDot = path.lastIndexOf('.');
if (lastDot >= 0) {
// cut off extension
file = path.substring(0, lastDot);
ext = path.substring(path.lastIndexOf('.'));
}
if (isSharePointFolder(sk, sk.location) && type === 'docx') {
// omit docx extension on sharepoint
ext = '';
}
if (type === 'xlsx' || type.includes('vnd.google-apps.spreadsheet')) {
// use json extension for spreadsheets
ext = 'json';
ext = '.json';
}
if (file === 'index') {
// folder root
Expand All @@ -1610,7 +1616,7 @@ import sampleRUM from './rum.js';
.replace(/[\u0300-\u036f]/g, '')
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-|-$/g, '');
return `${folder}${folder.endsWith('/') ? '' : '/'}${file}${ext ? `.${ext}` : ''}`;
return `${folder}${folder.endsWith('/') ? '' : '/'}${file}${ext}`;
};

const validateWebPaths = (paths) => {
Expand Down Expand Up @@ -1646,12 +1652,13 @@ import sampleRUM from './rum.js';
&& !row.querySelector('svg')?.parentElement.className.toLowerCase().includes('folder'))
// extract file name and type
.map((row) => {
const [path, type] = (row.getAttribute('aria-label') || row.querySelector('span')?.textContent)
?.split(',')
.map((detail) => detail.trim()) || [];
const info = row.getAttribute('aria-label') || row.querySelector('span')?.textContent;
// info format: bla.docx, docx File, Private, Modified 8/28/2023, edited by Jane, 1 KB
const type = info.match(/, ([a-z0-9]+) File,/)?.[1];
const path = type && info.split(`, ${type} File,`)[0];
return {
path,
type: type?.split(' ')[0],
type,
};
})
// validate selection
Expand Down Expand Up @@ -3191,7 +3198,7 @@ import sampleRUM from './rum.js';
.catch(({ message }) => {
this.status.error = message;
const modal = {
message: message.startsWith('error_') ? i18n(this, message) : [
message: i18n(this, message) || [
i18n(this, 'error_status_fatal'),
'https://status.adobe.com/',
],
Expand Down
61 changes: 60 additions & 1 deletion test/bulk-copy-urls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,70 @@ describe('Test bulk copy URLs plugin', () => {
loadModule: true,
}).run();
assert.ok(
clipboardText.endsWith('.json'),
clipboardText.endsWith('spreadsheet.json'),
`Preview URL does not have json extension: ${clipboardText}`,
);
});

it('Bulk copy preview URLs plugin handles file name with comma or dot in sharepoint views', async () => {
const { setup } = TESTS[0];
nock.sidekick(setup, {
persist: true,
});
nock.admin(setup, {
route: 'status',
type: 'admin',
persist: true,
});
const views = [
{
view: 'list',
fixture: 'admin-sharepoint.html',
},
{
view: 'grid',
fixture: 'admin-sharepoint-grid.html',
},
];
while (views.length > 0) {
const { view, fixture } = views.shift();
// eslint-disable-next-line no-await-in-loop
const { checkPageResult: clipboardText } = await new SidekickTest({
browser,
page,
fixture,
sidekickConfig: setup.sidekickConfig,
plugin: 'bulk-copy-preview-urls',
// pluginSleep: 1000,
pre: (p) => p.evaluate(() => {
// deselect all
document.getElementById('file-pdf').click();
document.getElementById('file-word').click();
// select only files with comma and dot
document.getElementById('file-comma').click();
document.getElementById('file-dot').click();
}),
post: (p) => p.evaluate(() => {
window.hlx.clipboardText = 'dummy';
window.navigator.clipboard.writeText = (text) => {
window.hlx.clipboardText = text;
};
}),
checkPage: (p) => p.evaluate(() => window.hlx.clipboardText),
loadModule: true,
}).run(setup.getUrl('edit', 'admin'));
const [fileComma, fileDot] = clipboardText.split('\n');
assert.ok(
fileComma.endsWith('/document-new'),
`Unexpected preview URL for file with comma in ${view} view: ${fileComma}`,
);
assert.ok(
fileDot.endsWith('/document-other'),
`Unexpected preview URL for file with dot in ${view} view: ${fileDot}`,
);
}
});

TESTS.forEach(({ env, fixture, setup }) => {
it(`Bulk copy preview URLs plugin copies preview URLs for existing selection to clipboard in ${env}`, async () => {
const { owner, repo, ref } = setup.sidekickConfig;
Expand Down
20 changes: 17 additions & 3 deletions test/fixtures/admin-sharepoint-grid.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,40 @@
<div data-automationid="name">folder</div>
</div>
<div id="file-pdf" role="row" aria-selected="true">
<span>file.pdf, pdf</span>
<span>file.pdf, pdf File, Private</span>
<i data-icon-name="svg/pdf_16x1.svg" aria-label="pdf">
<img src="./icons/icon.svg">
</i>
<div data-automationid="name">file.pdf</div>
</div>
<div id="file-word" role="row" aria-selected="true">
<span>document.docx, docx File</span>
<span>document.docx, docx File, Private</span>
<i data-icon-name="svg/word_16x1.svg" aria-label="docx">
<img src="./icons/docx.svg">
</i>
<div data-automationid="name">document.docx</div>
</div>
<div id="file-excel" role="row" aria-selected="false">
<span>spreadsheet.xlsx, xlsx File</span>
<span>spreadsheet.xlsx, xlsx File, Private</span>
<i data-icon-name="svg/excel_16x1.svg" aria-label="xlsx">
<img src="./icons/xlsx.svg">
</i>
<div data-automationid="name">spreadsheet.xlsx</div>
</div>
<div id="file-comma" role="row" aria-selected="false">
<span>document, new.docx, docx File, Private</span>
<i data-icon-name="svg/word_16x1.svg" aria-label="docx">
<img src="./icons/docx.svg">
</i>
<div data-automationid="name">document, new.docx</div>
</div>
<div id="file-dot" role="row" aria-selected="false">
<span>document.other.docx, docx File, Private</span>
<i data-icon-name="svg/word_16x1.svg" aria-label="docx">
<img src="./icons/docx.svg">
</i>
<div data-automationid="name">document, new.docx</div>
</div>
</div>
</div>
<script src="./admin-selection.js"></script>
Expand Down
14 changes: 11 additions & 3 deletions test/fixtures/admin-sharepoint.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,26 @@
</i>
<button>folder</button>
</div>
<div class="file" id="file-pdf" role="row" aria-selected="true" aria-label="file.pdf, pdf File">
<div class="file" id="file-pdf" role="row" aria-selected="true" aria-label="file.pdf, pdf File, Private">
<img src="./icons/icon.svg">
<button>file.pdf</button>
</div>
<div class="file" id="file-word" role="row" aria-selected="true" aria-label="document.docx, docx File">
<div class="file" id="file-word" role="row" aria-selected="true" aria-label="document.docx, docx File, Private">
<img src="./icons/docx.svg">
<button>document.docx</button>
</div>
<div class="file" id="file-excel" role="row" aria-selected="false" aria-label="spreadsheet.xlsx, xlsx File">
<div class="file" id="file-excel" role="row" aria-selected="false" aria-label="spreadsheet.xlsx, xlsx File, Private">
<img src="./icons/xlsx.svg">
<button>spreadsheet.xlsx</button>
</div>
<div id="file-comma" role="row" aria-selected="false" aria-label="document, new.docx, docx File, Private">
<img src="./icons/xlsx.svg">
<button>document, new.dox</button>
</div>
<div id="file-dot" role="row" aria-selected="false" aria-label="document.other.docx, docx File, Private">
<img src="./icons/xlsx.svg">
<button>document.other.dox</button>
</div>
</div>
</div>
<script src="./admin-selection.js"></script>
Expand Down
10 changes: 7 additions & 3 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ describe('Test sidekick', () => {
const setup = new Setup('blog');
const { sidekickConfig } = setup;
nock.sidekick(setup, { persist: true });
nock.admin(setup);
const { checkPageResult } = await new SidekickTest({
browser,
page,
Expand Down Expand Up @@ -125,6 +126,7 @@ describe('Test sidekick', () => {
{ status: 403, body: 'Forbidden' },
{ status: 500, body: 'Server error' },
{ status: 504, body: 'Gateway timeout' },
{ status: undefined, body: 'Network error' },
];
nock('https://admin.hlx.page')
.get('/status/adobe/blog/main/en/topics/bla?editUrl=auto')
Expand All @@ -134,7 +136,9 @@ describe('Test sidekick', () => {
.get('/status/adobe/blog/main/en/topics/bla?editUrl=auto')
.reply(500)
.get('/status/adobe/blog/main/en/topics/bla?editUrl=auto')
.reply(504);
.reply(504)
.get('/status/adobe/blog/main/en/topics/bla?editUrl=auto')
.reply(503);
nock.sidekick(new Setup('blog'), { persist: true }); // will be called multiple times
const test = new SidekickTest({
browser,
Expand All @@ -148,7 +152,7 @@ describe('Test sidekick', () => {
}),
});
while (errors.length) {
const { status } = errors.shift();
const { status = 'https://status.adobe.com' } = errors.shift();
// eslint-disable-next-line no-await-in-loop
const { notification, checkPageResult } = await test.run();
const sidekick = checkPageResult;
Expand Down Expand Up @@ -770,8 +774,8 @@ describe('Test sidekick', () => {
it('Hides notifications on overlay click', async () => {
// hides sticky modal on overlay click
const setup = new Setup('blog');
nock.admin(setup);
nock.sidekick(setup);
nock.admin(setup);
const { checkPageResult } = await new SidekickTest({
browser,
page,
Expand Down
2 changes: 1 addition & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ export function Nock() {
n.persist();
}
return n
.get(/.*/)
.get(/\/sidekick\/.*/)
.optionally()
.reply(() => (setup
? [200, configJson || setup.configJson]
Expand Down

0 comments on commit 5d7130b

Please sign in to comment.