Skip to content

Commit

Permalink
fix(excelCopy): Excel Copy Buffer should have sanitize option as export
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Apr 11, 2018
1 parent 75a4147 commit 8c57298
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { ExportService } from './export.service';
import { FilterService } from './filter.service';
import { SortService } from './sort.service';
import { sanitizeHtmlToText } from './../services/utilities';
import * as $ from 'jquery';

// using external non-typed js libraries
Expand Down Expand Up @@ -163,7 +164,11 @@ export class ControlAndPluginService {
if (!gridOptions.editable || !columnDef.editor) {
const isEvaluatingFormatter = (columnDef.exportWithFormatter !== undefined) ? columnDef.exportWithFormatter : gridOptions.exportOptions.exportWithFormatter;
if (columnDef.formatter && isEvaluatingFormatter) {
return columnDef.formatter(0, 0, item[columnDef.field], columnDef, item, this._grid);
const formattedOutput = columnDef.formatter(0, 0, item[columnDef.field], columnDef, item, this._grid);
if (columnDef.sanitizeDataExport || (gridOptions.exportOptions && gridOptions.exportOptions.sanitizeDataExport)) {
return sanitizeHtmlToText(formattedOutput);
}
return formattedOutput;
}
}

Expand Down
19 changes: 4 additions & 15 deletions aurelia-slickgrid/src/aurelia-slickgrid/services/export.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from './../models/index';
import { TextEncoder } from 'text-encoding-utf-8';
// import { TextEncoder } from 'utf8-encoding';
import { addWhiteSpaces, htmlEntityDecode } from './../services/utilities';
import { addWhiteSpaces, htmlEntityDecode, sanitizeHtmlToText } from './../services/utilities';
import * as $ from 'jquery';

// using external non-typed js libraries
Expand Down Expand Up @@ -226,7 +226,7 @@ export class ExportService {

// does the user want to sanitize the output data (remove HTML tags)?
if (columnDef.sanitizeDataExport || this._exportOptions.sanitizeDataExport) {
itemData = this.sanitizeHtmlToText(itemData);
itemData = sanitizeHtmlToText(itemData);
}

// when CSV we also need to escape double quotes twice, so " becomes ""
Expand All @@ -250,7 +250,7 @@ export class ExportService {
* @param itemObj
*/
readGroupedTitleRow(itemObj: any) {
let groupName = this.sanitizeHtmlToText(itemObj.title);
let groupName = sanitizeHtmlToText(itemObj.title);
const exportQuoteWrapper = this._exportQuoteWrapper || '';
const delimiter = this._exportOptions.delimiter;
const format = this._exportOptions.format;
Expand Down Expand Up @@ -292,7 +292,7 @@ export class ExportService {

// does the user want to sanitize the output data (remove HTML tags)?
if (columnDef.sanitizeDataExport || this._exportOptions.sanitizeDataExport) {
itemData = this.sanitizeHtmlToText(itemData);
itemData = sanitizeHtmlToText(itemData);
}

if (format === FileType.csv) {
Expand All @@ -308,17 +308,6 @@ export class ExportService {
return output;
}

/**
* Sanitize, return only the text without HTML tags
* @input htmlString
* @return text
*/
sanitizeHtmlToText(htmlString: string) {
const temp = document.createElement('div');
temp.innerHTML = htmlString;
return temp.textContent || temp.innerText;
}

/**
* Triggers download file with file format.
* IE(6-10) are not supported
Expand Down
11 changes: 11 additions & 0 deletions aurelia-slickgrid/src/aurelia-slickgrid/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,17 @@ export function parseUtcDate(inputDateString: string, useUtc: boolean): string |
return date;
}

/**
* Sanitize, return only the text without HTML tags
* @input htmlString
* @return text
*/
export function sanitizeHtmlToText(htmlString: string) {
const temp = document.createElement('div');
temp.innerHTML = htmlString;
return temp.textContent || temp.innerText;
}

/**
* Converts a string to camel case
* @param str the string to convert
Expand Down
2 changes: 2 additions & 0 deletions aurelia-slickgrid/src/examples/slickgrid/example12.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,13 @@ export class Example12 {
sidePadding: 15
},
enableAutoResize: true,
enableExcelCopyBuffer: true,
enableFiltering: true,
enableTranslate: true,
exportOptions: {
// set at the grid option level, meaning all column will evaluate the Formatter (when it has a Formatter defined)
exportWithFormatter: true,
sanitizeDataExport: true
},
gridMenu: {
showExportCsvCommand: true, // true by default, so it's optional
Expand Down

0 comments on commit 8c57298

Please sign in to comment.