Added space, single-quote char and other chars to the list of chars that fors parameter value

to be quoted when generating icalendar content.
Before generating ical content all double quotes from parameter values are replaced with single quotes,
as it is illegal to have double quotes in parameter values (https://github.com/collective/icalendar/issues/79)
Added tests
pull/81/head
Victor Varvaryuk 2013-01-15 16:30:14 +04:00
rodzic aa0bf3361d
commit 6698a6f5ec
2 zmienionych plików z 35 dodań i 1 usunięć

Wyświetl plik

@ -137,7 +137,9 @@ def validate_param_value(value, quoted=True):
raise ValueError(value)
QUOTABLE = re.compile('[,;:].')
# chars presence of which in parameter value will be cause the value
# to be enclosed in double-tuotes
QUOTABLE = re.compile("[,;: ']")
def dQuote(val):
@ -150,6 +152,9 @@ def dQuote(val):
>>> dQuote('name:value')
'"name:value"'
"""
# a double-quote character is forbidden to appear in a parameter value
# so replace it with a single-quote character
val = val.replace('"', "'")
if QUOTABLE.search(val):
return '"%s"' % val
return val

Wyświetl plik

@ -1,6 +1,9 @@
# coding: utf-8
import icalendar
import unittest
class TestPropertyParams(unittest.TestCase):
def test_property_params(self):
@ -19,3 +22,29 @@ class TestPropertyParams(unittest.TestCase):
# an icalendar string.
ical2 = icalendar.Calendar.from_ical(ical_str)
self.assertEqual(ical2.get('ORGANIZER').params.get('CN'), 'Doe, John')
def test_quoting(self):
# not double-quoted
self._test_quoting(u"Aramis", 'Aramis')
# if a space is present - enclose in double quotes
self._test_quoting(u"Aramis Alameda", '"Aramis Alameda"')
# a single quote in parameter value - double quote the value
self._test_quoting("Aramis d'Alameda", '"Aramis d\'Alameda"')
# double quote is replaced with single quote
self._test_quoting("Aramis d\"Alameda", '"Aramis d\'Alameda"')
self._test_quoting(u"Арамис д'Аламеда", '"Арамис д\'Аламеда"')
def _test_quoting(self, cn_param, cn_quoted):
"""
@param cn_param: CN parameter value to test for quoting
@param cn_quoted: expected quoted parameter in icalendar format
"""
vevent = icalendar.Event()
attendee = icalendar.vCalAddress('test@mail.com')
attendee.params['CN'] = cn_param
vevent.add('ATTENDEE', attendee)
self.assertEqual(
vevent.to_ical(),
'BEGIN:VEVENT\r\nATTENDEE;CN=%s:test@mail.com\r\nEND:VEVENT\r\n'
% cn_quoted
)