Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Colon in TYPE meta field gets escaped and breaks lables on e.g. iPhone #6

Merged
merged 1 commit into from Dec 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/vcard.js
Expand Up @@ -194,6 +194,15 @@
.replace(/,/g, '\\,')
};

var escapeTypeCharacters = function(v) {
if (typeof v === 'undefined') {
return '';
}
return v
.replace(/\n/g, '\\n')
.replace(/;/g, '\\;')
};

Object.keys(data).forEach(function (key) {
if (!data[key] || typeof data[key].forEach !== 'function') {
return;
Expand Down Expand Up @@ -233,7 +242,12 @@
}
value.meta[metaKey].forEach(function (metaValue) {
if (metaKey.length > 0) {
line += ';' + escapeCharacters(metaKey.toUpperCase()) + '=' + escapeCharacters(metaValue);
if (metaKey.toUpperCase() === 'TYPE') {
// Do not escape the comma when it is the type property. This breaks a lot.
line += ';' + escapeCharacters(metaKey.toUpperCase()) + '=' + escapeTypeCharacters(metaValue);
} else {
line += ';' + escapeCharacters(metaKey.toUpperCase()) + '=' + escapeCharacters(metaValue);
}
}
});
});
Expand Down
21 changes: 18 additions & 3 deletions test/vcard.generate.spec.js
Expand Up @@ -82,6 +82,21 @@ describe('vCard.generate', function () {
].join('\r\n'));
});

it('Should not break comma seperated type keys', function () {
var card = {
tel: [
{value: '78884545247', meta: {type: ['HOME,PREF']}}
]
};
var string = vCard.generate(card);

expect(string).toEqual([
PREFIX,
'TEL;TYPE=HOME,PREF:78884545247',
POSTFIX
].join('\r\n'));
});

it('Should generate vcard with multiple values of one metadata field', function () {
var card = {
tel: [
Expand Down Expand Up @@ -258,7 +273,7 @@ describe('vCard.generate', function () {
].join('\r\n'));
});

it('Should escape semicolon, colon and backslash in meta fields', function () {
it('Should escape semicolon and backslash in meta fields', function () {
var card = {
tel: [
{value: '78884545247', meta: {type: ['HO;,\\ME'], pref: ['1']}}
Expand All @@ -268,7 +283,7 @@ describe('vCard.generate', function () {

expect(string).toEqual([
PREFIX,
'TEL;TYPE=HO\\;\\,\\ME;PREF=1:78884545247',
'TEL;TYPE=HO\\;,\\ME;PREF=1:78884545247',
POSTFIX
].join('\r\n'));
});
Expand Down Expand Up @@ -298,7 +313,7 @@ describe('vCard.generate', function () {

expect(string).toEqual([
PREFIX,
'TEL;TYPE=HO\\;\\,\\ME;PREF=1:78884545247',
'TEL;TYPE=HO\\;,\\ME;PREF=1:78884545247',
POSTFIX
].join('\r\n'));
});
Expand Down