diff options
Diffstat (limited to '')
-rw-r--r-- | static/jcal-tests.js | 32 | ||||
-rw-r--r-- | static/jcal.js | 41 |
2 files changed, 59 insertions, 14 deletions
diff --git a/static/jcal-tests.js b/static/jcal-tests.js new file mode 100644 index 00000000..c84d9bd1 --- /dev/null +++ b/static/jcal-tests.js @@ -0,0 +1,32 @@ +/* "Test cases" for jcal.js. + ideally we would actually have runnable tests, but + `document' is only available in the browser. +*/ + +let doc = document.implementation.createDocument(xcal, 'icalendar'); + +jcal = ['key', {}, 'text', 'value']; + +jcal_property_to_xcal_property(doc, jcal); + + + +jcal_to_xcal(['vcalendar', [], [['vevent', [['key', {}, 'text', 'value']], []]]]).childNodes[0].outerHTML + +/* returns (except not pretty printee) +<icalendar xmlns="urn:ietf:params:xml:ns:icalendar-2.0"> + <vcalendar> + <properties/> + <components> + <vevent> + <properties> + <key> + <text>value</text> + </key> + </properties> + <components/> + </vevent> + </components> + </vcalendar> +</icalendar> +*/ diff --git a/static/jcal.js b/static/jcal.js index ebf80cbd..38aeaae9 100644 --- a/static/jcal.js +++ b/static/jcal.js @@ -54,23 +54,36 @@ function jcal_property_to_xcal_property(doc, jcal) { let tag = doc.createElementNS(xcal, propertyName); - if (params !== {}) { - let params = doc.createElementNS(xcal, params); - for (var key in params) { - let el = doc.createElementNS(xcal, key); - - for (let v of asList(params[key])) { - let text = doc.createElementNS(xcal, 'text'); - text.innerHTML = '' + v; - el.appendChild(text); - } - - params.appendChild(el); + /* setup parameters */ + let paramEl = doc.createElementNS(xcal, 'params'); + for (var key in params) { + /* Check if the key actually belongs to us. + At least (my) format also appears when iterating + over the parameters. Probably a case of builtins + vs user defined. + + This is also the reason we can't check if params + is empty beforehand, and instead check the + number of children of paramEl below. + */ + if (! params.hasOwnProperty(key)) continue; + + let el = doc.createElementNS(xcal, key); + + for (let v of asList(params[key])) { + let text = doc.createElementNS(xcal, 'text'); + text.innerHTML = '' + v; + el.appendChild(text); } - tag.appendChild(params); + paramEl.appendChild(el); + } + + if (paramEl.childCount > 0) { + tag.appendChild(paramEl); } + /* setup value (and type) */ // let typeEl = doc.createElementNS(xcal, type); switch (propertyName) { @@ -140,7 +153,7 @@ function jcal_to_xcal_inner(doc, jcal) { let xcal_properties = doc.createElementNS(xcal, 'properties'); for (let property of properties) { - xcal_properties.appendChild(jcal_property_to_xcal_property(property)); + xcal_properties.appendChild(jcal_property_to_xcal_property(doc, property)); } let xcal_children = doc.createElementNS(xcal, 'components'); |