Skip to content

Commit a7e68b1

Browse files
authored
Rewrite negative responsive units. (ampproject#23204)
1 parent aea52cd commit a7e68b1

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

extensions/amp-story/1.0/amp-story.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ export class AmpStory extends AMP.BaseElement {
491491
this.initializeMediaQueries_(mediaQueryEls);
492492
}
493493

494-
const styleEl = document.querySelector('style[amp-custom]');
494+
const styleEl = this.win.document.querySelector('style[amp-custom]');
495495

496496
if (styleEl) {
497497
this.rewriteStyles_(styleEl);
@@ -537,10 +537,10 @@ export class AmpStory extends AMP.BaseElement {
537537
// ../../../extensions/amp-animation/0.1/web-animations.js
538538
this.mutateElement(() => {
539539
styleEl.textContent = styleEl.textContent
540-
.replace(/([\d.]+)vh/gim, 'calc($1 * var(--story-page-vh))')
541-
.replace(/([\d.]+)vw/gim, 'calc($1 * var(--story-page-vw))')
542-
.replace(/([\d.]+)vmin/gim, 'calc($1 * var(--story-page-vmin))')
543-
.replace(/([\d.]+)vmax/gim, 'calc($1 * var(--story-page-vmax))');
540+
.replace(/(-?[\d.]+)vh/gim, 'calc($1 * var(--story-page-vh))')
541+
.replace(/(-?[\d.]+)vw/gim, 'calc($1 * var(--story-page-vw))')
542+
.replace(/(-?[\d.]+)vmin/gim, 'calc($1 * var(--story-page-vmin))')
543+
.replace(/(-?[\d.]+)vmax/gim, 'calc($1 * var(--story-page-vmax))');
544544
});
545545
}
546546

extensions/amp-story/1.0/test/test-amp-story.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,6 +1549,50 @@ describes.realWin(
15491549
});
15501550
});
15511551

1552+
describe('amp-story rewriteStyles', () => {
1553+
beforeEach(() => {
1554+
toggleExperiment(win, 'amp-story-responsive-units', true);
1555+
});
1556+
1557+
afterEach(() => {
1558+
toggleExperiment(win, 'amp-story-responsive-units', false);
1559+
});
1560+
1561+
it('should rewrite vw styles', () => {
1562+
createPages(story.element, 1, ['cover']);
1563+
const styleEl = win.document.createElement('style');
1564+
styleEl.setAttribute('amp-custom', '');
1565+
styleEl.textContent = 'foo {transform: translate3d(100vw, 0, 0);}';
1566+
win.document.head.appendChild(styleEl);
1567+
1568+
story.buildCallback();
1569+
1570+
return story.layoutCallback().then(() => {
1571+
expect(styleEl.textContent).to.equal(
1572+
'foo {transform: ' +
1573+
'translate3d(calc(100 * var(--story-page-vw)), 0, 0);}'
1574+
);
1575+
});
1576+
});
1577+
1578+
it('should rewrite negative vh styles', () => {
1579+
createPages(story.element, 1, ['cover']);
1580+
const styleEl = win.document.createElement('style');
1581+
styleEl.setAttribute('amp-custom', '');
1582+
styleEl.textContent = 'foo {transform: translate3d(-100vh, 0, 0);}';
1583+
win.document.head.appendChild(styleEl);
1584+
1585+
story.buildCallback();
1586+
1587+
return story.layoutCallback().then(() => {
1588+
expect(styleEl.textContent).to.equal(
1589+
'foo {transform: ' +
1590+
'translate3d(calc(-100 * var(--story-page-vh)), 0, 0);}'
1591+
);
1592+
});
1593+
});
1594+
});
1595+
15521596
describe('amp-story branching', () => {
15531597
beforeEach(() => {
15541598
toggleExperiment(win, 'amp-story-branching', true);

0 commit comments

Comments
 (0)