从html< pre>中复制Web浏览器中的矩形选择。元素具有许多嵌套水平的< span>后代?

发布于 2025-01-18 13:48:23 字数 1501 浏览 0 评论 0 原文

我有 HTML

 元素,其中包含 160 列 x 62 行文本;终端屏幕截图,设置为等宽字体。文本由各种  元素标记;例如,设置文本颜色、背景颜色、字体粗细、下划线;还有封装有关内容的元数据的类名,
不一定显示属性。  元素可以嵌套到任意级别。

我想要原始 160x62

 元素的“裁剪”版本;原始矩形选区,保留  标记。

我不想将矩形选择捕获为位图屏幕截图。我想将内容保留为 HTML。

在网络浏览器中,我想在原始 160x62

 元素中选取矩形文本区域,然后获取一个 
 元素表示“裁剪”区域(例如,30x30;无论选区的尺寸是什么)。

裁剪后的

 必须保留原始  元素>

对于所选矩形区域的每一行,创建裁剪的

 的软件必须识别哪个 元素在裁剪区域左边缘的原始 
 中打开,并启动适当的  元素集来重现该“上下文” 。

理想情况下,有一个我忽略的现有软件可以做到这一点,并且这个问题的答案只是该软件的链接(例如浏览器内的 JavaScript)。

否则,在 Web 浏览器中显示的

 元素中执行逐字符矩形文本选择的好技术是什么,它提供了开始和结束坐标(列号和行号) ) 
 中的选择?

我见过一些浏览器扩展可以突出显示网络浏览器中的矩形选区,但没有一个能够实现我在​​此描述的功能。

我很乐意编写探索 DOM 的 JavaScript。给定矩形选择的基于字符的坐标(原始

 内的行号和列号),我认为我可以编写代码来创建裁剪后的 
 code>,在选择的每一行上开始和结束必要的“活动”/先行  元素。不过,我不太确定如何对 
 内容执行逐字符选取框选择。我更愿意被告知“已经完成了,在这里”。欢迎咨询。

I have HTML <pre> elements containing 160 columns by 62 rows of text; terminal screen captures, set in a monospace font. The text is marked-up by various <span> elements; for example, to set text color, background color, font weight, underlining; and also class names that encapsulate meta data about the content,
not necessarily display attributes. The <span> elements can be nested to arbitrary levels.

I want a "cropped" version of that original 160x62 <pre> element; a rectangular selection from the original, retaining the <span> markup.

I don't want to capture the rectangular selection as a bitmapped screenshot. I want to keep the content as HTML.

In a web browser, I want to marquee-select a rectangular area of text in the original 160x62 <pre> element, and then get a <pre> element that represents that "crop" area (for example, 30x30; whatever the dimensions of the selection might be).

The cropped <pre> must preserve all of the formatting, all of the "active" <span> elements, of that area in the original <pre>.

For each row of the selected rectangular area, the software that creates the cropped <pre> must recognize which <span> elements are open in the original <pre> at the left edge of the cropped area, and start the appropriate set of <span> elements to reproduce that "context".

Ideally, there is existing software to do this that I have overlooked, and the answer to this question is simply a link to that software (e.g. in-browser JavaScript).

Otherwise, what's a good technique for performing a character-by-character rectangular selection of text in a <pre> element displayed in a web browser, that provides the start and end coordinates (column and row numbers) of the selection in the <pre>?

I've seen browser extensions that highlight a rectangular selection in a web browser, but none that do what I've described here.

I'm comfortable writing JavaScript that explores the DOM. Given character-based coordinates of a rectangular selection (row and column numbers inside the original <pre>), I reckon I could write the code to create the cropped <pre>, starting and ending the necessary "active"/antecedent <span> elements on each row of the selection. I'm less sure how to perform the character-by-character marquee selection of the <pre> contents, though. I'd prefer to be told "It's been done, here". Advice welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半葬歌 2025-01-25 13:48:23

解决方法:将 HTML

 嵌入 SVG 

如果这对其他人有用:我开发了一种实现裁剪视图的解决方法而不实际裁剪

 标记。不涉及代码库。只是标记。

我承认,严格来说,这并不是对我原来问题的直接回答。但达到了我想要的效果。

解决方法:将 HTML

 元素嵌入到  元素内的 SVG  元素中。调整 SVG viewBox 以仅显示您想要的区域。

我省略了很多细节(例如,根据行数和列数以及字体长宽比等详细信息计算 viewBox 值),但这就是基本思想。

Workaround: embed the HTML <pre> in an SVG <foreignObject>

In case this is useful to anyone else: I've developed a workaround that achieves a cropped view without actually cropping the <pre> markup. No code libraries involved. Just markup.

I acknowledge that, strictly speaking, this is not a direct answer to my original question. But it achieves the effect I want.

The workaround: embed the HTML <pre> element in an SVG <foreignObject> element inside an <svg> element. Adjust the SVG viewBox to show only the area that you want.

I've elided a lot of detail (e.g. calculating the viewBox values based on details such as number of rows and columns, and the font aspect ratio), but that's the basic idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文