From 9e862ca701454e2ab497a4dba01927e36d7985af Mon Sep 17 00:00:00 2001 From: Filip Stanis Date: Wed, 15 Jan 2020 16:19:33 +0000 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Add=20module=20/=20CJS=20a?= =?UTF-8?q?nd=20types=20to=20purifier=20npm=20package=20(#26297)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/purifier/README.md | 9 ++++-- src/purifier/package.json | 9 ++++-- src/purifier/purifier.d.ts | 40 ++++++++++++++++++++++++ src/purifier/rollup.config.js | 58 ++++++++++++++++++++++------------- 4 files changed, 89 insertions(+), 27 deletions(-) create mode 100644 src/purifier/purifier.d.ts diff --git a/src/purifier/README.md b/src/purifier/README.md index 1f29019f5197..0dc0bbe4ea81 100644 --- a/src/purifier/README.md +++ b/src/purifier/README.md @@ -31,10 +31,13 @@ import Mustache from 'mustache'; import {Purifier} from '@ampproject/purifier'; const purifier = new Purifier(document); -Mustache..setUnescapedSanitizer(value => - purifier.purifyTagsForTripleMustache(value) -); +const _unescapedValue = Mustache.Writer.prototype.unescapedValue; +Mustache.Writer.prototype.unescapedValue = function(token, context) { + const result = _unescapedValue(token, context); + return purifier.purifyTagsForTripleMustache(result); +}; const html = Mustache.render(template, data); + const body = purifier.purifyHtml(html); for (const child of body.children) { targetElement.appendChild(child); diff --git a/src/purifier/package.json b/src/purifier/package.json index 9bc7ad30cfed..dde225b2c98e 100644 --- a/src/purifier/package.json +++ b/src/purifier/package.json @@ -1,11 +1,13 @@ { "name": "@ampproject/purifier", - "version": "1.0.0", + "version": "1.0.2", "description": "AMP-specific sanitization library", "author": "The AMP HTML Authors", "license": "Apache-2.0", "main": "dist/purifier.js", - "files": ["dist/*"], + "module": "dist/purifier.mjs", + "types": "purifier.d.ts", + "files": ["dist/*", "purifier.d.ts"], "scripts": { "build": "rollup -c rollup.config.js" }, @@ -22,5 +24,8 @@ }, "dependencies": { "dompurify": "2.0.7" + }, + "optionalDependencies": { + "@types/dompurify": "2.0.1" } } diff --git a/src/purifier/purifier.d.ts b/src/purifier/purifier.d.ts new file mode 100644 index 000000000000..b539bb0a0bfc --- /dev/null +++ b/src/purifier/purifier.d.ts @@ -0,0 +1,40 @@ +/** + * Copyright 2020 The AMP HTML Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/// + +export type AttributeRewriterDef = ( + tagName: string, + attrName: string, + attrValue: string +) => string; + +export class Purifier { + constructor( + doc: Document, + opt_config?: DOMPurify.Config, + opt_attrRewrite?: AttributeRewriterDef + ); + + purifyHtml(dirty: string): HTMLElement; + purifyTagsForTripleMustache(dirty: string): string; + getAllowedTags(): {[key: string]: boolean}; + validateAttributeChange( + node: Node, + attr: string, + value: string | null + ): boolean; +} diff --git a/src/purifier/rollup.config.js b/src/purifier/rollup.config.js index 340bd7a6c994..560944a661c9 100644 --- a/src/purifier/rollup.config.js +++ b/src/purifier/rollup.config.js @@ -20,26 +20,40 @@ import alias from '@rollup/plugin-alias'; // eslint-disable-next-line no-undef const projectRootDir = path.resolve(__dirname); -export default { - input: 'purifier.js', - output: { - file: 'dist/purifier.js', - format: 'es', - sourcemap: true, +const ROLLUP_PLUGINS = [ + alias({ + entries: [ + { + find: /.*\/log$/, + replacement: path.resolve(projectRootDir, './noop.js'), + }, + { + find: /.*\/config$/, + replacement: path.resolve(projectRootDir, './noop.js'), + }, + ], + }), +]; + +export default [ + { + input: 'purifier.js', + output: { + file: 'dist/purifier.mjs', + format: 'es', + sourcemap: true, + }, + external: ['dompurify'], + plugins: ROLLUP_PLUGINS, + }, + { + input: 'purifier.js', + output: { + file: 'dist/purifier.js', + format: 'cjs', + sourcemap: true, + }, + external: ['dompurify'], + plugins: ROLLUP_PLUGINS, }, - external: ['dompurify'], - plugins: [ - alias({ - entries: [ - { - find: /.*\/log$/, - replacement: path.resolve(projectRootDir, './noop.js'), - }, - { - find: /.*\/config$/, - replacement: path.resolve(projectRootDir, './noop.js'), - }, - ], - }), - ], -}; +];