#28377 closed Cleanup/optimization (fixed)
Retain order of form media assets during aggregation
Reported by: | Johannes Maron | Owned by: | |
---|---|---|---|
Component: | Forms | Version: | 1.11 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
The order of static asset can matter, be it JavaScript code, that depends on jQuery or CSS files, that overwrite each other.
Currently Django does not retain the order specified in the Media
property. When merging to Media objects, like widget and form media, unique entries only get amended which can change the order of assets.
Example:
>>> from django.forms import Media >>> m1 = Media(js=('jQuery.js', 'script.js', 'noConflict.js')) >>> m2 = Media(js=('jQuery.js', 'yet_another_script.js', 'noConflict.js')) >>> print(m1 + m2) <script type="text/javascript" src="/static/jQuery.js"></script> <script type="text/javascript" src="/static/script.js"></script> <script type="text/javascript" src="/static/noConflict.js"></script> <script type="text/javascript" src="/static/yet_another_script.js"></script>
Here the very important order of assets for m2 has changed.
Using proper merging, the order can be maintained. Furthermore Django should warn developers, if two media classes have the same assets in a reversed order.
This is how the order should be:
>>> from django.forms import Media >>> m1 = Media(js=('jQuery.js', 'script.js', 'noConflict.js')) >>> m2 = Media(js=('jQuery.js', 'yet_another_script.js', 'noConflict.js')) >>> print(m1 + m2) <script type="text/javascript" src="/static/jQuery.js"></script> <script type="text/javascript" src="/static/script.js"></script> <script type="text/javascript" src="/static/yet_another_script.js"></script> <script type="text/javascript" src="/static/noConflict.js"></script>
or
>>> from django.forms import Media >>> m1 = Media(js=('jQuery.js', 'script.js', 'noConflict.js')) >>> m2 = Media(js=('jQuery.js', 'yet_another_script.js', 'noConflict.js')) >>> print(m1 + m2) <script type="text/javascript" src="/static/jQuery.js"></script> <script type="text/javascript" src="/static/yet_another_script.js"></script> <script type="text/javascript" src="/static/script.js"></script> <script type="text/javascript" src="/static/noConflict.js"></script>
Both cases have no duplicates and the order of both individual form media assets is preserved.
Change History (5)
comment:1 by , 7 years ago
Triage Stage: | Unreviewed → Accepted |
---|---|
Type: | Bug → Cleanup/optimization |
PR