gradio-pr-bot commited on
Commit
e1f579a
·
verified ·
1 Parent(s): 39eb7dd

Upload folder using huggingface_hub

Browse files
6.25.0/sanitize/browser.ts ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import Amuchina from "amuchina";
2
+
3
+ const should_open_link_in_new_tab = (link: string | null): boolean => {
4
+ const href = link?.trim();
5
+ return !!href && !href.startsWith("#");
6
+ };
7
+
8
+ // <style> elements and stylesheet <link>s apply to the whole document once
9
+ // injected, so drop them, matching the server-side sanitize-html defaults.
10
+ const configuration = Amuchina.getDefaultConfiguration();
11
+ configuration.allowElements = (configuration.allowElements ?? []).filter(
12
+ (element: string) =>
13
+ element !== "style" && element !== "svg:style" && element !== "link"
14
+ );
15
+ const amuchina = new Amuchina(configuration);
16
+
17
+ export function sanitize(source: string): string {
18
+ const node = new DOMParser().parseFromString(source, "text/html");
19
+ const sanitized_node = amuchina.sanitize(node);
20
+ walk_nodes(sanitized_node.body, "A", (node) => {
21
+ if (node instanceof HTMLElement && "target" in node) {
22
+ if (should_open_link_in_new_tab(node.getAttribute("href"))) {
23
+ node.setAttribute("target", "_blank");
24
+ node.setAttribute("rel", "noopener noreferrer");
25
+ }
26
+ }
27
+ });
28
+
29
+ return sanitized_node.body.innerHTML;
30
+ }
31
+
32
+ function walk_nodes(
33
+ node: Node | null | HTMLElement,
34
+ test: string | ((node: Node | HTMLElement) => boolean),
35
+ callback: (node: Node | HTMLElement) => void
36
+ ): void {
37
+ if (
38
+ node &&
39
+ ((typeof test === "string" && node.nodeName === test) ||
40
+ (typeof test === "function" && test(node)))
41
+ ) {
42
+ callback(node);
43
+ }
44
+ const children = node?.childNodes || [];
45
+ for (let i = 0; i < children.length; i++) {
46
+ // @ts-ignore
47
+ walk_nodes(children[i], test, callback);
48
+ }
49
+ }
6.25.0/sanitize/index.d.ts ADDED
@@ -0,0 +1 @@
 
 
1
+ export declare function sanitize(source: string): string;
6.25.0/sanitize/package.json ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/sanitize",
3
+ "version": "0.4.1",
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.25.0/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
+ }