0
0
mirror of https://github.com/django/django.git synced 2024-11-24 20:07:01 +01:00

Address review comments, thanks!

Co-authored-by: Matthias Kestenholz <matthiask@fastmail.com>
This commit is contained in:
Johannes Maron 2024-11-08 12:47:33 +01:00
parent b907a8a66a
commit 19b35b2a22
No known key found for this signature in database
2 changed files with 5 additions and 20 deletions

View File

@ -9,13 +9,13 @@ from collections import defaultdict
from graphlib import CycleError, TopologicalSorter
from itertools import chain
from django.forms.utils import to_current_timezone
from django.forms.utils import flatatt, to_current_timezone
from django.templatetags.static import static
from django.utils import formats
from django.utils.choices import normalize_choices
from django.utils.dates import MONTHS
from django.utils.formats import get_format
from django.utils.html import conditional_escape, format_html, html_safe
from django.utils.html import format_html, html_safe
from django.utils.regex_helper import _lazy_re_compile
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
@ -83,7 +83,7 @@ class MediaAsset:
return format_html(
self.element_template,
path=self.path,
attributes=mark_safe(" ".join(self.escaped_attributes)),
attributes=flatatt(self.attributes),
)
def __repr__(self):
@ -99,18 +99,9 @@ class MediaAsset:
return self._path
return static(self._path)
@property
def escaped_attributes(self):
"""Render attributes as a key=value string or key-only if the value is True."""
for key, value in self.attributes.items():
if value is True:
yield conditional_escape(key)
elif value:
yield format_html('{}="{}"', key, value)
class CSS(MediaAsset):
element_template = '<link href="{path}" {attributes}>'
element_template = '<link href="{path}"{attributes}>'
def __init__(self, path, **attributes):
super().__init__(path, **attributes)
@ -118,7 +109,7 @@ class CSS(MediaAsset):
class JS(MediaAsset):
element_template = '<script src="{path}" {attributes}>'
element_template = '<script src="{path}"{attributes}></script>'
@html_safe

View File

@ -57,12 +57,6 @@ class MediaAssetTestCase(SimpleTestCase):
asset = MediaAsset("http://media.other.com/path/to/css")
self.assertEqual(asset.path, "http://media.other.com/path/to/css")
def test_escaped_attributes(self):
asset = MediaAsset(
"path/to/css", media="all", disabled=True, autocomplete=False
)
self.assertEqual(list(asset.escaped_attributes), ['media="all"', "disabled"])
@override_settings(
STATIC_URL="http://media.example.com/static/",