misc: allow variables in URLs in href/src of text cells (#17360)
This commit is contained in:
parent
f80bac14e0
commit
c9b4ed5d4e
|
@ -744,8 +744,16 @@ class TextCell(CellBase):
|
|||
|
||||
def get_cell_extra_context(self, context):
|
||||
extra_context = super(TextCell, self).get_cell_extra_context(context)
|
||||
extra_context['text'] = self.text
|
||||
text = self.text
|
||||
render_skeleton = context.get('render_skeleton')
|
||||
|
||||
def sub_variadic_url(match):
|
||||
attribute = match.group(1)
|
||||
url = match.group(2)
|
||||
url = utils.get_templated_url(url, context=context)
|
||||
return '%s="%s"' % (attribute, url)
|
||||
text = re.sub(r'(href|src)="(.*?)"', sub_variadic_url, text)
|
||||
|
||||
if render_skeleton:
|
||||
request = context.get('request')
|
||||
def sub_src(match):
|
||||
|
@ -758,8 +766,9 @@ class TextCell(CellBase):
|
|||
else:
|
||||
url = request.build_absolute_uri(url)
|
||||
return 'href="%s"' % url
|
||||
extra_context['text'] = re.sub(r'src="(.*?)"', sub_src, self.text)
|
||||
extra_context['text'] = re.sub(r'href="(.*?)"', sub_href, extra_context['text'])
|
||||
text = re.sub(r'src="(.*?)"', sub_src, text)
|
||||
text = re.sub(r'href="(.*?)"', sub_href, text)
|
||||
extra_context['text'] = text
|
||||
return extra_context
|
||||
|
||||
|
||||
|
|
|
@ -72,6 +72,23 @@ def test_additional_label():
|
|||
assert len(cell.get_additional_label()) < 100
|
||||
assert '...' in cell.get_additional_label()
|
||||
|
||||
def test_text_cell_variadic_url():
|
||||
page = Page()
|
||||
page.save()
|
||||
|
||||
cell = TextCell(page=page, order=0)
|
||||
cell.text = '<a href="{{test_url}}">test</a> <a href="/plop">test2</a> <img src="{{test_url}}"'
|
||||
cell.save()
|
||||
with override_settings(TEMPLATE_VARS={'test_url': 'http://example.net'}):
|
||||
ctx = {}
|
||||
assert 'href="http://example.net"' in cell.render(ctx)
|
||||
assert 'href="/plop"' in cell.render(ctx)
|
||||
assert 'src="http://example.net"' in cell.render(ctx)
|
||||
|
||||
# consider unknown variables as empty
|
||||
assert 'href=""' in cell.render(ctx)
|
||||
assert 'href="/plop"' in cell.render(ctx)
|
||||
|
||||
def test_link_cell():
|
||||
page = Page(title='example page', slug='example-page')
|
||||
page.save()
|
||||
|
|
Loading…
Reference in New Issue