Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect the right design set for vcard3 #680

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/ical/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const NAME_INDEX = 0;
const PROPERTY_INDEX = 1;
const COMPONENT_INDEX = 2;

const PROPERTY_NAME_INDEX = 0;
const PROPERTY_VALUE_INDEX = 3;

/**
* Wraps a jCal component, adding convenience methods to add, remove and update subcomponents and
* properties.
Expand Down Expand Up @@ -98,6 +101,17 @@ class Component {
*/
get _designSet() {
let parentDesign = this.parent && this.parent._designSet;
if (!parentDesign && this.name == "vcard") {
// We can't decide on vcard3 vs vcard4 just based on the component name, the version number is
// in the version property. We also can't use hydrated properties here because it would lead
// to recursion, but the spec says that the version property needs to be the very first one.
let versionProp = this.jCal[PROPERTY_INDEX]?.[0];

if (versionProp && versionProp[PROPERTY_NAME_INDEX] == "version" && versionProp[PROPERTY_VALUE_INDEX] == "3.0") {
return design.getDesignSet("vcard3");
}
}

return parentDesign || design.getDesignSet(this.name);
}

Expand Down
3 changes: 3 additions & 0 deletions lib/ical/design.js
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,7 @@ let vcard3Properties = extend(commonProperties, {
* @type {ICAL.design.designSet}
*/
let icalSet = {
name: "ical",
value: icalValues,
param: icalParams,
property: icalProperties,
Expand All @@ -909,6 +910,7 @@ let icalSet = {
* @type {ICAL.design.designSet}
*/
let vcardSet = {
name: "vcard4",
value: vcardValues,
param: vcardParams,
property: vcardProperties,
Expand All @@ -920,6 +922,7 @@ let vcardSet = {
* @type {ICAL.design.designSet}
*/
let vcard3Set = {
name: "vcard3",
value: vcard3Values,
param: vcard3Params,
property: vcard3Properties,
Expand Down
35 changes: 35 additions & 0 deletions test/design_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -964,4 +964,39 @@ suite('design', function() {
});
});
});

suite('design sets', function() {
test('detection', function() {
let component = new ICAL.Component(ICAL.parse(
'BEGIN:VCARD\n' +
'VERSION:4.0\n' +
'FN:Fun Name\n' +
'BDAY:--0203\n' +
'END:VCARD'
));
assert.equal(component._designSet?.name, 'vcard4');
assert.equal(component.getFirstProperty('fn')._designSet?.name, 'vcard4');

component = new ICAL.Component(ICAL.parse(
'BEGIN:VCARD\n'+
'VERSION:3.0\n'+
'FN:Fun Name\n'+
'TEL;TYPE=VOICE,MSG,WORK:+1-555-937-3419\n'+
'TEL;TYPE=FAX,WORK:+1-555-528-4164\n'+
'EMAIL;TYPE=INTERNET:[email protected]\n'+
'END:VCARD'
));
assert.equal(component._designSet?.name, 'vcard3');
assert.equal(component.getFirstProperty('fn')._designSet?.name, 'vcard3');

component = new ICAL.Component(ICAL.parse(
'BEGIN:VCALENDAR\n'+
'PRODID:-//Google Inc//Google Calendar 70.9054//EN\n' +
'VERSION:2.0\n'+
'END:VCALENDAR'
));
assert.equal(component._designSet?.name, 'ical');
assert.equal(component.getFirstProperty('version')._designSet?.name, 'ical');
});
});
});