Skip to content

Commit

Permalink
Emit warning if attempting to load a 0 height/width amp-script
Browse files Browse the repository at this point in the history
  • Loading branch information
samouri committed Jan 23, 2020
1 parent d6ecf7c commit e9d27c8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
15 changes: 15 additions & 0 deletions extensions/amp-script/0.1/amp-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,21 @@ export class AmpScript extends AMP.BaseElement {
});
}

/**
* @override
*/
onLayoutMeasure() {
super.onLayoutMeasure();
const {width, height} = this.getLayoutBox();
if (width === 0 && height === 0) {
user().warn(
TAG,
'Skipped initializing amp-script due to zero width and height.',
this.element
);
}
}

/**
* @param {!AmpScriptService} service
* @visibleForTesting
Expand Down
14 changes: 14 additions & 0 deletions extensions/amp-script/0.1/test/unit/test-amp-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from '../../amp-script';
import {FakeWindow} from '../../../../../testing/fake-dom';
import {Services} from '../../../../../src/services';
import {user} from '../../../../../src/log';

describes.fakeWin('AmpScript', {amp: {runtimeOn: false}}, env => {
let element;
Expand Down Expand Up @@ -93,6 +94,19 @@ describes.fakeWin('AmpScript', {amp: {runtimeOn: false}}, env => {
expect(service.checkSha384).to.be.called;
});

it('should warn if there is zero width/height', () => {
const warnStub = env.sandbox.stub(user(), 'warn');
env.sandbox.stub(script, 'getLayoutBox').returns({height: 0, width: 0});
script.onLayoutMeasure();

expect(warnStub).calledWith(
'amp-script',
'Skipped initializing amp-script due to zero width and height.',
script.element
);
expect(warnStub).to.have.callCount(1);
});

it('should fail on invalid sha384(author_js) for cross-origin src', () => {
env.sandbox.stub(env.ampdoc, 'getUrl').returns('https://foo.example/');
element.setAttribute('src', 'https://bar.example/bar.js');
Expand Down

0 comments on commit e9d27c8

Please sign in to comment.