Skip to content

Commit 3d6af61

Browse files
MC-41569: "Image Size" sporadically changing from "%" to pixels
1 parent 1a6e77a commit 3d6af61

File tree

3 files changed

+61
-17
lines changed
  • app/code/Magento/PageBuilder/view/adminhtml/web
  • dev/tests/js/jasmine/tests/app/code/Magento/PageBuilder/view/adminhtml/web/js/utils

3 files changed

+61
-17
lines changed

app/code/Magento/PageBuilder/view/adminhtml/web/js/utils/editor.js

Lines changed: 20 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/utils/editor.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,18 @@ export function parseAttributesString(attributes: string): { [key: string]: stri
170170
* @param element
171171
*/
172172
export function lockImageSize(element: HTMLElement) {
173-
[].slice.call(element.querySelectorAll("img")).forEach((image: HTMLImageElement) => {
174-
if (image.style.width.length === 0 && image.style.height.length === 0) {
175-
image.style.width = `${image.width}px`;
176-
image.style.height = `${image.height}px`;
173+
[].slice.call(element.querySelectorAll('img')).forEach((image: HTMLImageElement) => {
174+
if (image.style.width.length === 0) {
175+
image.style.width = /^\d+$/.test(image.getAttribute('width')) ?
176+
image.getAttribute('width') + 'px' :
177+
image.getAttribute('width');
178+
image.setAttribute('data-width-locked', 'true');
179+
}
180+
if (image.style.height.length === 0) {
181+
image.style.height = /^\d+$/.test(image.getAttribute('height')) ?
182+
image.getAttribute('height') + 'px':
183+
image.getAttribute('height');
184+
image.setAttribute('data-height-locked', 'true');
177185
}
178186
});
179187
}
@@ -184,9 +192,15 @@ export function lockImageSize(element: HTMLElement) {
184192
* @param element
185193
*/
186194
export function unlockImageSize(element: HTMLElement) {
187-
[].slice.call(element.querySelectorAll("img")).forEach((image: HTMLImageElement) => {
188-
image.style.width = null;
189-
image.style.height = null;
195+
[].slice.call(element.querySelectorAll('img')).forEach((image: HTMLImageElement) => {
196+
if (image.getAttribute('data-width-locked')) {
197+
image.style.width = null;
198+
image.removeAttribute('data-width-locked');
199+
}
200+
if (image.getAttribute('data-height-locked')) {
201+
image.style.height = null;
202+
image.removeAttribute('data-height-locked');
203+
}
190204
});
191205
}
192206

dev/tests/js/jasmine/tests/app/code/Magento/PageBuilder/view/adminhtml/web/js/utils/editor.test.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,30 @@ define([
1313
describe('Magento_PageBuilder/js/utils/editor.js', function () {
1414
describe('lockImageSize', function () {
1515
it('Should not change the defined image sizes', function () {
16-
var image = new Image();
16+
var image = document.createElement('img'),
17+
element = document.createElement('div');
1718

18-
$(document.body).append(image);
19-
$(image).width('100%');
19+
image.setAttribute('width', '100%');
20+
$(element).append(image);
2021

21-
utils.lockImageSize(document.body);
22+
utils.lockImageSize(element);
2223
expect(image.style.width).toEqual('100%');
2324
});
2425
});
26+
27+
describe('unlockImageSize', function () {
28+
it('Should unlock locked images sizes only', function () {
29+
var image = document.createElement('img'),
30+
element = document.createElement('div');
31+
32+
$(image).css({'width' : '100%', 'height' : '100%'});
33+
$(image).attr('data-height-locked', 'true');
34+
$(element).append(image);
35+
36+
utils.unlockImageSize(element);
37+
expect(image.style.width).toEqual('100%');
38+
expect(image.style.height).toEqual('');
39+
});
40+
});
2541
});
2642
});

0 commit comments

Comments
 (0)