Home Forums General XMetaL Discussion Highlighting current XML element with CSS Reply To: Highlighting current XML element with CSS

Derek Read

Reply to: Highlighting current XML element with CSS

I can think of two ways to do this.

1. The first (assuming you have script that already positions the selection inside the element) would be to use the Selection.ContainerStyle property.

Getting rid of the highlighting will be the tricky part however, as an appropriate event will need to be used that fires when you want to reset the CSS to remove it. You will also need to remember which element you changed ContainerStyle for and reset the value so that it matches the original (before the highlighting change). It will very likely be necessary to store the original Selection object (the one you are navigating to in your script) as a variable using the NameVariantProperties API so that this value can be retrieved from another event macro, in order to reset the CSS for that element (to remove the highlighting).

The alternative might be to switch views, however, ContainerStyle is remembered unless you switch to PlainText view and back, or reload the document.

2.The second would be to rewrite the CSS file and then reload it using ActiveDocument.RefreshCSSStyle() method.

One simple way to do that would be to add an import statement to your existing CSS that imports a second CSS file (path should be relative to the first one) and then use Windows FSO to write that second CSS file out using script just before calling RefreshCSSStyle(). The CSS “@import” statement is used for this.

The file you write out would contain only the CSS with the proper selector to affect the element in question. You seem to be lucky enough to have an id attribute on the element in question, which I assume must be a unique id value. So, the CSS you put into your file might be something as simple as the following:

emb[id="3"] {
  background-color: yellow;

Your script would need to be smart enough to alter the value of the id attribute every time it writes out the imported CSS file so that only that particular element is affected. This might be a better solution than the first one above because there will be no need to reset the style for the element because ContainerStyle is not being used and normal CSS cascading rules apply (meaning that once a new CSS file is loaded that does not contain the selector emb[id="3"] that element will revert to whatever styling is given to it by the main CSS file.