Highlighting the current context element

We received a request from a customer who wanted to highlight the current context element, i.e. the element where the cursor is currently in. As we already highlight elements when you hover over menu items, it took little effort to make this functionality available in the API. Here is the script that you can use:

var highlightNode;
Editor.addEventListener("xmlContextChange", function(evt)
{
  if (highlightNode)
    highlightNode.unhighlight(); highlightNode = evt.newNode;
  if (!highlightNode)
    return;
   
  if (highlightNode.getNodeType() == 2)
    highlightNode = highlightNode.getOwnerElement();
  else if (highlightNode.getNodeType() != 1)
    highlightNode = highlightNode.getParentNode();
 
  highlightNode.highlight();
});

There are 2 new methods on XML nodes: highlight and unhighlight. And there is a previously unexposed event xmlContextChange, which has 2 event properties: newNode and oldNode.

Modified: January 15th 2009
By: Sjoerd Visscher