android - 范围反序列化不适用于 webview android

标签 android serialization rangy

我正在尝试使用 webview 在我的一个 Android 项目中为 epub 阅读器制作“Highlighter”。 我正在使用 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>

有什么猜测吗??

最佳答案

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

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

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

在原始 DOM 上反序列化显然行不通。 尝试更改命令的顺序,以便您先序列化,然后才使用高亮显示。

正确顺序:

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

关于android - 范围反序列化不适用于 webview android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17009552/

相关文章:

java - 缺少 Android JRE 系统库 (java.awt.*)

javascript - Rangey:获取选定节点的新方法

javascript - 以特定偏移量将范围分成两部分

java - ibm 集成总线中具有相同类型和缓存的 ClassCastException

jQuery:获取表中的所有输入值并序列化

javascript - PHP 序列化失败并以半序列化格式存储

javascript - Firefox 中范围广泛、内容可编辑的插入符定位和焦点

android - Textview 边框和 drawableLeft 之间的填充

android - 在服务器上构建(不开发)您的Android应用.apk(渐变构建)?

java - Android 应用程序存储和搜索数据