Ticket #13978: inlinemedia.diff
File inlinemedia.diff, 3.5 KB (added by , 14 years ago) |
---|
-
django/forms/widgets.py
28 28 MEDIA_TYPES = ('css','js') 29 29 30 30 class Media(StrAndUnicode): 31 class Inline(object): 32 def __init__(self, content): 33 self.content = content 34 31 35 def __init__(self, media=None, **kwargs): 32 36 if media: 33 37 media_attrs = media.__dict__ … … 51 55 return mark_safe(u'\n'.join(chain(*[getattr(self, 'render_' + name)() for name in MEDIA_TYPES]))) 52 56 53 57 def render_js(self): 54 return [u'<script type="text/javascript" src="%s"></script>' % self.absolute_path(path) for path in self._js] 58 html = [] 59 for script in self._js: 60 if isinstance(script, self.Inline): 61 html.append(u'<script type="text/javascript">%s</script>' % script.content) 62 else: 63 html.append(u'<script type="text/javascript" src="%s"></script>' % self.absolute_path(script)) 64 return html 55 65 56 66 def render_css(self): 57 67 # To keep rendering order consistent, we can't just iterate over items(). 58 68 # We need to sort the keys, and iterate over the sorted list. 59 69 media = self._css.keys() 60 70 media.sort() 61 return chain(*[ 62 [u'<link href="%s" type="text/css" media="%s" rel="stylesheet" />' % (self.absolute_path(path), medium) 63 for path in self._css[medium]] 64 for medium in media]) 71 html = [] 72 for medium in media: 73 for style in self._css[medium]: 74 if isinstance(style, self.Inline): 75 html.append(u'<style type="text/css" media="%s">%s</style>' % (medium, style.content)) 76 else: 77 html.append(u'<link href="%s" type="text/css" media="%s" rel="stylesheet" />' % (self.absolute_path(style), medium)) 78 return html 65 79 66 80 def absolute_path(self, path): 67 81 if path.startswith(u'http://') or path.startswith(u'https://') or path.startswith(u'/'): … … 74 88 return Media(**{str(name): getattr(self, '_' + name)}) 75 89 raise KeyError('Unknown media type "%s"' % name) 76 90 77 def add_js(self, data): 78 if data: 79 for path in data: 80 if path not in self._js: 81 self._js.append(path) 91 def add_js(self, scripts): 92 if scripts: 93 for script in scripts: 94 if isinstance(script, self.Inline): 95 self._js.append(script) 96 else: 97 if script not in self._js: 98 self._js.append(script) 82 99 83 def add_css(self, data): 84 if data: 85 for medium, paths in data.items(): 86 for path in paths: 87 if not self._css.get(medium) or path not in self._css[medium]: 88 self._css.setdefault(medium, []).append(path) 100 def add_css(self, media): 101 if media: 102 for medium, styles in media.items(): 103 for style in styles: 104 if isinstance(style, self.Inline): 105 self._css.setdefault(medium, []).append(style) 106 else: 107 if not self._css.get(medium) or style not in self._css[medium]: 108 self._css.setdefault(medium, []).append(style) 89 109 90 110 def __add__(self, other): 91 111 combined = Media()