如何使用webkit浏览器选择范围内的节点? [英] How to select a node in a range with webkit browsers?

查看:165
本文介绍了如何使用webkit浏览器选择范围内的节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究WYSIWYG解决方案,并且需要一个函数来正确选择一个节点范围(我的意思是节点本身,不仅仅是它的内容)

I'm currently working on a WYSIWYG solution and need a function to correctly select with a range a node (by this I mean the node itself, not only it's content)

DOM lvl2显然具有此功能 w3.org/ranges 并定义

DOM lvl2 clearly have a function for this w3.org/ranges and define that

range.selectNodeContents() //选择节点内的内容



range.selectNode()//选择一个节点及其内容

问题是当selectNode()完全适用于Firefox甚至ie9时,似乎无法使用任何Webkit浏览器实现(在最后一个Chrome和Safari)。在Webkit上,selectNodeContents()和selectNode()只选择节点内容(根据规格,我认为这是一个错误)

The problem is that when selectNode() perfectly work with Firefox or even ie9, it seem to be impossible to achieve with any Webkit browser (at last Chrome and Safari). On Webkit both selectNodeContents() and selectNode() select the node content only (which according to the specs seems to me to be a bug)

为了使它工作,我试过使用

模拟selectNode()目标 range.setStartBefore(node);

range.setEndAfter(node);


但结果仍然相同,它可以在任何地方工作(当然> 8)但不在webkit浏览器上..

To make it work I tried to emulate the selectNode() goal by using
range.setStartBefore(node);
range.setEndAfter(node);

but the result is still the same, it work everywhere (ie>8 of course) but not on webkit browsers..

如果你想尝试一下,我做了一个小小的jsfiddle你可以玩这里(只需打开控制台,然后查看webkit上的console.warn结果:/)

If you want to try it I made a little jsfiddle with which you can play here (just open the console and look at the console.warn result which is not the same on webkit :/ )

我搜索了很多,但我不能找到一种在webkit浏览器上实际选择完整节点的方法。
通过奇迹你们中的一些人会知道这样做的方法,甚至只是任何提示继续我的任务吗? :/

I searched a lot but I can't find a way to actually select a full node on webkit browsers. By miracle would some of you know a way to do it or even just any tip to continue my quest ? :/

ps:我知道rangylib但是我不能在我的项目中使用它,从我在源代码中看到的,我甚至都不是确定他们无论如何处理那件事:/

ps : I know "rangy" lib but I can't use it in my project and ,from what I read in the source, I'm not even sure they deal with that thing anyway :/

推荐答案

问题不在于 selectNode(),在WebKit中工作得很好,但是使用WebKit的选择对象,其选择范围以适应其自身(并非总是不合理)的选择边界和插入位置允许的位置。使用WebKit存在针对此的错误:

The problem is not with selectNode(), which works fine in WebKit, but with WebKit's selection object, which mangles ranges to fit in with its own (not always unreasonable) idea of where selection boundaries and caret positions are allowed to be. There are bugs filed against this with WebKit:

  • https://bugs.webkit.org/show_bug.cgi?id=23189
  • https://bugs.webkit.org/show_bug.cgi?id=15256

你是对的,Rangy没有处理这个问题,而是倾向于反映浏览器原生选择的现实。假装选择不同会很快导致奇怪的行为。

You're right, Rangy does not deal with this, preferring instead to reflect the reality of the browser's native selection. Pretending the selection is different would quickly lead to odd behaviour.

这篇关于如何使用webkit浏览器选择范围内的节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆