Skip to content

Commit cb2510f

Browse files
committed
[FIX] html_editor: remove forced image dimensions on edit
Problem: When resized images (e.g., with `style.width: 50%`) are processed by `convert_inline`, `width` and `height` attributes may be added for email client compatibility. These attributes override resizing changes, forcing the last saved dimensions and preventing fallback to the image's default size. Solution: Remove `width` and `height` attributes from the content inserted into the editor. They will be correctly re-applied on save if needed. Steps to reproduce: - Create a new email template - Add an image - Resize the image to 100% - Save - Resize the image back to default - The image keeps the 100% size opw-4863515 closes odoo#213786 Signed-off-by: David Monjoie (dmo) <[email protected]>
1 parent 264ec41 commit cb2510f

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

addons/html_editor/static/src/utils/sanitize.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ export function initElementForEdition(element, options = {}) {
1515
baseContainerNodeName: "DIV",
1616
});
1717
}
18+
19+
// During `convert_inline`, image elements may receive `width` and `height` attributes,
20+
// along with inline styles. These attributes force specific dimensions, which breaks
21+
// the fallback to default sizing. We remove them here to allow proper resizing behavior.
22+
// The attributes will be re-applied on save.
23+
for (const img of element.querySelectorAll("img[width], img[height]")) {
24+
const width = img.getAttribute("width");
25+
const height = img.getAttribute("height");
26+
img.removeAttribute("height");
27+
img.removeAttribute("width");
28+
img.style.setProperty("width", width);
29+
img.style.setProperty("height", height);
30+
}
1831
}
1932

2033
/**

addons/html_editor/static/tests/editor.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,14 @@ test("Convert self closing t elements to opening/closing tags", async () => {
137137
'<div> <t t-out="object.partner_id" data-oe-t-inline="true" contenteditable="false"></t> </div>'
138138
);
139139
});
140+
141+
test("Remove `width`, `height` attributes from image and apply them to style", async () => {
142+
const { el } = await setupEditor(`
143+
<div>
144+
<img src="#" width="50%" height="50%">
145+
</div>
146+
`);
147+
expect(el.innerHTML.trim().replace(/\s+/g, " ")).toBe(
148+
`<div class="o-paragraph"> <img src="#" style="width: 50%; height: 50%;"> </div>`
149+
);
150+
});

0 commit comments

Comments
 (0)