Skip to content

Commit

Permalink
Made changes to the javascript functionality:
Browse files Browse the repository at this point in the history
Changed to HTML5 syntax.
Added ability to mark javascript tag with async or defer.
  • Loading branch information
dobcey committed Mar 4, 2011
1 parent c2c9aaf commit 38153e9
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions django_static/templatetags/django_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ def do_staticallfiles(parser, token):
SCRIPTS_REGEX = re.compile('<script [^>]*src=["\']([^"\']+)["\'].*?</script>')
STYLES_REGEX = re.compile('<link.*?href=["\']([^"\']+)["\'].*?>', re.M|re.DOTALL)
IMG_REGEX = re.compile('<img.*?src=["\']((?!data:)[^"\']+)["\'].*?>', re.M|re.DOTALL)
ASYNC_DEFER_REGEX = re.compile('async|defer')

class StaticFilesNode(template.Node):
"""find all static files in the wrapped code and run staticfile (or
Expand Down Expand Up @@ -267,6 +268,7 @@ def render(self, context):
new_js_filenames = []
for match in SCRIPTS_REGEX.finditer(code):
whole_tag = match.group()
async_defer = ASYNC_DEFER_REGEX.search(whole_tag)
for filename in match.groups():

optimize_if_possible = self.optimize_if_possible
Expand Down Expand Up @@ -328,8 +330,11 @@ def image_replacer(match):

if new_js_filename:
# Now is the time to apply the name prefix if there is one
new_tag = '<script type="text/javascript" src="%s"></script>' % \
new_js_filename
if async_defer:
new_tag = ('<script %s src="%s"></script>' %
(async_defer.group(0), new_js_filename))
else:
new_tag = '<script src="%s"></script>' % new_js_filename
code = "%s%s" % (new_tag, code)

for media_type, new_css_filename in new_css_filenames_combined.items():
Expand Down

0 comments on commit 38153e9

Please sign in to comment.