Skip to content

Commit

Permalink
fix(context): strin hidden special chars on context menu Copy command
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Nov 8, 2021
1 parent fe92320 commit 221c05d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
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.

0 comments on commit 221c05d

Please sign in to comment.