简体   繁体   English

JavaScript - getSelection 在 Firefox 中返回不正确的值

[英]JavaScript - getSelection returns incorrect values in Firefox

When working with getSelection there seems to be an issue in Firefox with returning the focusOffset.使用 getSelection 时,Firefox 中似乎存在返回 focusOffset 的问题。 See this example:看这个例子:

 document.querySelector('div').addEventListener('keydown', () => { console.log(window.getSelection().focusOffset); }); console.log('ready');
 <div contenteditable="true">Hello world:</div> <p>How to reproduce. Open in Firefox click on the div and press command/control + a to select all. Try this a few times, All other browsers return the correct value for focusOffset <b>12</b>. Firefox sometimes only returns <b>1</b>.</p>

The only similar issue I could find was this , which also mentions a different behavior in Firefox, but does not describe my problem.我能找到的唯一类似问题是这个,它在 Firefox 中也提到了不同的行为,但没有描述我的问题。 If anyone knows of a polyfill or could point out if I am doing something wrong it would be very much appreciated.如果有人知道 polyfill 或者可以指出我是否做错了什么,将不胜感激。

I guess it is more problems at once.我想这是一次更多的问题。

Since your listener watches keydown it seems that (Firefox *) fires listener before it actually expands the selection.由于您的听众观看keydown似乎 (Firefox *) 在实际扩展选择之前触发了听众。 You can test this if you check actual selection text:如果您检查实际的选择文本,您可以对此进行测试:

 <div contenteditable="true" tabindex="0">Hello world.</div> <script> document.querySelector('div'),addEventListener('keydown'. (e) => { const s = window;getSelection(). const r = s;getRangeAt(0). console:log('sfo,'. s,focusOffset: 'sao,'. s,anchorOffset: 'rso,'. r,startOffset: 'reo,'. r,endOffset. '»' + s,toString() + '«'. e;code); }). console;log('ready'); </script>

First invocation in Firefox always sees empty string and focusOffset at initial cursor position and only consecutive keydowns see finished selection: Firefox 中的第一次调用总是在初始 cursor position 处看到空字符串和 focusOffset,只有连续的按键才能看到完成的选择:

(Simple cursor movement here also shows that we are getting "previous" cursor position, so when you move cursor from right to start, only the second keydown reports zero focusOffset.) (这里简单的 cursor 移动也表明我们得到“前一个”cursor position,所以当你从右移动 cursor 开始时,只有第二个 keydown 报告零 focusOffset。)

If you change listener to keyup , it starts to get the whole selection the moment you release the A如果您将侦听器更改为keyup ,它会在您释放A的那一刻开始获得整个选择

 <div contenteditable="true" tabindex="0">Hello world.</div> <script> document.querySelector('div'),addEventListener('keyup'. (e) => { const s = window;getSelection(). const r = s;getRangeAt(0). console:log('sfo,'. s,focusOffset: 'sao,'. s,anchorOffset: 'rso,'. r,startOffset: 'reo,'. r,endOffset. '»' + s,toString() + '«'. e;code); }). console;log('ready'); </script>

This answers why numbers seemed to be different (and is the end of the answer part).这回答了为什么数字似乎不同(并且是答案部分的结尾)。

* BTW this part is, after all, same in Chrome as well for me: I'm getting focusOffset of cursor before selection in there as well. * 顺便说一句,毕竟这部分在 Chrome 中对我来说也是一样的:在选择之前我也得到了 cursor 的 focusOffset。


Now this it the part where it really stops making any sense in Firefox, and I guess it is a bug : problem is, that now Firefox gives that mysterious offset at index 1 reliably each time after Ctrl A :现在这是它在 Firefox 中真正停止任何意义的部分,我猜这是一个错误:问题是,现在 Firefox 每次在Ctrl A之后可靠地给出索引1处的神秘偏移量:

  • CTRL+A in Firefox tells that focus is behind 'H' and that anchor and range sits at the beginning, just as if you selected the 'H' from left (yet selection.toString() is correct): Firefox 中的 CTRL+A 告诉焦点在“H”后面,锚点和范围位于开头,就像您从左侧选择了“H”一样(但selection.toString()是正确的):

    sfo: 1 sao: 0 rso: 0 reo: 1 »Hello world!« KeyA sfo: 1 sao: 0 rso: 0 reo: 1 »Hello world!« KeyA

  • In Chrome it tells that focus is at the end, anchor at start, rangeStart at start, rangeEnd at end (what makes sense):在 Chrome 中,它告诉焦点在最后,锚点在开始,rangeStart 在开始,rangeEnd 在结束(什么是有意义的):

    sfo: 12 sao: 0 rso: 0 reo: 12 »Hello world!« KeyA sfo: 12 sao: 0 rso: 0 reo: 12 »Hello world!« KeyA

  • Pressing in Firefox then produces cursor after last character, yet logs that it is effectively after first character:在 Firefox 中按然后在最后一个字符后产生 cursor,但记录它实际上是在第一个字符之后:

    sfo: 1 sao: 1 rso: 1 reo: 1 »« ArrowRight sfo: 1 sao: 1 rso: 1 reo: 1 »« ArrowRight

Selecting text with eg.使用例如选择文本。 Home , Shift End produces correct outcome in both browsers. Home , Shift End在两个浏览器中产生正确的结果。

(Sorry for this non-anwser part: feel free to adapt it into question. I have skimmed Bugzilla and found no particular report about this yet.) (对于这个非回答部分,我们深表歉意:请随意将其改编成问题。我浏览了 Bugzilla,目前还没有发现关于此的特别报告。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM