Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
b9d9287f
Kaydet (Commit)
b9d9287f
authored
10 years ago
tarafından
Claude Paroz
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed urlize after smart_urlquote rewrite
Refs #22267.
üst
4b8a1d2c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
8 deletions
+48
-8
html.py
django/utils/html.py
+27
-4
filters.py
tests/template_tests/filters.py
+20
-4
test_html.py
tests/utils_tests/test_html.py
+1
-0
No files found.
django/utils/html.py
Dosyayı görüntüle @
b9d9287f
...
...
@@ -251,6 +251,7 @@ def smart_urlquote(url):
return
urlunsplit
((
scheme
,
netloc
,
path
,
query
,
fragment
))
def
urlize
(
text
,
trim_url_limit
=
None
,
nofollow
=
False
,
autoescape
=
False
):
"""
Converts any URLs in text into clickable links.
...
...
@@ -268,11 +269,31 @@ def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False):
If autoescape is True, the link text and URLs will be autoescaped.
"""
safe_input
=
isinstance
(
text
,
SafeData
)
def
trim_url
(
x
,
limit
=
trim_url_limit
):
if
limit
is
None
or
len
(
x
)
<=
limit
:
return
x
return
'
%
s...'
%
x
[:
max
(
0
,
limit
-
3
)]
safe_input
=
isinstance
(
text
,
SafeData
)
def
unescape
(
text
,
trail
):
"""
If input URL is HTML-escaped, unescape it so as we can safely feed it to
smart_urlquote. For example:
http://example.com?x=1&y=<2> => http://example.com?x=1&y=<2>
"""
if
not
safe_input
:
return
text
,
text
,
trail
unescaped
=
(
text
+
trail
)
.
replace
(
'&'
,
'&'
)
.
replace
(
'<'
,
'<'
)
.
replace
(
'>'
,
'>'
)
.
replace
(
'"'
,
'"'
)
.
replace
(
'''
,
"'"
)
# ';' in trail can be either trailing punctuation or end-of-entity marker
if
unescaped
.
endswith
(
';'
):
return
text
,
unescaped
[:
-
1
],
trail
else
:
text
+=
trail
return
text
,
unescaped
,
''
words
=
word_split_re
.
split
(
force_text
(
text
))
for
i
,
word
in
enumerate
(
words
):
if
'.'
in
word
or
'@'
in
word
or
':'
in
word
:
...
...
@@ -296,9 +317,11 @@ def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False):
url
=
None
nofollow_attr
=
' rel="nofollow"'
if
nofollow
else
''
if
simple_url_re
.
match
(
middle
):
url
=
smart_urlquote
(
middle
)
middle
,
middle_unescaped
,
trail
=
unescape
(
middle
,
trail
)
url
=
smart_urlquote
(
middle_unescaped
)
elif
simple_url_2_re
.
match
(
middle
):
url
=
smart_urlquote
(
'http://
%
s'
%
middle
)
middle
,
middle_unescaped
,
trail
=
unescape
(
middle
,
trail
)
url
=
smart_urlquote
(
'http://
%
s'
%
middle_unescaped
)
elif
':'
not
in
middle
and
simple_email_re
.
match
(
middle
):
local
,
domain
=
middle
.
rsplit
(
'@'
,
1
)
try
:
...
...
@@ -313,7 +336,7 @@ def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False):
trimmed
=
trim_url
(
middle
)
if
autoescape
and
not
safe_input
:
lead
,
trail
=
escape
(
lead
),
escape
(
trail
)
url
,
trimmed
=
escape
(
url
),
escape
(
trimmed
)
trimmed
=
escape
(
trimmed
)
middle
=
'<a href="
%
s"
%
s>
%
s</a>'
%
(
url
,
nofollow_attr
,
trimmed
)
words
[
i
]
=
mark_safe
(
'
%
s
%
s
%
s'
%
(
lead
,
middle
,
trail
))
else
:
...
...
This diff is collapsed.
Click to expand it.
tests/template_tests/filters.py
Dosyayı görüntüle @
b9d9287f
...
...
@@ -151,8 +151,16 @@ def get_filter_tests():
'filter-upper01'
:
(
'{
%
autoescape off
%
}{{ a|upper }} {{ b|upper }}{
%
endautoescape
%
}'
,
{
"a"
:
"a & b"
,
"b"
:
mark_safe
(
"a & b"
)},
"A & B A & B"
),
'filter-upper02'
:
(
'{{ a|upper }} {{ b|upper }}'
,
{
"a"
:
"a & b"
,
"b"
:
mark_safe
(
"a & b"
)},
"A & B A &AMP; B"
),
'filter-urlize01'
:
(
'{
%
autoescape off
%
}{{ a|urlize }} {{ b|urlize }}{
%
endautoescape
%
}'
,
{
"a"
:
"http://example.com/?x=&y="
,
"b"
:
mark_safe
(
"http://example.com?x=&y="
)},
'<a href="http://example.com/?x=&y=" rel="nofollow">http://example.com/?x=&y=</a> <a href="http://example.com?x=&y=" rel="nofollow">http://example.com?x=&y=</a>'
),
'filter-urlize02'
:
(
'{{ a|urlize }} {{ b|urlize }}'
,
{
"a"
:
"http://example.com/?x=&y="
,
"b"
:
mark_safe
(
"http://example.com?x=&y="
)},
'<a href="http://example.com/?x=&y=" rel="nofollow">http://example.com/?x=&y=</a> <a href="http://example.com?x=&y=" rel="nofollow">http://example.com?x=&y=</a>'
),
'filter-urlize01'
:
(
'{
%
autoescape off
%
}{{ a|urlize }} {{ b|urlize }}{
%
endautoescape
%
}'
,
{
"a"
:
"http://example.com/?x=&y="
,
"b"
:
mark_safe
(
"http://example.com?x=&y=<2>"
)},
'<a href="http://example.com/?x=&y=" rel="nofollow">http://example.com/?x=&y=</a> '
'<a href="http://example.com?x=&y=
%3
C2
%3
E" rel="nofollow">http://example.com?x=&y=<2></a>'
),
'filter-urlize02'
:
(
'{{ a|urlize }} {{ b|urlize }}'
,
{
"a"
:
"http://example.com/?x=&y="
,
"b"
:
mark_safe
(
"http://example.com?x=&y="
)},
'<a href="http://example.com/?x=&y=" rel="nofollow">http://example.com/?x=&y=</a> '
'<a href="http://example.com?x=&y=" rel="nofollow">http://example.com?x=&y=</a>'
),
'filter-urlize03'
:
(
'{
%
autoescape off
%
}{{ a|urlize }}{
%
endautoescape
%
}'
,
{
"a"
:
mark_safe
(
"a & b"
)},
'a & b'
),
'filter-urlize04'
:
(
'{{ a|urlize }}'
,
{
"a"
:
mark_safe
(
"a & b"
)},
'a & b'
),
...
...
@@ -165,8 +173,16 @@ def get_filter_tests():
'filter-urlize07'
:
(
'{{ a|urlize }}'
,
{
"a"
:
"Email me at me@example.com"
},
'Email me at <a href="mailto:me@example.com">me@example.com</a>'
),
'filter-urlize08'
:
(
'{{ a|urlize }}'
,
{
"a"
:
"Email me at <me@example.com>"
},
'Email me at <<a href="mailto:me@example.com">me@example.com</a>>'
),
'filter-urlizetrunc01'
:
(
'{
%
autoescape off
%
}{{ a|urlizetrunc:"8" }} {{ b|urlizetrunc:"8" }}{
%
endautoescape
%
}'
,
{
"a"
:
'"Unsafe" http://example.com/x=&y='
,
"b"
:
mark_safe
(
'"Safe" http://example.com?x=&y='
)},
'"Unsafe" <a href="http://example.com/x=&y=" rel="nofollow">http:...</a> "Safe" <a href="http://example.com?x=&y=" rel="nofollow">http:...</a>'
),
'filter-urlizetrunc02'
:
(
'{{ a|urlizetrunc:"8" }} {{ b|urlizetrunc:"8" }}'
,
{
"a"
:
'"Unsafe" http://example.com/x=&y='
,
"b"
:
mark_safe
(
'"Safe" http://example.com?x=&y='
)},
'"Unsafe" <a href="http://example.com/x=&y=" rel="nofollow">http:...</a> "Safe" <a href="http://example.com?x=&y=" rel="nofollow">http:...</a>'
),
'filter-urlizetrunc01'
:
(
'{
%
autoescape off
%
}{{ a|urlizetrunc:"8" }} {{ b|urlizetrunc:"8" }}{
%
endautoescape
%
}'
,
{
"a"
:
'"Unsafe" http://example.com/x=&y='
,
"b"
:
mark_safe
(
'"Safe" http://example.com?x=&y='
)},
'"Unsafe" <a href="http://example.com/x=&y=" rel="nofollow">http:...</a> '
'"Safe" <a href="http://example.com?x=&y=" rel="nofollow">http:...</a>'
),
'filter-urlizetrunc02'
:
(
'{{ a|urlizetrunc:"8" }} {{ b|urlizetrunc:"8" }}'
,
{
"a"
:
'"Unsafe" http://example.com/x=&y='
,
"b"
:
mark_safe
(
'"Safe" http://example.com?x=&y='
)},
'"Unsafe" <a href="http://example.com/x=&y=" rel="nofollow">http:...</a> '
'"Safe" <a href="http://example.com?x=&y=" rel="nofollow">http:...</a>'
),
'filter-wordcount01'
:
(
'{
%
autoescape off
%
}{{ a|wordcount }} {{ b|wordcount }}{
%
endautoescape
%
}'
,
{
"a"
:
"a & b"
,
"b"
:
mark_safe
(
"a & b"
)},
"3 3"
),
'filter-wordcount02'
:
(
'{{ a|wordcount }} {{ b|wordcount }}'
,
{
"a"
:
"a & b"
,
"b"
:
mark_safe
(
"a & b"
)},
"3 3"
),
...
...
This diff is collapsed.
Click to expand it.
tests/utils_tests/test_html.py
Dosyayı görüntüle @
b9d9287f
...
...
@@ -174,6 +174,7 @@ class TestUtilsHtml(TestCase):
self
.
assertEqual
(
quote
(
'http://example.com/path/öäü/'
),
'http://example.com/path/
%
C3
%
B6
%
C3
%
A4
%
C3
%
BC/'
)
self
.
assertEqual
(
quote
(
'http://example.com/
%
C3
%
B6/ä/'
),
'http://example.com/
%
C3
%
B6/
%
C3
%
A4/'
)
self
.
assertEqual
(
quote
(
'http://example.com/?x=1&y=2+3&z='
),
'http://example.com/?x=1&y=2+3&z='
)
self
.
assertEqual
(
quote
(
'http://example.com/?x=<>"
\'
'
),
'http://example.com/?x=
%3
C
%3
E
%22%27
'
)
self
.
assertEqual
(
quote
(
'http://example.com/?q=http://example.com/?x=1
%26
q=django'
),
'http://example.com/?q=http
%3
A
%2
F
%2
Fexample.com
%2
F
%3
Fx
%3
D1
%26
q
%3
Ddjango'
)
self
.
assertEqual
(
quote
(
'http://example.com/?q=http
%3
A
%2
F
%2
Fexample.com
%2
F
%3
Fx
%3
D1
%26
q
%3
Ddjango'
),
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment