2022-02-11 10:14:00 -05:00
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
2019-02-06 07:58:29 -05:00
|
|
|
import initCopyAsGFM, { CopyAsGFM } from '~/behaviors/markdown/copy_as_gfm';
|
2017-10-12 17:31:47 -04:00
|
|
|
|
|
|
|
describe('CopyAsGFM', () => {
|
|
|
|
describe('CopyAsGFM.pasteGFM', () => {
|
2021-07-01 17:08:38 -04:00
|
|
|
let target;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
target = document.createElement('input');
|
|
|
|
target.value = 'This is code: ';
|
|
|
|
});
|
|
|
|
|
|
|
|
// When GFM code is copied, we put the regular plain text
|
|
|
|
// on the clipboard as `text/plain`, and the GFM as `text/x-gfm`.
|
|
|
|
// This emulates the behavior of `getData` with that data.
|
|
|
|
function callPasteGFM(data = { 'text/plain': 'code', 'text/x-gfm': '`code`' }) {
|
2017-10-12 17:31:47 -04:00
|
|
|
const e = {
|
|
|
|
originalEvent: {
|
|
|
|
clipboardData: {
|
|
|
|
getData(mimeType) {
|
2021-07-01 17:08:38 -04:00
|
|
|
return data[mimeType] || null;
|
2017-10-12 17:31:47 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
preventDefault() {},
|
2021-07-01 17:08:38 -04:00
|
|
|
target,
|
2017-10-12 17:31:47 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
CopyAsGFM.pasteGFM(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
it('wraps pasted code when not already in code tags', () => {
|
2021-07-01 17:08:38 -04:00
|
|
|
callPasteGFM();
|
|
|
|
|
|
|
|
expect(target.value).toBe('This is code: `code`');
|
|
|
|
});
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2021-07-01 17:08:38 -04:00
|
|
|
it('does not wrap pasted code when already in code tags', () => {
|
|
|
|
target.value = 'This is code: `';
|
2017-10-12 17:31:47 -04:00
|
|
|
|
|
|
|
callPasteGFM();
|
2021-07-01 17:08:38 -04:00
|
|
|
|
|
|
|
expect(target.value).toBe('This is code: `code');
|
2017-10-12 17:31:47 -04:00
|
|
|
});
|
|
|
|
|
2021-07-01 17:08:38 -04:00
|
|
|
it('does not allow xss in x-gfm-html', () => {
|
|
|
|
const testEl = document.createElement('div');
|
|
|
|
jest.spyOn(document, 'createElement').mockReturnValueOnce(testEl);
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2021-07-01 17:08:38 -04:00
|
|
|
callPasteGFM({ 'text/plain': 'code', 'text/x-gfm-html': 'code<img/src/onerror=alert(1)>' });
|
2017-10-12 17:31:47 -04:00
|
|
|
|
2021-07-01 17:08:38 -04:00
|
|
|
expect(testEl.innerHTML).toBe('code<img src="">');
|
2017-10-12 17:31:47 -04:00
|
|
|
});
|
|
|
|
});
|
2018-06-27 04:47:29 -04:00
|
|
|
|
|
|
|
describe('CopyAsGFM.copyGFM', () => {
|
|
|
|
// Stub getSelection to return a purpose-built object.
|
|
|
|
const stubSelection = (html, parentNode) => ({
|
|
|
|
getRangeAt: () => ({
|
|
|
|
commonAncestorContainer: { tagName: parentNode },
|
|
|
|
cloneContents: () => {
|
|
|
|
const fragment = document.createDocumentFragment();
|
|
|
|
const node = document.createElement('div');
|
|
|
|
node.innerHTML = html;
|
2020-12-23 16:10:24 -05:00
|
|
|
Array.from(node.childNodes).forEach((item) => fragment.appendChild(item));
|
2018-06-27 04:47:29 -04:00
|
|
|
return fragment;
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
rangeCount: 1,
|
|
|
|
});
|
|
|
|
|
|
|
|
const clipboardData = {
|
|
|
|
setData() {},
|
|
|
|
};
|
|
|
|
|
|
|
|
const simulateCopy = () => {
|
|
|
|
const e = {
|
|
|
|
originalEvent: {
|
|
|
|
clipboardData,
|
|
|
|
},
|
|
|
|
preventDefault() {},
|
|
|
|
stopPropagation() {},
|
|
|
|
};
|
|
|
|
CopyAsGFM.copyAsGFM(e, CopyAsGFM.transformGFMSelection);
|
2022-02-11 10:14:00 -05:00
|
|
|
|
|
|
|
return waitForPromises();
|
2018-06-27 04:47:29 -04:00
|
|
|
};
|
|
|
|
|
2022-02-11 10:14:00 -05:00
|
|
|
beforeAll(() => {
|
2019-02-06 07:58:29 -05:00
|
|
|
initCopyAsGFM();
|
|
|
|
|
|
|
|
// Fake call to nodeToGfm so the import of lazy bundle happened
|
2022-02-11 10:14:00 -05:00
|
|
|
return CopyAsGFM.nodeToGFM(document.createElement('div'));
|
2019-02-06 07:58:29 -05:00
|
|
|
});
|
|
|
|
|
2020-06-08 14:08:27 -04:00
|
|
|
beforeEach(() => jest.spyOn(clipboardData, 'setData'));
|
2018-06-27 04:47:29 -04:00
|
|
|
|
|
|
|
describe('list handling', () => {
|
2022-02-11 10:14:00 -05:00
|
|
|
it('uses correct gfm for unordered lists', async () => {
|
2018-06-27 04:47:29 -04:00
|
|
|
const selection = stubSelection('<li>List Item1</li><li>List Item2</li>\n', 'UL');
|
2019-02-06 07:58:29 -05:00
|
|
|
|
2020-06-08 14:08:27 -04:00
|
|
|
window.getSelection = jest.fn(() => selection);
|
2022-02-11 10:14:00 -05:00
|
|
|
await simulateCopy();
|
2018-06-27 04:47:29 -04:00
|
|
|
|
2022-02-11 10:14:00 -05:00
|
|
|
const expectedGFM = '* List Item1\n* List Item2';
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2022-02-11 10:14:00 -05:00
|
|
|
expect(clipboardData.setData).toHaveBeenCalledWith('text/x-gfm', expectedGFM);
|
2018-06-27 04:47:29 -04:00
|
|
|
});
|
|
|
|
|
2022-02-11 10:14:00 -05:00
|
|
|
it('uses correct gfm for ordered lists', async () => {
|
2018-06-27 04:47:29 -04:00
|
|
|
const selection = stubSelection('<li>List Item1</li><li>List Item2</li>\n', 'OL');
|
2019-02-06 07:58:29 -05:00
|
|
|
|
2020-06-08 14:08:27 -04:00
|
|
|
window.getSelection = jest.fn(() => selection);
|
2022-02-11 10:14:00 -05:00
|
|
|
await simulateCopy();
|
2018-06-27 04:47:29 -04:00
|
|
|
|
2022-02-11 10:14:00 -05:00
|
|
|
const expectedGFM = '1. List Item1\n1. List Item2';
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2022-02-11 10:14:00 -05:00
|
|
|
expect(clipboardData.setData).toHaveBeenCalledWith('text/x-gfm', expectedGFM);
|
2018-06-27 04:47:29 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-07-23 05:09:18 -04:00
|
|
|
|
|
|
|
describe('CopyAsGFM.quoted', () => {
|
|
|
|
const sampleGFM = '* List 1\n* List 2\n\n`Some code`';
|
|
|
|
|
2022-02-11 10:14:00 -05:00
|
|
|
it('adds quote char `> ` to each line', () => {
|
2020-07-23 05:09:18 -04:00
|
|
|
const expectedQuotedGFM = '> * List 1\n> * List 2\n> \n> `Some code`';
|
|
|
|
expect(CopyAsGFM.quoted(sampleGFM)).toEqual(expectedQuotedGFM);
|
|
|
|
});
|
|
|
|
});
|
2017-10-12 17:31:47 -04:00
|
|
|
});
|