有没有办法让 ruby​​ 文本转换为 HTML 文本,以用于索引目的?或者如何准确获取 HTML 中文本的索引?

发布于 2024-12-14 12:31:44 字数 768 浏览 3 评论 0原文

在此处输入图像描述

在上图中,我选择了从索引 140 开始的短语“macro up myself”,并在索引 155 处结束。
(索引是通过父元素(包含所有文本的 div)的 .outerHTML 计算的)

在此处输入图像描述 现在,在第二张图片中,您可以看到跨度(在 HTML 屏幕截图中创建浅蓝色突出显示的部分)未放置在应有的位置。另外,记下左上角的数字。开始索引是相同的,结束索引只是第一张图片的结束索引+

长度获取索引: 从 javascript 方面:(如第一张图片所示)

   start_index = parent_element.html().indexOf(selection[0].outerHTML) - 33; // already have a large arbitrary offset, but I'd prefer to know why the indexes aren't lined up.
   end_index = start_index + html.length;

这些索引被传递到 Rails 服务器,它应该在文本中插入 Span,但索引与 HTML 中 Span 突出显示的位置不匹配。

所以我的问题是:如何获得准确的索引?

enter image description here

In the above image, I've selected the phrase 'macro up myself' which starts at index 140, and ends at index 155.
(Indexes are calculated via .outerHTML of the parent element (the div holding all the text))

enter image description here
Now, here in the second image, you can see that the span (the part that creates the light blue highlight in the HTML screenshot) isn't placed where it should be. Also, make note of the numbers in teh top left. The start index is the same, and the end index is just the end index from the first picture + the length of <span class="cha... ...50">

How I get the indexes:
From the javascript side: (like in the first picture)

   start_index = parent_element.html().indexOf(selection[0].outerHTML) - 33; // already have a large arbitrary offset, but I'd prefer to know why the indexes aren't lined up.
   end_index = start_index + html.length;

These indexes are passed along to the rails server, where it should insert spans into the text, but the indexes don't match the location of the span highlight in the HTML.

So my question is: how do I get an accurate index?

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

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

发布评论

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

评论(1

千纸鹤带着心事 2024-12-21 12:31:44

也许您需要一个更清晰的文本版本(看起来有空格)。类似于:

start_index = $.trim(parent_element.text())

在 ruby​​ 方面,您可能需要执行相同的操作以确保其中任何一个都没有空格。另外,您的输出显示了一些 html 实体 ('),因此在 ruby​​ 代码中,您可能需要确保正在使用字符串 before 特殊字符被 html 编码。

Maybe you need a cleaner version of the text to work with (you've got white space in there, it looks like). Something like:

start_index = $.trim(parent_element.text())

On the ruby side you may need to do the same thing to make sure you have no whitespace on either one. Also your output shows some html entities ('), so in the ruby code you might need to make sure you're working with the indexes of the string before the special characters get html-encoded.

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