From 28aae733d08a62a06003e1a390452c026c61a955 Mon Sep 17 00:00:00 2001 From: William Chou Date: Fri, 19 Oct 2018 12:11:09 -0400 Subject: [PATCH] amp-script: Use AMP purifier config and hooks (#18789) * Hook up AMP purify config/hooks to amp-script. * Upgrade to worker-dom 0.1.4. * Fix types. * Fix hello-world example. * Fix dep-check. * Use '.patched' suffix to fix SP issue. Such a long name. --- build-system/dep-check-config.js | 3 +- build-system/tasks/compile.js | 3 +- build-system/tasks/update-packages.js | 13 +++--- examples/amp-script/hello-world.amp.html | 12 ++++-- examples/amp-script/hello-world.html | 8 ++++ examples/amp-script/hello-world.js | 43 +++++++++++++----- extensions/amp-script/0.1/amp-script.js | 27 ++++++++++-- package.json | 2 +- src/purifier.js | 55 +++++++++++++++++------- yarn.lock | 8 ++-- 10 files changed, 127 insertions(+), 47 deletions(-) create mode 100644 examples/amp-script/hello-world.html diff --git a/build-system/dep-check-config.js b/build-system/dep-check-config.js index f9c9a8d67b8b..cb5081bf56ba 100644 --- a/build-system/dep-check-config.js +++ b/build-system/dep-check-config.js @@ -59,8 +59,9 @@ exports.rules = [ mustNotDependOn: 'src/purifier.js', whitelist: [ 'src/sanitizer.js->src/purifier.js', - 'extensions/amp-mustache/0.2/amp-mustache.js->src/purifier.js', 'extensions/amp-bind/0.1/bind-impl.js->src/purifier.js', + 'extensions/amp-mustache/0.2/amp-mustache.js->src/purifier.js', + 'extensions/amp-script/0.1/amp-script.js->src/purifier.js', ], }, { diff --git a/build-system/tasks/compile.js b/build-system/tasks/compile.js index b27b6d751b36..c4e9466e320d 100644 --- a/build-system/tasks/compile.js +++ b/build-system/tasks/compile.js @@ -256,7 +256,8 @@ function compile(entryModuleFilenames, outputDir, outputFilename, options) { 'node_modules/set-dom/src/**/*.js', 'node_modules/web-animations-js/web-animations.install.js', 'node_modules/web-activities/activity-ports.js', - 'node_modules/@ampproject/worker-dom/dist/**/*.js', + 'node_modules/@ampproject/worker-dom/dist/' + + 'unminified.index.safe.mjs.patched.js', 'node_modules/document-register-element/build/' + 'document-register-element.patched.js', // 'node_modules/core-js/modules/**.js', diff --git a/build-system/tasks/update-packages.js b/build-system/tasks/update-packages.js index dd3c85772f1e..248af63a0bb8 100644 --- a/build-system/tasks/update-packages.js +++ b/build-system/tasks/update-packages.js @@ -110,15 +110,14 @@ function patchRegisterElement() { } /** - * Patch @ampproject/worker-dom/dist/index.safe.js with ES6 export. + * Closure Compiler doesn't recognize .mjs extension yet, so copy the file to + * have a .js file extension. */ function patchWorkerDom() { - replaceInFile( - 'node_modules/@ampproject/worker-dom/dist/index.safe.js', - 'node_modules/@ampproject/worker-dom/dist/index.safe.patched.js', - // Replace local var with an ES6 export. - 'var MainThread=function(R){', - 'export const MainThread=function(R){'); + const dir = 'node_modules/@ampproject/worker-dom/dist/'; + fs.copyFileSync( + dir + 'unminified.index.safe.mjs', + dir + 'unminified.index.safe.mjs.patched.js'); } /** diff --git a/examples/amp-script/hello-world.amp.html b/examples/amp-script/hello-world.amp.html index 40a120759c21..425ffd4ff18f 100644 --- a/examples/amp-script/hello-world.amp.html +++ b/examples/amp-script/hello-world.amp.html @@ -4,16 +4,20 @@ + - + + - -
-
+
diff --git a/examples/amp-script/hello-world.html b/examples/amp-script/hello-world.html new file mode 100644 index 000000000000..1247fda690bd --- /dev/null +++ b/examples/amp-script/hello-world.html @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/examples/amp-script/hello-world.js b/examples/amp-script/hello-world.js index b96f16075de1..a770bae44f8b 100644 --- a/examples/amp-script/hello-world.js +++ b/examples/amp-script/hello-world.js @@ -15,17 +15,40 @@ */ const root = document.createElement('div'); -const btn = document.createElement('button'); -const text = document.createTextNode('Insert Hello World!'); - root.className = "root"; -btn.appendChild(text); -root.appendChild(btn); +document.body.appendChild(root); + +function createButton(label, onClick) { + const btn = document.createElement('button'); + const text = document.createTextNode(label); + btn.appendChild(text); + btn.addEventListener('click', onClick); + root.appendChild(btn); +} + +createButton('Insert Hello World!', () => { + const el = document.createElement('h1'); + el.textContent = 'Hello World!'; + document.body.appendChild(el); +}); -btn.addEventListener('click', () => { - const h1 = document.createElement('h1'); - h1.textContent = 'Hello World!' - document.body.appendChild(h1); +// should be allowed. +createButton('Insert amp-img', () => { + const el = document.createElement('amp-img'); + el.setAttribute('width', '300'); + el.setAttribute('height', '200'); + el.setAttribute('src', '/examples/img/hero@1x.jpg') + document.body.appendChild(el); }); -document.body.appendChild(root); \ No newline at end of file +//