Commits

Anonymous committed 7e11e83

tests for Html conv and its "add_" interface

  • Participants
  • Parent commits c1f1a8a
  • Branches unstable

Comments (0)

Files changed (3)

File insanities/forms/convs.py

 
 class Html(Char):
     
-    allowed_elements = ('a', 'p', 'br', 'li', 'ul', 'ol', 'hr', 'u', 'i', 'b',
-                        'blockquote', 'sub', 'sup')
-    allowed_attributes = ('href', 'src', 'alt', 'title', 'class', 'rel')
-    drop_empty_tags = ('p', 'a', 'u', 'i', 'b', 'sub', 'sup')
+    allowed_elements = frozenset(('a', 'p', 'br', 'li', 'ul', 'ol', 'hr', 'u',
+                                  'i', 'b', 'blockquote', 'sub', 'sup'))
+    allowed_attributes = frozenset(('href', 'src', 'alt', 'title', 'class', 'rel'))
+    drop_empty_tags = frozenset(('p', 'a', 'u', 'i', 'b', 'sub', 'sup'))
     allowed_classes = {}
     
     def __init__(self, **kwargs):
-        from ..utils.html import Sanitizer, PROPERTIES
+        from ..utils.html import Sanitizer, PROPERTIES, LIST_PROPERTIES
         
-        #sa_kwargs = kwargs.copy()
+        def _load_arg(obj, kwargs, opt, default):
+            # loads arg from object or kwargs
+            default = getattr(obj, opt, default)
+            return 
+        
         for opt in PROPERTIES:
             if hasattr(self, opt):
                 kwargs.setdefault(opt, getattr(self, opt))
-            if hasattr(self, 'add_' + opt):
-                kwargs.setdefault('add_' + opt, getattr(self, 'add_' + opt))
+        for opt in LIST_PROPERTIES:
+            add_key = 'add_' + opt
+            if hasattr(self, add_key) or add_key in kwargs:
+                kwargs[opt] = set(kwargs.get(opt, LIST_PROPERTIES[opt]))
+                kwargs[opt].update(kwargs.pop(add_key))
 
         super(Html, self).__init__(**kwargs)
 
     
     @property
     def tags(self):
-        return self.sanitizer.allowed_elements
+        return self.allowed_elements
     @property
     def attrs(self):
-        return self.sanitizer.allowed_attributes
+        return self.allowed_attributes
 
 
 class List(Converter):

File insanities/utils/html.py

 
 SAFE_CLASSES = {}
 
-def _add_options_helper(obj, kwargs, key):
-    # addition to kwargs as in convs.Html API
-    if key.startswith('add_') and key[4:] in obj.options:
-        key_to_add = key[4:]
-        default = getattr(obj, key_to_add)
-        if type(default) in (list, tuple):
-            value = kwargs.get(key_to_add, default)
-            value = list(value) + list(kwargs.pop(key))
-            setattr(obj, key_to_add, value)
-            kwargs[key_to_add] = value
-            return True
-
 class TokenSanitazer(sanitizer.HTMLSanitizer):
     escape_invalid_tags = False
     
             if old in kwargs:
                 # XXX write warning
                 kwargs[new] = kwargs.pop(old)
-            if 'add_' + old in kwargs:
-                # XXX write warning
-                kwargs['add_' + new] = kwargs.pop('add_' + old)
                 
         for key in kwargs.keys():
-            add = _add_options_helper(self, kwargs, key)
             if key in self.options:
                 setattr(self, key, kwargs.pop(key))
-            elif not add and key not in ('encoding', 'parseMeta', 'useChardet', 
-                                         'lowercaseElementName',
-                                         'lowercaseAttrName'):
+            elif key not in ('encoding', 'parseMeta', 'useChardet', 
+                             'lowercaseElementName', 'lowercaseAttrName'):
                 kwargs.pop(key)
         super(TokenSanitazer, self).__init__(*args, **kwargs)
     
     return clean
 
 class Sanitizer(object):
-    allowed_elements = sanitizer.HTMLSanitizer.acceptable_elements
-    allowed_attributes = sanitizer.HTMLSanitizer.acceptable_attributes    
     dom_callbacks = [remove_a_tags_without_href]
     string_callbacks = [strip_empty_tags]
     method = 'xhtml'
 
     def __init__(self, **kwargs):
         for key in kwargs.keys():
-            add = _add_options_helper(self, kwargs, key)
             if key in self.options:
                 setattr(self, key, kwargs.pop(key))
         self.kwargs = kwargs
 
         return unicode(clean)
 
+def _get_list_props(cls):
+    return [(opt, getattr(cls, opt))
+            for opt in cls.options
+            if type(getattr(cls, opt)) in (list, tuple)]
 
-PROPERTIES = list(TokenSanitazer.options) + \
-             [x[0] for x in TokenSanitazer.property_aliases] + \
-             list(Sanitizer.options)
-
-
+PROPERTIES = list(TokenSanitazer.options) + list(Sanitizer.options)
+LIST_PROPERTIES = dict(_get_list_props(TokenSanitazer) + _get_list_props(Sanitizer))
 
 def sanitize(buf, **kwargs):
     return Sanitizer(**kwargs).sanitize(buf)

File tests/forms/convs.py

+# -*- coding: utf-8 -*-
+import unittest
+from insanities.forms import convs
+
+class TestChar(unittest.TestCase):
+    conv_class = convs.Char
+    def setUp(self):
+        self.attrs = {
+        }
+
+    @property
+    def conv(self):
+        return self.conv_class(**self.attrs)
+    
+    def test_clean_any_value(self):
+        txt = 'any random value'
+        clean = self.conv.clean_value(txt)
+        self.assertEqual(clean, txt)
+    
+class TestHtml(TestChar):
+    '''Tests for html converter'''
+    conv_class = convs.Html
+
+    def setUp(self):
+        self.attrs = {
+            'allowed_elements': ('a', 'p'),
+            'allowed_attributes': ('title', 'href'),
+        }
+    def assertEqualSets(self, a, b):
+        return self.assertEqual(set(a), set(b))
+
+    def test_setting(self):
+        conv = self.conv
+        self.assertEqualSets(conv.tags, ['a', 'p'])
+
+    def test_double_setting(self):
+        conv = self.conv()
+        self.assertEqualSets(conv.tags, ['a', 'p'])
+    
+    def test_double_adding(self):
+        conv = self.conv(add_allowed_elements=['span'])
+        self.assertEqualSets(conv.tags, ['a', 'p', 'span'])
+    
+    def test_triple_adding(self):
+        conv = self.conv(add_allowed_elements=['span'])(add_allowed_elements=['b'])
+        self.assertEqualSets(conv.tags, ['a', 'p', 'b', 'span'])
+    
+    def test_adding_independence(self):
+        conv = self.conv
+        conv1 = conv(add_allowed_elements=['b'])
+        conv2 = conv(add_allowed_elements=['span'])
+
+        self.assertEqualSets(conv1.tags, ['a', 'p', 'b'])
+        self.assertEqualSets(conv2.tags, ['a', 'p', 'span'])
+        self.assertEqualSets(conv.tags, ['a', 'p'])
+
+    def test_empty_attrs(self):
+        self.attrs = {}
+        assert len(self.conv.tags) > 0
+    
+
+if __name__ == '__main__':
+    unittest.main()