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

Remove measure APIs from amp-auto-lightbox #33663

Merged
merged 4 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build-system/test-configs/forbidden-terms.js
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,6 @@ const forbiddenTermsSrcInclusive = {
'src/service/resources-impl.js',
'src/service/video-manager-impl.js',
'extensions/amp-a4a/0.1/amp-a4a.js',
'extensions/amp-auto-lightbox/0.1/amp-auto-lightbox.js',
'extensions/amp-fx-flying-carpet/0.1/amp-fx-flying-carpet.js',
'extensions/amp-script/0.1/amp-script.js',
'extensions/amp-story/1.0/amp-story-page.js',
Expand Down
25 changes: 15 additions & 10 deletions extensions/amp-auto-lightbox/0.1/amp-auto-lightbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
whenUpgradedToCustomElement,
} from '../../../src/dom';
import {dev} from '../../../src/log';
import {measure} from './measure';
import {toArray} from '../../../src/types';
import {tryParseJson} from '../../../src/json';

Expand Down Expand Up @@ -124,11 +125,13 @@ const getRootNode = (ampdoc) => ampdoc.getRootNode();
export class Criteria {
/**
* @param {!Element} element
* @param {number} renderWidth
* @param {number} renderHeight
* @return {boolean}
*/
static meetsAll(element) {
static meetsAll(element, renderWidth, renderHeight) {
return (
Criteria.meetsSizingCriteria(element) &&
Criteria.meetsSizingCriteria(element, renderWidth, renderHeight) &&
Criteria.meetsTreeShapeCriteria(element)
);
}
Expand All @@ -152,15 +155,15 @@ export class Criteria {

/**
* @param {!Element} element
* @param {number} renderWidth
* @param {number} renderHeight
* @return {boolean}
*/
static meetsSizingCriteria(element) {
static meetsSizingCriteria(element, renderWidth, renderHeight) {
const {naturalWidth, naturalHeight} = getMaxNaturalDimensions(
dev().assertElement(element.querySelector('img'))
);

const {width: renderWidth, height: renderHeight} = element.getLayoutSize();

const viewport = Services.viewportForDoc(element);
const {width: vw, height: vh} = viewport.getSize();

Expand Down Expand Up @@ -440,11 +443,13 @@ export function runCandidates(ampdoc, candidates) {
if (candidate.signals().get(CommonSignals.UNLOAD)) {
return;
}
if (!Criteria.meetsAll(candidate)) {
return;
}
dev().info(TAG, 'apply', candidate);
return apply(ampdoc, candidate);
return measure(ampdoc, candidate).then(({width, height}) => {
if (!Criteria.meetsAll(candidate, width, height)) {
return;
}
dev().info(TAG, 'apply', candidate);
return apply(ampdoc, candidate);
});
}, NOOP)
);
}
Expand Down
59 changes: 59 additions & 0 deletions extensions/amp-auto-lightbox/0.1/measure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {Deferred} from '../../../src/utils/promise';

/** @type {!WeakMap<!Window, !IntersectionObserver>} */
const intersectionObserversByWin = new WeakMap();

/** @type {!WeakMap<!Element, !Deferred>} */
const measuringElements = new WeakMap();

/**
* @param {!../../../src/service/ampdoc-impl.AmpDoc} ampdoc
* @param {!Element} element
* @return {!Promise<!ClientRect>}
*/
export function measure(ampdoc, element) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we promote this to src/dom.js?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, was going to comment on that and forgot. @samouri has already created src/utils/intersection and I can reuse that. The only nit is that the src/utils/intersection uses an IntersectionObserver{root:document}, which triggers the polyfill path in more cases than a simple {root:null} that I do here. So I wasn't sure if it's worth reusing the existing util or rolling this as a single-use utility for the extension. LMK what you think.

// Uses the `IntersectionObserver` for size measurements. It'd be more
// appropriate to use `size-observer.measureBorderBoxSize`, but it's very
// poorly supported. While, `IntersectionObserver` is implemented on most
// of platforms in this basic form.
const {win} = ampdoc;
let observer = intersectionObserversByWin.get(win);
if (!observer) {
observer = new win.IntersectionObserver((entries) => {
for (let i = 0; i < entries.length; i++) {
const {target, boundingClientRect} = entries[i];
const deferred = measuringElements.get(target);
if (deferred) {
deferred.resolve(boundingClientRect);
measuringElements.delete(target);
observer.unobserve(target);
}
}
});
intersectionObserversByWin.set(win, observer);
}

let deferred = measuringElements.get(element);
if (!deferred) {
deferred = new Deferred();
measuringElements.set(element, deferred);
observer.observe(element);
}
return deferred.promise;
}
80 changes: 80 additions & 0 deletions extensions/amp-auto-lightbox/0.1/test/test-measure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {installIntersectionObserverStub} from '../../../../testing/intersection-observer-stub';
import {measure} from '../measure';

describes.realWin('amp-auto-lightbox:measure', {amp: true}, (env) => {
let win, doc, ampdoc;
let intersectionObserverStub;
let element;

beforeEach(() => {
win = env.win;
doc = win.document;
ampdoc = env.ampdoc;
intersectionObserverStub = installIntersectionObserverStub(
env.sandbox,
win
);
element = doc.createElement('div');
});

it('should measure an element via IntersectionObserver', async () => {
expect(intersectionObserverStub.isObserved(element)).to.be.false;

const promise = measure(ampdoc, element);
expect(
intersectionObserverStub.isObserved(element, {root: null, threshold: 0})
).to.be.true;

intersectionObserverStub.notifySync(
{
target: element,
boundingClientRect: {width: 101, height: 102},
},
{
root: null,
threshold: 0,
}
);

const {width, height} = await promise;
expect(width).to.equal(101);
expect(height).to.equal(102);
expect(intersectionObserverStub.isObserved(element)).to.be.false;
});

it('should only measure once at a time', async () => {
const promise1 = measure(ampdoc, element);
const promise2 = measure(ampdoc, element);

intersectionObserverStub.notifySync(
{
target: element,
boundingClientRect: {width: 101, height: 102},
},
{
root: null,
threshold: 0,
}
);

expect(await promise1).to.deep.equal({width: 101, height: 102});
expect(await promise2).to.deep.equal({width: 101, height: 102});
expect(intersectionObserverStub.isObserved(element)).to.be.false;
});
});
19 changes: 0 additions & 19 deletions extensions/amp-auto-lightbox/0.1/utils/promise.js

This file was deleted.