changeset 4559:1f5b1e0423fb

id generation: do not quote colon as it is valid in IDs, add test for identity transformation of valid IDs
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 07 Feb 2009 19:27:16 +0100
parents a14a30ec80af
children 050428d1c044
files MoinMoin/_tests/test_wikiutil.py MoinMoin/wikiutil.py
diffstat 2 files changed, 8 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/_tests/test_wikiutil.py	Sat Feb 07 14:39:03 2009 +0100
+++ b/MoinMoin/_tests/test_wikiutil.py	Sat Feb 07 19:27:16 2009 +0100
@@ -913,6 +913,9 @@
             (u'level_2',              'level_2'),
             (u'',                     'A'),
             (u'123',                  'A123'),
+            # make sure that a valid anchor is not modified:
+            (u'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789:_.-',
+             u'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789:_.-')
         ]
         for text, expected in tests:
             yield self._check, text, expected
--- a/MoinMoin/wikiutil.py	Sat Feb 07 14:39:03 2009 +0100
+++ b/MoinMoin/wikiutil.py	Sat Feb 07 19:27:16 2009 +0100
@@ -2417,10 +2417,13 @@
 
 def anchor_name_from_text(text):
     '''
-    Generate an anchor name from the given text
+    Generate an anchor name from the given text.
     This function generates valid HTML IDs matching: [A-Za-z][A-Za-z0-9:_.-]*
+    Note: this transformation has a special feature: when you feed it with a
+          valid ID/name, it will return it without modification (identity
+          transformation).
     '''
-    quoted = urllib.quote_plus(text.encode('utf-7'))
+    quoted = urllib.quote_plus(text.encode('utf-7'), safe=':')
     res = quoted.replace('%', '.').replace('+', '_')
     if not res[:1].isalpha():
         return 'A%s' % res