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

✨Make "layout=intrinsic" compatible with SSR #23831

Merged
merged 2 commits into from
Aug 9, 2019
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
26 changes: 24 additions & 2 deletions src/custom-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,14 +653,36 @@ function createBaseCustomElementClass(win) {
getSizer_() {
if (
this.sizerElement === undefined &&
this.layout_ === Layout.RESPONSIVE
(this.layout_ === Layout.RESPONSIVE ||
this.layout_ === Layout.INTRINSIC)
) {
// Expect sizer to exist, just not yet discovered.
this.sizerElement = this.querySelector('i-amphtml-sizer');
}
return this.sizerElement || null;
}

/**
* @param {Element} sizer
* @private
*/
resetSizer_(sizer) {
if (this.layout_ === Layout.RESPONSIVE) {
setStyle(sizer, 'paddingTop', '0');
return;
}
if (this.layout_ === Layout.INTRINSIC) {
const intrinsicSizerImg = sizer.querySelector(
'.i-amphtml-intrinsic-sizer'
);
if (!intrinsicSizerImg) {
return;
}
intrinsicSizerImg.setAttribute('src', '');
return;
}
}

/**
* If the element has a media attribute, evaluates the value as a media
* query and based on the result adds or removes the class
Expand Down Expand Up @@ -742,7 +764,7 @@ function createBaseCustomElementClass(win) {
// responsible for managing its height. Aspect ratio is no longer
// preserved.
this.sizerElement = null;
setStyle(sizer, 'paddingTop', '0');
this.resetSizer_(sizer);
this.mutateOrInvoke_(() => {
dom.removeElement(sizer);
});
Expand Down
28 changes: 26 additions & 2 deletions test/unit/test-custom-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -1307,8 +1307,6 @@ describes.realWin('CustomElement', {amp: true}, env => {
const sizer = doc.createElement('div');
element.sizerElement = sizer;
element.changeSize(111, 222, {top: 1, right: 2, bottom: 3, left: 4});
expect(parseInt(sizer.style.paddingTop, 10)).to.equal(0);
expect(element.sizerElement).to.be.null;
expect(element.style.height).to.equal('111px');
expect(element.style.width).to.equal('222px');
expect(element.style.marginTop).to.equal('1px');
Expand All @@ -1317,6 +1315,32 @@ describes.realWin('CustomElement', {amp: true}, env => {
expect(element.style.marginLeft).to.equal('4px');
});

it('should reset sizer for responsive layout', () => {
const element = new ElementClass();
element.layout_ = Layout.RESPONSIVE;
const sizer = doc.createElement('div');
element.sizerElement = sizer;
element.changeSize(111, 222, {top: 1, right: 2, bottom: 3, left: 4});
expect(sizer.style.paddingTop).to.equal('0px');
expect(element.sizerElement).to.be.null;
});

it('should reset sizer for intrinsic layout', () => {
const element = new ElementClass();
element.layout_ = Layout.INTRINSIC;
const sizer = doc.createElement('i-amphtml-sizer');
const intrinsicSizer = doc.createElement('img');
intrinsicSizer.classList.add('i-amphtml-intrinsic-sizer');
intrinsicSizer.setAttribute(
'src',
'data:image/svg+xml;charset=utf-8,<svg height=&quot;610&quot; width=&quot;1080&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; version=&quot;1.1&quot;/>'
);
sizer.appendChild(intrinsicSizer);
element.appendChild(sizer);
element.changeSize(111);
expect(intrinsicSizer.getAttribute('src')).to.equal('');
});

it('should NOT apply media condition in template', () => {
const element1 = new ElementClass();
element1.setAttribute('media', '(min-width: 1px)');
Expand Down