Author: chrisz
Date: Sun Jan 27 09:16:50 2008
New Revision: 4060
URL: http://trac.turbogears.org/changeset/4060
Log:
Added support for the "charset" attribute to the JSLink widget. This will help
in fixing #1323. While I'm at it, also added support for the "defer" attribute
to JSSource and JSLink. This can improve performance by delaying execution of
scripts until after the body content is parsed and rendered.
Modified:
branches/1.0/turbogears/widgets/base.py
branches/1.0/turbogears/widgets/tests/test_widgets.py
branches/1.1/turbogears/widgets/base.py
branches/1.1/turbogears/widgets/tests/test_widgets.py
Modified: branches/1.0/turbogears/widgets/base.py
==============================================================================
--- branches/1.0/turbogears/widgets/base.py (original)
+++ branches/1.0/turbogears/widgets/base.py Sun Jan 27 09:16:50 2008
@@ -537,8 +537,14 @@
class JSLink(Link):
template = """
- <script type="text/javascript" src="$link"></script>
+ <script type="text/javascript" src="$link"
+ charset="$charset" defer="${defer and 'defer' or None}"/>
"""
+ params = ["charset", "defer"]
+ params_doc = {'charset': 'The character encoding of the linked script',
+ 'defer': 'If true, browser may defer execution of the script'}
+ charset = None
+ defer = False
location = js_location.head
@@ -575,7 +581,7 @@
"""
template = """
- <script xmlns:py="http://purl.org/kid/ns#" py:if="not suppress"
type="text/javascript" src="$link"></script>
+ <script xmlns:py="http://purl.org/kid/ns#" py:if="not suppress"
type="text/javascript" src="$link"/>
"""
def update_params(self, p):
@@ -702,16 +708,21 @@
class JSSource(Source):
template = """
- <script type="text/javascript">$src</script>
+ <script type="text/javascript"
+ defer="${defer and 'defer' or None}">$src</script>
"""
+ params = ["defer"]
+ params_doc = {'defer': 'If true, browser may defer execution of the
script'}
+ defer = False
+
location = js_location.head
- def __init__(self, src, location=None):
+ def __init__(self, src, location=None, **kw):
if location:
if location not in js_location:
raise ValueError, "JSSource location should be in %s" %
js_location
self.location = location
- super(JSSource, self).__init__(src)
+ super(JSSource, self).__init__(src, **kw)
retrieve_javascript = set_with_self
Modified: branches/1.0/turbogears/widgets/tests/test_widgets.py
==============================================================================
--- branches/1.0/turbogears/widgets/tests/test_widgets.py (original)
+++ branches/1.0/turbogears/widgets/tests/test_widgets.py Sun Jan 27
09:16:50 2008
@@ -63,7 +63,6 @@
assert 'value="2"' in output
value = "2"
value = textfield.validator.to_python(value)
- print value
assert value == 2
def test_unicode_input():
@@ -77,7 +76,7 @@
except ValueError, e:
pass
else:
- assert 0, "ValueError not raised: non-unicode input not detected"
+ assert False, "ValueError not raised: non-unicode input not detected"
#tf2 = widgets.TextField("name", validator=validators.String())
# simon: failed inputs are no longer being removed.
@@ -121,19 +120,52 @@
"Widgets can require JavaScript resources"
js = widgets.JSLink(mod=widgets.static, name="foo.js")
js2 = widgets.JSLink(mod=widgets.static, name="foo.js")
- assert js == js2
- js3 = widgets.CSSLink(mod=widgets.static, name="bar.js")
- assert js3 != js2
- js4 = widgets.JSSource(src="foo.js")
- assert js != js4
- rendered = js.render(format='xhtml').strip()
+ 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>'
+ ' type="text/javascript"></script>'
assert rendered == expected
- js = widgets.JSSource("alert('hello');")
- rendered = js.render(format='xhtml').strip()
- expected = """<script type="text/javascript">alert('hello');</script>"""
+ 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_url():
"It might be needed to insert an URL somewhere"
@@ -313,7 +345,6 @@
a = self.A()
assert a.a == None
output = a.render(format='xhtml')
- print output
assert 'a=' not in output
def test_overridal(self):
@@ -326,10 +357,8 @@
c = self.C(a="test")
assert c.a == "test"
output = c.render(format='xhtml')
- print output
assert 'a="test"' in output
output = c.render(a="another", format='xhtml')
- print output
assert 'a="another"' in output
def test_template_overridal():
@@ -467,7 +496,6 @@
passwd2="fado")
values, errors = catch_validation_errors(self.form, values)
- print values
assert values['age'] == 99
assert not errors
@@ -476,7 +504,6 @@
passwd2="fadO")
values, errors = catch_validation_errors(self.form, values)
- print errors
assert "passwd2" in errors.keys()
class TestSchemaValidationWithChildWidgetsValidators:
@@ -500,7 +527,6 @@
passwd2="fado")
values, errors = catch_validation_errors(self.form, values)
- print values
assert values['age'] == 99
assert not errors.keys()
@@ -508,7 +534,6 @@
values = dict(name=u'Jos\xc3\xa9', age="ninetynine", passwd="fado",
passwd2="fado")
values, errors = catch_validation_errors(self.form, values)
- print values, errors
assert "age" in errors.keys()
@@ -516,7 +541,6 @@
values = dict(name=u'Jos\xc3\xa9', age="ninetynine", passwd="fado",
passwd2="fadO")
values, errors = catch_validation_errors(self.form, values)
- print values, errors
assert "age" in errors.keys()
assert "passwd2" in errors.keys()
Modified: branches/1.1/turbogears/widgets/base.py
==============================================================================
--- branches/1.1/turbogears/widgets/base.py (original)
+++ branches/1.1/turbogears/widgets/base.py Sun Jan 27 09:16:50 2008
@@ -544,8 +544,14 @@
class JSLink(Link):
template = """
- <script type="text/javascript" src="$link"></script>
+ <script type="text/javascript" src="$link"
+ charset="$charset" defer="${defer and 'defer' or None}"/>
"""
+ params = ["charset", "defer"]
+ params_doc = {'charset': 'The character encoding of the linked script',
+ 'defer': 'If true, browser may defer execution of the script'}
+ charset = None
+ defer = False
location = js_location.head
@@ -712,16 +718,21 @@
class JSSource(Source):
template = """
- <script type="text/javascript">$src</script>
+ <script type="text/javascript"
+ defer="${defer and 'defer' or None}">$src</script>
"""
+ params = ["defer"]
+ params_doc = {'defer': 'If true, browser may defer execution of the
script'}
+ defer = False
+
location = js_location.head
- def __init__(self, src, location=None):
+ def __init__(self, src, location=None, **kw):
if location:
if location not in js_location:
raise ValueError, "JSSource location should be in %s" %
js_location
self.location = location
- super(JSSource, self).__init__(src)
+ super(JSSource, self).__init__(src, **kw)
retrieve_javascript = set_with_self
Modified: branches/1.1/turbogears/widgets/tests/test_widgets.py
==============================================================================
--- branches/1.1/turbogears/widgets/tests/test_widgets.py (original)
+++ branches/1.1/turbogears/widgets/tests/test_widgets.py Sun Jan 27
09:16:50 2008
@@ -63,7 +63,6 @@
assert 'value="2"' in output
value = "2"
value = textfield.validator.to_python(value)
- print value
assert value == 2
def test_unicode_input():
@@ -77,7 +76,7 @@
except ValueError, e:
pass
else:
- assert 0, "ValueError not raised: non-unicode input not detected"
+ assert False, "ValueError not raised: non-unicode input not detected"
#tf2 = widgets.TextField("name", validator=validators.String())
# simon: failed inputs are no longer being removed.
@@ -121,19 +120,52 @@
"Widgets can require JavaScript resources"
js = widgets.JSLink(mod=widgets.static, name="foo.js")
js2 = widgets.JSLink(mod=widgets.static, name="foo.js")
- assert js == js2
- js3 = widgets.CSSLink(mod=widgets.static, name="bar.js")
- assert js3 != js2
- js4 = widgets.JSSource(src="foo.js")
- assert js != js4
- rendered = js.render(format='xhtml').strip()
+ 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>'
+ ' type="text/javascript"></script>'
assert rendered == expected
- js = widgets.JSSource("alert('hello');")
- rendered = js.render(format='xhtml').strip()
- expected = """<script type="text/javascript">alert('hello');</script>"""
+ 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_url():
"It might be needed to insert an URL somewhere"
@@ -313,7 +345,6 @@
a = self.A()
assert a.a == None
output = a.render(format='xhtml')
- print output
assert 'a=' not in output
def test_overridal(self):
@@ -326,10 +357,8 @@
c = self.C(a="test")
assert c.a == "test"
output = c.render(format='xhtml')
- print output
assert 'a="test"' in output
output = c.render(a="another", format='xhtml')
- print output
assert 'a="another"' in output
def test_template_overridal():
@@ -467,7 +496,6 @@
passwd2="fado")
values, errors = catch_validation_errors(self.form, values)
- print values
assert values['age'] == 99
assert not errors
@@ -476,7 +504,6 @@
passwd2="fadO")
values, errors = catch_validation_errors(self.form, values)
- print errors
assert "passwd2" in errors.keys()
class TestSchemaValidationWithChildWidgetsValidators:
@@ -500,7 +527,6 @@
passwd2="fado")
values, errors = catch_validation_errors(self.form, values)
- print values
assert values['age'] == 99
assert not errors.keys()
@@ -508,7 +534,6 @@
values = dict(name=u'Jos\xc3\xa9', age="ninetynine", passwd="fado",
passwd2="fado")
values, errors = catch_validation_errors(self.form, values)
- print values, errors
assert "age" in errors.keys()
@@ -516,7 +541,6 @@
values = dict(name=u'Jos\xc3\xa9', age="ninetynine", passwd="fado",
passwd2="fadO")
values, errors = catch_validation_errors(self.form, values)
- print values, errors
assert "age" in errors.keys()
assert "passwd2" in errors.keys()