From 9727fde513a6f5c071ef8a24cd5a02af4ba8ca7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Sun, 5 May 2019 12:32:39 +0200 Subject: [PATCH] misc: use absolute URIs for text cell images in skeleton mode (#24681) --- combo/data/models.py | 12 ++++++++++++ combo/public/templates/combo/text-cell.html | 2 +- tests/test_public.py | 9 +++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/combo/data/models.py b/combo/data/models.py index 692a1c6a..369d9df9 100644 --- a/combo/data/models.py +++ b/combo/data/models.py @@ -742,6 +742,18 @@ class TextCell(CellBase): d[0]['order'] = -1 return d + def get_cell_extra_context(self, context): + extra_context = super(TextCell, self).get_cell_extra_context(context) + extra_context['text'] = self.text + render_skeleton = context.get('render_skeleton') + if render_skeleton: + request = context.get('request') + def sub_src(match): + url = request.build_absolute_uri(match.group(1)) + return 'src="%s"' % url + extra_context['text'] = re.sub(r'src="(.*?)"', sub_src, self.text) + return extra_context + @register_cell_class class FortuneCell(CellBase): diff --git a/combo/public/templates/combo/text-cell.html b/combo/public/templates/combo/text-cell.html index f7cc9865..12fef1d6 100644 --- a/combo/public/templates/combo/text-cell.html +++ b/combo/public/templates/combo/text-cell.html @@ -1,3 +1,3 @@ {% block cell-content %} -{{cell.text|safe}} +{{text|safe}} {% endblock %} diff --git a/tests/test_public.py b/tests/test_public.py index fc9da15d..d1f31f9c 100644 --- a/tests/test_public.py +++ b/tests/test_public.py @@ -275,6 +275,15 @@ def test_page_skeleton(app): resp = app.get('/__skeleton__/?source=%s' % quote('http://127.0.0.1:8999/')) assert 'http://testserver/plop' in resp.text + # check images in text cell use full URL + cell = TextCell(page=page, placeholder='footer', order=0) + cell.text = ' vs ' + cell.save() + resp = app.get('/__skeleton__/?source=%s' % quote('http://127.0.0.1:8999/')) + assert 'http://testserver/test/foobar.png' in resp.text + # check absolute URIs are not modified + assert 'src="http://www.example.com/test.png"' in resp.text + # add a bad redirection page (don't use it, do not crash) page = Page(title='BadRedirection', slug='badredir', template_name='standard', redirect_url='[foo_bar]')