Index: django/forms/widgets.py
===================================================================
--- django/forms/widgets.py (revision 13444)
+++ django/forms/widgets.py (working copy)
@@ -28,6 +28,10 @@
MEDIA_TYPES = ('css','js')
class Media(StrAndUnicode):
+ class Inline(object):
+ def __init__(self, content):
+ self.content = content
+
def __init__(self, media=None, **kwargs):
if media:
media_attrs = media.__dict__
@@ -51,17 +55,27 @@
return mark_safe(u'\n'.join(chain(*[getattr(self, 'render_' + name)() for name in MEDIA_TYPES])))
def render_js(self):
- return [u'' % self.absolute_path(path) for path in self._js]
+ html = []
+ for script in self._js:
+ if isinstance(script, self.Inline):
+ html.append(u'' % script.content)
+ else:
+ html.append(u'' % self.absolute_path(script))
+ return html
def render_css(self):
# To keep rendering order consistent, we can't just iterate over items().
# We need to sort the keys, and iterate over the sorted list.
media = self._css.keys()
media.sort()
- return chain(*[
- [u'' % (self.absolute_path(path), medium)
- for path in self._css[medium]]
- for medium in media])
+ html = []
+ for medium in media:
+ for style in self._css[medium]:
+ if isinstance(style, self.Inline):
+ html.append(u'' % (medium, style.content))
+ else:
+ html.append(u'' % (self.absolute_path(style), medium))
+ return html
def absolute_path(self, path):
if path.startswith(u'http://') or path.startswith(u'https://') or path.startswith(u'/'):
@@ -74,18 +88,24 @@
return Media(**{str(name): getattr(self, '_' + name)})
raise KeyError('Unknown media type "%s"' % name)
- def add_js(self, data):
- if data:
- for path in data:
- if path not in self._js:
- self._js.append(path)
+ def add_js(self, scripts):
+ if scripts:
+ for script in scripts:
+ if isinstance(script, self.Inline):
+ self._js.append(script)
+ else:
+ if script not in self._js:
+ self._js.append(script)
- def add_css(self, data):
- if data:
- for medium, paths in data.items():
- for path in paths:
- if not self._css.get(medium) or path not in self._css[medium]:
- self._css.setdefault(medium, []).append(path)
+ def add_css(self, media):
+ if media:
+ for medium, styles in media.items():
+ for style in styles:
+ if isinstance(style, self.Inline):
+ self._css.setdefault(medium, []).append(style)
+ else:
+ if not self._css.get(medium) or style not in self._css[medium]:
+ self._css.setdefault(medium, []).append(style)
def __add__(self, other):
combined = Media()