From fbd9d588ecc6f8dd978f33bf6745df5b6c12835f Mon Sep 17 00:00:00 2001 From: Asaf David Date: Wed, 5 Mar 2014 07:02:37 +0200 Subject: [PATCH] Escape double quotes #12 --- build/ng-csv.js | 19 +++++++++++++++---- build/ng-csv.min.js | 4 ++-- src/ng-csv/directives/ng-csv.js | 19 +++++++++++++++---- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/build/ng-csv.js b/build/ng-csv.js index 757440b..fe4f221 100644 --- a/build/ng-csv.js +++ b/build/ng-csv.js @@ -51,6 +51,20 @@ angular.module('ngCsv.directives', []). '$attrs', '$transclude', function ($scope, $element, $attrs, $transclude) { + var stringifyCell = function(data) { + if (typeof data === 'string') { + data = data.replace(/"/g, '""'); // Escape double qoutes + if ($scope.txtDelim) data = $scope.txtDelim + data + $scope.txtDelim; + return data; + } + + if (typeof data === 'boolean') { + return data ? 'TRUE' : 'FALSE'; + } + + return data; + }; + $scope.csv = ''; $scope.$watch(function (newValue) { @@ -77,10 +91,7 @@ angular.module('ngCsv.directives', []). angular.forEach(data, function (row, index) { var infoArray = []; angular.forEach(row, function (field) { - if (typeof field === 'string' && $scope.txtDelim) { - field = $scope.txtDelim + field + $scope.txtDelim; - } - this.push(field); + this.push(stringifyCell(field)); }, infoArray); dataString = infoArray.join($scope.fieldSep || ','); csvContent += index < data.length ? dataString + '\r\n' : dataString; diff --git a/build/ng-csv.min.js b/build/ng-csv.min.js index 571ec64..ef9a703 100644 --- a/build/ng-csv.min.js +++ b/build/ng-csv.min.js @@ -1,2 +1,2 @@ -/*! ngcsv 17-01-2014 */ -!function(){angular.module("ngCsv.config",[]).value("ngCsv.config",{debug:!0}).config(["$compileProvider",function(a){angular.isDefined(a.urlSanitizationWhitelist)?a.urlSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|data):/):a.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|data):/)}]),angular.module("ngCsv.directives",[]),angular.module("ngCsv",["ngCsv.config","ngCsv.directives","ngSanitize"]),angular.module("ngCsv.directives",[]).directive("ngCsv",["$parse",function(){return{restrict:"AC",replace:!0,transclude:!0,scope:{data:"&ngCsv",filename:"@filename",header:"&csvHeader",txtDelim:"@textDelimiter",fieldSep:"@fieldSeparator",ngClick:"&"},controller:["$scope","$element","$attrs","$transclude",function(a){a.csv="",a.$watch(function(){a.buildCsv()},!0),a.buildCsv=function(){var b=a.data(),c="data:text/csv;charset=utf-8,",d=a.header();if(d){var e=[];angular.forEach(d,function(a){this.push(a)},e);var f=e.join(a.fieldSep||",");c+=f+"\r\n"}return angular.forEach(b,function(d,e){var f=[];angular.forEach(d,function(b){"string"==typeof b&&a.txtDelim&&(b=a.txtDelim+b+a.txtDelim),this.push(b)},f),dataString=f.join(a.fieldSep||","),c+=e
',link:function(a,b){var c=angular.element(b.children()[0]),d=angular.element(b.children()[1]);c.bind("click",function(){d[0].click(),a.ngClick&&a.ngClick()})}}}])}(window,document); \ No newline at end of file +/*! ngcsv 05-03-2014 */ +!function(){angular.module("ngCsv.config",[]).value("ngCsv.config",{debug:!0}).config(["$compileProvider",function(a){angular.isDefined(a.urlSanitizationWhitelist)?a.urlSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|data):/):a.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|data):/)}]),angular.module("ngCsv.directives",[]),angular.module("ngCsv",["ngCsv.config","ngCsv.directives","ngSanitize"]),angular.module("ngCsv.directives",[]).directive("ngCsv",["$parse",function(){return{restrict:"AC",replace:!0,transclude:!0,scope:{data:"&ngCsv",filename:"@filename",header:"&csvHeader",txtDelim:"@textDelimiter",fieldSep:"@fieldSeparator",ngClick:"&"},controller:["$scope","$element","$attrs","$transclude",function(a){var b=function(b){return"string"==typeof b?(b=b.replace(/"/g,'""'),a.txtDelim&&(b=a.txtDelim+b+a.txtDelim),b):"boolean"==typeof b?b?"TRUE":"FALSE":b};a.csv="",a.$watch(function(){a.buildCsv()},!0),a.buildCsv=function(){var c=a.data(),d="data:text/csv;charset=utf-8,",e=a.header();if(e){var f=[];angular.forEach(e,function(a){this.push(a)},f);var g=f.join(a.fieldSep||",");d+=g+"\r\n"}return angular.forEach(c,function(e,f){var g=[];angular.forEach(e,function(a){this.push(b(a))},g),dataString=g.join(a.fieldSep||","),d+=f
',link:function(a,b){var c=angular.element(b.children()[0]),d=angular.element(b.children()[1]);c.bind("click",function(){d[0].click(),a.ngClick&&a.ngClick()})}}}])}(window,document); \ No newline at end of file diff --git a/src/ng-csv/directives/ng-csv.js b/src/ng-csv/directives/ng-csv.js index 0c8feb1..834ac9b 100644 --- a/src/ng-csv/directives/ng-csv.js +++ b/src/ng-csv/directives/ng-csv.js @@ -24,6 +24,20 @@ angular.module('ngCsv.directives', []). '$attrs', '$transclude', function ($scope, $element, $attrs, $transclude) { + var stringifyCell = function(data) { + if (typeof data === 'string') { + data = data.replace(/"/g, '""'); // Escape double qoutes + if ($scope.txtDelim) data = $scope.txtDelim + data + $scope.txtDelim; + return data; + } + + if (typeof data === 'boolean') { + return data ? 'TRUE' : 'FALSE'; + } + + return data; + }; + $scope.csv = ''; $scope.$watch(function (newValue) { @@ -50,10 +64,7 @@ angular.module('ngCsv.directives', []). angular.forEach(data, function (row, index) { var infoArray = []; angular.forEach(row, function (field) { - if (typeof field === 'string' && $scope.txtDelim) { - field = $scope.txtDelim + field + $scope.txtDelim; - } - this.push(field); + this.push(stringifyCell(field)); }, infoArray); dataString = infoArray.join($scope.fieldSep || ','); csvContent += index < data.length ? dataString + '\r\n' : dataString;