gradio-pr-bot commited on
Commit
d139853
·
verified ·
1 Parent(s): b9f67db

Upload folder using huggingface_hub

Browse files
6.0.2/sanitize/browser.ts ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import Amuchina from "amuchina";
2
+
3
+ const is_external_url = (
4
+ link: string | null,
5
+ root = location.href
6
+ ): boolean => {
7
+ try {
8
+ return !!link && new URL(link).origin !== new URL(root).origin;
9
+ } catch (e) {
10
+ return false;
11
+ }
12
+ };
13
+
14
+ export function sanitize(source: string): string {
15
+ const amuchina = new Amuchina();
16
+ const node = new DOMParser().parseFromString(source, "text/html");
17
+ walk_nodes(node.body, "A", (node) => {
18
+ if (node instanceof HTMLElement && "target" in node) {
19
+ if (is_external_url(node.getAttribute("href"), location.href)) {
20
+ node.setAttribute("target", "_blank");
21
+ node.setAttribute("rel", "noopener noreferrer");
22
+ }
23
+ }
24
+ });
25
+
26
+ return amuchina.sanitize(node).body.innerHTML;
27
+ }
28
+
29
+ function walk_nodes(
30
+ node: Node | null | HTMLElement,
31
+ test: string | ((node: Node | HTMLElement) => boolean),
32
+ callback: (node: Node | HTMLElement) => void
33
+ ): void {
34
+ if (
35
+ node &&
36
+ ((typeof test === "string" && node.nodeName === test) ||
37
+ (typeof test === "function" && test(node)))
38
+ ) {
39
+ callback(node);
40
+ }
41
+ const children = node?.childNodes || [];
42
+ for (let i = 0; i < children.length; i++) {
43
+ // @ts-ignore
44
+ walk_nodes(children[i], test, callback);
45
+ }
46
+ }
6.0.2/sanitize/index.d.ts ADDED
@@ -0,0 +1 @@
 
 
1
+ export declare function sanitize(source: string): string;
6.0.2/sanitize/package.json ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/sanitize",
3
+ "version": "0.3.0",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "author": "",
7
+ "main": "./dist/server.js",
8
+ "license": "ISC",
9
+ "dependencies": {
10
+ "amuchina": "^1.0.12",
11
+ "sanitize-html": "^2.17.0"
12
+ },
13
+ "devDependencies": {
14
+ "@types/sanitize-html": "^2.16.0"
15
+ },
16
+ "main_changeset": true,
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/gradio-app/gradio.git",
20
+ "directory": "js/utils"
21
+ },
22
+ "exports": {
23
+ ".": {
24
+ "types": "./dist/index.d.ts",
25
+ "browser": {
26
+ "gradio": "./browser.ts",
27
+ "default": "./dist/browser.js"
28
+ },
29
+ "default": {
30
+ "gradio": "./server.ts",
31
+ "default": "./dist/server.js"
32
+ }
33
+ },
34
+ "./package.json": "./package.json"
35
+ },
36
+ "scripts": {
37
+ "package": "svelte-package --input=. --tsconfig=../../tsconfig.json"
38
+ }
39
+ }
6.0.2/sanitize/server.ts ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ import { default as sanitize_html_ } from "sanitize-html";
2
+
3
+ export function sanitize(source: string): string {
4
+ return sanitize_html_(source);
5
+ }