mirror of
https://github.com/django/django.git
synced 2025-06-05 11:39:13 +00:00
Removed utils.text.javascript_quote() per deprecation timeline; refs #21725.
This commit is contained in:
parent
00a11994a5
commit
df3f3bbe29
@ -4,9 +4,7 @@ import re
|
|||||||
import unicodedata
|
import unicodedata
|
||||||
from gzip import GzipFile
|
from gzip import GzipFile
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
import warnings
|
|
||||||
|
|
||||||
from django.utils.deprecation import RemovedInDjango19Warning
|
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
from django.utils.functional import allow_lazy, SimpleLazyObject
|
from django.utils.functional import allow_lazy, SimpleLazyObject
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
@ -328,33 +326,6 @@ def compress_sequence(sequence):
|
|||||||
zfile.close()
|
zfile.close()
|
||||||
yield buf.read()
|
yield buf.read()
|
||||||
|
|
||||||
ustring_re = re.compile("([\u0080-\uffff])")
|
|
||||||
|
|
||||||
|
|
||||||
def javascript_quote(s, quote_double_quotes=False):
|
|
||||||
msg = (
|
|
||||||
"django.utils.text.javascript_quote() is deprecated. "
|
|
||||||
"Use django.utils.html.escapejs() instead."
|
|
||||||
)
|
|
||||||
warnings.warn(msg, RemovedInDjango19Warning, stacklevel=2)
|
|
||||||
|
|
||||||
def fix(match):
|
|
||||||
return "\\u%04x" % ord(match.group(1))
|
|
||||||
|
|
||||||
if type(s) == bytes:
|
|
||||||
s = s.decode('utf-8')
|
|
||||||
elif type(s) != six.text_type:
|
|
||||||
raise TypeError(s)
|
|
||||||
s = s.replace('\\', '\\\\')
|
|
||||||
s = s.replace('\r', '\\r')
|
|
||||||
s = s.replace('\n', '\\n')
|
|
||||||
s = s.replace('\t', '\\t')
|
|
||||||
s = s.replace("'", "\\'")
|
|
||||||
s = s.replace('</', '<\\/')
|
|
||||||
if quote_double_quotes:
|
|
||||||
s = s.replace('"', '"')
|
|
||||||
return ustring_re.sub(fix, s)
|
|
||||||
javascript_quote = allow_lazy(javascript_quote, six.text_type)
|
|
||||||
|
|
||||||
# Expression to match some_token and some_token="with spaces" (and similarly
|
# Expression to match some_token and some_token="with spaces" (and similarly
|
||||||
# for single-quoted strings).
|
# for single-quoted strings).
|
||||||
|
@ -1,13 +1,8 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from unittest import skipUnless
|
from django.test import SimpleTestCase
|
||||||
import warnings
|
|
||||||
|
|
||||||
from django.test import SimpleTestCase, ignore_warnings
|
|
||||||
from django.test.utils import reset_warning_registry
|
|
||||||
from django.utils import six, text
|
from django.utils import six, text
|
||||||
from django.utils.deprecation import RemovedInDjango19Warning
|
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
from django.utils.functional import lazy
|
from django.utils.functional import lazy
|
||||||
from django.utils.translation import override
|
from django.utils.translation import override
|
||||||
@ -197,30 +192,3 @@ class TestUtilsText(SimpleTestCase):
|
|||||||
def test_get_valid_filename(self):
|
def test_get_valid_filename(self):
|
||||||
filename = "^&'@{}[],$=!-#()%+~_123.txt"
|
filename = "^&'@{}[],$=!-#()%+~_123.txt"
|
||||||
self.assertEqual(text.get_valid_filename(filename), "-_123.txt")
|
self.assertEqual(text.get_valid_filename(filename), "-_123.txt")
|
||||||
|
|
||||||
@ignore_warnings(category=RemovedInDjango19Warning)
|
|
||||||
def test_javascript_quote(self):
|
|
||||||
input = "<script>alert('Hello \\xff.\n Welcome\there\r');</script>"
|
|
||||||
output = r"<script>alert(\'Hello \\xff.\n Welcome\there\r\');<\/script>"
|
|
||||||
self.assertEqual(text.javascript_quote(input), output)
|
|
||||||
|
|
||||||
# Exercising quote_double_quotes keyword argument
|
|
||||||
input = '"Text"'
|
|
||||||
self.assertEqual(text.javascript_quote(input), '"Text"')
|
|
||||||
self.assertEqual(text.javascript_quote(input, quote_double_quotes=True),
|
|
||||||
'"Text"')
|
|
||||||
|
|
||||||
@ignore_warnings(category=RemovedInDjango19Warning)
|
|
||||||
@skipUnless(IS_WIDE_BUILD, 'Not running in a wide build of Python')
|
|
||||||
def test_javascript_quote_unicode(self):
|
|
||||||
input = "<script>alert('Hello \\xff.\n Wel𝕃come\there\r');</script>"
|
|
||||||
output = r"<script>alert(\'Hello \\xff.\n Wel𝕃come\there\r\');<\/script>"
|
|
||||||
self.assertEqual(text.javascript_quote(input), output)
|
|
||||||
|
|
||||||
def test_deprecation(self):
|
|
||||||
reset_warning_registry()
|
|
||||||
with warnings.catch_warnings(record=True) as w:
|
|
||||||
warnings.simplefilter("always")
|
|
||||||
text.javascript_quote('thingy')
|
|
||||||
self.assertEqual(len(w), 1)
|
|
||||||
self.assertIn('escapejs()', repr(w[0].message))
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user