Skip to content

Commit

Permalink
♻️ Add module / CJS and types to purifier npm package (#26297)
Browse files Browse the repository at this point in the history
  • Loading branch information
fstanis authored Jan 15, 2020
1 parent bcb7470 commit 9e862ca
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 27 deletions.
9 changes: 6 additions & 3 deletions src/purifier/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions src/purifier/package.json
Original file line number Diff line number Diff line change
@@ -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"
},
Expand All @@ -22,5 +24,8 @@
},
"dependencies": {
"dompurify": "2.0.7"
},
"optionalDependencies": {
"@types/dompurify": "2.0.1"
}
}
40 changes: 40 additions & 0 deletions src/purifier/purifier.d.ts
Original file line number Diff line number Diff line change
@@ -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.
*/

/// <reference types="dompurify"/>

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;
}
58 changes: 36 additions & 22 deletions src/purifier/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
},
],
}),
],
};
];

0 comments on commit 9e862ca

Please sign in to comment.