Skip to content

Commit

Permalink
Merge pull request #13 from sunrise/pe/escape-attendee-name
Browse files Browse the repository at this point in the history
Person’s CN escaping
  • Loading branch information
Pierre-Élie Fauché @ Sunrise committed Nov 30, 2014
2 parents 822b485 + b1664b3 commit 5b575e9
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 68 deletions.
9 changes: 0 additions & 9 deletions lib/vobject/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,5 @@ module.exports = function(name) {
return output.join('\r\n');
};

component.escape = function(str) {
str = str || '';
return str.replace(/\n/g, '\\n').replace(/;/g, '\\;').replace(/,/g, '\\,').replace(/"/g, '\\"');
};

component.unescape = function(str) {
return str.replace(/\\n/g, '\n').replace(/\\;/g, ';').replace(/\\,/g, ',').replace(/\\"/g, '"');
};

return component;
};
13 changes: 7 additions & 6 deletions lib/vobject/event.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var helpers = require('./helpers');
var Property = require('./property');
var Component = require('./component');
var DateValue = require('./dateValue');
Expand All @@ -20,12 +21,12 @@ module.exports = function() {

// http://tools.ietf.org/html/rfc5545#section-3.8.1.12
event.setSummary = function(summary) {
event.setProperty(new Property('SUMMARY', event.escape(summary)));
event.setProperty(new Property('SUMMARY', helpers.escape(summary)));
};

event.getSummary = function() {
var property = event.getProperty('SUMMARY');
return (property) ? event.unescape(property.value) : undefined;
return (property) ? helpers.unescape(property.value) : undefined;
};

// http://tools.ietf.org/html/rfc5545#section-3.8.2.4
Expand Down Expand Up @@ -90,22 +91,22 @@ module.exports = function() {

// http://tools.ietf.org/html/rfc5545#section-3.8.1.5
event.setDescription = function(description) {
event.setProperty(new Property('DESCRIPTION', event.escape(description)));
event.setProperty(new Property('DESCRIPTION', helpers.escape(description)));
};

event.getDescription = function() {
var property = event.getProperty('DESCRIPTION');
return (property) ? event.unescape(property.value) : undefined;
return (property) ? helpers.unescape(property.value) : undefined;
};

// http://tools.ietf.org/html/rfc5545#section-3.8.1.7
event.setLocation = function(location) {
event.setProperty(new Property('LOCATION', event.escape(location)));
event.setProperty(new Property('LOCATION', helpers.escape(location)));
};

event.getLocation = function() {
var property = event.getProperty('LOCATION');
return (property) ? event.unescape(property.value) : undefined;
return (property) ? helpers.unescape(property.value) : undefined;
};

// http://tools.ietf.org/html/rfc5545#section-3.8.1.11
Expand Down
9 changes: 9 additions & 0 deletions lib/vobject/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

exports.escape = function(str) {
str = str || '';
return str.replace(/\n/g, '\\n').replace(/;/g, '\\;').replace(/,/g, '\\,').replace(/"/g, '\\"');
};

exports.unescape = function(str) {
return str.replace(/\\n/g, '\n').replace(/\\;/g, ';').replace(/\\,/g, ',').replace(/\\"/g, '"');
};
6 changes: 4 additions & 2 deletions lib/vobject/person.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var helpers = require('./helpers');
var Property = require('./property');

module.exports = function(name) {
Expand All @@ -16,11 +17,12 @@ module.exports = function(name) {
// CN
// http://tools.ietf.org/html/rfc5545#section-3.2.2
person.setCN = function(cn) {
person.setParameter('CN', cn);
person.setParameter('CN', helpers.escape(cn));
};

person.getCN = function() {
return person.getParameter('CN');
var cn = person.getParameter('CN');
return (cn) ? helpers.unescape(cn) : undefined;
};

// Mail Address
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"bugs": {
"url": "https://github.com/sunrise/vobject-js/issues"
},
"version": "0.0.29",
"version": "0.0.30",
"dependencies": {
"moment-timezone": "0.2.x",
"underscore": "1.7.x"
Expand Down
44 changes: 0 additions & 44 deletions test/vobject/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,48 +156,4 @@ describe('lib/vobject/component.js', function() {
assert.equal(component.toICS(), 'a\r\nb\r\n');
});
});

describe('escape', function() {
it('should escape newline', function() {
var str = 'this\nthat';
assert.equal(vobject.component().escape(str), 'this\\nthat');
});

it('should escape semicolon', function() {
var str = 'this;that';
assert.equal(vobject.component().escape(str), 'this\\;that');
});

it('should escape comma', function() {
var str = 'this,that';
assert.equal(vobject.component().escape(str), 'this\\,that');
});

it('should escape double quote', function() {
var str = 'this"that';
assert.equal(vobject.component().escape(str), 'this\\"that');
});
});

describe('unescape', function() {
it('should unescape newline', function() {
var str = 'this\\nthat';
assert.equal(vobject.component().unescape(str), 'this\nthat');
});

it('should unescape semicolon', function() {
var str = 'this\\;that';
assert.equal(vobject.component().unescape(str), 'this;that');
});

it('should unescape comma', function() {
var str = 'this\\,that';
assert.equal(vobject.component().unescape(str), 'this,that');
});

it('should unescape double quote', function() {
var str = 'this\\"that';
assert.equal(vobject.component().unescape(str), 'this"that');
});
});
});
48 changes: 48 additions & 0 deletions test/vobject/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var assert = require('assert');
var helpers = require('../../lib/vobject/helpers');

describe('lib/vobject/helpers.js', function() {
describe('escape', function() {
it('should escape newline', function() {
var str = 'this\nthat';
assert.equal(helpers.escape(str), 'this\\nthat');
});

it('should escape semicolon', function() {
var str = 'this;that';
assert.equal(helpers.escape(str), 'this\\;that');
});

it('should escape comma', function() {
var str = 'this,that';
assert.equal(helpers.escape(str), 'this\\,that');
});

it('should escape double quote', function() {
var str = 'this"that';
assert.equal(helpers.escape(str), 'this\\"that');
});
});

describe('unescape', function() {
it('should unescape newline', function() {
var str = 'this\\nthat';
assert.equal(helpers.unescape(str), 'this\nthat');
});

it('should unescape semicolon', function() {
var str = 'this\\;that';
assert.equal(helpers.unescape(str), 'this;that');
});

it('should unescape comma', function() {
var str = 'this\\,that';
assert.equal(helpers.unescape(str), 'this,that');
});

it('should unescape double quote', function() {
var str = 'this\\"that';
assert.equal(helpers.unescape(str), 'this"that');
});
});
});
17 changes: 11 additions & 6 deletions test/vobject/person.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,27 @@ describe('lib/vobject/person.js', function() {
});

describe('setCN', function() {
it('should set CN', function(done) {
it('should set CN (escaped)', function(done) {
var person = VObject.person();
person.setParameter = function(name, value) {
assert.equal(name, 'CN');
assert.equal(value, 'value');
assert.equal(value, 'value\\n\\;\\,');
done();
};
person.setCN('value');
person.setCN('value\n;,');
});
});

describe('getCN', function() {
it('should get CN', function() {
it('should default to undefined', function() {
var person = VObject.person();
assert.equal(person.getCN(), undefined);
});

it('should get CN (unescaped)', function() {
var person = VObject.person();
person.setCN('value');
assert.equal(person.getCN(), 'value');
person.parameters['CN'] = 'value\\n\\;\\,';
assert.equal(person.getCN(), 'value\n;,');
});
});

Expand Down

0 comments on commit 5b575e9

Please sign in to comment.