2010-01-25 117 views
8

目前我正在使用google-diff-match-patch来实现一个实时编辑工具,它可以同步多个用户之间的文本。当操作只是纯文本时,一切都很好,每个用户的操作(添加/删除文本)都可以通过与旧文本快照与google-diff的帮助进行比较来区分。但是当涉及丰富格式的文本(如粗体/斜体)时,比较htmlstring时,google-diff不能很好地工作。 <和>的字符出现会将差异结果搞乱,特别是当粗体/斜体格式相互嵌入时。是否有一个针对htmlstring的JS diff库,就像纯文本上的google-diff-match-patch一样?

任何人都可以提出一个类似的库,如google-diff来比较htmlstrings吗?或者有什么建议可以解决我的问题与谷歌差异?我明白谷歌差异是专为纯文本,但真的没有找到一个比它更好的库,所以它也可以工作,如果可行的增强谷歌diff可以提供帮助。

回答

0

看一看SynchroEdit,可能是有用的。

+0

Gamers2000,感谢您的评论。我曾尝试过SynchoEdit,但沙箱和开发版本都没有工作。顺便说一句,我也在你原来的“OT库问题”中提出一个问题,你是否也在使用google-diff-match-patc?你如何使用它丰富的格式htmlstrings?感谢您的任何意见。 – Steve 2010-01-27 02:17:10

+0

您好Steve,我正在使用diff-match-patch,但我正在使用它来同步纯文本。 另外,我实际上使用了MobWrite(http://code.google.com/p/google-mobwrite),它是一个diff-match-patch的实现。 对不起,我不能有太大的帮助! – gamers2000 2010-01-27 03:38:06

+0

感谢您的快速评论。 – Steve 2010-01-27 05:01:26

2

Pretty Diff可以处理所有你需要的东西,除非你需要更新DOM响应,以便在点击按钮时针对“onkeyup”事件启动diff。

http://prettydiff.com/

7

在谷歌的Diff-比赛贴片项目股份的一些想法的维基。从 http://code.google.com/p/google-diff-match-patch/wiki/Plaintext

One method is to strip the tags from the HTML using a simple regex or node-walker. Then diff the HTML content against the text content. Don't perform any diff cleanups. This diff enables one to map character positions from one version to the other (see the diff_xIndex function). After this, one can apply all the patches one wants against the plain text, then safely map the changes back to the HTML. The catch with this technique is that although text may be freely edited, HTML tags are immutable.

Another method is to walk the HTML and replace every opening and closing tag with a Unicode character. Check the Unicode spec for a range that is not in use. During the process, create a hash table of Unicode characters to the original tags. The result is a block of text which can be patched without fear of inserting text inside a tag or breaking the syntax of a tag. One just has to be careful when reconverting the content back to HTML that no closing tags are lost.

我有一种预感,第二个想法,地图HTML标签对Unicode的占位符,可能会更好地工作比一个原本想......特别是如果你的HTML标签是从一些缩小的集合,以及在显示交错(删除线/加下划线)diff标记时可以执行一点点打开/关闭修改。

另一种可能使用简单样式的方法是删除HTML标签,但记住受影响的字符索引。例如,“职位8-15是粗体”。然后,执行明文差异。最后,使用wiki第一种方法中的diff_xIndex位置映射思想,智能地重新插入HTML标签以重新应用样式到存活/添加的范围。 (也就是说,如果老位置8-13活了下来,但转移到20-25,插入周围还有在B标记。)

+0

这又如何:转义html字符(<, >,&),做所有的差异/补丁/合并工作和unescape结果。似乎是我最稳定的解决方案。 – ayke 2012-01-25 12:31:03

+1

我想你会发现这种方法会导致完全相同的输出而不是逃避它们。差异算法没有像任何其他字符一样处理它们的问题;问题在于保持它们的平衡,而逃避它们并不能解决这个问题。 – gojomo 2012-01-25 18:20:35

+2

我经历了这个,最终创建了一个包装库来帮助使用'diff_match_patch'所需的“演示工作”:https://github.com/arnab/jQuery.PrettyTextDiff – arnab 2013-01-24 09:42:54

相关问题