Browse Source

Merge PR #651 into 11.0

Signed-off-by simahawk
pull/658/head
OCA-git-bot 4 months ago
parent
commit
36dc3cc215
3 changed files with 42 additions and 4 deletions
  1. +1
    -1
      website_lazy_load_image/__manifest__.py
  2. +3
    -3
      website_lazy_load_image/models/ir_ui_view.py
  3. +38
    -0
      website_lazy_load_image/tests/test_lazy_load_image.py

+ 1
- 1
website_lazy_load_image/__manifest__.py View File

@@ -8,7 +8,7 @@
'license': 'AGPL-3',
'website': 'https://github.com/oca/website',
'category': 'Website',
'version': '11.0.1.0.0',
'version': '11.0.1.1.0',
'depends': [
'website'
],

+ 3
- 3
website_lazy_load_image/models/ir_ui_view.py View File

@@ -2,7 +2,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, models
from lxml import etree
import lxml


class IrUiView(models.Model):
@@ -22,7 +22,7 @@ class IrUiView(models.Model):
website_id = self.env.context.get('website_id')
if website_id and not \
self.env['website'].browse(website_id).is_publisher():
html = etree.HTML(res)
html = lxml.html.fromstring(res.decode('UTF-8'))
imgs = html.xpath(
'//main//img[@src][not(hasclass("lazyload-disable"))]'
) + html.xpath(
@@ -32,5 +32,5 @@ class IrUiView(models.Model):
src = img.attrib['src']
img.attrib['src'] = self.LAZYLOAD_DEFAULT_SRC
img.attrib['data-src'] = src
res = etree.tostring(html, method='html')
res = lxml.etree.tostring(html, method='html', encoding='UTF-8')
return res

+ 38
- 0
website_lazy_load_image/tests/test_lazy_load_image.py View File

@@ -27,6 +27,23 @@ class LazyLoadTest(SavepointCase):
'arch_base': arch_2,
'mode': 'primary'
})
arch_3 = '<t t-name="test"><span>content not wrapped</span></t>'
cls.view_3 = cls.env['ir.ui.view'].create({
'name': 'Test 3',
'key': 'website_lazy_load_image.test_3',
'type': 'qweb',
'arch_base': arch_3,
'mode': 'primary'
})
arch_4 = ('<t t-name="test"><main><span>Teléfono, means phone'
'</span></main></t>')
cls.view_4 = cls.env['ir.ui.view'].create({
'name': 'Test 4',
'key': 'website_lazy_load_image.test_4',
'type': 'qweb',
'arch_base': arch_4,
'mode': 'primary'
})
cls.website_id = cls.env.ref('website.default_website').id
cls.default_img = cls.env['ir.ui.view'].LAZYLOAD_DEFAULT_SRC

@@ -60,3 +77,24 @@ class LazyLoadTest(SavepointCase):
imgs = res.xpath('//main//img')
self.assertTrue('data-src' not in imgs[0].attrib)
self.assertEqual(imgs[0].attrib['src'], 'hello.jpg')

def test_no_wrap_content(self):
"""Check content not be wrapped into extra html tags"""
public_user_id = self.ref('base.public_user')
ui_view = self.env['ir.ui.view'].sudo(
public_user_id).with_context(website_id=self.website_id)
res = ui_view.render_template(self.view_3.id).decode('UTF-8')
arch = '<span>content not wrapped</span>'
self.assertEqual(res, arch)

def test_encoding_render(self):
"""Check content is correctly enconded"""
public_user_id = self.ref('base.public_user')
ui_view = self.env['ir.ui.view'].sudo(
public_user_id).with_context(website_id=self.website_id)
res = ui_view.render_template(self.view_4.id).decode('UTF-8')
arch = '<main><span>Teléfono, means phone</span></main>'
self.assertEqual(res, arch)
robots = self.env.ref('website.robots').render()
self.assertNotIn('<html>', robots.decode(
'UTF-8'), "Robots must not be wrapped into html DOM")

Loading…
Cancel
Save