Skip to content

Commit 14f0ed3

Browse files
author
Sepand Parhami
committed
Review comments.
1 parent 8dcb1e9 commit 14f0ed3

9 files changed

+233
-106
lines changed

src/img-dimensions.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,52 +42,52 @@
4242
* @param naturalSize The natural dimensions of the image.
4343
* @param containerSize The size of the container we want to render the image
4444
* in.
45-
* @param toMin If we whould cap the smaller dimension of the image to fit the
45+
* @param toMin If we should cap the smaller dimension of the image to fit the
4646
* container (`object-fit: cover`) or the larger dimension
4747
* (`object-fit: contain`).
4848
* @return The Size that the image should be rendered as.
4949
*/
5050
function constrain(
5151
naturalSize: Size, containerSize: Size, toMin: boolean): Size {
52-
const {width, height} = containerSize;
53-
const elAspectRatio = width / height;
52+
const elAspectRatio = containerSize.width / containerSize.height;
5453
const naturalAspectRatio = naturalSize.width / naturalSize.height;
5554

5655
if (naturalAspectRatio > elAspectRatio !== toMin) {
5756
return {
58-
width: height * naturalAspectRatio,
59-
height,
57+
width: containerSize.height * naturalAspectRatio,
58+
height: containerSize.height,
6059
};
6160
}
6261

6362
return {
64-
width,
65-
height: width / naturalAspectRatio,
63+
width: containerSize.width,
64+
height: containerSize.width / naturalAspectRatio,
6665
};
6766
}
6867

69-
function getDimensionsForCover(
68+
function getDimensionsForObjectFitCover(
7069
naturalSize: Size, containerSize: Size): Size {
7170
return constrain(naturalSize, containerSize, false);
7271
}
7372

74-
function getDimensionsForContain(
73+
function getDimensionsForObjectFitContain(
7574
naturalSize: Size, containerSize: Size): Size {
7675
return constrain(naturalSize, containerSize, true);
7776
}
7877

79-
function getDimensionsForFill(containerSize: Size): Size {
78+
function getDimensionsForObjectFitFill(containerSize: Size): Size {
8079
return containerSize;
8180
}
8281

83-
function getDimensionsForNone(naturalSize: Size): Size {
82+
function getDimensionsForObjectFitNone(naturalSize: Size): Size {
8483
return naturalSize;
8584
}
8685

87-
function getDimensionsForScaleDown(
86+
function getDimensionsForObjectFitScaleDown(
8887
naturalSize: Size, containerSize: Size): Size {
89-
const noneSize = getDimensionsForNone(naturalSize);
90-
const containSize = getDimensionsForContain(naturalSize, containerSize);
88+
const noneSize = getDimensionsForObjectFitNone(naturalSize);
89+
const containSize = getDimensionsForObjectFitContain(
90+
naturalSize, containerSize);
9191

9292
// Since both have the same aspect ratio, we can simply take the smaller
9393
// dimension for both.
@@ -114,15 +114,15 @@ export function getRenderedDimensions(
114114

115115
switch(objectFit) {
116116
case 'cover':
117-
return getDimensionsForCover(naturalSize, containerSize);
117+
return getDimensionsForObjectFitCover(naturalSize, containerSize);
118118
case 'contain':
119-
return getDimensionsForContain(naturalSize, containerSize);
119+
return getDimensionsForObjectFitContain(naturalSize, containerSize);
120120
case 'fill':
121-
return getDimensionsForFill(containerSize);
121+
return getDimensionsForObjectFitFill(containerSize);
122122
case 'none':
123-
return getDimensionsForNone(naturalSize);
123+
return getDimensionsForObjectFitNone(naturalSize);
124124
case 'scale-down':
125-
return getDimensionsForScaleDown(naturalSize, containerSize);
125+
return getDimensionsForObjectFitScaleDown(naturalSize, containerSize);
126126
default:
127127
throw new Error(`object-fit: ${objectFit} not supported`);
128128
}

src/imitation-img.ts renamed to src/intermdediate-img.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17+
/**
18+
* @fileoverview This file is used to create an image for use in an `<img>` to
19+
* `<img>` animation. It is implemented in a way to allow for animating the
20+
* cropping of the rendered image. Once the animation is completed, the
21+
* intermediate image can be removed to show the destination `<img>` instead.
22+
*/
23+
1724
import {Size, getRenderedDimensions} from './img-dimensions.js';
1825

1926
/**
@@ -29,7 +36,7 @@ import {Size, getRenderedDimensions} from './img-dimensions.js';
2936
* provided if already measured.
3037
* @return The replacement container along with structural information.
3138
*/
32-
export function createImitationImg(
39+
export function createItermediateImg(
3340
srcImg: HTMLImageElement,
3441
srcImgRect: ClientRect = srcImg.getBoundingClientRect(),
3542
imageDimensions: Size = getRenderedDimensions(srcImg, srcImgRect),

src/test-img-dimensions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ describe('getDimensions', () => {
148148

149149
const {width, height} = getRenderedDimensions(img, dimensions(2, 2));
150150
expect(width).to.equal(2);
151-
expect(height).to.equal(1.5);
151+
expect(height).to.equal(3/2);
152152
});
153153
});
154154
});

src/test-imitation-img.ts renamed to src/test-intermediate-img.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {createImitationImg} from './imitation-img';
17+
import {createItermediateImg} from './intermdediate-img';
1818
import {imgLoadPromise} from './testing/utils';
1919

2020
const {expect} = chai;
@@ -55,7 +55,7 @@ describe('createImitationImg', () => {
5555

5656
describe('the scaleElement', () => {
5757
it('should size correctly', async () => {
58-
const {scaleElement} = createImitationImg(srcImg);
58+
const {scaleElement} = createItermediateImg(srcImg);
5959
testContainer.appendChild(scaleElement);
6060

6161
const {width, height} = scaleElement.getBoundingClientRect();
@@ -68,7 +68,7 @@ describe('createImitationImg', () => {
6868
let imgElement;
6969

7070
beforeEach(() => {
71-
const {img, scaleElement} = createImitationImg(srcImg);
71+
const {img, scaleElement} = createItermediateImg(srcImg);
7272
imgElement = img;
7373
testContainer.appendChild(scaleElement);
7474
scaleElement.style.position = 'fixed';
@@ -107,7 +107,7 @@ describe('createImitationImg', () => {
107107
let imgElement;
108108

109109
beforeEach(() => {
110-
const {img, scaleElement} = createImitationImg(srcImg);
110+
const {img, scaleElement} = createItermediateImg(srcImg);
111111
imgElement = img;
112112
testContainer.appendChild(scaleElement);
113113
scaleElement.style.position = 'fixed';
@@ -145,7 +145,7 @@ describe('createImitationImg', () => {
145145
let imgElement;
146146

147147
beforeEach(() => {
148-
const {img, scaleElement} = createImitationImg(srcImg);
148+
const {img, scaleElement} = createItermediateImg(srcImg);
149149
imgElement = img;
150150
testContainer.appendChild(scaleElement);
151151
scaleElement.style.position = 'fixed';

src/testing/animation-test-controller.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ const styleText = elementSelectors
129129
* this must be re-created each time, using cloneNode(true) will not work.
130130
* @return An HTMLStyleElement that stops all animations on all Elements.
131131
*/
132-
const createStopperStyle = function(): HTMLStyleElement {
133-
const stopperStyle = document.createElement('style');
134-
stopperStyle.appendChild(document.createTextNode(styleText));
135-
return stopperStyle;
132+
const creatseAnimationPauseStyle = function(): HTMLStyleElement {
133+
const styleElement = document.createElement('style');
134+
styleElement.appendChild(document.createTextNode(styleText));
135+
return styleElement;
136136
}
137137

138138
/**
@@ -148,9 +148,9 @@ function after(obj: Object, prop: string, callback: Function) {
148148

149149
const old = obj[prop];
150150
obj[prop] = function() {
151-
const retn = old.apply(this, arguments);
152-
callback(retn);
153-
return retn;
151+
const returnValue = old.apply(this, arguments);
152+
callback(returnValue);
153+
return returnValue;
154154
}
155155
}
156156

@@ -179,7 +179,7 @@ function attachStyleElement(root: Document|ShadowRoot) {
179179
return;
180180
}
181181

182-
const style = createStopperStyle();
182+
const style = creatseAnimationPauseStyle();
183183

184184
if (root == document) {
185185
root.head!.appendChild(style);

src/testing/test-animation-test-controller.ts

Lines changed: 95 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,17 @@ describe('Animation test controller', () => {
2828
before(() => {
2929
const style = document.createElement('style');
3030
style.textContent = `
31-
.animate {
31+
.animate,
32+
.animate-after::after,
33+
.animate-before::before,
34+
.animate-backdrop::backdrop {
3235
animation: 1000ms foo infinite;
3336
}
3437
35-
.delayed {
36-
animation-delay: 125ms;
37-
}
38-
39-
.animate-before::before {
40-
animation: 1000ms foo infinite;
41-
}
42-
43-
.delayed-before::before {
38+
.delayed,
39+
.delayed-after::after,
40+
.delayed-before::before,
41+
.delayed-backdrop::backdrop {
4442
animation-delay: 125ms;
4543
}
4644
@@ -68,7 +66,7 @@ describe('Animation test controller', () => {
6866
});
6967

7068
describe('document', () => {
71-
it('should stop animations on existing Elements', () => {
69+
it('should pause animations on existing Elements', () => {
7270
const el = document.createElement('div');
7371
el.className = 'animate';
7472

@@ -78,7 +76,7 @@ describe('Animation test controller', () => {
7876
expect(getComputedStyle(el).animationPlayState).to.equal('paused');
7977
});
8078

81-
it('should stop animations on newly added Elements', () => {
79+
it('should pause animations on newly added Elements', () => {
8280
const el = document.createElement('div');
8381
el.className = 'animate';
8482

@@ -88,14 +86,34 @@ describe('Animation test controller', () => {
8886
expect(getComputedStyle(el).animationPlayState).to.equal('paused');
8987
});
9088

91-
it('should stop animations on psuedo elements', () => {
89+
it('should pause animations on pseudo after', () => {
90+
const el = document.createElement('div');
91+
el.className = 'animate-after';
92+
93+
setupAnimations();
94+
container.appendChild(el);
95+
96+
expect(getComputedStyle(el, '::after').animationPlayState).to.equal('paused');
97+
});
98+
99+
it('should pause animations on pseudo before', () => {
92100
const el = document.createElement('div');
93101
el.className = 'animate-before';
94102

95103
setupAnimations();
96104
container.appendChild(el);
97105

98-
expect(getComputedStyle(el, ':before').animationPlayState).to.equal('paused');
106+
expect(getComputedStyle(el, '::before').animationPlayState).to.equal('paused');
107+
});
108+
109+
it('should pause animations on pseudo backdrop', () => {
110+
const el = document.createElement('div');
111+
el.className = 'animate-backdrop';
112+
113+
setupAnimations();
114+
container.appendChild(el);
115+
116+
expect(getComputedStyle(el, '::backdrop').animationPlayState).to.equal('paused');
99117
});
100118

101119
it('should resume animations', () => {
@@ -131,18 +149,64 @@ describe('Animation test controller', () => {
131149
expect(getComputedStyle(el).animationDelay).to.equal('0.025s');
132150
});
133151

134-
it('should offset a delayed animation on a psuedo element', () => {
152+
it('should offset a delayed animation on a pseudo after', () => {
153+
const el = document.createElement('div');
154+
el.className = 'animate-after delayed-after';
155+
156+
container.appendChild(el);
157+
setupAnimations();
158+
159+
offset(100);
160+
expect(getComputedStyle(el, '::after').animationDelay).to.equal('0.025s');
161+
});
162+
163+
it('should offset a delayed animation on a pseudo before', () => {
135164
const el = document.createElement('div');
136165
el.className = 'animate-before delayed-before';
137166

138167
container.appendChild(el);
139168
setupAnimations();
140169

141170
offset(100);
142-
expect(getComputedStyle(el, ':before').animationDelay).to.equal('0.025s');
171+
expect(getComputedStyle(el, '::before').animationDelay).to.equal('0.025s');
172+
});
173+
174+
it('should offset a delayed animation on a pseudo backdrop', () => {
175+
const el = document.createElement('div');
176+
el.className = 'animate-backdrop delayed-backdrop';
177+
178+
container.appendChild(el);
179+
setupAnimations();
180+
181+
offset(100);
182+
expect(getComputedStyle(el, '::backdrop').animationDelay).to.equal('0.025s');
143183
});
144184

145185
it('should offset multiple times', () => {
186+
const el = document.createElement('div');
187+
el.className = 'animate delayed';
188+
189+
container.appendChild(el);
190+
setupAnimations();
191+
192+
offset(100);
193+
offset(300);
194+
expect(getComputedStyle(el).animationDelay).to.equal('-0.175s');
195+
});
196+
197+
it('should offset multiple times on pseudo after', () => {
198+
const el = document.createElement('div');
199+
el.className = 'animate-after delayed-after';
200+
201+
container.appendChild(el);
202+
setupAnimations();
203+
204+
offset(100);
205+
offset(300);
206+
expect(getComputedStyle(el, '::after').animationDelay).to.equal('-0.175s');
207+
});
208+
209+
it('should offset multiple times on pseudo before', () => {
146210
const el = document.createElement('div');
147211
el.className = 'animate-before delayed-before';
148212

@@ -151,7 +215,19 @@ describe('Animation test controller', () => {
151215

152216
offset(100);
153217
offset(300);
154-
expect(getComputedStyle(el).animationDelay).to.equal('-0.3s');
218+
expect(getComputedStyle(el, '::before').animationDelay).to.equal('-0.175s');
219+
});
220+
221+
it('should offset multiple times on pseudo backdrop', () => {
222+
const el = document.createElement('div');
223+
el.className = 'animate-backdrop delayed-backdrop';
224+
225+
container.appendChild(el);
226+
setupAnimations();
227+
228+
offset(100);
229+
offset(300);
230+
expect(getComputedStyle(el, '::backdrop').animationDelay).to.equal('-0.175s');
155231
});
156232
});
157233

@@ -160,7 +236,7 @@ describe('Animation test controller', () => {
160236
return;
161237
}
162238

163-
it('should stop animations on existing Elements', () => {
239+
it('should pause animations on existing Elements', () => {
164240
const outer = document.createElement('div');
165241
const sr = outer.attachShadow({ mode: 'closed' });
166242
const el = document.createElement('div');
@@ -173,7 +249,7 @@ describe('Animation test controller', () => {
173249
expect(getComputedStyle(el).animationPlayState).to.equal('paused');
174250
});
175251

176-
it('should stop animations on newly added Elements', () => {
252+
it('should pause animations on newly added Elements', () => {
177253
const outer = document.createElement('div');
178254
const sr = outer.attachShadow({ mode: 'closed' });
179255
const el = document.createElement('div');

0 commit comments

Comments
 (0)