diff --git a/docs/en/languages/python.mdx b/docs/en/languages/python.mdx index 36e44519a8..56f6941e23 100644 --- a/docs/en/languages/python.mdx +++ b/docs/en/languages/python.mdx @@ -95,7 +95,7 @@ The same page lists the installed packages of the selected interpreter. You can The **Python Packages** tool window (**View | Tool Windows**, or Find Action) manages the same packages next to the code. It lists what is installed and marks what can be upgraded. Type in its search field to filter the list; PyPI projects whose names contain the text and that are not installed appear under **PyPI**, the exact name first, then names that start with the text. Select a package to see its summary and its versions, then **Install** it (the newest release that fits the interpreter, or a version you pick), **Upgrade** or **Uninstall** it. The window manages the interpreter that runs use for the active file (in a workspace, its member's virtualenv) and shows its path. PyPI has no search API, so the search runs on a copy of PyPI's list of project names: the first search downloads it (about 10 MB) into the IDE's cache, and a search a day later refreshes it; searching the copy sends nothing. Until it is there, or when it cannot be downloaded, a project is still found by its exact name. Only pypi.org is searched, not a private index configured for pip, although installing does use pip's configuration. The exact-name lookup sends what you type to pypi.org; the name of an installed package is sent only when you click **Show PyPI Details**. When pip or uv is configured to install from another index, the exact-name lookup and the search results wait until you click **Search PyPI for …**, since a private package may have a public namesake. -When it has a package's details from PyPI, the pane also lists the project's links (its PyPI page, homepage, documentation, source, …) and its description from PyPI: rendered when it is Markdown, as plain text otherwise. Links open in the browser. +When it has a package's details from PyPI, the pane also lists the project's links (its PyPI page, homepage, documentation, source, …) and its description from PyPI: rendered when it is Markdown, as plain text otherwise. Links open in the browser. Images in the description (badges, screenshots) come from other sites such as GitHub, so they load, and those sites see the request, only when you click **Load Images**. The **+** button in its toolbar installs a package from elsewhere: **Install from Version Control…** takes a repository URL (`git+https://…`; a plain `https://`, `ssh://` or `git@host:path` URL is installed with Git), **Install from Disk…** a project directory or a wheel or source archive; a relative path is relative to the project, and a name alone is a folder, never a PyPI package. With **Install as editable (-e)**, a project directory is installed in editable mode, so changes to its sources apply without reinstalling. Only a local directory can be editable: to edit a repository's code, clone it and install the clone from disk. diff --git a/docs/zh/languages/python.mdx b/docs/zh/languages/python.mdx index 1054bdbc45..2d6e3194ae 100644 --- a/docs/zh/languages/python.mdx +++ b/docs/zh/languages/python.mdx @@ -95,7 +95,7 @@ Ruff 只为位于 Ruff 配置之下的文件启动:`ruff.toml`、`.ruff.toml` **Python 软件包** 工具窗口(**视图 | 工具窗口**,或通过查找操作打开)可以在代码旁边管理同一批软件包。它会列出已安装的软件包并标出可升级的。在搜索框中输入文本可筛选列表;名称包含该文本且尚未安装的 PyPI 项目会显示在 **PyPI** 分组下,名称完全相同的排在最前,其次是以该文本开头的名称。选中软件包可查看摘要和版本,然后 **安装**(适合该解释器的最新版本,或您选择的版本)、**升级** 或 **卸载**。该窗口管理的是运行当前文件时使用的解释器(在工作区中为其所属成员的虚拟环境),并显示其路径。PyPI 没有搜索接口,因此搜索在 PyPI 项目名称列表的本地副本上进行:第一次搜索时会把它下载到 IDE 的缓存中(约 10 MB),一天之后的搜索会刷新它;在本地副本中搜索不会发送任何内容。在列表下载完成之前,或者无法下载时,仍然可以按精确名称找到项目。只搜索 pypi.org,不会查询为 pip 配置的私有索引,但安装时仍使用 pip 的配置。按精确名称查询时,输入的文本会发送到 pypi.org;已安装软件包的名称只有在点击 **显示 PyPI 详情** 时才会发送。如果 pip 或 uv 配置了从其他索引安装,按精确名称查询和搜索结果都要等到点击 **在 PyPI 中搜索 …** 之后才会出现,因为私有软件包可能在 PyPI 上有同名的公开项目。 -取得 PyPI 上的软件包信息后,详情面板还会列出项目链接(PyPI 页面、主页、文档、源码等)和 PyPI 上的项目说明:Markdown 格式会渲染显示,其他格式按纯文本显示。链接在浏览器中打开。 +取得 PyPI 上的软件包信息后,详情面板还会列出项目链接(PyPI 页面、主页、文档、源码等)和 PyPI 上的项目说明:Markdown 格式会渲染显示,其他格式按纯文本显示。链接在浏览器中打开。说明中的图片(徽章、截图等)来自 GitHub 等其他网站,只有点击 **加载图片** 后才会加载,这些网站也只有这时才会收到请求。 工具栏中的 **+** 按钮可以从其他来源安装软件包:**从版本控制安装…** 接受仓库 URL(`git+https://…`;普通的 `https://`、`ssh://` 或 `git@host:path` URL 会按 Git 安装),**从磁盘安装…** 接受项目目录或 wheel、源码归档文件;相对路径相对于项目,只写一个名称时也按文件夹处理,不会当作 PyPI 软件包。勾选 **以可编辑模式安装 (-e)** 后,项目目录以可编辑模式安装,修改源码后无需重新安装即可生效。只有本地目录可以以可编辑模式安装:要修改仓库中的代码,请先克隆仓库,再从磁盘安装克隆的目录。 diff --git a/src/features/python-packages/PackagesView.test.tsx b/src/features/python-packages/PackagesView.test.tsx index 6a4687c99b..ac55aeea44 100644 --- a/src/features/python-packages/PackagesView.test.tsx +++ b/src/features/python-packages/PackagesView.test.tsx @@ -60,9 +60,13 @@ const SIX = { summary: "Python 2 and 3 compatibility utilities", latest: "1.17.0", versions: ["1.17.0", "1.16.0"], - description: "# Six\n\nRead the [documentation](https://six.readthedocs.io/).", + description: + "# Six\n\n![build](https://img.shields.io/six.svg)\n\nRead the [documentation](https://six.readthedocs.io/).", markdown: true, - links: [{ label: "Source", url: "https://github.com/benjaminp/six" }], + links: [ + { label: "Download", url: "https://pypi.org/project/six" }, + { label: "Source", url: "https://github.com/benjaminp/six" }, + ], }; function open(interpreter = "") { @@ -190,6 +194,12 @@ describe("Python Packages", () => { fireEvent.click(screen.getByRole("link", { name: "PyPI" })); fireEvent.click(screen.getByRole("link", { name: "documentation" })); expect(mocks.openUrl.mock.calls).toEqual([["https://pypi.org/project/six/"], ["https://six.readthedocs.io/"]]); + // Images load from their sites only when asked for. + const doc = screen.getByRole("document", { name: "Package description" }); + expect(doc.querySelector("img")).toBeNull(); + expect(doc).toHaveTextContent("build"); + fireEvent.click(screen.getByRole("button", { name: "Load Images" })); + expect(doc.querySelector("img")).toHaveAttribute("src", "https://img.shields.io/six.svg"); }); it("shows a description that is not Markdown as text", async () => { diff --git a/src/features/python-packages/PackagesView.tsx b/src/features/python-packages/PackagesView.tsx index ead1193b70..ce28a69e6b 100644 --- a/src/features/python-packages/PackagesView.tsx +++ b/src/features/python-packages/PackagesView.tsx @@ -40,6 +40,8 @@ import { requirementOf, rowKey, vcsRequirement, + withoutMedia, + isPypiPage, type Row, } from "./model"; import styles from "./PythonPackages.module.css"; @@ -704,11 +706,17 @@ function openLink(e: MouseEvent) { void openExternalUrl(a.getAttribute("href") ?? ""); } -/** The project's links and its description from PyPI: rendered when Markdown, else as text. */ +/** + * The project's links and its description from PyPI: rendered when Markdown, else as text. Its + * images come from other sites (badges, GitHub), so they load only when asked for. + */ function Docs({ found }: { found: PypiPackage }) { const tx = useTx(); + const [media, setMedia] = useState(false); const html = useMarkupHtml({ kind: "markdown", value: found.markdown ? found.description : "" }); - const links = [{ label: "PyPI", url: `https://pypi.org/project/${found.name}/` }, ...found.links]; + const shown = useMemo(() => (media ? { html, count: 0 } : withoutMedia(html)), [media, html]); + const pypi = `https://pypi.org/project/${found.name}/`; + const links = [{ label: "PyPI", url: pypi }, ...found.links.filter((l) => !isPypiPage(l.url, found.name))]; return ( <> + {shown.count > 0 && ( +
+ + Images in the description load from other sites. + {" "} + +
+ )} {found.description.trim() && (found.markdown ? (
) : (
diff --git a/src/features/python-packages/model.test.ts b/src/features/python-packages/model.test.ts
index c3efb48ec2..4a2b9a8b84 100644
--- a/src/features/python-packages/model.test.ts
+++ b/src/features/python-packages/model.test.ts
@@ -9,6 +9,8 @@ import {
   diskRequirement,
   isArchive,
   isVcsUrl,
+  withoutMedia,
+  isPypiPage,
 } from "./model";
 
 const INSTALLED = [
@@ -130,3 +132,44 @@ describe("install from version control or disk", () => {
     expect(isArchive(" dist/demo-1.0-py3-none-any.whl ")).toBe(true);
   });
 });
+
+describe("withoutMedia", () => {
+  it("takes out what would load from other sites, keeping an image's alt text", () => {
+    const { html, count } = withoutMedia(
+      '

build ok

', + ); + expect(html).toBe("

build ok

"); + expect(count).toBe(3); + expect(withoutMedia("

text

")).toEqual({ html: "

text

", count: 0 }); + }); + + it("holds back what the sanitizer lets through: image inputs and background attributes", async () => { + const { markdownToSafeHtml } = await import("@/editor/lsp/markdownParser"); + const safe = markdownToSafeHtml( + '
c
', + ); + expect(safe).toContain("https://t/"); + const { html, count } = withoutMedia(safe); + expect(html).not.toContain("https://t/"); + expect(count).toBe(2); + }); +}); + +describe("isPypiPage", () => { + it("knows a project's PyPI page however it is spelled", () => { + for (const url of [ + "https://pypi.org/project/zope.interface/", + "http://pypi.org/project/zope-interface", + "https://www.pypi.org/project/Zope_Interface/", + ]) + expect(isPypiPage(url, "zope.interface")).toBe(true); + for (const url of [ + "https://pypi.org/project/zope.interface/#files", + "https://pypi.org/project/zope.interface/1.0/", + "https://pypi.org/project/zope.schema/", + "https://github.com/zopefoundation/zope.interface", + "not a url", + ]) + expect(isPypiPage(url, "zope.interface")).toBe(false); + }); +}); diff --git a/src/features/python-packages/model.ts b/src/features/python-packages/model.ts index 80bb3244b5..6161e0d235 100644 --- a/src/features/python-packages/model.ts +++ b/src/features/python-packages/model.ts @@ -86,3 +86,46 @@ export function diskRequirement(path: string) { /** A wheel or source archive, which pip can install but not in editable mode. */ export const isArchive = (path: string) => /\.(whl|zip|tar\.gz|tgz|tar\.bz2|tar\.xz)$/i.test(path.trim()); + +/** + * `html` with its images, videos and audio taken out, and any other element's `src`-like + * attributes, so nothing loads from other sites: an image becomes its alt text. `count` is how + * many there were. + */ +export function withoutMedia(html: string): { html: string; count: number } { + const template = document.createElement("template"); + template.innerHTML = html; + let count = 0; + for (const el of template.content.querySelectorAll("img, video, audio, source, picture")) { + if (el.localName === "picture") el.replaceWith(...el.childNodes); + else if (el.localName === "source") el.remove(); + else { + count++; + el.replaceWith(el.getAttribute("alt") ?? ""); + } + } + // Anything else that would fetch: ``, ``. + for (const el of template.content.querySelectorAll("[src], [srcset], [background], [poster]")) { + count++; + for (const name of ["src", "srcset", "background", "poster"]) el.removeAttribute(name); + } + return { html: template.innerHTML, count }; +} + +/** Whether `url` is the PyPI page of project `name`, however it is spelled (scheme, slash, PEP 503). */ +export function isPypiPage(url: string, name: string) { + let u: URL; + try { + u = new URL(url); + } catch { + return false; + } + const project = /^\/project\/([^/]+)\/?$/.exec(u.pathname)?.[1]; + return ( + /^(www\.)?pypi\.org$/i.test(u.hostname) && + !u.search && + !u.hash && + !!project && + normalize(decodeURIComponent(project)) === normalize(name) + ); +} diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index ba906e6387..d22e03b192 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -4233,6 +4233,8 @@ "Show PyPI Details": "Show PyPI Details", "Project links": "Project links", "Package description": "Package description", + "Images in the description load from other sites.": "Images in the description load from other sites.", + "Load Images": "Load Images", "PyPI": "PyPI", "Search PyPI for {name}": "Search PyPI for {name}", "Installing uses another package index, so names are not sent to PyPI on their own.": "Installing uses another package index, so names are not sent to PyPI on their own.", diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 8093c20e92..212524b1df 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -10769,6 +10769,8 @@ "Show PyPI Details": "显示 PyPI 详情", "Project links": "项目链接", "Package description": "包说明", + "Images in the description load from other sites.": "说明中的图片来自其他网站。", + "Load Images": "加载图片", "PyPI": "PyPI", "Search PyPI for {name}": "在 PyPI 中搜索 {name}", "Installing uses another package index, so names are not sent to PyPI on their own.": "安装使用的是另一个软件包索引,因此不会自动把名称发送到 PyPI。",