asafdav / ng-csv

Simple directive that turns arrays and objects into downloadable CSV files
MIT License
573 stars 215 forks source link

# character causes fail in FireFox #41

Closed petecleary closed 9 years ago

petecleary commented 9 years ago

The rendering of the csv content stops in FireFox when the code first encounters a # character.

Only tested in FireFox 30 to date.

See the issue @ http://jsfiddle.net/z3bs2/

Pete

petecleary commented 9 years ago

Hi

I have done some more investigation and it appears to be because your are encoding using encodeURI which will omit encoding URL characters , / ? : @ & = + $ #

By changing the encoding to the field item and encoding it fully then it renders correctly within FireFox, this only works with the text delimiter set.

Below is my reworked buildCSV function.

$scope.buildCsv = function () {
            var data = $scope.data();
            var csvContent = 'data:text/csv;charset=utf-8,';

            // Check if there's a provided header array
            var header = $scope.header();
            if (header) {
              var encodingArray = [];
              angular.forEach(header, function (title) {
                this.push(title);
              }, encodingArray);

              var headerString = encodingArray.join($scope.fieldSep || ',');
              //csvContent += headerString + '\r\n'; *** REPLACE WITH THE ENCODED LINE ENDINGS
              csvContent += headerString + '%0D%0A';
            }

            // Process the data
            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; *** REPLACE WITH ENCODING THE FIELD VALUE
                    field = $scope.txtDelim + encodeURIComponent(field) + $scope.txtDelim;
                }
                this.push(field);
              }, infoArray);
              dataString = infoArray.join($scope.fieldSep || ',');
              //csvContent += index < data.length ? dataString + '\r\n' : dataString; *** REPLACE WITH THE ENCODED LINE ENDINGS
              csvContent += index < data.length ? dataString + '%0D%0A' : dataString;
            });

            //$scope.csv = encodeURI(csvContent); *** REPLACE WITH NOT ENCODING AS IT IS DONE ABOVE
            $scope.csv = csvContent;
            return $scope.csv;
          };

Pete

asafdav commented 9 years ago

Hi, Can you please send a pull request ? It would be much easier to go over your code.

Thanks again!

petecleary commented 9 years ago

Hi

I've forked your code and it has changed from the version I re-wrote locally. I will need some time to check your current version and change accordingly.

Pete

asafdav commented 9 years ago

What ng-csv version did you started with ?

On Tue, Jul 15, 2014 at 10:14 AM, Pete Cleary notifications@github.com wrote:

Hi

I've forked your code and it has changed from the version I re-wrote locally. I will need some time to check your current version and change accordingly.

Pete

— Reply to this email directly or view it on GitHub https://github.com/asafdav/ng-csv/issues/41#issuecomment-48997955.

petecleary commented 9 years ago

Good question, I built the initial system back in Feb, so it looks like the version compiled from Jan https://github.com/asafdav/ng-csv/blob/1e29fcb45f7404499d71acd85a331f997fecd75c/build/ng-csv.js

petecleary commented 9 years ago

My JS Fiddle with the new code still fails in FF. http://jsfiddle.net/z3bs2/

asafdav commented 9 years ago

Ok I'll take a look, thanks! On Jul 15, 2014 10:44 AM, "Pete Cleary" notifications@github.com wrote:

My JS Fiddle with the new code still fails in FF. http://jsfiddle.net/z3bs2/

— Reply to this email directly or view it on GitHub https://github.com/asafdav/ng-csv/issues/41#issuecomment-49000278.

asafdav commented 9 years ago

Hi, Thanks for the fiddle, I went through it, you didn't use the latest version of ngCsv but the issue persisted in the newest version as well, I just pushed a fix.

Checkout the updated fiddle, http://jsfiddle.net/utCt3/ It seems like working now.

Thanks again for your help, I hope you can switch back to the main branch.

petecleary commented 9 years ago

Thank you, it is working well.