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(context): strip hidden special chars on context menu Copy command #537

Merged
merged 1 commit into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ describe('contextMenuExtension', () => {
it('should call "copyToClipboard" and get the value even when there is a "queryFieldNameGetterFn" callback defined with dot notation the command triggered is "copy"', () => {
const copyGridOptionsMock = { ...gridOptionsMock, enableExcelExport: false, enableTextExport: false, contextMenu: { hideCopyCellValueCommand: false } } as GridOption;
const columnMock = { id: 'firstName', name: 'First Name', field: 'firstName', queryFieldNameGetterFn: () => 'lastName' } as Column;
const dataContextMock = { id: 123, firstName: 'John', lastName: 'Doe', age: 50 };
const dataContextMock = { id: 123, firstName: '\u034f\u034fJohny', lastName: '\u034f\u034fDoe', age: 50 };
jest.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(copyGridOptionsMock);
const execSpy = jest.spyOn(window.document, 'execCommand');
extension.register();
Expand All @@ -667,7 +667,7 @@ describe('contextMenuExtension', () => {
it('should call "copyToClipboard" and get the value even when there is a "queryFieldNameGetterFn" callback defined with dot notation the command triggered is "copy"', () => {
const copyGridOptionsMock = { ...gridOptionsMock, enableExcelExport: false, enableTextExport: false, contextMenu: { hideCopyCellValueCommand: false } } as GridOption;
const columnMock = { id: 'firstName', name: 'First Name', field: 'firstName', queryFieldNameGetterFn: () => 'user.lastName' } as Column;
const dataContextMock = { id: 123, user: { firstName: 'John', lastName: 'Doe', age: 50 } };
const dataContextMock = { id: 123, user: { firstName: 'John', lastName: '·\u034f ⮞ Doe', age: 50 } };
jest.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(copyGridOptionsMock);
const execSpy = jest.spyOn(window.document, 'execCommand');
extension.register();
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/extensions/contextMenuExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ export class ContextMenuExtension implements Extension {
}

// remove any unwanted Tree Data/Grouping symbols from the beginning of the string before copying (e.g.: "⮟ Task 21" or "· Task 2")
const finalTextToCopy = textToCopy.replace(/^([·|⮞|⮟]\s*)|([·|⮞|⮟])\s*/g, '');
const finalTextToCopy = textToCopy.replace(/^([·|⮞|⮟]\s*)|([·|⮞|⮟])\s*/gi, '').replace(/\u034f/gi, '').trim();

// create fake <textarea> (positioned outside of the screen) to copy into clipboard & delete it from the DOM once we're done
const tmpElem = document.createElement('textarea') as HTMLTextAreaElement;
Expand All @@ -437,7 +437,7 @@ export class ContextMenuExtension implements Extension {
tmpElem.value = finalTextToCopy;
document.body.appendChild(tmpElem);
tmpElem.select();
const success = document.execCommand('copy', false, textToCopy);
const success = document.execCommand('copy', false, finalTextToCopy);
if (success) {
tmpElem.remove();
}
Expand Down
5 changes: 3 additions & 2 deletions packages/common/src/formatters/formatterUtilities.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FieldType } from '../enums/fieldType.enum';
import { Column, ExcelExportOption, Formatter, GridOption, SlickGrid, TextExportOption } from '../interfaces/index';
import { mapMomentDateFormatWithFieldType } from '../services/utilities';
import { mapMomentDateFormatWithFieldType, sanitizeHtmlToText } from '../services/utilities';
import * as moment_ from 'moment-mini';
import { multipleFormatter } from './multipleFormatter';
const moment = (moment_ as any)['default'] || moment_; // patch to fix rollup "moment has no default export" issue, document here https://github.com/rollup/rollup/issues/670
Expand Down Expand Up @@ -113,7 +113,8 @@ export function exportWithFormatterWhenDefined<T = any>(row: number, col: number
formatter = columnDef.formatter;
}

return parseFormatterWhenExist(formatter, row, col, columnDef, dataContext, grid);
const output = parseFormatterWhenExist(formatter, row, col, columnDef, dataContext, grid);
return exportOptions?.sanitizeDataExport ? sanitizeHtmlToText(output) : output;
}

/**
Expand Down
Binary file not shown.