Skip to content

Commit

Permalink
better field encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
asafdav committed Jul 16, 2014
1 parent 331cc9f commit 18740ad
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
15 changes: 5 additions & 10 deletions build/ng-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ angular.module('ngCsv',
angular.module('ngCsv.services').
service('CSV', ['$q', function($q) {

var EOL = '\r\n';
var EOL = encodeURIComponent('\r\n');
var DATA_URI_PREFIX = "data:text/csv;charset=utf-8,";

/**
* Stringify one field
Expand All @@ -45,7 +46,7 @@ angular.module('ngCsv.services').
if (typeof data === 'string') {
data = data.replace(/"/g, '""'); // Escape double qoutes
if (quoteText || data.indexOf(',') > -1 || data.indexOf('\n') > -1 || data.indexOf('\r') > -1) data = delimier + data + delimier;
return data;
return encodeURIComponent(data);
}

if (typeof data === 'boolean') {
Expand All @@ -69,13 +70,7 @@ angular.module('ngCsv.services').

var that = this;
var csv;
var csvContent;

if(window.navigator.msSaveOrOpenBlob) {
csvContent = "";
}else{
csvContent = "data:text/csv;charset=utf-8,";
}
var csvContent = "";

var dataPromise = $q.when(data).then(function (responseData)
{
Expand Down Expand Up @@ -122,7 +117,7 @@ angular.module('ngCsv.services').
if(window.navigator.msSaveOrOpenBlob) {
csv = csvContent;
}else{
csv = encodeURI(csvContent);
csv = DATA_URI_PREFIX + csvContent;
}
def.resolve(csv);
});
Expand Down
2 changes: 1 addition & 1 deletion build/ng-csv.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 5 additions & 10 deletions src/ng-csv/services/csv-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
angular.module('ngCsv.services').
service('CSV', ['$q', function($q) {

var EOL = '\r\n';
var EOL = encodeURIComponent('\r\n');
var DATA_URI_PREFIX = "data:text/csv;charset=utf-8,";

/**
* Stringify one field
Expand All @@ -16,7 +17,7 @@ angular.module('ngCsv.services').
if (typeof data === 'string') {
data = data.replace(/"/g, '""'); // Escape double qoutes
if (quoteText || data.indexOf(',') > -1 || data.indexOf('\n') > -1 || data.indexOf('\r') > -1) data = delimier + data + delimier;
return data;
return encodeURIComponent(data);
}

if (typeof data === 'boolean') {
Expand All @@ -40,13 +41,7 @@ angular.module('ngCsv.services').

var that = this;
var csv;
var csvContent;

if(window.navigator.msSaveOrOpenBlob) {
csvContent = "";
}else{
csvContent = "data:text/csv;charset=utf-8,";
}
var csvContent = "";

var dataPromise = $q.when(data).then(function (responseData)
{
Expand Down Expand Up @@ -93,7 +88,7 @@ angular.module('ngCsv.services').
if(window.navigator.msSaveOrOpenBlob) {
csv = csvContent;
}else{
csv = encodeURI(csvContent);
csv = DATA_URI_PREFIX + csvContent;
}
def.resolve(csv);
});
Expand Down
23 changes: 22 additions & 1 deletion test/unit/ngCsv/directives/ngCsv.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ describe('ngCsv directive', function () {
expect(scope.$eval(scope.data)).toEqual($rootScope.testDelim);

scope.buildCSV(scope.data).then(function() {
expect(scope.csv).toBe('data:text/csv;charset=utf-8,1,%22a,b%22,2%0D%0Ab,c,3%0D%0A');
expect(scope.csv).toBe('data:text/csv;charset=utf-8,1,%22a%2Cb%22,2%0D%0Ab,c,3%0D%0A');
done();
});
scope.$apply();
Expand All @@ -183,6 +183,27 @@ describe('ngCsv directive', function () {
scope.$apply();
});

it('Handles # in fields properly', function (done) {
$rootScope.testDelim = [{a: 1, b: 'a#b', c: 2}, {a: 'b', b: 'c', c: 3}];
var element = $compile(
'<div ng-csv="testDelim"' +
' filename="custom.csv">' +
'</div>')($rootScope);

$rootScope.$digest();

var scope = element.isolateScope();

// Check that the compiled element contains the templated content
expect(scope.$eval(scope.data)).toEqual($rootScope.testDelim);

scope.buildCSV(scope.data).then(function() {
expect(scope.csv).toBe('data:text/csv;charset=utf-8,1,a%23b,2%0D%0Ab,c,3%0D%0A');
done();
});
scope.$apply();
});

it('Accepts optional field-separator attribute (input array)', function (done) {
var element = $compile(
'<div ng-csv="test" field-separator=\';\'' +
Expand Down

0 comments on commit 18740ad

Please sign in to comment.