File tree 5 files changed +44
-6
lines changed
5 files changed +44
-6
lines changed Original file line number Diff line number Diff line change @@ -64,7 +64,7 @@ def render_block_image(
64
64
height : Optional [str ] = None ,
65
65
** attrs : Any ,
66
66
) -> str :
67
- img = '<img src="' + src + '"'
67
+ img = '<img src="' + escape_text ( src ) + '"'
68
68
style = ''
69
69
if alt :
70
70
img += ' alt="' + escape_text (alt ) + '"'
@@ -90,7 +90,7 @@ def render_block_image(
90
90
91
91
target = attrs .get ('target' )
92
92
if target :
93
- href = escape_text ( self .safe_url (target ) )
93
+ href = self .safe_url (target )
94
94
outer = '<a class="' + _cls + '" href="' + href + '">'
95
95
return outer + img + '</a>\n '
96
96
else :
Original file line number Diff line number Diff line change @@ -53,17 +53,17 @@ def safe_url(self, url: str) -> str:
53
53
links, images, and etc.
54
54
"""
55
55
if self ._allow_harmful_protocols is True :
56
- return url
56
+ return escape_text ( url )
57
57
58
58
_url = url .lower ()
59
59
if self ._allow_harmful_protocols and \
60
60
_url .startswith (tuple (self ._allow_harmful_protocols )):
61
- return url
61
+ return escape_text ( url )
62
62
63
63
if _url .startswith (self .HARMFUL_PROTOCOLS ) and \
64
64
not _url .startswith (self .GOOD_DATA_PROTOCOLS ):
65
65
return '#harmful-link'
66
- return url
66
+ return escape_text ( url )
67
67
68
68
def text (self , text : str ) -> str :
69
69
if self ._escape :
Original file line number Diff line number Diff line change @@ -36,7 +36,7 @@ def escape_url(link: str) -> str:
36
36
'!$&()*+,;=' # sub-delims - "'" (rfc3986)
37
37
'%' # leave already-encoded octets alone
38
38
)
39
- return escape ( quote (unescape (link ), safe = safe ) )
39
+ return quote (unescape (link ), safe = safe )
40
40
41
41
42
42
def safe_entity (s : str ) -> str :
Original file line number Diff line number Diff line change 81
81
.
82
82
<a class="block-image align-left" href="https://lepture.com"><img src="picture.png" alt="description" width="100" height="50" /></a>
83
83
````````````````````````````````
84
+
85
+ ## ampersand in source
86
+
87
+ ```````````````````````````````` example
88
+ ~~~{image} https://example.com/picture.png?foo=qux&test=me
89
+ ~~~
90
+ .
91
+ <div class="block-image"><img src="https://example.com/picture.png?foo=qux&test=me" /></div>
92
+ ````````````````````````````````
93
+
94
+ ## ampersand in target
95
+
96
+ ```````````````````````````````` example
97
+ ~~~{image} picture.png
98
+ :target: https://example.com/rickroll?a=1&b=2
99
+ ~~~
100
+ .
101
+ <a class="block-image" href="https://example.com/rickroll?a=1&b=2"><img src="picture.png" /></a>
102
+ ````````````````````````````````
Original file line number Diff line number Diff line change @@ -97,6 +97,25 @@ def test_ast_output(self):
97
97
]
98
98
self .assertEqual (result , expected )
99
99
100
+ def test_ast_url (self ):
101
+ md = mistune .create_markdown (escape = False , renderer = None )
102
+ label = 'hi &<>"'
103
+ url = 'https://example.com/foo?a=1&b=2'
104
+ text = '[{}]({})' .format (label , url )
105
+ result = md (text )
106
+ expected = [
107
+ {
108
+ 'type' : 'paragraph' ,
109
+ 'children' : [
110
+ {
111
+ 'type' : 'link' ,
112
+ 'children' : [{'type' : 'text' , 'raw' : label }],
113
+ 'attrs' : {'url' : url },
114
+ },
115
+ ],
116
+ },
117
+ ]
118
+ self .assertEqual (result , expected )
100
119
101
120
def test_emsp (self ):
102
121
md = mistune .create_markdown (escape = False , hard_wrap = True )
You can’t perform that action at this time.
0 commit comments