class w1(widgets.FormField): javascript = [ widgets.JSLink(__module__, "foo.js"), widgets.JSSource("alert('foo');"), widgets.JSSource("alert('foo again');", widgets.js_location.bodybottom) ] css = [widgets.CSSLink(__module__, "foo.css")] register = False
def test_rendering_without_engine(): """Helpful error when rendering widgets with no templating engine loaded""" from turbogears import view engines = view.engines view.engines = {} try: widgets.CSSLink("foo")() except Exception, msg: msg = str(msg)
def test_widget_js(): """Widgets can require JavaScript resources""" js = widgets.JSLink(mod=widgets.static, name="foo.js") js2 = widgets.JSLink(mod=widgets.static, name="foo.js") assert js2 == js2 js2 = widgets.JSLink(mod=widgets.static, name="bar.js") assert js2 != js js2 = widgets.CSSLink(mod=widgets.static, name="foo.js") assert js2 != js js2 = widgets.JSSource(src="foo.js") assert js2 != js rendered = js.render(format='xhtml') expected = '<script src="/tg_widgets/turbogears.widgets/foo.js"' \ ' type="text/javascript"></script>' assert rendered == expected js3 = widgets.JSLink(mod=widgets.static, name="foo.js", defer=False, charset=None) assert js3 == js rendered = js3.render(format='xhtml') assert rendered == expected js3 = widgets.JSLink(mod=widgets.static, name="foo.js", defer=True) assert js3 != js rendered = js3.render(format='html').lower() assert ' defer' in rendered \ and expected == rendered.replace(' defer', '', 1) rendered = js3.render(format='xhtml') assert ' defer="defer"' in rendered \ and expected == rendered.replace(' defer="defer"', '', 1) js3 = widgets.JSLink(mod=widgets.static, name="foo.js", charset='Latin-1') assert js3 != js rendered = js3.render(format='xhtml') assert ' charset="Latin-1"' in rendered \ and expected == rendered.replace(' charset="Latin-1"', '', 1) js3 = widgets.JSSource("alert('hello');") assert js3 != js and js3 != js2 rendered = js3.render(format='xhtml') expected = '<script type="text/javascript">alert(\'hello\');</script>' assert rendered == expected js4 = widgets.JSSource("alert('hello');", defer=False) assert js4 == js3 rendered = js4.render(format='xhtml') assert rendered == expected js4 = widgets.JSSource("alert('hello');", defer=True) assert js4 != js3 rendered = js4.render(format='html').lower() assert ' defer' in rendered \ and expected == rendered.replace(' defer', '', 1) rendered = js4.render(format='xhtml') assert ' defer="defer"' in rendered \ and expected == rendered.replace(' defer="defer"', '', 1)
def test_widget_css(): """Widgets can require CSS resources""" css = widgets.CSSLink(mod=widgets.static, name="foo.css") css2 = widgets.CSSLink(mod=widgets.static, name="foo.css") assert css == css2 cssset = set() cssset.add(css) cssset.add(css2) assert len(cssset) == 1 css3 = widgets.CSSLink(mod=widgets.static, name="bar.css") assert css3 != css2 css4 = widgets.CSSSource(src="foo.css") assert css != css4 rendered = css.render(format='xhtml') assert 'link' in rendered assert 'href="/tg_widgets/turbogears.widgets/foo.css"' in rendered assert 'type="text/css"' in rendered assert 'rel="stylesheet"' in rendered assert 'media="all"' in rendered rendered = css.render(media="printer", format='xhtml') assert 'media="printer"' in rendered css = widgets.CSSSource("h1 { color: black }") rendered = css.render(format='xhtml') assert 'h1 { color: black }' in rendered
class MyTableWithJS(widgets.TableForm): javascript = [ widgets.JSLink(mod=widgets.static, name="foo.js"), widgets.JSSource("alert('hello');") ] css = [widgets.CSSLink(mod=widgets.static, name="foo.css")]
class w2(widgets.FormField): javascript = [widgets.JSLink(__module__, "foo.js")] css = [widgets.CSSLink(__module__, "foo.css")] register = False