-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add code * Add code * add changeset * compatible lockfile * add code * add changeset * trigger-ci --------- Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com> Co-authored-by: pngwn <hello@pngwn.io>
- Loading branch information
1 parent
f118587
commit 7134fc2
Showing
11 changed files
with
122 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
"@gradio/atoms": patch | ||
"@gradio/markdown": patch | ||
"@gradio/sanitize": patch | ||
"@self/build": patch | ||
"gradio": patch | ||
--- | ||
|
||
fix:Custom component fixes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import Amuchina from "amuchina"; | ||
|
||
const is_external_url = (link: string | null, root: string): boolean => { | ||
try { | ||
return !!link && new URL(link).origin !== new URL(root).origin; | ||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
|
||
export function sanitize(source: string, root: string): string { | ||
const amuchina = new Amuchina(); | ||
const node = new DOMParser().parseFromString(source, "text/html"); | ||
walk_nodes(node.body, "A", (node) => { | ||
if (node instanceof HTMLElement && "target" in node) { | ||
if (is_external_url(node.getAttribute("href"), root)) { | ||
node.setAttribute("target", "_blank"); | ||
node.setAttribute("rel", "noopener noreferrer"); | ||
} | ||
} | ||
}); | ||
|
||
return amuchina.sanitize(node).body.innerHTML; | ||
} | ||
|
||
function walk_nodes( | ||
node: Node | null | HTMLElement, | ||
test: string | ((node: Node | HTMLElement) => boolean), | ||
callback: (node: Node | HTMLElement) => void | ||
): void { | ||
if ( | ||
node && | ||
((typeof test === "string" && node.nodeName === test) || | ||
(typeof test === "function" && test(node))) | ||
) { | ||
callback(node); | ||
} | ||
const children = node?.childNodes || []; | ||
for (let i = 0; i < children.length; i++) { | ||
// @ts-ignore | ||
walk_nodes(children[i], test, callback); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export declare function sanitize(source: string, root: string): string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "@gradio/sanitize", | ||
"version": "0.1.0", | ||
"description": "Gradio UI packages", | ||
"type": "module", | ||
"author": "", | ||
"main": "./dist/server.js", | ||
"license": "ISC", | ||
"dependencies": { | ||
"sanitize-html": "^2.13.0", | ||
"amuchina": "^1.0.12" | ||
}, | ||
"devDependencies": { | ||
"@types/sanitize-html": "^2.13.0" | ||
}, | ||
"main_changeset": true, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/gradio-app/gradio.git", | ||
"directory": "js/utils" | ||
}, | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"browser": "./dist/browser.js", | ||
"import": "./dist/server.js", | ||
"default": "./dist/server.js" | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"scripts": { | ||
"package": "svelte-package --input=. --cwd=../../.config/" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { default as sanitize_html_ } from "sanitize-html"; | ||
|
||
export function sanitize(source: string, root: string): string { | ||
return sanitize_html_(source); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.