Skip to content

[ADD] checks_odoo_module_xml: new xml-img-without-size check - #204

Merged
moylop260 merged 1 commit into
OCA:mainfrom
vauxoo-dev:xml-img-size-moy
Nov 30, 2026
Merged

moylop260 merged 1 commit into
OCA:mainfrom
vauxoo-dev:xml-img-size-moy

Conversation

@moylop260

@moylop260 moylop260 commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Adds xml-img-without-size: an <img> of a QWeb template with neither width nor height.

Why

The browser reserves no space for the picture until it arrives, so the content below it moves when it does. Lighthouse and GTmetrix report it as "Use explicit width and height on image elements" and it counts for Cumulative Layout Shift. Today it is only found after the page is deployed and measured; this finds it in the commit.

Use the intrinsic size of the image in pixels. With img-fluid or height: auto the picture still scales down with its container, but the width attribute is the width it uses, so a smaller number than the real one shrinks it (measured in headless Chrome with a 600x300 image: width="16" height="9" rendered it at 16x8).

What is accepted

width and height; t-att-width, t-attf-width (and the height ones); a t-att or t-att-style/t-attf-style that builds them at render time; a width and a height in the inline style; an inline aspect-ratio.

What is not reported

The check only reports where a layout shift matters and the size can be stated. Every rule is a structural fact of the XML, not a guess about the picture:

  • PDF reports and mails: the template inherits or calls a report or mail layout (web.external_layout*, web.html_container, mail.*, mass_mailing.*), it is the template of an ir.actions.report of the module, or its file is in a report/ directory or has mail, mailing or digest in its name.
  • Content that the user replaces in the website builder: a picture with a fixed src inside a data-snippet, an oe_structure/o_editable zone, a snippet template (s_*) or a page template. A picture whose src is built by the template (t-att-src, t-attf-src: the logo of the website, the photo of a record) is still reported, because nobody replaces it and the size is known only to the template.
  • A size already fixed by the class (o_image_64_cover), a picture out of the flow (position-absolute, position: absolute) and a pixel inside <noscript>.

Anything else can be silenced with the usual oca-hooks:disable comment of the file, which this check honours like its neighbours.

Impact (measured)

On odoo/odoo 18.0 addons/ and six OCA repositories (18.0) the first version of the check reported 472 findings, 467 of them in odoo/odoo and 5 in the OCA repositories; a classification of the 472 showed that about 85 % were reports, mails, snippets or sizes already fixed. Replaying the exemptions over the same 472 findings leaves 103, and 63 of the 69 findings that were classified as genuine are kept (the other 6 are a tracking pixel in <noscript> and static pictures of snippets that the user replaces). Typical OCA repository: 0 to 3 findings.

No autofix: the right size depends on the picture and cannot be guessed from the XML.

Tests

Full suite: 125 passed, 7 skipped. The fixture has 3 reported images and one case per exemption; with the exemptions switched off the same fixture reports 13. The README sections were added by hand, because the generated README changes its indentation with the Python version used to build it.

@moylop260
moylop260 force-pushed the xml-img-size-moy branch 2 times, most recently from 4228034 to b1048f8 Compare November 30, 2026 09:26
An `<img>` of a QWeb template with no width and height makes the browser
reserve no space for the picture until it arrives, so the content below
it moves when it does. Lighthouse and GTmetrix report it as "Use explicit
width and height on image elements" and it counts for Cumulative Layout
Shift.

The attributes should state the intrinsic size of the image: with
`img-fluid` or `height: auto` the picture still scales down, but the
`width` attribute is the width it uses. The dynamic forms `t-att-width`,
`t-attf-width`, `t-att`, `t-att-style`, a width and a height in the
inline style and an inline `aspect-ratio` are accepted, since the size
is then decided at render time.

The check only reports where a layout shift matters and the size can be
stated. It skips the PDF reports and the mails, the pictures that the
user replaces in the website builder (a fixed `src` inside a snippet or
an editable zone), a size fixed by the class (`o_image_64_cover`), the
pictures out of the flow and the pixels inside `<noscript>`. A picture
whose `src` is built by the template, such as the logo of the website,
is still reported. The usual comment of the file silences the rest.
@moylop260
moylop260 merged commit 4507704 into OCA:main Nov 30, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant