Rangy反序列化在Android WebView上无法工作

3

我正在尝试为我的Android项目使用webview创建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>

有什么猜测吗?
1个回答

0

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

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

如果您在序列化之前运行 highlightSelection,它将无法正常工作。 这是因为高亮实际上是将文本包装在标签中,这意味着 DOM 被操作了。

在原始 DOM 上反序列化显然不会起作用。 尝试更改命令的顺序,先进行序列化,然后再使用高亮。

正确的顺序:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接