def test_apply_checkbox_style(): from iommi import Form from iommi import Field class MyForm(Form): class Meta: style = 'bootstrap' foo = Field.boolean() form = MyForm() form.bind(request=None) assert get_style_for(form.fields.foo) == 'bootstrap' assert get_style_obj_for_object(style=get_style_for(form.fields.foo), obj=form.fields.foo)['attrs'] == { 'class': { 'form-group': True, 'form-check': True } } assert render_attrs( form.fields.foo.attrs) == ' class="form-check form-group"' assert render_attrs( form.fields.foo.input.attrs ) == ' class="form-check-input" id="id_foo" name="foo" type="checkbox"' assert render_attrs(form.fields.foo.label.attrs ) == ' class="form-check-label" for="id_foo"'
def fragment__render(fragment, context): rendered_children = fragment.render_text_or_children(context=context) if fragment.template: return render_template( fragment.request(), fragment.template, { **context, **fragment.evaluate_attribute_kwargs(), rendered_children: rendered_children }) is_void_element = fragment.tag in _void_elements if fragment.tag: if rendered_children: assert not is_void_element return format_html( '<{tag}{attrs}>{children}</{tag}>', tag=fragment.tag, attrs=render_attrs(fragment.attrs), children=rendered_children, ) else: return format_html( '<{tag}{attrs}>' if is_void_element else '<{tag}{attrs}></{tag}>', tag=fragment.tag, attrs=render_attrs(fragment.attrs), ) else: return format_html( '{}', rendered_children, )
def test_render_attrs_non_standard_types(): assert render_attrs({ 'apa': True, 'bepa': '', 'cepa': None, 'class': 'bar baz' }) == ' apa bepa="" class="bar baz"'
def test_render_class(): assert render_attrs({ 'apa': True, 'bepa': '', 'cepa': None, 'class': dict(foo=False, bar=True, baz=True) }) == ' apa bepa="" class="bar baz"'
def test_render_attrs_quote(): assert render_attrs( dict( a='"1"', b="'1'", style=dict( foo='url("foo")', bar="url('bar')", ), ) ) == ' a=""1"" b="\'1\'" style="bar: url(\'bar\'); foo: url("foo")"'
def test_render_attrs_raises_for_some_common_pitfall_types(): with pytest.raises(TypeError) as e: render_attrs(dict(foo=dict(a=1))) assert str( e.value ) == "Only the class and style attributes can be dicts, you sent {'a': 1}" with pytest.raises(TypeError) as e: render_attrs(dict(foo=[])) assert str(e.value) == "Attributes can't be of type list, you sent []" with pytest.raises(TypeError) as e: render_attrs(dict(foo=tuple())) assert str(e.value) == "Attributes can't be of type tuple, you sent ()" with pytest.raises(TypeError) as e: render_attrs(dict(foo=lambda foo: foo)) assert re.match("Attributes can't be callable, you sent <function .*>", str(e.value))
def test_render_attrs_empty_style(): assert render_attrs(Namespace( style__foo=None, style__bar=None, )) == ' '
def test_render_attrs_empty_class(): assert render_attrs(Namespace( class__foo=False, class__bar=False, )) == ' '
def test_render_attrs(): assert render_attrs(None) == '' assert render_attrs({ 'foo': 'bar', 'baz': 'quux' }) == ' baz="quux" foo="bar"'
def test_render_with_empty(): assert render_attrs(dict( a='1', style={}, z='2', )) == ' a="1" z="2"'
def test_render_style(): assert render_attrs(dict(style=dict( foo='1', bar='2', ))) == ' style="bar: 2; foo: 1"'