简体   繁体   English

Firefox显示插入符号位置错误?

[英]Firefox showing caret in wrong position?

I'm making an editor and I have a DOM structure like 我正在做一个编辑器,我有一个DOM结构,例如

<div id="container" contenteditable=true>
  <div id="one">Some fancy text</div>
  <div id="two">Some other text</div>
  <div id="three">and that's enough!</div>
</div>

In this editor, text written is inside the div 'container' which is a content editable div and then divided in several internal divs, changing while the user writes. 在此编辑器中,编写的文本位于div“容器”内部,该div是一个内容可编辑的div,然后分成多个内部div,并在用户书写时更改。 In my structure, programmatically, during the webpage life, a lot of divs get added, removed and moved inside 'container', even changing id. 在我的结构中,以编程方式,在网页生存期间,很多div都在“容器”中添加,删除和移动,甚至更改了id。 I need no extra div block being inside the main divs (the ones with the numeric ids) and so I force programmatically adding a br on pression of a return instead of wrapping text in divs like firefox would normally do. 我不需要在主div(带有数字ID的div)内放置额外的div块,因此我以编程方式强制在返回的压力上添加br,而不是像通常的firefox那样将文本包装在div中。

Due to a problem in Firefox, the caret (cursor) acts really weird and I can't solve the issue. 由于Firefox中的问题,插入符号(光标)的行为确实很奇怪,我无法解决问题。

The problem seems to be known. 这个问题似乎是已知的。 Firefox solves this putting extra divs in html around the text when a return is pressed. Firefox解决了此问题,当按下返回键时,会在文本周围的html中添加额外的div。

In my case, avoiding this, the first time you press a return at the end of the last div, the caret moves correctly (if you keep writing, it writes in the correct position on a new line), but displays in a strange position, like at the beginning of the previous line. 就我而言,为避免这种情况,您第一次在最后一个div末尾按回车键时,插入记号可以正确移动(如果继续书写,它将在新行上的正确位置书写),但显示的位置很奇怪,就像上一行的开头一样。 This happens only at the very end of the last div. 这仅发生在最后一个div的末尾。

It is shown here too, but proposed solution are not working for me Firefox sets wrong caret position contentEditable with :before 它也在此处显示,但是建议的解决方案对我不起作用Firefox设置了错误的插入符位置contentEditable with:before

Here you can see the problem showing itself: 在这里,您可以看到显示自身的问题:

 window.addEventListener('keydown', function (event) { if(event.keyCode === 13){ event.preventDefault(); // Ensure it is only this code that runs console.log("+++ Pressed return and adding a br!"); addHtmlElementAtCursor('br'); return false; } }); function keyPress(event) { } function addHtmlElementAtCursor(element) { var sel, range, textNode; if (window.getSelection) { sel = window.getSelection(); if (sel.getRangeAt && sel.rangeCount) { range = sel.getRangeAt(0); range.deleteContents(); if (element==' ') { textNode = document.createTextNode('\ '); }else{ textNode = document.createElement(element); } range.insertNode(textNode); // Move caret to the end of the newly inserted text node range.setStartAfter(textNode, textNode.length); range.setEndAfter(textNode, textNode.length); sel.removeAllRanges(); sel.addRange(range); } } else if (document.selection && document.selection.createRange) { range = document.selection.createRange(); range.pasteHTML(text); } } 
 <div id="container" contenteditable=true> <div id="one">some text</div> <div id="two">some other text</div> <div id="three">some other other text, try to give a return after the laste e of 'here' -> HERE</div> </div> 

I beleive this is still a know bug within Firefox. 我相信这仍然是Firefox中的已知错误。 See https://bugzilla.mozilla.org/show_bug.cgi?id=904846 .Even though the bug was created over 6 years ago, it was updated 8 months ago with the same "Open" status. 请参阅https://bugzilla.mozilla.org/show_bug.cgi?id=904846 。即使该错误是在6年前创建的,但仍在8个月前以“打开”状态进行了更新。 I would like for this open bug to get resolved as well. 我也希望这个开放的错误也得到解决。

似乎将此CSS规则添加到“容器”可以解决大多数情况下的问题:

white-space: pre-wrap;

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

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