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

amp-script: Create 'fill content' container for responsive/fluid #26400

Merged
merged 2 commits into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 24 additions & 5 deletions extensions/amp-script/0.1/amp-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,23 @@ export class AmpScript extends AMP.BaseElement {

/** @override */
layoutCallback() {
// Layouts that use sizers (responsive, fluid) require the worker-dom
// subtree to be wrapped in a "fill content" container. This is because
// these layouts do _not_ constrain the size of the amp-script element
// via inline styles (they use sizerElement). However, this breaks
// "container" layout so only do it selectively.
let container;
if (this.element.sizerElement) {
container = this.win.document.createElement('div');
this.applyFillContent(container, true);
// Reparent all real children to the container.
const realChildren = this.getRealChildren();
for (let i = 0; i < realChildren.length; i++) {
container.appendChild(realChildren[i]);
}
this.element.appendChild(container);
}

this.userActivation_ = new UserActivationTracker(this.element);

// The displayed name of the combined script in dev tools.
Expand Down Expand Up @@ -218,11 +235,13 @@ export class AmpScript extends AMP.BaseElement {
};

// Create worker and hydrate.
WorkerDOM.upgrade(this.element, workerAndAuthorScripts, config).then(
workerDom => {
this.workerDom_ = workerDom;
}
);
WorkerDOM.upgrade(
container || this.element,
workerAndAuthorScripts,
config
).then(workerDom => {
this.workerDom_ = workerDom;
});
return workerAndAuthorScripts;
}

Expand Down
24 changes: 23 additions & 1 deletion extensions/amp-script/0.1/test-e2e/test-amp-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
describes.endtoend(
'amp-script e2e',
{
testUrl: 'http://localhost:8000/test/manual/amp-script/test1.amp.html',
testUrl:
'http://localhost:8000/test/fixtures/e2e/amp-script/basic.amp.html',
initialRect: {width: 600, height: 600},
environments: ['single'],
browsers: ['chrome', 'safari'],
Expand Down Expand Up @@ -51,5 +52,26 @@ describes.endtoend(
const h1 = await controller.findElement('h1');
await expect(controller.getElementText(h1)).to.equal('Hello World!');
});

// In layout=responsive|fluid, amp-script creates a fill-content container
// div and reparents children to it. This ensures that the element sizing
// is respected.
it('should respect aspect ratio in layout=responsive', async () => {
const element = await controller.findElement(
'amp-script[layout="responsive"]'
);
await expect(controller.getElementAttribute(element, 'class')).to.contain(
'i-amphtml-layout'
);

const width = await controller.getElementAttribute(element, 'width');
const height = await controller.getElementAttribute(element, 'height');
const targetRatio = Number(width) / Number(height);

const rect = await controller.getElementRect(element);
const realRatio = rect.width / rect.height;

await expect(realRatio).to.equal(targetRatio);
});
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<script async custom-template="amp-script" src="https://cdn.ampproject.org/v0/amp-script-0.1.js"></script>
</head>
<body>
<amp-script id=remote layout=container src="./test1.js">
<amp-script id=remote layout=container src="./basic.js">
<div class="root">
<button id="simple">Simple</button>
<button id="tail">Tail</button>
Expand All @@ -28,5 +28,9 @@
<script type=text/plain target=amp-script id="foo">
document.body.textContent = 'Hello World!';
</script>

<amp-script id=responsive script=foo layout=responsive width=300 height=50>
<p style="height: 1000px">Some very long content</p>
</amp-script>
</body>
</html>
File renamed without changes.