-
Notifications
You must be signed in to change notification settings - Fork 550
Fix broken images (bug 990054) #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,8 @@ | |
| from jingo import register, env | ||
| # Needed to make sure our own |f filter overrides jingo's one. | ||
| from jingo import helpers # noqa | ||
| from jingo_minify.helpers import (_build_html, _get_compiled_css_url, get_path, | ||
| is_external) | ||
| from tower import ugettext as _, strip_whitespace | ||
|
|
||
| import amo | ||
|
|
@@ -600,3 +602,54 @@ def f(string, *args, **kwargs): | |
| if not isinstance(string, six.text_type): | ||
| string = six.text_type(string) | ||
| return string.format(*args, **kwargs) | ||
|
|
||
|
|
||
| def _relative_to_absolute(url): | ||
| """ | ||
| Prepends relative URLs with MEDIA_URL to turn those inline-able. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a comment explaining that this function is used as the "replace" parameter of re.sub |
||
| This method is intended to be used as a ``replace`` parameter of | ||
| ``re.sub``. | ||
| """ | ||
| url = url.group(1).strip('"\'') | ||
| if not url.startswith(('data:', 'http:', 'https:', '//')): | ||
| url = url.replace('../../', settings.MEDIA_URL) | ||
| return 'url(%s)' % url | ||
|
|
||
|
|
||
| @register.function | ||
| def inline_css(bundle, media=False, debug=None): | ||
| """ | ||
| If we are in debug mode, just output a single style tag for each css file. | ||
| If we are not in debug mode, return a style that contains bundle-min.css. | ||
| Forces a regular css() call for external URLs (no inline allowed). | ||
|
|
||
| Extracted from jingo-minify and re-registered, see: | ||
| https://github.com/jsocol/jingo-minify/pull/41 | ||
| Added: turns relative links to absolute ones using MEDIA_URL. | ||
| """ | ||
| if debug is None: | ||
| debug = getattr(settings, 'TEMPLATE_DEBUG', False) | ||
|
|
||
| if debug: | ||
| items = [_get_compiled_css_url(i) | ||
| for i in settings.MINIFY_BUNDLES['css'][bundle]] | ||
| else: | ||
| items = ['css/%s-min.css' % bundle] | ||
|
|
||
| if not media: | ||
| media = getattr(settings, 'CSS_MEDIA_DEFAULT', 'screen,projection,tv') | ||
|
|
||
| contents = [] | ||
| for css in items: | ||
| if is_external(css): | ||
| return _build_html([css], '<link rel="stylesheet" media="%s" ' | ||
| 'href="%%s" />' % media) | ||
| with open(get_path(css), 'r') as f: | ||
| css_content = f.read() | ||
| css_parsed = re.sub(r'url\(([^)]*?)\)', | ||
| _relative_to_absolute, | ||
| css_content) | ||
| contents.append(css_parsed) | ||
|
|
||
| return _build_html(contents, '<style type="text/css" media="%s">%%s' | ||
| '</style>' % media) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this still needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, yes.