Skip to content

Commit 3d148f5

Browse files
committed
amp-script: nodom variant
- adds support for nodom flag to amp-script, for protocol adapters. - upgrades worker-dom to the latest (which has support for callFunction, and lite binaries). - adds e2e test for "script" uri in amp-list.
1 parent 0290a90 commit 3d148f5

File tree

7 files changed

+129
-6
lines changed

7 files changed

+129
-6
lines changed

build-system/tasks/extension-helpers.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,11 @@ async function buildExtensionJs(path, name, version, latestVersion, options) {
568568
fs.copyFileSync(dir + 'worker.js', `${file}.js`);
569569
// The "mjs" output is unminified ES6 and has debugging flags enabled.
570570
fs.copyFileSync(dir + 'worker.mjs', `${file}.max.js`);
571+
572+
// Same as above but for the nodom worker variant.
573+
const noDomFile = `dist/v0/amp-script-worker-nodom-${version}`;
574+
fs.copyFileSync(dir + 'worker.nodom.js', `${noDomFile}.js`);
575+
fs.copyFileSync(dir + 'worker.nodom.mjs', `${noDomFile}.max.js`);
571576
}
572577
}
573578

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright 2019 The AMP HTML Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS-IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
describes.endtoend(
18+
'amp-list "amp-script:" uri',
19+
{
20+
testUrl: 'http://localhost:8000/test/fixtures/e2e/amp-list/amp-list-function-src.html',
21+
experiments: ['protocol-adapters'],
22+
environments: ['single'],
23+
},
24+
async (env) => {
25+
let controller;
26+
27+
beforeEach(async () => {
28+
controller = env.controller;
29+
});
30+
31+
it.configure()
32+
.run('should render list backed by amp-script data', async function () {
33+
const container = await getListContainer(controller);
34+
await verifyContainer(controller, container);
35+
36+
// Verify that all items rendered.
37+
const listItems = await getListItems(controller);
38+
await expect(listItems).to.have.length(2);
39+
});
40+
}
41+
);
42+
43+
function getListContainer(controller) {
44+
return controller.findElement('div[role=list]');
45+
}
46+
47+
function getListItems(controller) {
48+
return controller.findElements('div[role=listitem]');
49+
}
50+
51+
async function verifyContainer(controller, container) {
52+
await expect(controller.getElementAttribute(container, 'class')).to.equal(
53+
'i-amphtml-fill-content i-amphtml-replaced-content'
54+
);
55+
}

extensions/amp-script/0.1/amp-script.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ export class AmpScript extends AMP.BaseElement {
109109
* @private {boolean}
110110
*/
111111
this.development_ = false;
112+
113+
/**
114+
* If true, signals that the `nodom` variant of worker-dom should be used.
115+
* The worker js will have a much smaller bundle size, but no access to dom
116+
* functions.
117+
*
118+
* @private {boolean}
119+
*/
120+
this.nodom_ = false;
112121
}
113122

114123
/** @override */
@@ -118,6 +127,7 @@ export class AmpScript extends AMP.BaseElement {
118127

119128
/** @override */
120129
buildCallback() {
130+
this.nodom_ = this.element.hasAttribute('nodom');
121131
this.development_ =
122132
this.element.hasAttribute('data-ampdevmode') ||
123133
this.element.ownerDocument.documentElement.hasAttribute(
@@ -297,7 +307,7 @@ export class AmpScript extends AMP.BaseElement {
297307
const useLocal = getMode().localDev || getMode().test;
298308
const workerUrl = calculateExtensionScriptUrl(
299309
location,
300-
'amp-script-worker',
310+
this.nodom_ ? 'amp-script-worker-nodom' : 'amp-script-worker',
301311
'0.1',
302312
useLocal
303313
);

extensions/amp-script/0.1/test/unit/test-amp-script.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,33 @@ describes.fakeWin('AmpScript', {amp: {runtimeOn: false}}, (env) => {
8484
return script.layoutCallback().should.be.rejected;
8585
});
8686

87+
it('should support nodom variant', async () => {
88+
element.setAttribute('nodom', '');
89+
element.setAttribute('src', 'https://foo.example/foo.txt');
90+
env.sandbox.stub(env.ampdoc, 'getUrl').returns('https://foo.example/');
91+
stubFetch(
92+
'https://foo.example/foo.txt',
93+
{'Content-Type': 'text/javascript; charset=UTF-8'}, // Valid content-type.
94+
'alert(1)'
95+
);
96+
97+
xhr.fetchText
98+
.withArgs(env.sandbox.match(/amp-script-worker-0.1.js/))
99+
.rejects();
100+
xhr.fetchText
101+
.withArgs(env.sandbox.match(/amp-script-worker-nodom-0.1.js/))
102+
.resolves({text: () => Promise.resolve('/* noop */')});
103+
registerServiceBuilderForDoc(
104+
env.win.document,
105+
'amp-script',
106+
AmpScriptService
107+
);
108+
109+
await script.buildCallback();
110+
await script.layoutCallback();
111+
resetServiceForTesting(env.win, 'amp-script');
112+
});
113+
87114
it('should work with "text/javascript" content-type for same-origin src', () => {
88115
env.sandbox.stub(env.ampdoc, 'getUrl').returns('https://foo.example/');
89116
element.setAttribute('src', 'https://foo.example/foo.txt');

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"@ampproject/animations": "0.2.2",
1818
"@ampproject/toolbox-cache-url": "2.5.4",
1919
"@ampproject/viewer-messaging": "1.1.0",
20-
"@ampproject/worker-dom": "0.25.1",
20+
"@ampproject/worker-dom": "0.25.2",
2121
"dompurify": "2.0.7",
2222
"intersection-observer": "0.11.0",
2323
"moment": "2.24.0",
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!doctype html>
2+
<html allow-viewer-render-template>
3+
<head>
4+
<meta charset="utf-8">
5+
<script async src="https://cdn.ampproject.org/v0.js"></script>
6+
<script async custom-element="amp-list" src="https://cdn.ampproject.org/v0/amp-list-0.1.js"></script>
7+
<script async custom-element="amp-script" src="https://cdn.ampproject.org/v0/amp-script-0.1.js"></script>
8+
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
9+
<style amp4email-boilerplate>body{visibility:hidden}</style>
10+
</head>
11+
<body>
12+
13+
<amp-script id="fns" width="1" height="1" script="fruit-script" nodom data-ampdevmode></amp-script>
14+
<script id="fruit-script" type="text/plain" target="amp-script">
15+
exportFunction('getFruit', () => ({items: [{name: 'Pineapple'}, {name: 'Mango'}]}));
16+
</script>
17+
18+
<amp-list id="amp-list" width="auto" height="100" layout="fixed-height"
19+
src="amp-script:fns.getFruit">
20+
<template type="amp-mustache">
21+
<div class="fruit">{{name}}</div>
22+
</template>
23+
</amp-list>
24+
25+
</body>
26+
</html>

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@
7373
resolved "https://registry.yarnpkg.com/@ampproject/viewer-messaging/-/viewer-messaging-1.1.0.tgz#404f92c5754bac61014ed2272dd5e87174b9af84"
7474
integrity sha512-SoR1dGl2Pl8eJlyGCU/9Gz/orOggmW/13wUh7NnuGvDYqLdlhnRBzvEGqEAlq/fQKel9ZM6RNtu85Jw8WC3K2Q==
7575

76-
"@ampproject/[email protected].1":
77-
version "0.25.1"
78-
resolved "https://registry.yarnpkg.com/@ampproject/worker-dom/-/worker-dom-0.25.1.tgz#3f97441b94ee5d84f882f4f1262ca0c4888b11b6"
79-
integrity sha512-6qfHzg7YFNwcUr9a2iWWQp0ZTUlM+O4UUDFClM5O3ZZ28CooK4xdhOdLCOQr04zQ0td+6zd02rUip2JBcWx4aQ==
76+
"@ampproject/[email protected].2":
77+
version "0.25.2"
78+
resolved "https://registry.yarnpkg.com/@ampproject/worker-dom/-/worker-dom-0.25.2.tgz#910eed5b0f842ed139ab84719097297ca67fa23a"
79+
integrity sha512-l6H1vJ2AfvvkXiGH2a5HDT4ft+YDhnbtzd0GslIiRDYcmKJ6yiJXdicvpBWQu9PLapa59Q4hW0HMxEaYRZOHeg==
8080

8181
"@ava/babel-plugin-throws-helper@^4.0.0":
8282
version "4.0.0"

0 commit comments

Comments
 (0)