Estoy intentando agregar un atributo cuando uso un editor wysiwyg que usa el comando "createLink". Pensé que sería trivial recuperar el nodo que se creó después de que la búsqueda ejecute ese comando.Recuperar el nodo principal de la selección (rango) en Gecko y Webkit
Resulta que solo puedo capturar este nodo recién creado en IE. ¿Algunas ideas?
El código siguiente muestra el problema (los registros de depuración en la parte inferior muestran salida diferente en cada navegador):
var getSelectedHTML = function() {
if ($.browser.msie) {
return this.getRange().htmlText;
} else {
var elem = this.getRange().cloneContents();
return $("<p/>").append($(elem)).html();
}
};
var getSelection = function() {
if ($.browser.msie) {
return this.editor.selection;
} else {
return this.iframe[0].contentDocument.defaultView.getSelection();
}
};
var getRange = function() {
var s = this.getSelection();
return (s.getRangeAt) ? s.getRangeAt(0) : s.createRange();
};
var getSelectedNode = function() {
var range = this.getRange();
var parent = range.commonAncestorContainer ? range.commonAncestorContainer :
range.parentElement ? range.parentElement():
range.item(0);
return parent;
};
// **** INSIDE SOME EVENT HANDLER ****
if ($.browser.msie) {
this.ec("createLink", true);
} else {
this.ec("createLink", false, prompt("Link URL:", "http://"));
}
var linkNode = $(this.getSelectedNode());
linkNode.attr("rel", "external");
$.log(linkNode.get(0).tagName);
// Gecko: "body"
// IE: "a"
// Webkit: "undefined"
$.log(this.getSelectedHTML());
// Gecko: "<a href="http://site.com">foo</a>"
// IE: "<A href="http://site.com" rel=external>foo</A>"
// Webkit: "foo"
$.log(this.getSelection());
// Gecko: "foo"
// IE: [object Selection]
// Webkit: "foo"
Gracias por cualquier ayuda en esto, he rastreado preguntas relacionadas sobre SO sin éxito!
@ Jason - ¿Hay algo malo con mi respuesta? Todavía no he recibido ningún comentario tuyo ... – gnarf
Lo siento, tu ejemplo funciona, y el código es casi el mismo que el que tengo más compacto. Sin embargo, todavía no funciona para mi implementación, me pregunto si podría tener algo que ver con el iframe o el editor del navegador interfiriendo. Actualizaré cuando lo tenga completamente funcionando. ¡Gracias por la ayuda! – Jason