[FIX] website: adapt background image URLs on language switch

Steps to reproduce (on Chrome with browser cache enabled):

- Go to website > Create a new blog post.
- Switch to edit mode > Add an image to the blog cover from unsplash.
- After save, switch to a secondary language > Unsplash images disappear
and you need to refresh the page to get them to appear.

This behaviour is a "very specific" Chrome related issue: when switching
language, Chrome cannot load inline background images correctly right
after the redirect... and unlike other image URLs, background images
don't have the language code prefix (The `url_for()` will add the
language code before the image "src" E.g. `<img src="/unsplash/...."/>`
=> `<img src="/fr_BE/unsplash/...."/>` when switching to `fr_BE`...).

The goal of this commit is to prevent this behaviour by adapting the
"background-image" URLs in the same way as image "src".

opw-3412961

closes odoo/odoo#131891

X-original-commit: 5f20f50f9a9fd4bc5145d380609cdca80a8d7511
Signed-off-by: Romain Derie (rde) <rde@odoo.com>
This commit is contained in:
xO-Tx
2023-08-16 14:21:46 +02:00
parent 805735fd9f
commit e70cc2ea86
+11 -5
View File
@@ -112,8 +112,11 @@ class IrQWeb(models.AbstractModel):
return atts
name = self.URL_ATTRS.get(tagName)
if request and name and name in atts:
atts[name] = url_for(atts[name])
if request:
if name and name in atts:
atts[name] = url_for(atts[name])
# Adapt background-image URL in the same way as image src.
atts = self._adapt_style_background_image(atts, url_for)
if not website.cdn_activated:
return atts
@@ -125,8 +128,11 @@ class IrQWeb(models.AbstractModel):
atts[name] = website.get_cdn_url(atts[name])
if data_name in atts:
atts[data_name] = website.get_cdn_url(atts[data_name])
if isinstance(atts.get('style'), str) and 'background-image' in atts['style']:
atts = OrderedDict(atts)
atts['style'] = re_background_image.sub(lambda m: '%s%s' % (m.group(1), website.get_cdn_url(m.group(2))), atts['style'])
atts = self._adapt_style_background_image(atts, website.get_cdn_url)
return atts
def _adapt_style_background_image(self, atts, url_adapter):
if isinstance(atts.get('style'), str) and 'background-image' in atts['style']:
atts['style'] = re_background_image.sub(lambda m: '%s%s' % (m[1], url_adapter(m[2])), atts['style'])
return atts