3

我正在尝试在我的一个使用 webview 的 Android 项目中为 epub 阅读器制作“荧光笔”。我正在使用 Rangy 获取选定的文本。从下面的示例 HTML 中选择文本后,序列化函数给了我这个值:

2/5/3/5/1/2:0,13/5/3/5/1/2:24

我将其存储在数据库中。当用户返回此页面时,我正在检索相同的选择并尝试反序列化,但反序列化函数会引发以下错误:

Error in Rangy Serializer module: deserializePosition failed: node <DIV>[7] has no child with index 13, 0

我明白为什么会发生这种情况??即使我正在尝试使用 XPath 做同样的事情,但仍然是同样的问题。

<html>
<head>
<script></script>
</head>
<body>
<div id="mainpage" class="highlighter-context">
<div>       Some text here also....... </div>
<div>      Some text here also.........</div>
<div>
  <h1 class="heading"></h1>
  <div class="left_side">
<ol></ol>
<h1></h1>
<div class="text_bio">
In human beings, height, colour of eyes, complexion, chin, etc. are 
some recognisable features. A feature that can be recognised is known as 
character or trait. Human beings reproduce through sexual reproduction. In this                
process, two individuals one male and another female are involved. Male produces   
male gamete or sperm and female produces female gamete or ovum. These gametes fuse 
to form zygote which develops into a new young one which resembles to their parent. 
During the process of sexual reproduction 
</div>
  </div>
  <div class="righ_side">
  Some text here also.........
  </div>
  <div class="clr">
    Some text here also.......
  </div>
</div>
</div>
</body>
</html>

有什么猜测吗??

4

1 回答 1

0

您可能正在使用以下内容:

highlighter.highlightSelection();
rangy.serializeSelection();  

如果您highlightSelection在序列化之前运行它将不起作用。这是因为突出显示实际上是将文本包装在标签中,这意味着对 DOM 进行了操作。

对原始 DOM 进行反序列化显然是行不通的。尝试更改命令的顺序,以便您首先序列化,然后才使用突出显示。

正确的顺序:

rangy.serializeSelection();    
highlighter.highlightSelection();
于 2018-05-20T19:31:44.567 回答