allBlogsList

Insert Sitecore Link Clears Out All Attributes

Currently in Sitecore 6.6.0 (rev. 130214) .  If you have a link with attributes like class or target in the <a tag, when you use the Insert Sitecore Link in the HTML editor to overwrite your current link your attributes are removed.  I have not tested this in other versions, but I have submitted it to Sitecore support and it has been recorded as a bug.   Along with this support pointed me in the direction of a work around. From the root of your site Navigate down to Sitecore\shell\Controls\Rich Text Editor\RichText Commands.js  .  Inside RichText Commands.js is a function called scInsertSitecoreLink which is what is used to insert the link.  In here you need to grab your previous attributes and pass them back through.  Here are my modifications:

function scInsertSitecoreLink(sender, returnValue) {
  if (!returnValue) {
    return;
  }

    var d = scEditor.getSelection().getParentElement();
    var cssClass = d.getAttribute("class");
    var target = d.getAttribute("target");
    var title = d.getAttribute("title");
    var attributes = "";
    if (cssClass != "" && cssClass != null) {
        attributes = " class=\"" + cssClass + "\"";
    }
    if (target != "" && target != null) {
        attributes += " target=\"" + target + "\"";
    }
    if (title != "" && title != null) {
        attributes += " title=\"" + title + "\"";
    }

  if ($telerik.isFirefox && d.tagName == "A") {
    d.parentNode.removeChild(d);
  } else {
    scEditor.fire("Unlink");
  }

  var text = scEditor.getSelectionHtml();

  if (text == "" || text == null || ((text != null) && (text.length == 15) && (text.substring(2, 15).toLowerCase() == "<p> </p>"))) {
      text = returnValue.text;
  }
  else {
    // if selected string is a full paragraph, we want to insert the link inside the paragraph, and not the other way around.
    var regex = /^[\s]*<p>(.+)<\/p>[\s]*$/i;
    var match = regex.exec(text);
    if (match && match.length >= 2) {
        scEditor.pasteHtml("<p><a href=\"" + returnValue.url + "\"" + attributes + ">" + match[1] + "</a></p>", "DocumentManager");
      return;
    }
  }

    scEditor.pasteHtml("<a href=\"" + returnValue.url + "\"" + attributes + ">" + text + "</a>", "DocumentManager");
}

Simply put, the standard script grabs the original element to parse in the text for the link.  You just piggy back on that to return any elements you want to go along with it.  I am pulling through class, target, and title.  Others could be added and appended to the attributes variable as needed.