Exemplo n.º 1
0
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"'
Exemplo n.º 2
0
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,
        )
Exemplo n.º 3
0
def test_render_attrs_non_standard_types():
    assert render_attrs({
        'apa': True,
        'bepa': '',
        'cepa': None,
        'class': 'bar baz'
    }) == ' apa bepa="" class="bar baz"'
Exemplo n.º 4
0
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"'
Exemplo n.º 5
0
def test_render_attrs_quote():
    assert render_attrs(
        dict(
            a='"1"',
            b="'1'",
            style=dict(
                foo='url("foo")',
                bar="url('bar')",
            ),
        )
    ) == ' a="&quot;1&quot;" b="\'1\'" style="bar: url(\'bar\'); foo: url(&quot;foo&quot;)"'
Exemplo n.º 6
0
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))
Exemplo n.º 7
0
def test_render_attrs_empty_style():
    assert render_attrs(Namespace(
        style__foo=None,
        style__bar=None,
    )) == ' '
Exemplo n.º 8
0
def test_render_attrs_empty_class():
    assert render_attrs(Namespace(
        class__foo=False,
        class__bar=False,
    )) == ' '
Exemplo n.º 9
0
def test_render_attrs():
    assert render_attrs(None) == ''
    assert render_attrs({
        'foo': 'bar',
        'baz': 'quux'
    }) == ' baz="quux" foo="bar"'
Exemplo n.º 10
0
def test_render_with_empty():
    assert render_attrs(dict(
        a='1',
        style={},
        z='2',
    )) == ' a="1" z="2"'
Exemplo n.º 11
0
def test_render_style():
    assert render_attrs(dict(style=dict(
        foo='1',
        bar='2',
    ))) == ' style="bar: 2; foo: 1"'