Tengo un problema inusual con un documento de IE con contentEditable establecido en verdadero. Al llamar a select() en un rango que se ubica al final de un nodo de texto que precede inmediatamente a un elemento de bloque, la selección se desplaza al carácter correcto y aparece donde no debería. He enviado un error a Microsoft contra IE8. Si puedes, vota por este problema para que se solucione.El método de selección IE TextRange no funciona correctamente
https://connect.microsoft.com/IE/feedback/ViewFeedback.aspx?FeedbackID=390995
He escrito un caso de prueba para demostrar el efecto:
<html>
<body>
<iframe id="editable">
<html>
<body>
<div id="test">
Click to the right of this line ->
<p id="par">Block Element</p>
</div>
</body>
</html>
</iframe>
<input id="mytrigger" type="button" value="Then Click here to Save and Restore" />
<script type="text/javascript">
window.onload = function() {
var iframe = document.getElementById('editable');
var doc = iframe.contentDocument || iframe.contentWindow.document;
// An IFRAME without a source points to a blank document. Here we'll
// copy the content we stored in between the IFRAME tags into that
// document. It's a hack to allow us to use only one HTML file for this
// test.
doc.body.innerHTML = iframe.textContent || iframe.innerHTML;
// Marke the IFRAME as an editable document
if (doc.body.contentEditable) {
doc.body.contentEditable = true;
} else {
var mydoc = doc;
doc.designMode = 'On';
}
// A function to demonstrate the bug.
var myhandler = function() {
// Step 1 Get the current selection
var selection = doc.selection || iframe.contentWindow.getSelection();
var range = selection.createRange ? selection.createRange() : selection.getRangeAt(0);
// Step 2 Restore the selection
if (range.select) {
range.select();
} else {
selection.removeAllRanges();
selection.addRange(range);
doc.body.focus();
}
}
// Set up the button to perform the test code.
var button = document.getElementById('mytrigger');
if (button.addEventListener) {
button.addEventListener('click', myhandler, false);
} else {
button.attachEvent('onclick', myhandler);
}
}
</script>
</body>
</html>
El problema se expone en la función myhandler. Esto es todo lo que estoy haciendo, no hay Paso 3 entre guardar y restaurar la selección, y sin embargo, el cursor se mueve. No parece suceder a menos que la selección esté vacía (es decir, tengo un cursor parpadeante, pero no texto), y solo parece suceder cuando el cursor está al final de un nodo de texto que precede inmediatamente a un nodo de bloque.
Parece que el rango sigue en la posición correcta (si llamo parentElement al rango devuelve el div), pero si obtengo un nuevo rango de la selección actual, el nuevo rango está dentro de la etiqueta de párrafo, y ese es su elemento principal.
¿Cómo puedo solucionar esto y siempre guardar y restaurar la selección en Internet Explorer?
Después de haber dedicado un tiempo a esto, estoy razonablemente convencido de que no hay forma de colocar programáticamente el cursor al final de un nodo de texto que precede inmediatamente a un elemento de bloque. –