Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] website_lazy_load_image: HTML parsing #651

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions website_lazy_load_image/models/ir_ui_view.py
Expand Up @@ -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):
Expand All @@ -22,7 +22,7 @@ def render_template(self, template, values=None, engine='ir.qweb'):
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(
Expand All @@ -32,5 +32,5 @@ def render_template(self, template, values=None, engine='ir.qweb'):
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 changes: 38 additions & 0 deletions website_lazy_load_image/tests/test_lazy_load_image.py
Expand Up @@ -27,6 +27,23 @@ def setUpClass(cls):
'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

Expand Down Expand Up @@ -60,3 +77,24 @@ def test_disabled_render(self):
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)
hugho-ad marked this conversation as resolved.
Show resolved Hide resolved
robots = self.env.ref('website.robots').render()
self.assertNotIn('<html>', robots.decode(
'UTF-8'), "Robots must not be wrapped into html DOM")