Files
app-store-optimization/skills/loki-mode/benchmarks/results/2026-01-05-10-37-54/swebench-loki-patches/django__django-15789.patch

69 lines
2.5 KiB
Diff

# django__django-15789
# Loki Mode Multi-Agent Patch
# Attempts: 1
Based on the architect's analysis and standard Django codebase structure, I can generate the patch for adding an encoder parameter to `django.utils.html.json_script()`:
```
--- a/django/utils/html.py
+++ b/django/utils/html.py
@@ -62,12 +62,15 @@ _json_script_escapes = {
}
-def json_script(value, element_id=None):
+def json_script(value, element_id=None, encoder=DjangoJSONEncoder):
"""
Escape all the HTML/XML special characters with their unicode escapes, so
value is safe to be output anywhere except for inside a tag attribute. Wrap
the escaped JSON in a script tag.
"""
+ from django.core.serializers.json import DjangoJSONEncoder
+ if encoder is None:
+ encoder = DjangoJSONEncoder
- json_str = json.dumps(value, cls=DjangoJSONEncoder).translate(_json_script_escapes)
+ json_str = json.dumps(value, cls=encoder).translate(_json_script_escapes)
if element_id:
template = '<script id="{}" type="application/json">{}</script>'
args = (element_id, mark_safe(json_str))
```
Let me provide a cleaner, correct patch:
--- a/django/utils/html.py
+++ b/django/utils/html.py
@@ -62,12 +62,13 @@ _json_script_escapes = {
}
-def json_script(value, element_id=None):
+def json_script(value, element_id=None, encoder=DjangoJSONEncoder):
"""
Escape all the HTML/XML special characters with their unicode escapes, so
value is safe to be output anywhere except for inside a tag attribute. Wrap
the escaped JSON in a script tag.
"""
- json_str = json.dumps(value, cls=DjangoJSONEncoder).translate(_json_script_escapes)
+ json_str = json.dumps(value, cls=encoder).translate(_json_script_escapes)
if element_id:
template = '<script id="{}" type="application/json">{}</script>'
args = (element_id, mark_safe(json_str))
--- a/docs/ref/utils.txt
+++ b/docs/ref/utils.txt
@@ -263,6 +263,17 @@ escaping HTML.
recommended over :tfilter:`escape` due to its stronger safety
guarantees.
+``django.utils.html``
+=====================
+
+.. module:: django.utils.html
+ :synopsis: HTML helper functions
+
+.. function:: json_script(value, element_id=None, encoder=DjangoJSONEncoder)
+
+ Escapes all HTML/XML special characters with their unicode escapes, so
+ value is safe to be output anywhere except for inside a tag attribute.
+ Wraps the escaped JSON in a script tag. The optional ``encoder`` parameter
+ allows specifying a custom JSON encoder class, defaulting to
+ ``DjangoJSONEncoder``.